diff options
author | Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com> | 2010-08-26 10:17:37 +0100 |
---|---|---|
committer | Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com> | 2010-08-26 10:17:37 +0100 |
commit | e2f75e06656c1bd0cd0cecb4a99b8220f468fd8d (patch) | |
tree | f0ac19bdd7be13845c8a706df50f37333bf02a86 /pykolab/logger.py | |
download | pykolab-e2f75e06656c1bd0cd0cecb4a99b8220f468fd8d.tar.gz |
Initial commit
Diffstat (limited to 'pykolab/logger.py')
-rw-r--r-- | pykolab/logger.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/pykolab/logger.py b/pykolab/logger.py new file mode 100644 index 0000000..168b338 --- /dev/null +++ b/pykolab/logger.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- +# Copyright 2010 Kolab Systems AG (http://www.kolabsys.com) +# +# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com> +# +# This program 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 2 only +# +# This program 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 Library General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# + +import logging +import logging.handlers +import sys + +# Translation +from pykolab.translate import _, N_ + +class Logger: + def __init__(self, loglevel=logging.INFO, debuglevel=0, logfile="/var/log/kolab/kolabd.log"): + self.loglevel = loglevel + self.debuglevel = debuglevel + + plaintextformatter = logging.Formatter("%(message)s") + + console_stdout = logging.StreamHandler(sys.stdout) + console_stdout.setFormatter(plaintextformatter) + + try: + filelog_handler = logging.FileHandler(filename=logfile) + filelog_handler.setFormatter(plaintextformatter) + except IOError, e: + print >> sys.stderr, _("Cannot log to file %s: %s") % (logfile, e) + + self.log = logging.getLogger() + self.log.addHandler(console_stdout) + try: + self.log.addHandler(filelog_handler) + except: + pass + + self.log.setLevel(self.loglevel) + + def set_config(self, cfg): + """Let the Logger instance know what our configuration is and she might + be able to distinct between CLI and GUI mode, or even give more details + about what goes wrong""" + self.cfg = cfg + + def info(self, msg): + self.log.info(msg) + + def debug(self, msg, level=1): + # By default, level=1 so that debug messages are suppressed + if level <= self.debuglevel: + self.log.debug(msg) + + def error(self, msg, recoverable=True): + self.log.error(msg) + if recoverable: + self.error_prompt(msg) + else: + sys.exit(1) + + def warning(self, msg): + self.log.warning(msg) + self.warning_prompt(msg) + + def error_prompt(self, text): + """The error has already been logged to the console, try and catch some input""" + if not self.cfg.answer_yes: + sys.stderr.write(_("Do you want to continue? [Y/n]") + " ") + answer = sys.stdin.readline()[:-1] + if answer == "n": + self.error(_("Abort! Abort! Abort!"), recoverable=False) + sys.exit(1) + + def warning_prompt(self, text): + """The error has already been logged to the console, try and catch some input""" + if not self.cfg.answer_yes: + sys.stdout.write(_("Do you want to continue? [Y/n]") + " ") + answer = sys.stdin.readline()[:-1] + if answer == "n": + self.error(_("Abort! Abort! Abort!"), recoverable=False) + sys.exit(1) |