~mterry/duplicity/list-old-chains-0.6

« back to all changes in this revision

Viewing changes to duplicity/log.py

  • Committer: loafman
  • Date: 2008-12-15 12:35:17 UTC
  • Revision ID: vcs-imports@canonical.com-20081215123517-3xr8dy4tl3lab4tp
patch #6692: Print collection status in a machine-readable way
https://savannah.nongnu.org/patch/?6692

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
       more severe"""
46
46
    return DupToLoggerLevel(verb)
47
47
 
48
 
def Log(s, verb_level, code=1, extra=None):
 
48
def Log(s, verb_level, code=1, extra=None, force_print=False):
49
49
    """Write s to stderr if verbosity level low enough"""
50
50
    global _logger
51
51
    # controlLine is a terrible hack until duplicity depends on Python 2.5
56
56
        _logger.controlLine = '%d' % (code)
57
57
    if not s:
58
58
        s = '' # If None is passed, standard logging would render it as 'None'
 
59
    
 
60
    if force_print:
 
61
        initial_level = _logger.getEffectiveLevel()
 
62
        _logger.setLevel(DupToLoggerLevel(MAX))
 
63
    
59
64
    _logger.log(DupToLoggerLevel(verb_level), s)
60
65
    _logger.controlLine = None
 
66
    
 
67
    if force_print:
 
68
        _logger.setLevel(initial_level)
61
69
 
62
70
def Debug(s):
63
71
    """Shortcut used for debug message (verbosity 9)."""
69
77
       Don't use 0 or negative numbers."""
70
78
    generic = 1
71
79
    progress = 2
 
80
    collection_status = 3
72
81
 
73
82
def Info(s, code=InfoCode.generic):
74
83
    """Shortcut used for info messages (verbosity 5)."""
82
91
        controlLine = '%d' % current
83
92
    Log(s, INFO, InfoCode.progress, controlLine)
84
93
 
 
94
def PrintCollectionStatus(col_stats, force_print=False):
 
95
    """Prints a collection status to the log"""
 
96
    Log(str(col_stats), 8, InfoCode.collection_status,
 
97
        '\n' + '\n'.join(col_stats.to_log_info()), force_print)
 
98
 
85
99
def Notice(s):
86
100
    """Shortcut used for notice messages (verbosity 3, the default)."""
87
101
    Log(s, NOTICE)
170
184
    _logger = logging.getLogger("duplicity")
171
185
    
172
186
    # Set up our special level names
173
 
    logging.addLevelName(DupToLoggerLevel(ERROR), "ERROR")
174
 
    logging.addLevelName(DupToLoggerLevel(WARNING), "WARNING")
175
 
    logging.addLevelName(DupToLoggerLevel(NOTICE), "NOTICE")
176
 
    logging.addLevelName(DupToLoggerLevel(INFO), "INFO")
177
 
    logging.addLevelName(DupToLoggerLevel(DEBUG), "DEBUG")
 
187
    logging.addLevelName(DupToLoggerLevel(0), "ERROR")
 
188
    logging.addLevelName(DupToLoggerLevel(1), "WARNING")
 
189
    logging.addLevelName(DupToLoggerLevel(2), "WARNING")
 
190
    logging.addLevelName(DupToLoggerLevel(3), "NOTICE")
 
191
    logging.addLevelName(DupToLoggerLevel(4), "NOTICE")
 
192
    logging.addLevelName(DupToLoggerLevel(5), "INFO")
 
193
    logging.addLevelName(DupToLoggerLevel(6), "INFO")
 
194
    logging.addLevelName(DupToLoggerLevel(7), "INFO")
 
195
    logging.addLevelName(DupToLoggerLevel(8), "INFO")
 
196
    logging.addLevelName(DupToLoggerLevel(9), "DEBUG")
178
197
    
179
198
    # Default verbosity allows notices and above
180
199
    setverbosity(NOTICE)
192
211
    """Formatter that creates messages in a syntax easily consumable by other
193
212
       processes."""
194
213
    def __init__(self):
195
 
        logging.Formatter.__init__(self, "%(levelname)s %(controlLine)s\n%(message)s")
 
214
        # 'message' will be appended by format()
 
215
        logging.Formatter.__init__(self, "%(levelname)s %(controlLine)s")
196
216
    
197
217
    def format(self, record):
198
218
        s = logging.Formatter.format(self, record)
199
 
        # Indent each extra line with a dot and space so that the consumer
200
 
        # knows it's a continuation, not a new message.  Add a newline so
201
 
        # consumers know the message is over.
202
 
        # We rstrip in case 'message' was empty.
203
 
        return s.rstrip().replace('\n', '\n. ') + '\n'
 
219
        
 
220
        # Add user-text hint of 'message' back in, with each line prefixed by a
 
221
        # dot, so consumers know it's not part of 'controlLine'
 
222
        if record.message:
 
223
            s += ('\n' + record.message).replace('\n', '\n. ')
 
224
        
 
225
        # Add a newline so consumers know the message is over.
 
226
        return s + '\n'
204
227
 
205
228
class MachineFilter(logging.Filter):
206
229
    """Filter that only allows levels that are consumable by other processes."""