~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

  • Committer: Martin Pool
  • Date: 2006-06-04 22:04:20 UTC
  • mto: This revision was merged to the branch mainline in revision 1797.
  • Revision ID: mbp@sourcefrog.net-20060604220420-6bab3e2b6ebe013e
Cleanup more exception-formatting code

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
# We should perhaps change back to just simply doing it here.
52
52
 
53
53
 
 
54
import errno
 
55
import os
54
56
import sys
55
 
import os
56
57
import logging
57
58
 
58
59
import bzrlib
67
68
_bzr_log_file = None
68
69
 
69
70
 
70
 
class QuietFormatter(logging.Formatter):
71
 
    """Formatter that supresses the details of errors.
72
 
 
73
 
    This is used by default on stderr so as not to scare the user.
74
 
    """
75
 
    # At first I tried overriding formatException to suppress the
76
 
    # exception details, but that has global effect: no loggers
77
 
    # can get the exception details is we suppress them here.
78
 
 
79
 
    def format(self, record):
80
 
        if record.levelno >= logging.WARNING:
81
 
            s = 'bzr: ' + record.levelname + ': '
82
 
        else:
83
 
            s = ''
84
 
        s += record.getMessage()
85
 
        if record.exc_info:
86
 
            s += '\n' + format_exception_short(record.exc_info)
87
 
        return s
88
 
        
89
71
# configure convenient aliases for output routines
90
72
 
91
73
_bzr_logger = logging.getLogger('bzr')
184
166
    # FIXME: if this is run twice, things get confused
185
167
    global _stderr_handler, _file_handler, _trace_file, _bzr_log_file
186
168
    _stderr_handler = logging.StreamHandler()
187
 
    _stderr_handler.setFormatter(QuietFormatter())
188
169
    logging.getLogger('').addHandler(_stderr_handler)
189
170
    _stderr_handler.setLevel(logging.INFO)
190
171
    if not _file_handler:
192
173
    _trace_file = _bzr_log_file
193
174
    if _file_handler:
194
175
        _file_handler.setLevel(logging.DEBUG)
195
 
    _bzr_logger.setLevel(logging.DEBUG) 
196
 
 
 
176
    _bzr_logger.setLevel(logging.DEBUG)
197
177
 
198
178
 
199
179
def be_quiet(quiet=True):
257
237
 
258
238
 
259
239
def report_exception(exc_info, err_file):
260
 
    if isinstance(exc_info[1], (BzrError, BzrNewError)):
 
240
    exc_type, exc_object, exc_tb = exc_info
 
241
    if (isinstance(exc_object, IOError)
 
242
        and getattr(exc_object, 'errno', None) == errno.EPIPE):
 
243
        print >>err_file, "bzr: broken pipe"
 
244
    elif isinstance(exc_object, KeyboardInterrupt):
 
245
        print >>err_file, "bzr: interrupted"
 
246
    elif isinstance(exc_info[1], (BzrError, BzrNewError)):
261
247
        report_user_error(exc_info, err_file)
262
248
    else:
263
249
        report_bug(exc_info, err_file)
280
266
    """Report an exception that probably indicates a bug in bzr"""
281
267
    import traceback
282
268
    exc_type, exc_object, exc_tb = exc_info
283
 
    print >>err_file, "bzr: unhandled error: %s: %s" % (exc_type, exc_object)
 
269
    print >>err_file, "bzr: ERROR: %s: %s" % (exc_type, exc_object)
284
270
    print >>err_file
285
271
    traceback.print_exception(exc_type, exc_object, exc_tb, file=err_file)
286
272
    print >>err_file
 
273
    print >>err_file, 'bzr %s invoked on python %s (%s)' % \
 
274
                       (bzrlib.__version__,
 
275
                        '.'.join(map(str, sys.version_info)),
 
276
                        sys.platform)
 
277
    print >>err_file, '  arguments: %r' % sys.argv
 
278
    print >>err_file
287
279
    print >>err_file, "** please send this report to bazaar-ng@lists.ubuntu.com"
288
 
 
289
 
 
290
 
# TODO: Is this still used?
291
 
def format_exception_short(exc_info):
292
 
    """Make a short string form of an exception.
293
 
 
294
 
    This is used for display to stderr.  It specially handles exception
295
 
    classes without useful string methods.
296
 
 
297
 
    The result has no trailing newline, but does span a few lines and includes
298
 
    the function and line.
299
 
 
300
 
    :param exc_info: typically an exception from sys.exc_info()
301
 
    """
302
 
    exc_type, exc_object, exc_tb = exc_info
303
 
    try:
304
 
        if exc_type is None:
305
 
            return '(no exception)'
306
 
        if isinstance(exc_object, (BzrError, BzrNewError)):
307
 
            return str(exc_object)
308
 
        else:
309
 
            import traceback
310
 
            tb = traceback.extract_tb(exc_tb)
311
 
            msg = '%s: %s' % (exc_type, exc_object)
312
 
            if msg[-1] == '\n':
313
 
                msg = msg[:-1]
314
 
            if tb:
315
 
                msg += '\n  at %s line %d\n  in %s' % (tb[-1][:3])
316
 
            return msg
317
 
    except Exception, formatting_exc:
318
 
        # XXX: is this really better than just letting it run up?
319
 
        return '(error formatting exception of type %s: %s)' \
320
 
                % (exc_type, formatting_exc)
 
280
    print >>err_file, "   with a description of how and when the problem occurred"