Subversion Repositories freemyipod

Rev

Rev 400 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 400 Rev 401
Line 30... Line 30...
30
import libembiosdata
30
import libembiosdata
31
 
31
 
32
class Logger(object):
32
class Logger(object):
33
    """
33
    """
34
        Simple stdout logger.
34
        Simple stdout logger.
35
        Loglevel 4 is most verbose, Loglevel 0: Only say something if there is an error.
35
        Loglevel 3 is most verbose, Loglevel 0: Only log something if there is an error.
-
 
36
        Loglevel -1 means that nothing is logged.
36
        The log function doesn't care about the loglevel and always logs to stdout.
37
        The log function doesn't care about the loglevel and always logs to stdout.
37
    """
38
    """
38
    def __init__(self, loglevel = 3, logfile = "tools.log", target = "stdout"):
39
    def __init__(self, loglevel = 2, target = "stdout", logfile = "tools.log"):
39
        """
40
        """
40
            loglevel: Possible values: 0 (only errors), 1 (warnings), 2 (info,
41
            loglevel: Possible values: 0 (only errors), 1 (warnings), 2 (info,
41
                      recommended for production use), 3 and more (debug)
42
                      recommended for production use), 3 and more (debug)
42
            logfile: File to log to if using the target = "file"
43
            logfile: File to log to if using the target = "file"
43
            target: Default logging target. Can be "stdout", "file" or "string"
44
            target: Default logging target. Can be "stdout", "file" or "string"
Line 45... Line 46...
45
        self.loglevel = loglevel
46
        self.loglevel = loglevel
46
        self.logfile = logfile
47
        self.logfile = logfile
47
        self.target = target
48
        self.target = target
48
        
49
        
49
    def log(self, text, indent = 0, target = None):
50
    def log(self, text, indent = 0, target = None):
-
 
51
        if self.loglevel >= 0:
50
        if target is None: target = self.target
52
            if target is None: target = self.target
51
        text = (indent * " ") + text
53
            text = (indent * " ") + text
52
        text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
54
            text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
53
        if target == "stdout":
55
            if target == "stdout":
54
            sys.stdout.write(text)
56
                sys.stdout.write(text)
55
        elif target == "file":
57
            elif target == "file":
56
            with open(self.logfile, 'a') as f:
58
                with open(self.logfile, 'a') as f:
57
                f.write(text)
59
                    f.write(text)
58
                f.close()
60
                    f.close()
59
        elif target == "string":
61
            elif target == "string":
60
            return text
62
                return text
61
    
63
    
62
    def debug(self, text, indent = 0, target = None):
64
    def debug(self, text, indent = 0, target = None):
63
        if self.loglevel >= 3:
65
        if self.loglevel >= 3:
64
            self.log("DEBUG: " + text, indent, target)
66
            self.log("DEBUG: " + text, indent, target)
65
    
67
    
Line 70... Line 72...
70
    def warn(self, text, indent = 0, target = None):
72
    def warn(self, text, indent = 0, target = None):
71
        if self.loglevel >= 1:
73
        if self.loglevel >= 1:
72
            self.log("WARNING: " + text, indent, target)
74
            self.log("WARNING: " + text, indent, target)
73
    
75
    
74
    def error(self, text, indent = 0, target = None):
76
    def error(self, text, indent = 0, target = None):
-
 
77
        if self.loglevel >= 0:
75
        self.log("ERROR: " + text, indent, target)
78
            self.log("ERROR: " + text, indent, target)
76
 
79
 
77
 
80
 
78
class Bunch(dict):
81
class Bunch(dict):
79
    """
82
    """
80
        This is a dict whose items can also be accessed with
83
        This is a dict whose items can also be accessed with