~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
import os
60
60
import sys
61
61
import time
 
62
import tempfile
62
63
 
63
64
from bzrlib.lazy_import import lazy_import
64
65
lazy_import(globals(), """
122
123
    # FIXME: clearing the ui and then going through the abstract logging
123
124
    # framework is whack; we should probably have a logging Handler that
124
125
    # deals with terminal output if needed.
125
 
    import bzrlib.ui
126
 
    bzrlib.ui.ui_factory.clear_term()
 
126
    ui.ui_factory.clear_term()
127
127
    _bzr_logger.info(*args, **kwargs)
128
128
 
129
129
 
130
130
def warning(*args, **kwargs):
131
 
    import bzrlib.ui
132
 
    bzrlib.ui.ui_factory.clear_term()
 
131
    ui.ui_factory.clear_term()
133
132
    _bzr_logger.warning(*args, **kwargs)
134
133
 
135
134
 
469
468
                    note(line)
470
469
                    break
471
470
 
 
471
def _dump_memory_usage(err_file):
 
472
    try:
 
473
        try:
 
474
            fd, name = tempfile.mkstemp(prefix="bzr_memdump", suffix=".json")
 
475
            dump_file = os.fdopen(fd, 'w')
 
476
            from meliae import scanner
 
477
            scanner.dump_gc_objects(dump_file)
 
478
            err_file.write("Memory dumped to %s\n" % name)
 
479
        except ImportError:
 
480
            err_file.write("Dumping memory requires meliae module.\n")
 
481
            log_exception_quietly()
 
482
        except:
 
483
            err_file.write("Exception while dumping memory.\n")
 
484
            log_exception_quietly()
 
485
    finally:
 
486
        if dump_file is not None:
 
487
            dump_file.close()
 
488
        elif fd is not None:
 
489
            os.close(fd)
 
490
 
 
491
 
 
492
def _qualified_exception_name(eclass, unqualified_bzrlib_errors=False):
 
493
    """Give name of error class including module for non-builtin exceptions
 
494
 
 
495
    If `unqualified_bzrlib_errors` is True, errors specific to bzrlib will
 
496
    also omit the module prefix.
 
497
    """
 
498
    class_name = eclass.__name__
 
499
    module_name = eclass.__module__
 
500
    if module_name in ("exceptions", "__main__") or (
 
501
            unqualified_bzrlib_errors and module_name == "bzrlib.errors"):
 
502
        return class_name
 
503
    return "%s.%s" % (module_name, class_name)
 
504
 
472
505
 
473
506
def report_exception(exc_info, err_file):
474
507
    """Report an exception to err_file (typically stderr) and to .bzr.log.
492
525
        return errors.EXIT_ERROR
493
526
    elif isinstance(exc_object, MemoryError):
494
527
        err_file.write("bzr: out of memory\n")
 
528
        if 'mem_dump' in debug.debug_flags:
 
529
            _dump_memory_usage(err_file)
 
530
        else:
 
531
            err_file.write("Use -Dmem_dump to dump memory to a file.\n")
495
532
        return errors.EXIT_ERROR
496
533
    elif isinstance(exc_object, ImportError) \
497
534
        and str(exc_object).startswith("No module named "):