Subversion Repositories freemyipod

Rev

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

Rev 398 Rev 400
Line 33... Line 33...
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 4 is most verbose, Loglevel 0: Only say something if there is an error.
36
        The log function doesn't care about the loglevel and always logs to stdout.
36
        The log function doesn't care about the loglevel and always logs to stdout.
37
    """
37
    """
-
 
38
    def __init__(self, loglevel = 3, logfile = "tools.log", target = "stdout"):
38
    def __init__(self):
39
        """
-
 
40
            loglevel: Possible values: 0 (only errors), 1 (warnings), 2 (info,
39
        # Possible values: 0 (only errors), 1 (warnings), 2 (info, recommended for production use), 3 and more (debug)
41
                      recommended for production use), 3 and more (debug)
-
 
42
            logfile: File to log to if using the target = "file"
-
 
43
            target: Default logging target. Can be "stdout", "file" or "string"
-
 
44
        """
40
        self.loglevel = 3
45
        self.loglevel = loglevel
-
 
46
        self.logfile = logfile
-
 
47
        self.target = target
41
        
48
        
42
    def log(self, text, indent = 0, target = "stdout"):
49
    def log(self, text, indent = 0, target = None):
-
 
50
        if target is None: target = self.target
43
        text = (indent * " ") + text
51
        text = (indent * " ") + text
44
        text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
52
        text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
45
        if target == "stdout":
53
        if target == "stdout":
46
            sys.stdout.write(text)
54
            sys.stdout.write(text)
-
 
55
        elif target == "file":
-
 
56
            with open(self.logfile, 'a') as f:
-
 
57
                f.write(text)
-
 
58
                f.close()
47
        elif target == "string":
59
        elif target == "string":
48
            return text
60
            return text
49
    
61
    
50
    def debug(self, text, indent = 0, target = "stdout"):
62
    def debug(self, text, indent = 0, target = None):
51
        if self.loglevel >= 3:
63
        if self.loglevel >= 3:
52
            self.log("DEBUG: " + text, indent, target)
64
            self.log("DEBUG: " + text, indent, target)
53
    
65
    
54
    def info(self, text, indent = 0, target = "stdout"):
66
    def info(self, text, indent = 0, target = None):
55
        if self.loglevel >= 2:
67
        if self.loglevel >= 2:
56
            self.log(text, indent, target)
68
            self.log(text, indent, target)
57
    
69
    
58
    def warn(self, text, indent = 0, target = "stdout"):
70
    def warn(self, text, indent = 0, target = None):
59
        if self.loglevel >= 1:
71
        if self.loglevel >= 1:
60
            self.log("WARNING: " + text, indent, target)
72
            self.log("WARNING: " + text, indent, target)
61
    
73
    
62
    def error(self, text, indent = 0, target = "stdout"):
74
    def error(self, text, indent = 0, target = None):
63
        self.log("ERROR: " + text, indent, target)
75
        self.log("ERROR: " + text, indent, target)
64
 
76
 
65
 
77
 
66
class Bunch(dict):
78
class Bunch(dict):
67
    """
79
    """