freemyipod r501 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r500‎ | r501 | r502 >
Date:00:34, 1 February 2011
Author:farthen
Status:new
Tags:
Comment:
emcore tools: rename misc.Logger.log() to misc.Logger.write() and change the default logging destination to stderr
Modified paths:
  • /emcore/trunk/tools/emcore.py (modified) (history)
  • /emcore/trunk/tools/libemcore.py (modified) (history)
  • /emcore/trunk/tools/misc.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -65,26 +65,26 @@
6666 cmddict = Commandline.cmddict
6767 doc = getfuncdoc(cmddict)
6868 if not specific:
69 - logger.log("Please provide a command and (if needed) parameters as command line arguments\n\n")
70 - logger.log("Available commands:\n\n")
 69+ logger.write("Please provide a command and (if needed) parameters as command line arguments\n\n")
 70+ logger.write("Available commands:\n\n")
7171 else:
72 - logger.log("\n")
 72+ logger.write("\n")
7373 for function in sorted(doc.items()):
7474 function = function[0]
7575 if specific == False or specific == function:
76 - logger.log(function + " ", 2)
 76+ logger.write(function + " ", 2)
7777 for arg in doc[function]['args']:
78 - logger.log("<" + arg + "> ")
 78+ logger.write("<" + arg + "> ")
7979 if doc[function]['kwargs']:
8080 for kwarg, kwvalue in doc[function]['kwargs'].iteritems():
81 - logger.log("[" + kwarg + " = " + str(kwvalue) + "] ")
 81+ logger.write("[" + kwarg + " = " + str(kwvalue) + "] ")
8282 if doc[function]['varargs']:
83 - logger.log("<db1> ... <dbN>")
 83+ logger.write("<db1> ... <dbN>")
8484 if docstring and doc[function]['documentation']:
85 - logger.log("\n")
86 - logger.log(doc[function]['documentation']+"\n", 4)
87 - logger.log("\n")
88 - logger.log("\n")
 85+ logger.write("\n")
 86+ logger.write(doc[function]['documentation']+"\n", 4)
 87+ logger.write("\n")
 88+ logger.write("\n")
8989
9090 if errormsg:
9191 logger.error(str(errormsg)+"\n")
@@ -390,7 +390,7 @@
391391 """
392392 while True:
393393 resp = self.emcore.usbcread()
394 - self.logger.log(resp.data)
 394+ self.logger.write(resp.data)
395395 time.sleep(0.1 / resp.maxsize * (resp.maxsize - len(resp.data)))
396396
397397 @command
@@ -414,7 +414,7 @@
415415 bitmask = self._hexint(bitmask)
416416 while True:
417417 resp = self.emcore.cread()
418 - self.logger.log(resp.data)
 418+ self.logger.write(resp.data)
419419 time.sleep(0.1 / resp.maxsize * (resp.maxsize - len(resp.data)))
420420
421421 @command
Index: emcore/trunk/tools/misc.py
@@ -31,12 +31,12 @@
3232
3333 class Logger(object):
3434 """
35 - Simple stdout logger.
 35+ Simple stdout/stderr/file logger.
3636 Loglevel 3 is most verbose, Loglevel 0: Only log something if there is an error.
3737 Loglevel -1 means that nothing is logged.
38 - The log function doesn't care about the loglevel and always logs to stdout.
 38+ The write function doesn't care about the loglevel and always logs everything.
3939 """
40 - def __init__(self, loglevel = 2, target = "stdout", logfile = "tools.log"):
 40+ def __init__(self, loglevel = 2, target = "stderr", logfile = "tools.log"):
4141 """
4242 loglevel: Possible values: 0 (only errors), 1 (warnings), 2 (info,
4343 recommended for production use), 3 and more (debug)
@@ -47,13 +47,15 @@
4848 self.logfile = logfile
4949 self.target = target
5050
51 - def log(self, text, indent = 0, target = None):
 51+ def write(self, text, indent = 0, target = None):
5252 if self.loglevel >= 0:
5353 if target is None: target = self.target
5454 text = (indent * " ") + text
5555 text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
5656 if target == "stdout":
57 - sys.stdout.write(text)
 57+ sys.stderr.write(text)
 58+ if target == "stderr":
 59+ sys.stderr.write(text)
5860 elif target == "file":
5961 with open(self.logfile, 'a') as f:
6062 f.write(text)
@@ -63,19 +65,19 @@
6466
6567 def debug(self, text, indent = 0, target = None):
6668 if self.loglevel >= 3:
67 - self.log("DEBUG: " + text, indent, target)
 69+ self.write("DEBUG: " + text, indent, target)
6870
6971 def info(self, text, indent = 0, target = None):
7072 if self.loglevel >= 2:
71 - self.log(text, indent, target)
 73+ self.write(text, indent, target)
7274
7375 def warn(self, text, indent = 0, target = None):
7476 if self.loglevel >= 1:
75 - self.log("WARNING: " + text, indent, target)
 77+ self.write("WARNING: " + text, indent, target)
7678
7779 def error(self, text, indent = 0, target = None):
7880 if self.loglevel >= 0:
79 - self.log("ERROR: " + text, indent, target)
 81+ self.write("ERROR: " + text, indent, target)
8082
8183
8284 class Bunch(dict):
Index: emcore/trunk/tools/libemcore.py
@@ -1109,26 +1109,26 @@
11101110 if sys.argv[1] == "gendoc":
11111111 # Generates Documentation
11121112 from misc import gendoc
1113 - logger.log("Generating documentation\n")
 1113+ logger.write("Generating documentation\n")
11141114 cmddict = {}
11151115 for attr, value in Emcore.__dict__.iteritems():
11161116 if getattr(value, 'func', False):
11171117 if getattr(value.func, '_command', False):
11181118 cmddict[value.func.__name__] = value
1119 - logger.log(gendoc(cmddict))
 1119+ logger.write(gendoc(cmddict))
11201120
11211121 elif sys.argv[1] == "malloctest":
11221122 emcore = Emcore()
1123 - logger.log("Allocating 200 bytes of memory: ")
 1123+ logger.write("Allocating 200 bytes of memory: ")
11241124 addr = emcore.malloc(200)
1125 - logger.log("0x%x\n" % addr)
1126 - logger.log("Reallocating to 2000 bytes: ")
 1125+ logger.write("0x%x\n" % addr)
 1126+ logger.write("Reallocating to 2000 bytes: ")
11271127 addr = emcore.realloc(addr, 2000)
1128 - logger.log("0x%x\n" % addr)
1129 - logger.log("Freeing 0x%x\n" % addr)
 1128+ logger.write("0x%x\n" % addr)
 1129+ logger.write("Freeing 0x%x\n" % addr)
11301130 emcore.free(addr)
1131 - logger.log("Allocating 1000 bytes of memory aligned to 100 bytes: ")
 1131+ logger.write("Allocating 1000 bytes of memory aligned to 100 bytes: ")
11321132 addr = emcore.memalign(100, 1000)
1133 - logger.log("0x%x\n" % addr)
1134 - logger.log("Freeing 0x%x\n" % addr)
 1133+ logger.write("0x%x\n" % addr)
 1134+ logger.write("Freeing 0x%x\n" % addr)
11351135 emcore.free(addr)
\ No newline at end of file