~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-17 17:11:16 UTC
  • mfrom: (4797.2.17 2.1)
  • mto: (4797.2.18 2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: john@arbash-meinel.com-20100217171116-h7t9223ystbnx5h8
merge bzr.2.1 in preparation for NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Messages and logging for bazaar-ng.
 
17
"""Messages and logging.
18
18
 
19
19
Messages are supplied by callers as a string-formatting template, plus values
20
20
to be inserted into it.  The actual %-formatting is deferred to the log
33
33
 
34
34
Output to stderr depends on the mode chosen by the user.  By default, messages
35
35
of info and above are sent out, which results in progress messages such as the
36
 
list of files processed by add and commit.  In quiet mode, only warnings and
37
 
above are shown.  In debug mode, stderr gets debug messages too.
 
36
list of files processed by add and commit.  In debug mode, stderr gets debug messages too.
38
37
 
39
38
Errors that terminate an operation are generally passed back as exceptions;
40
39
others may be just emitted as messages.
83
82
    osutils,
84
83
    plugin,
85
84
    symbol_versioning,
 
85
    ui,
86
86
    )
87
87
""")
88
88
 
153
153
    _bzr_logger.error(*args, **kwargs)
154
154
 
155
155
 
156
 
_last_mutter_flush_time = None
157
 
 
158
 
 
159
156
def mutter(fmt, *args):
160
 
    global _last_mutter_flush_time
161
157
    if _trace_file is None:
162
158
        return
 
159
    # XXX: Don't check this every time; instead anyone who closes the file
 
160
    # ought to deregister it.  We can tolerate None.
163
161
    if (getattr(_trace_file, 'closed', None) is not None) and _trace_file.closed:
164
162
        return
165
163
 
182
180
    timestamp = '%0.3f  ' % (now - _bzr_log_start_time,)
183
181
    out = timestamp + out + '\n'
184
182
    _trace_file.write(out)
185
 
    # We flush if we haven't flushed for a few seconds. We don't want to flush
186
 
    # on every mutter, but when a command takes a while, it can be nice to see
187
 
    # updates in the debug log.
188
 
    if (_last_mutter_flush_time is None
189
 
        or (now - _last_mutter_flush_time) > 2.0):
190
 
        flush = getattr(_trace_file, 'flush', None)
191
 
        if flush is not None:
192
 
            flush()
193
 
        _last_mutter_flush_time = now
 
183
    # there's no explicit flushing; the file is typically line buffered.
194
184
 
195
185
 
196
186
def mutter_callsite(stacklevel, fmt, *args):
251
241
    _bzr_log_filename = _get_bzr_log_filename()
252
242
    _rollover_trace_maybe(_bzr_log_filename)
253
243
    try:
254
 
        bzr_log_file = open(_bzr_log_filename, 'at', 1) # line buffered
 
244
        bzr_log_file = open(_bzr_log_filename, 'at', buffering=0) # unbuffered
255
245
        # bzr_log_file.tell() on windows always return 0 until some writing done
256
246
        bzr_log_file.write('\n')
257
247
        if bzr_log_file.tell() <= 2:
260
250
            bzr_log_file.write("bug reports to https://bugs.launchpad.net/bzr/+filebug\n\n")
261
251
        return bzr_log_file
262
252
    except IOError, e:
263
 
        warning("failed to open trace file: %s" % (e))
 
253
        # If we are failing to open the log, then most likely logging has not
 
254
        # been set up yet. So we just write to stderr rather than using
 
255
        # 'warning()'. If we using warning(), users get the unhelpful 'no
 
256
        # handlers registered for "bzr"' when something goes wrong on the
 
257
        # server. (bug #503886)
 
258
        sys.stderr.write("failed to open trace file: %s\n" % (e,))
264
259
    # TODO: What should happen if we fail to open the trace file?  Maybe the
265
260
    # objects should be pointed at /dev/null or the equivalent?  Currently
266
261
    # returns None which will cause failures later.
370
365
    global _verbosity_level
371
366
    _verbosity_level = level
372
367
    _update_logging_level(level < 0)
 
368
    ui.ui_factory.be_quiet(level < 0)
373
369
 
374
370
 
375
371
def get_verbosity_level():
381
377
 
382
378
 
383
379
def be_quiet(quiet=True):
384
 
    # Perhaps this could be deprecated now ...
385
380
    if quiet:
386
381
        set_verbosity_level(-1)
387
382
    else:
445
440
 
446
441
    :return: The appropriate exit code for this error.
447
442
    """
448
 
    exc_type, exc_object, exc_tb = exc_info
449
443
    # Log the full traceback to ~/.bzr.log
450
444
    log_exception_quietly()
 
445
    if 'error' in debug.debug_flags:
 
446
        print_exception(exc_info, err_file)
 
447
        return errors.EXIT_ERROR
 
448
    exc_type, exc_object, exc_tb = exc_info
451
449
    if (isinstance(exc_object, IOError)
452
450
        and getattr(exc_object, 'errno', None) == errno.EPIPE):
453
451
        err_file.write("bzr: broken pipe\n")
494
492
    :param advice: Extra advice to the user to be printed following the
495
493
        exception.
496
494
    """
497
 
    if 'error' in debug.debug_flags:
498
 
        print_exception(exc_info, err_file)
499
 
        return
500
495
    err_file.write("bzr: ERROR: %s\n" % (exc_info[1],))
501
496
    if advice:
502
497
        err_file.write("%s\n" % (advice,))