-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
master: TCP server for remote logging
1 parent
9e2e233
commit cbda753
Showing
4 changed files
with
91 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import asyncio | ||
import logging | ||
|
||
from artiq.protocols.asyncio_server import AsyncioServer | ||
|
||
|
||
_fwd_logger = logging.getLogger("fwd") | ||
|
||
|
||
def log_with_name(name, *args, **kwargs): | ||
_fwd_logger.name = name | ||
_fwd_logger.log(*args, **kwargs) | ||
|
||
|
||
_name_to_level = { | ||
"CRITICAL": logging.CRITICAL, | ||
"ERROR": logging.ERROR, | ||
"WARN": logging.WARNING, | ||
"WARNING": logging.WARNING, | ||
"INFO": logging.INFO, | ||
"DEBUG": logging.DEBUG, | ||
} | ||
|
||
|
||
def parse_log_message(msg): | ||
for name, level in _name_to_level.items(): | ||
if msg.startswith(name + ":"): | ||
remainder = msg[len(name) + 1:] | ||
try: | ||
idx = remainder.index(":") | ||
except: | ||
continue | ||
return level, remainder[:idx], remainder[idx+1:] | ||
return logging.INFO, "print", msg | ||
|
||
|
||
_init_string = b"ARTIQ logging\n" | ||
|
||
|
||
class Server(AsyncioServer): | ||
async def _handle_connection_cr(self, reader, writer): | ||
try: | ||
line = await reader.readline() | ||
if line != _init_string: | ||
return | ||
|
||
while True: | ||
line = await reader.readline() | ||
if not line: | ||
break | ||
try: | ||
line = line.decode() | ||
except: | ||
return | ||
line = line[:-1] | ||
linesplit = line.split(":", 4) | ||
if len(linesplit) != 4: | ||
return | ||
source, levelname, name, message = linesplit | ||
try: | ||
level = _name_to_level[levelname] | ||
except KeyError: | ||
return | ||
log_with_name(name, level, message, | ||
extra={"source": source}) | ||
finally: | ||
writer.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters