67
68
_bzr_log_file = None
70
class QuietFormatter(logging.Formatter):
71
"""Formatter that supresses the details of errors.
73
This is used by default on stderr so as not to scare the user.
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.
79
def format(self, record):
80
if record.levelno >= logging.WARNING:
81
s = 'bzr: ' + record.levelname + ': '
84
s += record.getMessage()
86
s += '\n' + format_exception_short(record.exc_info)
89
71
# configure convenient aliases for output routines
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)
176
_bzr_logger.setLevel(logging.DEBUG)
199
179
def be_quiet(quiet=True):
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)
263
249
report_bug(exc_info, err_file)
280
266
"""Report an exception that probably indicates a bug in bzr"""
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)
285
271
traceback.print_exception(exc_type, exc_object, exc_tb, file=err_file)
273
print >>err_file, 'bzr %s invoked on python %s (%s)' % \
275
'.'.join(map(str, sys.version_info)),
277
print >>err_file, ' arguments: %r' % sys.argv
287
279
print >>err_file, "** please send this report to bazaar-ng@lists.ubuntu.com"
290
# TODO: Is this still used?
291
def format_exception_short(exc_info):
292
"""Make a short string form of an exception.
294
This is used for display to stderr. It specially handles exception
295
classes without useful string methods.
297
The result has no trailing newline, but does span a few lines and includes
298
the function and line.
300
:param exc_info: typically an exception from sys.exc_info()
302
exc_type, exc_object, exc_tb = exc_info
305
return '(no exception)'
306
if isinstance(exc_object, (BzrError, BzrNewError)):
307
return str(exc_object)
310
tb = traceback.extract_tb(exc_tb)
311
msg = '%s: %s' % (exc_type, exc_object)
315
msg += '\n at %s line %d\n in %s' % (tb[-1][:3])
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"