2018-09-05 02:59:34 +02:00
|
|
|
# Copyright (C) 2009 Bruno Tarquini <btarquini AT gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
import logging
|
2017-02-25 17:21:03 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2009-03-11 18:00:28 +01:00
|
|
|
|
2018-10-04 23:55:35 +02:00
|
|
|
from gajim.common.i18n import _
|
|
|
|
|
2009-03-11 18:00:28 +01:00
|
|
|
def parseLogLevel(arg):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Eiter numeric value or level name from logging module
|
|
|
|
"""
|
|
|
|
if arg.isdigit():
|
|
|
|
return int(arg)
|
2018-09-18 10:14:04 +02:00
|
|
|
if arg.isupper() and hasattr(logging, arg):
|
2010-02-08 15:08:40 +01:00
|
|
|
return getattr(logging, arg)
|
2019-02-16 10:36:27 +01:00
|
|
|
print(_('%s is not a valid loglevel') % repr(arg), file=sys.stderr)
|
2018-09-18 10:14:04 +02:00
|
|
|
return 0
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
def parseLogTarget(arg):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
[gajim.]c.x.y -> gajim.c.x.y
|
|
|
|
.other_logger -> other_logger
|
|
|
|
<None> -> gajim
|
|
|
|
"""
|
|
|
|
arg = arg.lower()
|
|
|
|
if not arg:
|
|
|
|
return 'gajim'
|
2018-09-18 10:14:04 +02:00
|
|
|
if arg.startswith('.'):
|
2010-02-08 15:08:40 +01:00
|
|
|
return arg[1:]
|
2018-09-18 10:14:04 +02:00
|
|
|
if arg.startswith('gajim'):
|
2010-02-08 15:08:40 +01:00
|
|
|
return arg
|
2018-09-18 10:14:04 +02:00
|
|
|
return 'gajim.' + arg
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
def parseAndSetLogLevels(arg):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
[=]LOGLEVEL -> gajim=LOGLEVEL
|
|
|
|
gajim=LOGLEVEL -> gajim=LOGLEVEL
|
|
|
|
.other=10 -> other=10
|
|
|
|
.=10 -> <nothing>
|
|
|
|
c.x.y=c.z=20 -> gajim.c.x.y=20
|
|
|
|
gajim.c.z=20
|
|
|
|
gajim=10,c.x=20 -> gajim=10
|
|
|
|
gajim.c.x=20
|
|
|
|
"""
|
|
|
|
for directive in arg.split(','):
|
|
|
|
directive = directive.strip()
|
|
|
|
if not directive:
|
|
|
|
continue
|
|
|
|
if '=' not in directive:
|
|
|
|
directive = '=' + directive
|
|
|
|
targets, level = directive.rsplit('=', 1)
|
|
|
|
level = parseLogLevel(level.strip())
|
|
|
|
for target in targets.split('='):
|
|
|
|
target = parseLogTarget(target.strip())
|
|
|
|
if target:
|
|
|
|
logging.getLogger(target).setLevel(level)
|
2019-02-16 10:36:27 +01:00
|
|
|
print("Logger %s level set to %d" % (target, level),
|
|
|
|
file=sys.stderr)
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
class colors:
|
2018-09-18 12:06:01 +02:00
|
|
|
# pylint: disable=C0326
|
2010-02-08 15:08:40 +01:00
|
|
|
NONE = chr(27) + "[0m"
|
|
|
|
BLACk = chr(27) + "[30m"
|
|
|
|
RED = chr(27) + "[31m"
|
|
|
|
GREEN = chr(27) + "[32m"
|
|
|
|
BROWN = chr(27) + "[33m"
|
|
|
|
BLUE = chr(27) + "[34m"
|
|
|
|
MAGENTA = chr(27) + "[35m"
|
|
|
|
CYAN = chr(27) + "[36m"
|
|
|
|
LIGHT_GRAY = chr(27) + "[37m"
|
|
|
|
DARK_GRAY = chr(27) + "[30;1m"
|
|
|
|
BRIGHT_RED = chr(27) + "[31;1m"
|
|
|
|
BRIGHT_GREEN = chr(27) + "[32;1m"
|
|
|
|
YELLOW = chr(27) + "[33;1m"
|
|
|
|
BRIGHT_BLUE = chr(27) + "[34;1m"
|
|
|
|
PURPLE = chr(27) + "[35;1m"
|
|
|
|
BRIGHT_CYAN = chr(27) + "[36;1m"
|
|
|
|
WHITE = chr(27) + "[37;1m"
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
def colorize(text, color):
|
2010-02-08 15:08:40 +01:00
|
|
|
return color + text + colors.NONE
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
class FancyFormatter(logging.Formatter):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
An eye-candy formatter with colors
|
|
|
|
"""
|
|
|
|
colors_mapping = {
|
2018-09-18 12:06:01 +02:00
|
|
|
'DEBUG': colors.BLUE,
|
|
|
|
'INFO': colors.GREEN,
|
|
|
|
'WARNING': colors.BROWN,
|
|
|
|
'ERROR': colors.RED,
|
|
|
|
'CRITICAL': colors.BRIGHT_RED,
|
2010-02-08 15:08:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, fmt, datefmt=None, use_color=False):
|
|
|
|
logging.Formatter.__init__(self, fmt, datefmt)
|
|
|
|
self.use_color = use_color
|
|
|
|
|
|
|
|
def formatTime(self, record, datefmt=None):
|
|
|
|
f = logging.Formatter.formatTime(self, record, datefmt)
|
|
|
|
if self.use_color:
|
|
|
|
f = colorize(f, colors.DARK_GRAY)
|
|
|
|
return f
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
level = record.levelname
|
|
|
|
record.levelname = '(%s)' % level[0]
|
|
|
|
|
|
|
|
if self.use_color:
|
|
|
|
c = FancyFormatter.colors_mapping.get(level, '')
|
|
|
|
record.levelname = colorize(record.levelname, c)
|
|
|
|
record.name = colorize(record.name, colors.CYAN)
|
|
|
|
else:
|
|
|
|
record.name += ':'
|
|
|
|
|
|
|
|
return logging.Formatter.format(self, record)
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
|
2017-02-25 17:21:03 +01:00
|
|
|
def init():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Iinitialize the logging system
|
|
|
|
"""
|
2017-02-25 17:21:03 +01:00
|
|
|
use_color = False
|
|
|
|
if os.name != 'nt':
|
|
|
|
use_color = sys.stderr.isatty()
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
consoleloghandler = logging.StreamHandler()
|
|
|
|
consoleloghandler.setFormatter(
|
2018-09-18 12:06:01 +02:00
|
|
|
FancyFormatter(
|
|
|
|
'%(asctime)s %(levelname)s %(name)s %(message)s',
|
|
|
|
'%x %H:%M:%S',
|
|
|
|
use_color
|
|
|
|
)
|
2010-02-08 15:08:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
root_log = logging.getLogger('gajim')
|
|
|
|
root_log.setLevel(logging.WARNING)
|
|
|
|
root_log.addHandler(consoleloghandler)
|
|
|
|
root_log.propagate = False
|
2009-03-11 18:00:28 +01:00
|
|
|
|
2012-12-30 18:54:49 +01:00
|
|
|
root_log = logging.getLogger('nbxmpp')
|
|
|
|
root_log.setLevel(logging.WARNING)
|
|
|
|
root_log.addHandler(consoleloghandler)
|
|
|
|
root_log.propagate = False
|
|
|
|
|
2019-02-09 21:39:14 +01:00
|
|
|
root_log = logging.getLogger('gnupg')
|
|
|
|
root_log.setLevel(logging.WARNING)
|
|
|
|
root_log.addHandler(consoleloghandler)
|
|
|
|
root_log.propagate = False
|
|
|
|
|
2009-03-11 18:00:28 +01:00
|
|
|
def set_loglevels(loglevels_string):
|
2010-02-08 15:08:40 +01:00
|
|
|
parseAndSetLogLevels(loglevels_string)
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
def set_verbose():
|
2017-01-04 00:57:11 +01:00
|
|
|
parseAndSetLogLevels('gajim=DEBUG')
|
|
|
|
parseAndSetLogLevels('.nbxmpp=INFO')
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
def set_quiet():
|
2010-02-08 15:08:40 +01:00
|
|
|
parseAndSetLogLevels('gajim=CRITICAL')
|
2013-05-16 21:06:41 +02:00
|
|
|
parseAndSetLogLevels('.nbxmpp=CRITICAL')
|
2009-03-11 18:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
# tests
|
|
|
|
if __name__ == '__main__':
|
2017-02-25 17:21:03 +01:00
|
|
|
init()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
set_loglevels('gajim.c=DEBUG,INFO')
|
|
|
|
|
|
|
|
log = logging.getLogger('gajim')
|
|
|
|
log.debug('debug')
|
|
|
|
log.info('info')
|
2013-01-05 11:26:22 +01:00
|
|
|
log.warning('warn')
|
2010-02-08 15:08:40 +01:00
|
|
|
log.error('error')
|
|
|
|
log.critical('critical')
|
|
|
|
|
|
|
|
log = logging.getLogger('gajim.c.x.dispatcher')
|
|
|
|
log.debug('debug')
|
|
|
|
log.info('info')
|
2013-01-05 11:26:22 +01:00
|
|
|
log.warning('warn')
|
2010-02-08 15:08:40 +01:00
|
|
|
log.error('error')
|
|
|
|
log.critical('critical')
|