~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

  • Committer: Karl Bielefeldt
  • Date: 2010-09-30 03:10:29 UTC
  • mto: (5483.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5484.
  • Revision ID: 7mq3cbbd9q@snkmail.com-20100930031029-cg5odj02snl0jwe0
Incorporated suggestions from code review:

* Require -Dmem_dump to dump memory
* Put dump file in temp directory
* Print file name and prompt to enable -Dmem_dump instead of just logging
* Log exceptions that may occur during memory dump

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(), """
469
470
                    note(line)
470
471
                    break
471
472
 
472
 
def _dump_memory_usage():
 
473
def _dump_memory_usage(err_file):
473
474
    try:
 
475
        fd, name = tempfile.mkstemp(prefix="bzr_memdump", suffix=".json")
 
476
        dump_file = os.fdopen(fd, 'w')
474
477
        from meliae import scanner
475
 
        scanner.dump_all_objects('bzr_memory_reference_dump.json')
476
 
        mutter("Dumped memory to bzr_memory_reference_dump.json")
 
478
        scanner.dump_all_objects(dump_file)
 
479
        err_file.write("Memory dumped to %s\n" % name)
477
480
    except ImportError:
478
 
        mutter("Unable to dump memory.  Please install the meliae module.")
479
 
        return
 
481
        err_file.write("Dumping memory requires meliae module.\n")
 
482
        log_exception_quietly()
480
483
    except:
481
 
        mutter("Exception while dumping memory to bzr_memory_reference_dump.json")
 
484
        err_file.write("Exception while dumping memory.\n")
 
485
        log_exception_quietly()
 
486
    finally:
 
487
        dump_file.close()
482
488
 
483
489
def report_exception(exc_info, err_file):
484
490
    """Report an exception to err_file (typically stderr) and to .bzr.log.
501
507
        err_file.write("bzr: interrupted\n")
502
508
        return errors.EXIT_ERROR
503
509
    elif isinstance(exc_object, MemoryError):
504
 
        _dump_memory_usage()
505
510
        err_file.write("bzr: out of memory\n")
 
511
        if 'mem_dump' in debug.debug_flags:
 
512
            _dump_memory_usage(err_file)
 
513
        else:
 
514
            err_file.write("Use -Dmem_dump to dump memory to a file.\n")
506
515
        return errors.EXIT_ERROR
507
516
    elif isinstance(exc_object, ImportError) \
508
517
        and str(exc_object).startswith("No module named "):