mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +01:00
log.py: Add utilities, better list-support in slog_m()
- Add functions: append_to_prefix(), remove_from_prefix() set_filename_length() - Make slog_m() log one list item per line - Add console_color_chars(prio) Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
c96ffe52c0
commit
d3faa8ee85
1 changed files with 31 additions and 1 deletions
|
|
@ -45,6 +45,8 @@ f_color = 'color'
|
||||||
f_default = [ f_position, f_stderr, f_prio, f_color ]
|
f_default = [ f_position, f_stderr, f_prio, f_color ]
|
||||||
|
|
||||||
_flags = set(f_default)
|
_flags = set(f_default)
|
||||||
|
_log_prefix = ''
|
||||||
|
_file_name_len = 20
|
||||||
|
|
||||||
_short_prio_str = {
|
_short_prio_str = {
|
||||||
EMERG : '<Y>',
|
EMERG : '<Y>',
|
||||||
|
|
@ -81,6 +83,9 @@ def slog_m(prio, *args, **kwargs): # export
|
||||||
if len(args):
|
if len(args):
|
||||||
margs = ''
|
margs = ''
|
||||||
for a in args:
|
for a in args:
|
||||||
|
if isinstance(a, list):
|
||||||
|
margs += '\n'.join([str(elem) for elem in a])
|
||||||
|
continue
|
||||||
margs += ' ' + str(a)
|
margs += ' ' + str(a)
|
||||||
caller = get_caller_pos(1)
|
caller = get_caller_pos(1)
|
||||||
for line in margs[1:].split('\n'):
|
for line in margs[1:].split('\n'):
|
||||||
|
|
@ -103,11 +108,13 @@ def slog(prio, *args, **kwargs): # export
|
||||||
name, line = kwargs['caller']
|
name, line = kwargs['caller']
|
||||||
else:
|
else:
|
||||||
name, line = get_caller_pos(1)
|
name, line = get_caller_pos(1)
|
||||||
msg += misc.pad(name, 20) + '[' + misc.pad(str(line), 4, True) + ']'
|
msg += misc.pad(name, _file_name_len) + '[' + misc.pad(str(line), 4, True) + ']'
|
||||||
|
|
||||||
if f_color in _flags:
|
if f_color in _flags:
|
||||||
color_on, color_off = _prio_colors[prio]
|
color_on, color_off = _prio_colors[prio]
|
||||||
|
|
||||||
|
msg += _log_prefix
|
||||||
|
|
||||||
if len(args):
|
if len(args):
|
||||||
margs = ''
|
margs = ''
|
||||||
for a in args:
|
for a in args:
|
||||||
|
|
@ -190,3 +197,26 @@ def set_flags(flags_): # export
|
||||||
#date
|
#date
|
||||||
#pid
|
#pid
|
||||||
#highlight_first_error
|
#highlight_first_error
|
||||||
|
|
||||||
|
def append_to_prefix(prefix): # export
|
||||||
|
global _log_prefix
|
||||||
|
r = _log_prefix
|
||||||
|
if prefix:
|
||||||
|
_log_prefix += prefix
|
||||||
|
return r
|
||||||
|
|
||||||
|
def remove_from_prefix(count): # export
|
||||||
|
global _log_prefix
|
||||||
|
r = _log_prefix
|
||||||
|
_log_prefix = _log_prefix[:-count]
|
||||||
|
return r
|
||||||
|
|
||||||
|
def set_filename_length(l): # export
|
||||||
|
global _file_name_len
|
||||||
|
r = _file_name_len
|
||||||
|
if l:
|
||||||
|
_file_name_len = l
|
||||||
|
return r
|
||||||
|
|
||||||
|
def console_color_chars(prio): # export
|
||||||
|
return _prio_colors[prio]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue