13
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# TODO: Could probably replace this with something based on Python logging
23
19
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
24
20
__author__ = "Martin Pool <mbp@canonical.com>"
27
"""Messages and logging for bazaar-ng
29
Nothing is actually logged unless you call bzrlib.open_tracefile().
35
25
######################################################################
36
26
# messages and logging
38
global _tracefile, _starttime
28
# Messages are always written to here, so that we have some
29
# information if something goes wrong. In a future version this
30
# file will be removed on successful completion.
31
_tracefile = file('.bzr.log', 'at')
33
## TODO: If --verbose is given then write to both stderr and
34
## _tracefile; perhaps replace _tracefile with a tee thing.
41
36
# used to have % (os.environ['USER'], time.time(), os.getpid()), 'w')
44
39
# If false, notes also go to stdout; should replace this with --silent
49
# fix this if we ever fork within python
51
_logprefix = '[%d] ' % _mypid
54
def _write_trace(msg):
56
_tracefile.write(_logprefix + msg + '\n')
60
sys.stderr.write('bzr: warning: ' + msg + '\n')
61
_write_trace('warning: ' + msg)
48
_tracefile.write('\n')
51
sys.stderr.write('- ' + msg + '\n')
68
55
b = '* ' + str(msg) + '\n'
70
57
sys.stderr.write(b)
71
_write_trace('note: ' + msg)
74
62
def log_error(msg):
75
sys.stderr.write(msg + '\n')
79
def _rollover_trace_maybe(trace_fname):
82
size = os.stat(trace_fname)[stat.ST_SIZE]
85
old_fname = trace_fname + '.old'
88
# must remove before rename on windows
94
# might fail if in use on windows
95
os.rename(trace_fname, old_fname)
103
def open_tracefile(argv=[], tracefilename='~/.bzr.log'):
104
# Messages are always written to here, so that we have some
105
# information if something goes wrong. In a future version this
106
# file will be removed on successful completion.
107
global _starttime, _tracefile
110
_starttime = os.times()[4]
112
trace_fname = os.path.join(os.path.expanduser(tracefilename))
113
_rollover_trace_maybe(trace_fname)
115
# buffering=1 means line buffered
117
_tracefile = codecs.open(trace_fname, 'at', 'utf8', buffering=1)
120
if os.fstat(t.fileno())[stat.ST_SIZE] == 0:
121
t.write("\nthis is a debug log for diagnosing/reporting problems in bzr\n")
122
t.write("you can delete or truncate this file, or include sections in\n")
123
t.write("bug reports to bazaar-ng@lists.canonical.com\n\n")
126
_write_trace('bzr %s invoked on python %s (%s)'
127
% (bzrlib.__version__,
128
'.'.join(map(str, sys.version_info)),
131
_write_trace(' arguments: %r' % argv)
132
_write_trace(' working dir: ' + os.getcwdu())
134
warning("failed to open trace file: %s" % (e))
138
mutter("finished, %.3fu/%.3fs cpu, %.3fu/%.3fs cum, %.3f elapsed"
139
% (times[:4] + ((times[4] - _starttime),)))
144
"""Log the last exception into the trace file."""
146
s = cgitb.text(sys.exc_info())
147
for l in s.split('\n'):