1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Transport decorator that logs transport operations to .bzr.log."""
20
# see also the transportstats plugin, which gives you some summary information
21
# in a machine-readable dump
28
from bzrlib.trace import mutter
29
from bzrlib.transport.decorator import (
32
from bzrlib.transport.trace import (
34
TransportTraceDecorator,
40
class TransportLogDecorator(TransportDecorator):
41
"""Decorator for Transports that logs interesting operations to .bzr.log.
43
In general we want to log things that usually take a network round trip
46
Not all operations are logged yet.
49
def __init__(self, *args, **kw):
50
super(TransportLogDecorator, self).__init__(*args, **kw)
51
def _make_hook(hookname):
52
def _hook(relpath, *args, **kw):
53
return self._log_call(hookname, relpath, *args, **kw)
62
'iter_files_recursive',
66
'put_bytes', 'put_bytes_non_atomic', 'put_file put_file_non_atomic',
67
'list_dir', 'lock_read', 'lock_write',
68
'readv', 'rename', 'rmdir',
72
setattr(self, methodname, _make_hook(methodname))
75
def _get_url_prefix(self):
78
def _log_call(self, methodname, relpath, *args, **kwargs):
81
kwargs_str = dict(kwargs)
85
% (methodname, self._decorated.abspath(relpath),
86
self._shorten(self._strip_tuple_parens(args)),
89
result = getattr(self._decorated, methodname)(relpath, *args)
92
mutter(" %.03fs" % (time.time() - before))
94
return self._show_result(before, methodname, result)
96
def _show_result(self, before, methodname, result):
98
if isinstance(result, types.GeneratorType):
99
# eagerly pull in all the contents, so that we can measure how
100
# long it takes to get them. this does make the behaviour a bit
101
# different, but we hope not noticably so
102
result = list(result)
103
if isinstance(result, (cStringIO.OutputType, StringIO.StringIO)):
104
val = repr(result.getvalue())
105
result_len = len(val)
106
shown_result = "%s(%s) (%d bytes)" % (result.__class__.__name__,
107
self._shorten(val), result_len)
108
elif methodname == 'readv':
109
num_hunks = len(result)
110
total_bytes = sum((len(d) for o,d in result))
111
shown_result = "readv response, %d hunks, %d total bytes" % (
112
num_hunks, total_bytes)
113
result_len = total_bytes
115
shown_result = self._shorten(self._strip_tuple_parens(result))
116
mutter(" --> %s" % shown_result)
117
# XXX: the time may be wrong when e.g. a generator object is returned from
118
# an http readv, if the object is returned before the bulk data
120
elapsed = time.time() - before
121
if result_len and elapsed > 0:
122
# this is the rate of higher-level data, not the raw network speed
123
mutter(" %9.03fs %8dkB/s" % (elapsed, result_len/elapsed/1024))
125
mutter(" %9.03fs" % (elapsed))
128
def _shorten(self, x):
133
def _strip_tuple_parens(self, t):
135
if t[0] == '(' and t[-1] == ')':
140
class LogDecoratorServer(DecoratorServer):
141
"""Server for testing."""
143
def get_decorator_class(self):
144
return TransportLogDecorator
147
def get_test_permutations():
148
"""Return the permutations to be used in testing."""
149
return [(TransportLogDecorator, LogDecoratorServer)]