add logging option
This commit is contained in:
parent
9b7e96ed6b
commit
801f9eb1f7
|
@ -18,7 +18,7 @@ pip3 install --user -e .
|
||||||
## usage
|
## usage
|
||||||
|
|
||||||
```
|
```
|
||||||
usage: megacom [-h] [-b BAUD] [-m MODE] [tty]
|
usage: megacom [-h] [-b BAUD] [-m MODE] [-l LOGFILE] [tty]
|
||||||
|
|
||||||
Alternative console-based UART client
|
Alternative console-based UART client
|
||||||
|
|
||||||
|
@ -29,6 +29,8 @@ optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-b BAUD, --baud BAUD UART baud rate [115200]
|
-b BAUD, --baud BAUD UART baud rate [115200]
|
||||||
-m MODE, --mode MODE UART mode string [8N1]
|
-m MODE, --mode MODE UART mode string [8N1]
|
||||||
|
-l LOGFILE, --logfile LOGFILE
|
||||||
|
file to log to
|
||||||
```
|
```
|
||||||
|
|
||||||
### sudo
|
### sudo
|
||||||
|
|
|
@ -7,7 +7,7 @@ import sys
|
||||||
import termios
|
import termios
|
||||||
import tty
|
import tty
|
||||||
from types import TracebackType
|
from types import TracebackType
|
||||||
from typing import Any, List, Optional, Tuple, Type
|
from typing import Any, BinaryIO, List, Optional, Tuple, Type
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
|
||||||
import serial
|
import serial
|
||||||
|
@ -103,7 +103,7 @@ class KeycodeHandler:
|
||||||
return byte
|
return byte
|
||||||
|
|
||||||
|
|
||||||
async def megacom(tty: str, baud: int, mode: str) -> None:
|
async def megacom(tty: str, baud: int, mode: str, logfile: Optional[str]) -> None:
|
||||||
(stdin, stdout) = await setup_async()
|
(stdin, stdout) = await setup_async()
|
||||||
|
|
||||||
m = MODE_RE.match(mode)
|
m = MODE_RE.match(mode)
|
||||||
|
@ -114,14 +114,24 @@ async def megacom(tty: str, baud: int, mode: str) -> None:
|
||||||
parity = MODE_LOOKUP["parity"][m.group(2)]
|
parity = MODE_LOOKUP["parity"][m.group(2)]
|
||||||
stopbits = MODE_LOOKUP["stopbits"][m.group(3)]
|
stopbits = MODE_LOOKUP["stopbits"][m.group(3)]
|
||||||
|
|
||||||
|
log: Optional[BinaryIO] = None
|
||||||
|
if logfile is not None:
|
||||||
|
try:
|
||||||
|
log = open(logfile, "wb")
|
||||||
|
except Exception as e:
|
||||||
|
sys.stderr.write(f"failed to open log file: {e}\n")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
keycodes = KeycodeHandler()
|
keycodes = KeycodeHandler()
|
||||||
return await megacom_main(stdin, stdout, tty, baud, bytesize, parity, stopbits, loop, keycodes)
|
return await megacom_main(stdin, stdout, tty, baud, bytesize, parity, stopbits, loop, keycodes,
|
||||||
|
log)
|
||||||
|
|
||||||
|
|
||||||
async def megacom_main(stdin: asyncio.StreamReader, stdout: asyncio.StreamWriter, tty: str,
|
async def megacom_main(stdin: asyncio.StreamReader, stdout: asyncio.StreamWriter, tty: str,
|
||||||
baud: int, bytesize: Any, parity: Any, stopbits: Any,
|
baud: int, bytesize: Any, parity: Any, stopbits: Any,
|
||||||
loop: asyncio.AbstractEventLoop, keycodes: KeycodeHandler) -> None:
|
loop: asyncio.AbstractEventLoop, keycodes: KeycodeHandler,
|
||||||
|
log: Optional[BinaryIO]) -> None:
|
||||||
printed_fnf = False
|
printed_fnf = False
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
@ -179,9 +189,11 @@ async def megacom_main(stdin: asyncio.StreamReader, stdout: asyncio.StreamWriter
|
||||||
c = keycodes.process(c)
|
c = keycodes.process(c)
|
||||||
if len(c) == 0:
|
if len(c) == 0:
|
||||||
continue
|
continue
|
||||||
# else:
|
else:
|
||||||
# stdout.write(f"\r\nout char: {c}\r\n".encode())
|
# stdout.write(f"\r\nout char: {c}\r\n".encode())
|
||||||
# await stdout.drain()
|
# await stdout.drain()
|
||||||
|
if log is not None:
|
||||||
|
log.write(c)
|
||||||
|
|
||||||
pout.write(c)
|
pout.write(c)
|
||||||
await pout.drain()
|
await pout.drain()
|
||||||
|
@ -236,7 +248,7 @@ async def megacom_main(stdin: asyncio.StreamReader, stdout: asyncio.StreamWriter
|
||||||
|
|
||||||
if do_retry:
|
if do_retry:
|
||||||
return await megacom_main(stdin, stdout, tty, baud, bytesize, parity, stopbits, loop,
|
return await megacom_main(stdin, stdout, tty, baud, bytesize, parity, stopbits, loop,
|
||||||
keycodes)
|
keycodes, log)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
@ -246,7 +258,9 @@ def main() -> None:
|
||||||
help="Path to UART device [/dev/ttyUSB0]")
|
help="Path to UART device [/dev/ttyUSB0]")
|
||||||
parser.add_argument("-b", "--baud", type=int, default=115200, help="UART baud rate [115200]")
|
parser.add_argument("-b", "--baud", type=int, default=115200, help="UART baud rate [115200]")
|
||||||
parser.add_argument("-m", "--mode", type=str, default="8N1", help="UART mode string [8N1]")
|
parser.add_argument("-m", "--mode", type=str, default="8N1", help="UART mode string [8N1]")
|
||||||
|
parser.add_argument("-l", "--logfile", type=str, default="", help="file to log to")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
with TtyRaw():
|
with TtyRaw():
|
||||||
asyncio.run(megacom(args.tty, args.baud, args.mode))
|
asyncio.run(megacom(args.tty, args.baud, args.mode,
|
||||||
|
args.logfile if args.logfile != "" else None))
|
||||||
|
|
Loading…
Reference in New Issue