From 65ba0dcf99019a285b30e47b6a8dc482d9e146a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Tue, 2 May 2006 16:31:15 +0000 Subject: [PATCH] * fix: log to /var/log/ only if we have permission to write (not like syslog) --- pisi/api.py | 17 +++++++++++------ pisi/cli/__init__.py | 12 ++++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pisi/api.py b/pisi/api.py index d8e7e649..a8a5d855 100644 --- a/pisi/api.py +++ b/pisi/api.py @@ -67,11 +67,15 @@ def init(database = True, write = True, else: ctx.ui = ui - logging.basicConfig(level=logging.DEBUG, - format='%(asctime)s %(levelname)-8s %(message)s', - datefmt='%a, %d %b %Y %H:%M:%S', - filename='/var/log/pisi.log', - filemode='w') + + if os.access('/var/log', os.W_OK): + ctx.log = True + logging.basicConfig(level=logging.DEBUG, + format='%(asctime)s %(levelname)-8s %(message)s', + datefmt='%a, %d %b %Y %H:%M:%S', + filename='/var/log/pisi.log') + else: + ctx.log = False # If given define stdout and stderr. Needed by buildfarm currently # but others can benefit from this too. @@ -110,7 +114,8 @@ def init(database = True, write = True, def finalize(): if ctx.initialized: - logging.shutdown() + if ctx.log: + logging.shutdown() pisi.repodb.finalize() pisi.installdb.finalize() diff --git a/pisi/cli/__init__.py b/pisi/cli/__init__.py index 43fcf106..5e737ff2 100644 --- a/pisi/cli/__init__.py +++ b/pisi/cli/__init__.py @@ -75,7 +75,8 @@ class CLI(UI): def warning(self, msg, verbose = False): msg = unicode(msg) - logging.warning(msg) + if ctx.log: + logging.warning(msg) if ctx.get_option('no_color'): self.output(_('Warning: ') + msg + '\n', err=True, verbose=verbose) else: @@ -83,7 +84,8 @@ class CLI(UI): def error(self, msg): msg = unicode(msg) - logging.error(msg) + if ctx.log: + logging.error(msg) if ctx.get_option('no_color'): self.output(_('Error: ') + msg + '\n', err=True) else: @@ -92,7 +94,8 @@ class CLI(UI): def action(self, msg, verbose = False): #TODO: this seems quite redundant? msg = unicode(msg) - logging.info(msg) + if ctx.log: + logging.info(msg) self.output(colorize(msg + '\n', 'green')) def choose(self, msg, opts): @@ -151,4 +154,5 @@ class CLI(UI): msg = None if msg: self.output(colorize(msg + '\n', 'cyan')) - logging.info(msg) + if ctx.log: + logging.info(msg)