Implement log files and stdin comments

This commit is contained in:
Kai Vogelgesang 2020-10-11 00:40:40 +02:00
parent 897d567334
commit 9d11c29091
4 changed files with 44 additions and 12 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# captures
capture_*.txt
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

View File

@ -7,15 +7,16 @@ d_c = Diff(Color.GREEN, Color.RED)
d_s = Diff(Color.BLUE, Color.YELLOW) d_s = Diff(Color.BLUE, Color.YELLOW)
def handle(tag: str, data: bytes): def handle(tag: str, data: str):
msg = data.decode().strip() if tag == "stdin":
if msg.startswith("0c") or msg.startswith("0a"): print(f"[stdin] {data}")
if data.startswith("0c") or data.startswith("0a"):
if tag == "server": if tag == "server":
print(f"{Color.YELLOW}{msg}{Color.RESET}") print(f"{Color.YELLOW}{data}{Color.RESET}")
else: elif tag == "client":
print(f"{Color.RED}{msg}{Color.RESET}") print(f"{Color.RED}{data}{Color.RESET}")
else: else:
if tag == "server": if tag == "server":
d_s.consume(msg) d_s.consume(data)
else: elif tag == "client":
d_c.consume(msg) d_c.consume(data)

20
main.py
View File

@ -3,16 +3,26 @@ import os
import importlib import importlib
import traceback import traceback
import signal import signal
import binascii
import sys
import datetime
import handler import handler
SERVER_PORT = 22023 SERVER_PORT = 22023
async def queue_handler(queue: asyncio.Queue): def stdin_callback(queue: asyncio.Queue):
asyncio.ensure_future(queue.put(("stdin", sys.stdin.readline().rstrip("\n"))))
async def queue_handler(queue: asyncio.Queue, log_filename: str = None):
last_edit_timestamp = os.stat(handler.__file__).st_mtime_ns last_edit_timestamp = os.stat(handler.__file__).st_mtime_ns
while True: while True:
tag, data = await queue.get() tag, data = await queue.get()
if log_filename is not None:
with open(log_filename, "a") as f:
f.write(f"{tag} {data}\n")
try: try:
timestamp = os.stat(handler.__file__).st_mtime_ns timestamp = os.stat(handler.__file__).st_mtime_ns
if timestamp > last_edit_timestamp: if timestamp > last_edit_timestamp:
@ -30,7 +40,7 @@ async def queue_handler(queue: asyncio.Queue):
async def read_to_queue(reader: asyncio.StreamReader, tag: str, queue: asyncio.Queue): async def read_to_queue(reader: asyncio.StreamReader, tag: str, queue: asyncio.Queue):
while True: while True:
data = await reader.readline() data = (await reader.readline()).decode().strip()
if not data: if not data:
break break
await queue.put((tag, data)) await queue.put((tag, data))
@ -40,6 +50,8 @@ async def main():
queue = asyncio.Queue() queue = asyncio.Queue()
asyncio.get_event_loop().add_reader(sys.stdin, stdin_callback, queue)
proc_server = await asyncio.create_subprocess_shell( proc_server = await asyncio.create_subprocess_shell(
f'tshark -l -f "udp src port {SERVER_PORT}" -T fields -e data', f'tshark -l -f "udp src port {SERVER_PORT}" -T fields -e data',
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
@ -64,7 +76,9 @@ async def main():
signal.SIGINT, lambda: print("terminating...") signal.SIGINT, lambda: print("terminating...")
) )
handler_task = asyncio.create_task(queue_handler(queue)) now = datetime.datetime.now()
log_filename = f"capture_{now.strftime('%Y-%m-%d_%H:%M:%S')}.txt"
handler_task = asyncio.create_task(queue_handler(queue, log_filename=log_filename))
await asyncio.gather(server_task, client_task) await asyncio.gather(server_task, client_task)

14
view.py Normal file
View File

@ -0,0 +1,14 @@
import handler
import sys
def main():
filename = sys.argv[1]
with open(filename, "r") as f:
for line in f.readlines():
tag, data = line.split()
handler.handle(tag, data)
if __name__ == "__main__":
main()