From fc6d232a9357211a44dad3300ff64571620aa1bf Mon Sep 17 00:00:00 2001 From: necoro <> Date: Fri, 13 Jul 2007 04:09:51 +0000 Subject: new fancier log output --- portato/__init__.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'portato/__init__.py') diff --git a/portato/__init__.py b/portato/__init__.py index 4b1f42f..0166170 100644 --- a/portato/__init__.py +++ b/portato/__init__.py @@ -3,9 +3,55 @@ # File: portato/__init__.py # This file is part of the Portato-Project, a graphical portage-frontend. # -# Copyright (C) 2006 René 'Necoro' Neumann +# Copyright (C) 2006-2007 René 'Necoro' Neumann # This is free software. You may redistribute copies of it under the terms of # the GNU General Public License version 2. # There is NO WARRANTY, to the extent permitted by law. # # Written by René 'Necoro' Neumann + +import logging +import sys +import os + +class OutputFormatter (logging.Formatter): + + colors = { + "blue" : 34, + "green" : 32, + "red" : 31, + "yellow": 33 + } + + def __init__(self, *args, **kwargs): + logging.Formatter.__init__(self, *args, **kwargs) + + for key, value in self.colors.iteritems(): + self.colors[key] = "\x1b[01;%02dm*\x1b[39;49;00m" % value + + def format (self, record): + string = logging.Formatter.format(self, record) + color = None + + if os.isatty(sys.stderr.fileno()): + if record.levelno <= logging.DEBUG: + color = self.colors["blue"] + elif record.levelno <= logging.INFO: + color = self.colors["green"] + elif record.levelno <= logging.WARNING: + color = self.colors["yellow"] + else: + color = self.colors["red"] + else: + color = "%s:" % record.levelname + + return "%s %s" % (color, string) + +# set the whole logging stuff +formatter = OutputFormatter("%(message)s (%(filename)s:%(lineno)s)") + +handler = logging.StreamHandler() +handler.setFormatter(formatter) +logging.getLogger().addHandler(handler) + +logging.getLogger().setLevel(logging.DEBUG) -- cgit v1.2.3