~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

Cleanup exception formatting stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
import logging
61
61
import traceback
62
62
 
 
63
import bzrlib
63
64
from bzrlib.errors import BzrNewError
64
65
 
65
66
 
81
82
            s = 'bzr: ' + record.levelname + ': '
82
83
        else:
83
84
            s = ''
84
 
            
85
85
        s += record.getMessage()
86
 
 
87
 
        ##import textwrap
88
 
        ##s = textwrap.fill(s)
89
 
            
90
86
        if record.exc_info:
91
 
            # give just a summary of the exception, not the whole thing
92
 
            exinfo = traceback.extract_tb(record.exc_info[2])
93
 
            # the format of this string matches one of the REs
94
 
            s += '\n'
95
 
            s += ('  at %s line %d, in %s()\n' % exinfo[-1][:3])
96
 
            s += '  see ~/.bzr.log for debug information'
97
 
 
 
87
            s += '\n' + format_exception_short(record.exc_info)
98
88
        return s
99
89
        
100
90
 
166
156
 
167
157
 
168
158
def log_startup(argv):
169
 
    import bzrlib
170
 
 
171
159
    debug('bzr %s invoked on python %s (%s)',
172
160
          bzrlib.__version__,
173
161
          '.'.join(map(str, sys.version_info)),
174
162
          sys.platform)
175
 
 
176
163
    debug('  arguments: %r', argv)
177
164
    debug('  working dir: %r', os.getcwdu())
178
165
 
183
170
    The exception string representation is used as the error
184
171
    summary, unless msg is given.
185
172
    """
186
 
    ei = sys.exc_info()
187
 
    if msg == None:
188
 
        msg = str(ei[1])
189
 
    if msg and (msg[-1] == '\n'):
190
 
        msg = msg[:-1]
191
 
    msg += '\n  command: %s' % ' '.join(repr(arg) for arg in sys.argv)
192
 
    msg += '\n      pwd: %r' % os.getcwdu()
193
 
    msg += '\n    error: %s' % ei[0]        # exception type
194
 
    _bzr_logger.exception(msg)
 
173
    exc_str = format_exception_short(sys.exc_info())
 
174
    if msg:
 
175
        _bzr_logger.exception(msg)
 
176
    _bzr_logger.error(exc_str)
195
177
 
196
178
 
197
179
def log_exception_quietly():
240
222
        l.removeHandler(_file_handler)
241
223
 
242
224
 
243
 
def format_exception_short():
 
225
def format_exception_short(exc_info):
244
226
    """Make a short string form of an exception.
245
227
 
246
228
    This is used for display to stderr.  It specially handles exception
247
229
    classes without useful string methods.
248
230
 
249
231
    The result has no trailing newline.
 
232
 
 
233
    exc_info - typically an exception from sys.exc_info()
250
234
    """
251
 
    exc_type, exc_info, exc_tb = sys.exc_info()
 
235
    exc_type, exc_object, exc_tb = exc_info
252
236
    if exc_type is None:
253
237
        return '(no exception)'
254
 
    if isinstance(exc_info, BzrNewError):
255
 
        return str(exc_info)
 
238
    if isinstance(exc_object, BzrNewError):
 
239
        return str(exc_object)
256
240
    else:
257
241
        import traceback
258
242
        tb = traceback.extract_tb(exc_tb)
259
 
        msg = '%s: %s' % (exc_type, exc_info)
 
243
        msg = '%s: %s' % (exc_type, exc_object)
260
244
        if msg[-1] == '\n':
261
245
            msg = msg[:-1]
262
246
        if tb: