freemyipod r400 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r399‎ | r400 | r401 >
Date:03:01, 30 December 2010
Author:farthen
Status:new
Tags:
Comment:
misc.py: Logger: Implement logging to a file
Modified paths:
  • /embios/trunk/tools/misc.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/misc.py
@@ -35,31 +35,43 @@
3636 Loglevel 4 is most verbose, Loglevel 0: Only say something if there is an error.
3737 The log function doesn't care about the loglevel and always logs to stdout.
3838 """
39 - def __init__(self):
40 - # Possible values: 0 (only errors), 1 (warnings), 2 (info, recommended for production use), 3 and more (debug)
41 - self.loglevel = 3
 39+ def __init__(self, loglevel = 3, logfile = "tools.log", target = "stdout"):
 40+ """
 41+ loglevel: Possible values: 0 (only errors), 1 (warnings), 2 (info,
 42+ recommended for production use), 3 and more (debug)
 43+ logfile: File to log to if using the target = "file"
 44+ target: Default logging target. Can be "stdout", "file" or "string"
 45+ """
 46+ self.loglevel = loglevel
 47+ self.logfile = logfile
 48+ self.target = target
4249
43 - def log(self, text, indent = 0, target = "stdout"):
 50+ def log(self, text, indent = 0, target = None):
 51+ if target is None: target = self.target
4452 text = (indent * " ") + text
4553 text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
4654 if target == "stdout":
4755 sys.stdout.write(text)
 56+ elif target == "file":
 57+ with open(self.logfile, 'a') as f:
 58+ f.write(text)
 59+ f.close()
4860 elif target == "string":
4961 return text
5062
51 - def debug(self, text, indent = 0, target = "stdout"):
 63+ def debug(self, text, indent = 0, target = None):
5264 if self.loglevel >= 3:
5365 self.log("DEBUG: " + text, indent, target)
5466
55 - def info(self, text, indent = 0, target = "stdout"):
 67+ def info(self, text, indent = 0, target = None):
5668 if self.loglevel >= 2:
5769 self.log(text, indent, target)
5870
59 - def warn(self, text, indent = 0, target = "stdout"):
 71+ def warn(self, text, indent = 0, target = None):
6072 if self.loglevel >= 1:
6173 self.log("WARNING: " + text, indent, target)
6274
63 - def error(self, text, indent = 0, target = "stdout"):
 75+ def error(self, text, indent = 0, target = None):
6476 self.log("ERROR: " + text, indent, target)
6577
6678