/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/trace.py

  • Committer: Martin Pool
  • Date: 2005-05-05 03:34:34 UTC
  • Revision ID: mbp@sourcefrog.net-20050505033434-5fff60df09cb61c6
- Per-branch locks in read and write modes.
  (Not on Windows yet.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
14
 
15
15
 
16
 
 
17
 
# TODO: Could probably replace this with something based on Python logging
18
 
# module.
19
 
 
20
 
 
21
 
 
22
 
 
23
16
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
24
17
__author__ = "Martin Pool <mbp@canonical.com>"
25
18
 
30
23
"""
31
24
 
32
25
 
33
 
import sys, os
 
26
import sys, os, time, socket, stat, codecs
 
27
import bzrlib
34
28
 
35
29
######################################################################
36
30
# messages and logging
52
46
 
53
47
 
54
48
def _write_trace(msg):
55
 
    if 0:
56
 
        if _tracefile:
57
 
            _tracefile.write(_logprefix + msg + '\n')
 
49
    if _tracefile:
 
50
        _tracefile.write(_logprefix + msg + '\n')
58
51
 
59
52
 
60
53
def warning(msg):
78
71
 
79
72
 
80
73
def _rollover_trace_maybe(trace_fname):
81
 
    import stat
82
74
    try:
83
75
        size = os.stat(trace_fname)[stat.ST_SIZE]
84
76
        if size <= 4 << 20:
101
93
 
102
94
 
103
95
 
104
 
def open_tracefile(argv=[], tracefilename='~/.bzr.log'):
 
96
def open_tracefile(argv):
105
97
    # Messages are always written to here, so that we have some
106
98
    # information if something goes wrong.  In a future version this
107
99
    # file will be removed on successful completion.
108
100
    global _starttime, _tracefile
109
 
    import stat, codecs
110
101
 
111
102
    _starttime = os.times()[4]
112
103
 
113
 
    trace_fname = os.path.join(os.path.expanduser(tracefilename))
 
104
    trace_fname = os.path.join(os.path.expanduser('~/.bzr.log'))
114
105
    _rollover_trace_maybe(trace_fname)
115
106
 
116
107
    # buffering=1 means line buffered
117
 
    try:
118
 
        _tracefile = codecs.open(trace_fname, 'at', 'utf8', buffering=1)
119
 
        t = _tracefile
120
 
 
121
 
        if os.fstat(t.fileno())[stat.ST_SIZE] == 0:
122
 
            t.write("\nthis is a debug log for diagnosing/reporting problems in bzr\n")
123
 
            t.write("you can delete or truncate this file, or include sections in\n")
124
 
            t.write("bug reports to bazaar-ng@lists.canonical.com\n\n")
125
 
 
126
 
        import bzrlib
127
 
        _write_trace('bzr %s invoked on python %s (%s)'
128
 
                     % (bzrlib.__version__,
129
 
                        '.'.join(map(str, sys.version_info)),
130
 
                        sys.platform))
131
 
 
132
 
        _write_trace('  arguments: %r' % argv)
133
 
        _write_trace('  working dir: ' + os.getcwdu())
134
 
    except IOError, e:
135
 
        warning("failed to open trace file: %s" % (e))
 
108
    _tracefile = codecs.open(trace_fname, 'at', 'utf8', buffering=1)
 
109
    t = _tracefile
 
110
 
 
111
    if os.fstat(t.fileno())[stat.ST_SIZE] == 0:
 
112
        t.write("\nthis is a debug log for diagnosing/reporting problems in bzr\n")
 
113
        t.write("you can delete or truncate this file, or include sections in\n")
 
114
        t.write("bug reports to bazaar-ng@lists.canonical.com\n\n")
 
115
 
 
116
    # TODO: If we failed to create the file, perhaps give a warning
 
117
    # but don't abort; send things to /dev/null instead?
 
118
 
 
119
    _write_trace('bzr %s invoked on python %s (%s)'
 
120
                 % (bzrlib.__version__,
 
121
                    '.'.join(map(str, sys.version_info)),
 
122
                    sys.platform))
 
123
 
 
124
    _write_trace('  arguments: %r' % argv)
 
125
    _write_trace('  working dir: ' + os.getcwdu())
 
126
 
136
127
 
137
128
def close_trace():
138
129
    times = os.times()
141
132
 
142
133
 
143
134
 
144
 
def log_exception():
145
 
    """Log the last exception into the trace file."""
146
 
    import cgitb
147
 
    s = cgitb.text(sys.exc_info())
148
 
    for l in s.split('\n'):
 
135
def log_exception(e):
 
136
    import traceback, cStringIO
 
137
    s = cStringIO.StringIO()
 
138
    traceback.print_exc(None, s)
 
139
    for l in s.getvalue().split('\n'):
149
140
        _write_trace(l)
150
141
        
151
142