Subversion Repositories freemyipod

Rev

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

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