~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-17 07:52:09 UTC
  • mfrom: (1910.3.4 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20060817075209-e85a1f9e05ff8b87
(andrew) Trivial fixes to NotImplemented errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 by 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
50
50
# is quite expensive, even when the message is not printed by any handlers.
51
51
# We should perhaps change back to just simply doing it here.
52
52
 
 
53
 
 
54
import errno
53
55
import os
54
56
import sys
55
 
import re
56
 
 
57
 
from bzrlib.lazy_import import lazy_import
58
 
lazy_import(globals(), """
59
 
from cStringIO import StringIO
60
 
import errno
61
 
import locale
62
57
import logging
63
 
import traceback
64
 
""")
65
58
 
66
59
import bzrlib
67
 
 
68
 
lazy_import(globals(), """
69
 
from bzrlib import (
70
 
    debug,
71
 
    errors,
72
 
    osutils,
73
 
    plugin,
74
 
    )
75
 
""")
 
60
from bzrlib.errors import BzrError, BzrNewError
 
61
from bzrlib.symbol_versioning import (deprecated_function,
 
62
        zero_nine,
 
63
        )
76
64
 
77
65
_file_handler = None
78
66
_stderr_handler = None
79
 
_verbosity_level = 0
 
67
_stderr_quiet = False
80
68
_trace_file = None
81
69
_trace_depth = 0
82
70
_bzr_log_file = None
83
 
_bzr_log_filename = None
84
71
 
85
72
 
86
73
# configure convenient aliases for output routines
106
93
def mutter(fmt, *args):
107
94
    if _trace_file is None:
108
95
        return
109
 
    if (getattr(_trace_file, 'closed', None) is not None) and _trace_file.closed:
 
96
    if hasattr(_trace_file, 'closed') and _trace_file.closed:
110
97
        return
111
 
 
112
 
    if isinstance(fmt, unicode):
113
 
        fmt = fmt.encode('utf8')
114
 
 
115
98
    if len(args) > 0:
116
99
        # It seems that if we do ascii % (unicode, ascii) we can
117
100
        # get a unicode cannot encode ascii error, so make sure that "fmt"
118
101
        # is a unicode string
119
 
        real_args = []
120
 
        for arg in args:
121
 
            if isinstance(arg, unicode):
122
 
                arg = arg.encode('utf8')
123
 
            real_args.append(arg)
124
 
        out = fmt % tuple(real_args)
 
102
        out = unicode(fmt) % args
125
103
    else:
126
104
        out = fmt
127
105
    out += '\n'
128
 
    _trace_file.write(out)
 
106
    try:
 
107
        _trace_file.write(out)
 
108
    except UnicodeError, e:
 
109
        warning('UnicodeError: %s', e)
 
110
        _trace_file.write(repr(out))
129
111
    # TODO: jam 20051227 Consider flushing the trace file to help debugging
130
112
    #_trace_file.flush()
131
 
 
132
 
 
133
 
def mutter_callsite(stacklevel, fmt, *args):
134
 
    """Perform a mutter of fmt and args, logging the call trace.
135
 
 
136
 
    :param stacklevel: The number of frames to show. None will show all
137
 
        frames.
138
 
    :param fmt: The format string to pass to mutter.
139
 
    :param args: A list of substitution variables.
140
 
    """
141
 
    outf = StringIO()
142
 
    traceback.print_stack(limit=stacklevel + 1, file=outf)
143
 
    formatted_lines = outf.getvalue().splitlines()
144
 
    formatted_stack = '\n'.join(formatted_lines[:-2])
145
 
    mutter(fmt + "\nCalled from:\n%s", *(args + (formatted_stack,)))
 
113
debug = mutter
146
114
 
147
115
 
148
116
def _rollover_trace_maybe(trace_fname):
152
120
        if size <= 4 << 20:
153
121
            return
154
122
        old_fname = trace_fname + '.old'
155
 
        osutils.rename(trace_fname, old_fname)
 
123
        from osutils import rename
 
124
        rename(trace_fname, old_fname)
156
125
    except OSError:
157
126
        return
158
127
 
159
128
 
160
 
def open_tracefile(tracefilename=None):
 
129
def open_tracefile(tracefilename='~/.bzr.log'):
161
130
    # Messages are always written to here, so that we have some
162
131
    # information if something goes wrong.  In a future version this
163
132
    # file will be removed on successful completion.
164
 
    global _file_handler, _bzr_log_file, _bzr_log_filename
 
133
    global _file_handler, _bzr_log_file
165
134
    import codecs
166
135
 
167
 
    if tracefilename is None:
168
 
        if sys.platform == 'win32':
169
 
            from bzrlib import win32utils
170
 
            home = win32utils.get_home_location()
171
 
        else:
172
 
            home = os.path.expanduser('~')
173
 
        _bzr_log_filename = os.path.join(home, '.bzr.log')
174
 
    else:
175
 
        _bzr_log_filename = tracefilename
176
 
 
177
 
    _bzr_log_filename = os.path.expanduser(_bzr_log_filename)
178
 
    _rollover_trace_maybe(_bzr_log_filename)
 
136
    trace_fname = os.path.join(os.path.expanduser(tracefilename))
 
137
    _rollover_trace_maybe(trace_fname)
179
138
    try:
180
139
        LINE_BUFFERED = 1
181
 
        #tf = codecs.open(trace_fname, 'at', 'utf8', buffering=LINE_BUFFERED)
182
 
        tf = open(_bzr_log_filename, 'at', LINE_BUFFERED)
 
140
        tf = codecs.open(trace_fname, 'at', 'utf8', buffering=LINE_BUFFERED)
183
141
        _bzr_log_file = tf
184
 
        # tf.tell() on windows always return 0 until some writing done
185
 
        tf.write('\n')
186
 
        if tf.tell() <= 2:
187
 
            tf.write("this is a debug log for diagnosing/reporting problems in bzr\n")
 
142
        if tf.tell() == 0:
 
143
            tf.write("\nthis is a debug log for diagnosing/reporting problems in bzr\n")
188
144
            tf.write("you can delete or truncate this file, or include sections in\n")
189
 
            tf.write("bug reports to bazaar@lists.canonical.com\n\n")
 
145
            tf.write("bug reports to bazaar-ng@lists.canonical.com\n\n")
190
146
        _file_handler = logging.StreamHandler(tf)
191
147
        fmt = r'[%(process)5d] %(asctime)s.%(msecs)03d %(levelname)s: %(message)s'
192
148
        datefmt = r'%a %H:%M:%S'
197
153
        warning("failed to open trace file: %s" % (e))
198
154
 
199
155
 
 
156
@deprecated_function(zero_nine)
 
157
def log_exception(msg=None):
 
158
    """Log the last exception to stderr and the trace file.
 
159
 
 
160
    The exception string representation is used as the error
 
161
    summary, unless msg is given.
 
162
    """
 
163
    if msg:
 
164
        error(msg)
 
165
    else:
 
166
        exc_str = format_exception_short(sys.exc_info())
 
167
        error(exc_str)
 
168
    log_exception_quietly()
 
169
 
 
170
 
200
171
def log_exception_quietly():
201
172
    """Log the last exception to the trace file only.
202
173
 
205
176
    errors loading plugins.
206
177
    """
207
178
    import traceback
208
 
    mutter(traceback.format_exc())
 
179
    debug(traceback.format_exc())
209
180
 
210
181
 
211
182
def enable_default_logging():
223
194
    _bzr_logger.setLevel(logging.DEBUG)
224
195
 
225
196
 
226
 
def set_verbosity_level(level):
227
 
    """Set the verbosity level.
228
 
 
229
 
    :param level: -ve for quiet, 0 for normal, +ve for verbose
230
 
    """
231
 
    global _verbosity_level
232
 
    _verbosity_level = level
233
 
    _update_logging_level(level < 0)
234
 
 
235
 
 
236
 
def get_verbosity_level():
237
 
    """Get the verbosity level.
238
 
 
239
 
    See set_verbosity_level() for values.
240
 
    """
241
 
    return _verbosity_level
242
 
 
243
 
 
244
197
def be_quiet(quiet=True):
245
 
    # Perhaps this could be deprecated now ...
246
 
    if quiet:
247
 
        set_verbosity_level(-1)
248
 
    else:
249
 
        set_verbosity_level(0)
250
 
 
251
 
 
252
 
def _update_logging_level(quiet=True):
253
 
    """Hide INFO messages if quiet."""
 
198
    global _stderr_handler, _stderr_quiet
 
199
    
 
200
    _stderr_quiet = quiet
254
201
    if quiet:
255
202
        _stderr_handler.setLevel(logging.WARNING)
256
203
    else:
258
205
 
259
206
 
260
207
def is_quiet():
261
 
    """Is the verbosity level negative?"""
262
 
    return _verbosity_level < 0
263
 
 
264
 
 
265
 
def is_verbose():
266
 
    """Is the verbosity level positive?"""
267
 
    return _verbosity_level > 0
 
208
    global _stderr_quiet
 
209
    return _stderr_quiet
268
210
 
269
211
 
270
212
def disable_default_logging():
313
255
 
314
256
 
315
257
def report_exception(exc_info, err_file):
316
 
    """Report an exception to err_file (typically stderr) and to .bzr.log.
317
 
 
318
 
    This will show either a full traceback or a short message as appropriate.
319
 
 
320
 
    :return: The appropriate exit code for this error.
321
 
    """
322
258
    exc_type, exc_object, exc_tb = exc_info
323
259
    # Log the full traceback to ~/.bzr.log
324
260
    log_exception_quietly()
325
261
    if (isinstance(exc_object, IOError)
326
262
        and getattr(exc_object, 'errno', None) == errno.EPIPE):
327
 
        err_file.write("bzr: broken pipe\n")
328
 
        return errors.EXIT_ERROR
 
263
        print >>err_file, "bzr: broken pipe"
329
264
    elif isinstance(exc_object, KeyboardInterrupt):
330
 
        err_file.write("bzr: interrupted\n")
331
 
        return errors.EXIT_ERROR
332
 
    elif not getattr(exc_object, 'internal_error', True):
 
265
        print >>err_file, "bzr: interrupted"
 
266
    elif getattr(exc_object, 'is_user_error', False):
333
267
        report_user_error(exc_info, err_file)
334
 
        return errors.EXIT_ERROR
335
268
    elif isinstance(exc_object, (OSError, IOError)):
336
269
        # Might be nice to catch all of these and show them as something more
337
270
        # specific, but there are too many cases at the moment.
338
271
        report_user_error(exc_info, err_file)
339
 
        return errors.EXIT_ERROR
340
272
    else:
341
273
        report_bug(exc_info, err_file)
342
 
        return errors.EXIT_INTERNAL_ERROR
343
274
 
344
275
 
345
276
# TODO: Should these be specially encoding the output?
346
277
def report_user_error(exc_info, err_file):
347
 
    """Report to err_file an error that's not an internal error.
348
 
 
349
 
    These don't get a traceback unless -Derror was given.
350
 
    """
351
 
    if 'error' in debug.debug_flags:
352
 
        report_bug(exc_info, err_file)
353
 
        return
354
 
    err_file.write("bzr: ERROR: %s\n" % (exc_info[1],))
 
278
    print >>err_file, "bzr: ERROR:", str(exc_info[1])
355
279
 
356
280
 
357
281
def report_bug(exc_info, err_file):
358
282
    """Report an exception that probably indicates a bug in bzr"""
359
283
    import traceback
360
284
    exc_type, exc_object, exc_tb = exc_info
361
 
    err_file.write("bzr: ERROR: %s.%s: %s\n" % (
362
 
        exc_type.__module__, exc_type.__name__, exc_object))
363
 
    err_file.write('\n')
 
285
    print >>err_file, "bzr: ERROR: %s: %s" % (exc_type, exc_object)
 
286
    print >>err_file
364
287
    traceback.print_exception(exc_type, exc_object, exc_tb, file=err_file)
365
 
    err_file.write('\n')
366
 
    err_file.write('bzr %s on python %s (%s)\n' % \
 
288
    print >>err_file
 
289
    print >>err_file, 'bzr %s on python %s (%s)' % \
367
290
                       (bzrlib.__version__,
368
291
                        '.'.join(map(str, sys.version_info)),
369
 
                        sys.platform))
370
 
    err_file.write('arguments: %r\n' % sys.argv)
371
 
    err_file.write(
372
 
        'encoding: %r, fsenc: %r, lang: %r\n' % (
373
 
            osutils.get_user_encoding(), sys.getfilesystemencoding(),
374
 
            os.environ.get('LANG')))
375
 
    err_file.write("plugins:\n")
376
 
    for name, a_plugin in sorted(plugin.plugins().items()):
377
 
        err_file.write("  %-20s %s [%s]\n" %
378
 
            (name, a_plugin.path(), a_plugin.__version__))
379
 
    err_file.write(
380
 
        "\n"
381
 
        "** Please send this report to bazaar@lists.ubuntu.com\n"
382
 
        "   with a description of what you were doing when the\n"
383
 
        "   error occurred.\n"
384
 
        )
 
292
                        sys.platform)
 
293
    print >>err_file, 'arguments: %r' % sys.argv
 
294
    print >>err_file
 
295
    print >>err_file, "** please send this report to bazaar-ng@lists.ubuntu.com"