mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
slog(): Add only_printable keyword argument
Add only_printable keyword argument to slog(). It defaults to False. If it's true, non-printable characters are not logged but replaced with a printable character. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
be4389a1f0
commit
8029b672bf
1 changed files with 17 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ from __future__ import print_function
|
||||||
import syslog
|
import syslog
|
||||||
import sys
|
import sys
|
||||||
import inspect
|
import inspect
|
||||||
|
import re
|
||||||
from os.path import basename
|
from os.path import basename
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple
|
||||||
|
|
@ -13,6 +14,18 @@ try:
|
||||||
except NameError:
|
except NameError:
|
||||||
basestring = str
|
basestring = str
|
||||||
|
|
||||||
|
_special_chars = {
|
||||||
|
'\a' : '\\a',
|
||||||
|
'\b' : '\\b',
|
||||||
|
'\t' : '\\t',
|
||||||
|
'\n' : '\\n',
|
||||||
|
'\v' : '\\v',
|
||||||
|
'\f' : '\\f',
|
||||||
|
'\r' : '\\r',
|
||||||
|
}
|
||||||
|
|
||||||
|
_special_char_regex = re.compile("(%s)" % "|".join(map(re.escape, _special_chars.keys())))
|
||||||
|
|
||||||
EMERG = syslog.LOG_EMERG
|
EMERG = syslog.LOG_EMERG
|
||||||
ALERT = syslog.LOG_ALERT
|
ALERT = syslog.LOG_ALERT
|
||||||
CRIT = syslog.LOG_CRIT
|
CRIT = syslog.LOG_CRIT
|
||||||
|
|
@ -101,7 +114,7 @@ def slog_m(prio: int, *args, **kwargs) -> None: # export
|
||||||
for line in margs[1:].split('\n'):
|
for line in margs[1:].split('\n'):
|
||||||
slog(prio, line, **kwargs, caller=caller)
|
slog(prio, line, **kwargs, caller=caller)
|
||||||
|
|
||||||
def slog(prio: int, *args, **kwargs) -> None: # export
|
def slog(prio: int, *args, only_printable=False, **kwargs) -> None: # export
|
||||||
|
|
||||||
if prio > _level:
|
if prio > _level:
|
||||||
return
|
return
|
||||||
|
|
@ -132,6 +145,9 @@ def slog(prio: int, *args, **kwargs) -> None: # export
|
||||||
margs = ''
|
margs = ''
|
||||||
for a in args:
|
for a in args:
|
||||||
margs += ' ' + str(a)
|
margs += ' ' + str(a)
|
||||||
|
if only_printable:
|
||||||
|
margs = _special_char_regex.sub(lambda mo: _special_chars[mo.string[mo.start():mo.end()]], margs)
|
||||||
|
margs = re.sub('[\x01-\x1f]', '.', margs)
|
||||||
msg += color_on + margs + color_off
|
msg += color_on + margs + color_off
|
||||||
|
|
||||||
if not len(msg):
|
if not len(msg):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue