1 # Samba common functions
3 # Copyright (C) Joe Guo <joeg@catalyst.net.nz>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from samba.colour import GREY, YELLOW, GREEN, RED, DARK_RED, C_NORMAL
24 logging.CRITICAL: DARK_RED,
26 logging.WARNING: YELLOW,
32 class ColoredFormatter(logging.Formatter):
33 """Add color to log according to level"""
35 def format(self, record):
36 log = super(ColoredFormatter, self).format(record)
37 color = LEVEL_COLORS.get(record.levelno, GREY)
38 return color + log + C_NORMAL
42 name='samba', stream=sys.stderr,
43 level=None, verbose=False, quiet=False,
44 fmt=('%(levelname)s %(asctime)s pid:%(process)d '
45 '%(pathname)s #%(lineno)d: %(message)s'),
48 Get a logger instance and config it.
50 logger = logging.getLogger(name)
53 # if level not specified, map options to level
54 level = ((verbose and logging.DEBUG) or
55 (quiet and logging.WARNING) or logging.INFO)
57 logger.setLevel(level)
59 if (hasattr(stream, 'isatty') and stream.isatty()):
60 Formatter = ColoredFormatter
62 Formatter = logging.Formatter
63 formatter = Formatter(fmt=fmt, datefmt=datefmt)
65 handler = logging.StreamHandler(stream=stream)
66 handler.setFormatter(formatter)
67 logger.addHandler(handler)