~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

  • Committer: Robert Collins
  • Date: 2006-11-08 00:36:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2124.
  • Revision ID: robertc@robertcollins.net-20061108003630-feb31613c83f7096
(Robert Collins) Extend the problem reporting command line UI to use
apport to report more detailed diagnostics which should help in in getting
faults reported in Malone and provides the basis for capturing more
information such as detailed logging data from the current invocation of
bzr in the future (without cluttering 'bzr.log' unnecessarily).
apport is available from Ubuntu Edgy onwards.

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
import os
 
54
import re
54
55
import sys
55
 
import re
56
56
 
57
57
from bzrlib.lazy_import import lazy_import
58
58
lazy_import(globals(), """
59
59
import errno
60
60
import logging
 
61
from cStringIO import StringIO
61
62
""")
62
63
 
63
64
import bzrlib
75
76
_trace_file = None
76
77
_trace_depth = 0
77
78
_bzr_log_file = None
 
79
_use_apport = True
78
80
 
79
81
 
80
82
# configure convenient aliases for output routines
279
281
        # specific, but there are too many cases at the moment.
280
282
        report_user_error(exc_info, err_file)
281
283
    else:
282
 
        report_bug(exc_info, err_file)
 
284
        return report_bug(exc_info, err_file)
283
285
 
284
286
 
285
287
# TODO: Should these be specially encoding the output?
296
298
 
297
299
def report_bug(exc_info, err_file):
298
300
    """Report an exception that probably indicates a bug in bzr"""
 
301
    # local import because its only needed here, and this is not a loop.
 
302
    import tempfile
 
303
    # local import because the other functions do it too.
 
304
    import traceback
 
305
    # local import due to circular dependency
 
306
    import bzrlib.plugin
 
307
    global _use_apport
 
308
    try:
 
309
        # detect apport presence.
 
310
        import apport_utils
 
311
        import problem_report
 
312
    except ImportError:
 
313
        # not present, dont use it.
 
314
        _use_apport = False
 
315
    if not _use_apport:
 
316
        # policy disabled, or not present, use the old ui.
 
317
        return _old_report_bug(exc_info, err_file)
 
318
 
 
319
    exc_type, exc_object, exc_tb = exc_info
 
320
    err_file.write(
 
321
        "bzr: ERROR: %s.%s: %s\n" % (
 
322
        exc_type.__module__, exc_type.__name__, exc_object)
 
323
        )
 
324
    report = problem_report.ProblemReport()
 
325
    report_file, report_filename = tempfile.mkstemp(
 
326
        suffix='.txt', prefix='bzr-crash-', dir='/tmp')
 
327
    python_report_file = os.fdopen(report_file, 'w')
 
328
    try:
 
329
        report['CommandLine'] = ' '.join(sys.argv)
 
330
        # assume we are packaged as bzr.
 
331
        apport_utils.report_add_package_info(report, 'bzr')
 
332
        report['BzrPlugins'] = ' '.join(bzrlib.plugin.all_plugins())
 
333
        tb_file = StringIO()
 
334
        traceback.print_exception(exc_type, exc_object, exc_tb, file=tb_file)
 
335
        report['Traceback'] = tb_file.getvalue()
 
336
        apport_utils.report_add_os_info(report)
 
337
        report.write(python_report_file)
 
338
        # give the user a pretty output.
 
339
 
 
340
        err_file.write(
 
341
            'This is an unexpected error within bzr and we would appreciate a bug report.\n'
 
342
            '\n'
 
343
            'bzr has written a crash report file that will assist our debugging of this\n'
 
344
            'in the file %s\n'
 
345
            '\n'
 
346
            'This is a plain text file, whose contents you can check if you have privacy\n'
 
347
            'concerns. We gather the package data about bzr, your command line, plugins\n'
 
348
            'And the backtrace from within bzr. If you had a password in the URL you\n'
 
349
            'provided to bzr, you should edit that file to remove the password.\n'
 
350
            '\n'
 
351
            '** To file a bug for this please visit our bugtracker at the URL \n'
 
352
            '"https://launchpad.net/products/bzr/+filebug" and report a bug describing\n'
 
353
            'what you were attempting and attach the bzr-crash file mentioned above.\n'
 
354
            'Alternatively you can email bazaar-ng@lists.canonical.com with the same\n'
 
355
            'description and attach the bzr-crash file to the email.\n' %
 
356
                report_filename
 
357
            )
 
358
    finally:
 
359
        python_report_file.close()
 
360
    return report, report_filename
 
361
 
 
362
def _old_report_bug(exc_info, err_file):
 
363
    """Write a synopsis of an exception that is probably a bug to err_file."""
299
364
    import traceback
300
365
    exc_type, exc_object, exc_tb = exc_info
301
366
    print >>err_file, "bzr: ERROR: %s.%s: %s" % (