~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

first cut at merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
Exceptions are reported in a brief form to stderr so as not to look scary.
29
29
BzrErrors are required to be able to format themselves into a properly
30
 
explanatory message.  This is not true for builtin exceptions such as
 
30
explanatory message.  This is not true for builtin excexceptions such as
31
31
KeyError, which typically just str to "0".  They're printed in a different
32
32
form.
33
33
"""
52
52
_stderr_handler = None
53
53
_stderr_quiet = False
54
54
_trace_file = None
55
 
_trace_depth = 0
56
55
_bzr_log_file = None
57
56
 
58
57
 
59
58
class QuietFormatter(logging.Formatter):
60
 
    """Formatter that suppresses the details of errors.
 
59
    """Formatter that supresses the details of errors.
61
60
 
62
61
    This is used by default on stderr so as not to scare the user.
63
62
    """
64
 
    # At first I tried overriding FormatException to suppress the
 
63
    # At first I tried overriding formatException to suppress the
65
64
    # exception details, but that has global effect: no loggers
66
65
    # can get the exception details is we suppress them here.
67
66
 
79
78
 
80
79
_bzr_logger = logging.getLogger('bzr')
81
80
 
82
 
def note(*args, **kwargs):
83
 
    import bzrlib.ui
84
 
    bzrlib.ui.ui_factory.clear_term()
85
 
    _bzr_logger.info(*args, **kwargs)
86
 
 
87
 
def warning(*args, **kwargs):
88
 
    import bzrlib.ui
89
 
    bzrlib.ui.ui_factory.clear_term()
90
 
    _bzr_logger.warning(*args, **kwargs)
91
 
 
92
 
info = note
 
81
info = note = _bzr_logger.info
 
82
warning =   _bzr_logger.warning
93
83
log_error = _bzr_logger.error
94
84
error =     _bzr_logger.error
95
85
 
100
90
    if hasattr(_trace_file, 'closed') and _trace_file.closed:
101
91
        return
102
92
    if len(args) > 0:
103
 
        # It seems that if we do ascii % (unicode, ascii) we can
104
 
        # get a unicode cannot encode ascii error, so make sure that "fmt"
105
 
        # is a unicode string
106
 
        out = unicode(fmt) % args
 
93
        out = fmt % args
107
94
    else:
108
95
        out = fmt
109
96
    out += '\n'
110
 
    try:
111
 
        _trace_file.write(out)
112
 
    except UnicodeError, e:
113
 
        warning('UnicodeError: %s', e)
114
 
        _trace_file.write(repr(out))
115
 
    # TODO: jam 20051227 Consider flushing the trace file to help debugging
116
 
    #_trace_file.flush()
 
97
    _trace_file.write(out)
117
98
debug = mutter
118
99
 
119
100
 
241
222
    """Redirect logging to a temporary file for a test
242
223
    
243
224
    returns an opaque reference that should be passed to disable_test_log
244
 
    after the test completes.
 
225
    after the test complete.
245
226
    """
246
227
    disable_default_logging()
247
228
    global _trace_file
248
 
    global _trace_depth
249
229
    hdlr = logging.StreamHandler(to_file)
250
230
    hdlr.setLevel(logging.DEBUG)
251
231
    hdlr.setFormatter(logging.Formatter('%(levelname)8s  %(message)s'))
252
232
    _bzr_logger.addHandler(hdlr)
253
233
    _bzr_logger.setLevel(logging.DEBUG)
254
 
    result = hdlr, _trace_file, _trace_depth
 
234
    result = hdlr, _trace_file
255
235
    _trace_file = to_file
256
 
    _trace_depth += 1
257
236
    return result
258
237
 
259
238
 
260
 
def disable_test_log((test_log_hdlr, old_trace_file, old_trace_depth)):
 
239
def disable_test_log((test_log_hdlr, old_trace_file)):
261
240
    _bzr_logger.removeHandler(test_log_hdlr)
262
 
    test_log_hdlr.close()
263
 
    global _trace_file
264
 
    global _trace_depth
265
241
    _trace_file = old_trace_file
266
 
    _trace_depth = old_trace_depth
267
 
    if not _trace_depth:
268
 
        enable_default_logging()
 
242
    enable_default_logging()
269
243
 
270
244
 
271
245
def format_exception_short(exc_info):