Index: embios/trunk/tools/misc.py |
— | — | @@ -35,31 +35,43 @@ |
36 | 36 | Loglevel 4 is most verbose, Loglevel 0: Only say something if there is an error.
|
37 | 37 | The log function doesn't care about the loglevel and always logs to stdout.
|
38 | 38 | """
|
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
|
42 | 49 |
|
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
|
44 | 52 | text = (indent * " ") + text
|
45 | 53 | text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
|
46 | 54 | if target == "stdout":
|
47 | 55 | sys.stdout.write(text)
|
| 56 | + elif target == "file":
|
| 57 | + with open(self.logfile, 'a') as f:
|
| 58 | + f.write(text)
|
| 59 | + f.close()
|
48 | 60 | elif target == "string":
|
49 | 61 | return text
|
50 | 62 |
|
51 | | - def debug(self, text, indent = 0, target = "stdout"):
|
| 63 | + def debug(self, text, indent = 0, target = None):
|
52 | 64 | if self.loglevel >= 3:
|
53 | 65 | self.log("DEBUG: " + text, indent, target)
|
54 | 66 |
|
55 | | - def info(self, text, indent = 0, target = "stdout"):
|
| 67 | + def info(self, text, indent = 0, target = None):
|
56 | 68 | if self.loglevel >= 2:
|
57 | 69 | self.log(text, indent, target)
|
58 | 70 |
|
59 | | - def warn(self, text, indent = 0, target = "stdout"):
|
| 71 | + def warn(self, text, indent = 0, target = None):
|
60 | 72 | if self.loglevel >= 1:
|
61 | 73 | self.log("WARNING: " + text, indent, target)
|
62 | 74 |
|
63 | | - def error(self, text, indent = 0, target = "stdout"):
|
| 75 | + def error(self, text, indent = 0, target = None):
|
64 | 76 | self.log("ERROR: " + text, indent, target)
|
65 | 77 |
|
66 | 78 |
|