1
# Copyright (C) 2005, Canonical Ltd
1
# Copyright (C) 2005, 2006 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3
17
"""Messages and logging for bazaar-ng.
35
# TODO: in debug mode, stderr should get full tracebacks and also
36
# debug messages. (Is this really needed?)
38
49
# FIXME: Unfortunately it turns out that python's logging module
39
50
# is quite expensive, even when the message is not printed by any handlers.
40
51
# We should perhaps change back to just simply doing it here.
157
168
debug(' working dir: %r', os.getcwdu())
160
def log_exception(msg=None):
161
"""Log the last exception to stderr and the trace file.
163
The exception string representation is used as the error
164
summary, unless msg is given.
169
exc_str = format_exception_short(sys.exc_info())
171
log_exception_quietly()
174
171
def log_exception_quietly():
175
172
"""Log the last exception to the trace file only.
259
256
enable_default_logging()
259
def report_unhandled_exception(exc_info, err_file):
260
"""Report to stderr than an exception hit the top level.
262
This is used only for exceptions that indicate a bug of some kind in bzr.
264
if isinstance(exc_info[1], (BzrError, BzrNewError)):
265
report_user_error(exc_info, err_file)
267
report_bug(exc_info, err_file)
270
# TODO: Should these be specially encoding the output?
271
def report_user_error(exc_info, err_file):
272
exc_type, exc_object, exc_tb = exc_info
273
print >>err_file, "bzr:",
275
print >>err_file, str(exc_object)
276
except Exception, formatting_exc:
277
# XXX: is this really better than just letting it run up?
279
'(error formatting exception of type %s: %s)' \
280
% (exc_type, formatting_exc)
283
def report_bug(exc_info, err_file):
285
exc_type, exc_object, exc_tb = exc_info
286
print >>err_file, "bzr: unhandled error: %s: %s" % (exc_type, exc_object)
288
traceback.print_exception(*exc_info)
290
print >>err_file, "** please send this report to bazaar-ng@lists.ubuntu.com"
293
# TODO: Is this still used?
262
294
def format_exception_short(exc_info):
263
295
"""Make a short string form of an exception.
265
297
This is used for display to stderr. It specially handles exception
266
298
classes without useful string methods.
268
The result has no trailing newline.
300
The result has no trailing newline, but does span a few lines and includes
301
the function and line.
270
exc_info - typically an exception from sys.exc_info()
303
:param exc_info: typically an exception from sys.exc_info()
272
305
exc_type, exc_object, exc_tb = exc_info