~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 21:22:51 UTC
  • mto: This revision was merged to the branch mainline in revision 1797.
  • Revision ID: mbp@sourcefrog.net-20060604212251-8f5dc15da9189eac
When an unhandled exception occurs, write the traceback to stderr.

Also encourage the user to send this to the list.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, Canonical Ltd
 
1
# Copyright (C) 2005, 2006 by Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
2
16
 
3
17
"""Messages and logging for bazaar-ng.
4
18
 
32
46
form.
33
47
"""
34
48
 
35
 
# TODO: in debug mode, stderr should get full tracebacks and also
36
 
# debug messages.  (Is this really needed?)
37
 
 
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())
158
169
 
159
170
 
160
 
def log_exception(msg=None):
161
 
    """Log the last exception to stderr and the trace file.
162
 
 
163
 
    The exception string representation is used as the error
164
 
    summary, unless msg is given.
165
 
    """
166
 
    if msg:
167
 
        error(msg)
168
 
    else:
169
 
        exc_str = format_exception_short(sys.exc_info())
170
 
        error(exc_str)
171
 
    log_exception_quietly()
172
 
 
173
 
 
174
171
def log_exception_quietly():
175
172
    """Log the last exception to the trace file only.
176
173
 
259
256
        enable_default_logging()
260
257
 
261
258
 
 
259
def report_unhandled_exception(exc_info, err_file):
 
260
    """Report to stderr than an exception hit the top level.
 
261
 
 
262
    This is used only for exceptions that indicate a bug of some kind in bzr.
 
263
    """
 
264
    if isinstance(exc_info[1], (BzrError, BzrNewError)):
 
265
        report_user_error(exc_info, err_file)
 
266
    else:
 
267
        report_bug(exc_info, err_file)
 
268
 
 
269
 
 
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:",
 
274
    try:
 
275
        print >>err_file, str(exc_object)
 
276
    except Exception, formatting_exc:
 
277
        # XXX: is this really better than just letting it run up?
 
278
        print >>err_file, \
 
279
                '(error formatting exception of type %s: %s)' \
 
280
                % (exc_type, formatting_exc)
 
281
 
 
282
 
 
283
def report_bug(exc_info, err_file):
 
284
    import traceback
 
285
    exc_type, exc_object, exc_tb = exc_info
 
286
    print >>err_file, "bzr: unhandled error: %s: %s" % (exc_type, exc_object)
 
287
    print >>err_file
 
288
    traceback.print_exception(*exc_info)
 
289
    print >>err_file
 
290
    print >>err_file, "** please send this report to bazaar-ng@lists.ubuntu.com"
 
291
 
 
292
 
 
293
# TODO: Is this still used?
262
294
def format_exception_short(exc_info):
263
295
    """Make a short string form of an exception.
264
296
 
265
297
    This is used for display to stderr.  It specially handles exception
266
298
    classes without useful string methods.
267
299
 
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.
269
302
 
270
 
    exc_info - typically an exception from sys.exc_info()
 
303
    :param exc_info: typically an exception from sys.exc_info()
271
304
    """
272
305
    exc_type, exc_object, exc_tb = exc_info
273
306
    try: