mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
log.(add|rm)_capture_stream(): Add functions
Add functions log.(add|rm)_capture_stream(). The functions are meant to capture everything passed to syslog into the stream objects installed by it. Not sure about future semantics changes: add_capture_stream() takes a currently unused flags argument, and it suppresses everything else logged until the stream is removed again. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
748b850532
commit
6e70529f7a
1 changed files with 33 additions and 11 deletions
|
|
@ -1,11 +1,11 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import syslog
|
|
||||||
import sys
|
from typing import List, Tuple, Optional, Any
|
||||||
import inspect
|
|
||||||
import re
|
import sys, re, io, syslog, inspect
|
||||||
|
|
||||||
from os.path import basename
|
from os.path import basename
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import List, Tuple, Optional, Any
|
|
||||||
from . import misc
|
from . import misc
|
||||||
|
|
||||||
# --- python 2 / 3 compatibility stuff
|
# --- python 2 / 3 compatibility stuff
|
||||||
|
|
@ -90,6 +90,23 @@ _prio_colors = {
|
||||||
EMERG : [ CONSOLE_FONT_BOLD + CONSOLE_FONT_MAGENTA, CONSOLE_FONT_OFF ],
|
EMERG : [ CONSOLE_FONT_BOLD + CONSOLE_FONT_MAGENTA, CONSOLE_FONT_OFF ],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Stream:
|
||||||
|
def __init__(self, stream, flags):
|
||||||
|
self.stream = stream
|
||||||
|
self.flags = flags
|
||||||
|
|
||||||
|
_streams: dict[int, io.IOBase] = dict()
|
||||||
|
_stream_descriptors = [reversed(range(1, 16))]
|
||||||
|
|
||||||
|
def add_capture_stream(stream, flags=0x0):
|
||||||
|
ret = _stream_descriptors.pop()
|
||||||
|
_streams[ret] = Stream(stream=stream, flags=flags)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def rm_capture_stream(sd):
|
||||||
|
del _streams[sd]
|
||||||
|
_stream_descriptors.append(sd)
|
||||||
|
|
||||||
def prio_gets_logged(prio: int) -> bool: # export
|
def prio_gets_logged(prio: int) -> bool: # export
|
||||||
if prio > _level:
|
if prio > _level:
|
||||||
return False
|
return False
|
||||||
|
|
@ -172,14 +189,19 @@ def slog(prio: int, *args, only_printable: bool=False, **kwargs) -> None: # expo
|
||||||
return
|
return
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
if f_stdout in _flags:
|
if 'capture' in kwargs:
|
||||||
files.append(sys.stdout)
|
files.append(kwargs['capture'])
|
||||||
|
elif _streams:
|
||||||
|
files = [s.stream for s in _streams.values()]
|
||||||
|
else:
|
||||||
|
if f_stdout in _flags:
|
||||||
|
files.append(sys.stdout)
|
||||||
|
|
||||||
if f_stderr in _flags:
|
if f_stderr in _flags:
|
||||||
files.append(sys.stderr)
|
files.append(sys.stderr)
|
||||||
|
|
||||||
if not len(files):
|
if not len(files):
|
||||||
files = [ sys.stdout ]
|
files = [ sys.stdout ]
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
print(msg, file=file)
|
print(msg, file=file)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue