~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/log.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2007 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Transport decorator that logs transport operations to .bzr.log."""
18
18
 
19
 
from __future__ import absolute_import
20
19
 
21
20
# see also the transportstats plugin, which gives you some summary information
22
21
# in a machine-readable dump
27
26
import types
28
27
 
29
28
from bzrlib.trace import mutter
30
 
from bzrlib.transport import decorator
31
 
 
32
 
 
33
 
class TransportLogDecorator(decorator.TransportDecorator):
 
29
from bzrlib.transport.decorator import (
 
30
    TransportDecorator,
 
31
    )
 
32
from bzrlib.transport.trace import (
 
33
    DecoratorServer,
 
34
    TransportTraceDecorator,
 
35
    )
 
36
 
 
37
 
 
38
 
 
39
 
 
40
class TransportLogDecorator(TransportDecorator):
34
41
    """Decorator for Transports that logs interesting operations to .bzr.log.
35
42
 
36
43
    In general we want to log things that usually take a network round trip
37
44
    and may be slow.
38
45
 
39
46
    Not all operations are logged yet.
40
 
 
41
 
    See also TransportTraceDecorator, that records a machine-readable log in 
42
 
    memory for eg testing.
43
47
    """
44
48
 
45
49
    def __init__(self, *args, **kw):
100
104
    def _show_result(self, before, methodname, result):
101
105
        result_len = None
102
106
        if isinstance(result, types.GeneratorType):
103
 
            # We now consume everything from the generator so that we can show
104
 
            # the results and the time it took to get them.  However, to keep
105
 
            # compatibility with callers that may specifically expect a result
106
 
            # (see <https://launchpad.net/bugs/340347>) we also return a new
107
 
            # generator, reset to the starting position.
 
107
            # eagerly pull in all the contents, so that we can measure how
 
108
            # long it takes to get them.  this does make the behaviour a bit
 
109
            # different, but we hope not noticably so
108
110
            result = list(result)
109
 
            return_result = iter(result)
110
 
        else:
111
 
            return_result = result
112
111
        if isinstance(result, (cStringIO.OutputType, StringIO.StringIO)):
113
112
            val = repr(result.getvalue())
114
113
            result_len = len(val)
123
122
        else:
124
123
            shown_result = self._shorten(self._strip_tuple_parens(result))
125
124
        mutter("  --> %s" % shown_result)
126
 
        # The log decorator no longer shows the elapsed time or transfer rate
127
 
        # because they're available in the log prefixes and the transport
128
 
        # activity display respectively.
129
 
        if False:
130
 
            elapsed = time.time() - before
131
 
            if result_len and elapsed > 0:
132
 
                # this is the rate of higher-level data, not the raw network
133
 
                # speed using base-10 units (see HACKING.txt).
134
 
                mutter("      %9.03fs %8dkB/s"
135
 
                       % (elapsed, result_len/elapsed/1000))
136
 
            else:
137
 
                mutter("      %9.03fs" % (elapsed))
138
 
        return return_result
 
125
        # XXX: the time may be wrong when e.g. a generator object is returned from
 
126
        # an http readv, if the object is returned before the bulk data
 
127
        # is read.
 
128
        elapsed = time.time() - before
 
129
        if result_len and elapsed > 0:
 
130
            # this is the rate of higher-level data, not the raw network speed
 
131
            mutter("      %9.03fs %8dkB/s" % (elapsed, result_len/elapsed/1024))
 
132
        else:
 
133
            mutter("      %9.03fs" % (elapsed))
 
134
        return result
139
135
 
140
136
    def _shorten(self, x):
141
137
        if len(x) > 70:
149
145
        return t
150
146
 
151
147
 
 
148
class LogDecoratorServer(DecoratorServer):
 
149
    """Server for testing."""
 
150
 
 
151
    def get_decorator_class(self):
 
152
        return TransportLogDecorator
 
153
 
 
154
 
152
155
def get_test_permutations():
153
156
    """Return the permutations to be used in testing."""
154
 
    from bzrlib.tests import test_server
155
 
    return [(TransportLogDecorator, test_server.LogDecoratorServer)]
 
157
    return [(TransportLogDecorator, LogDecoratorServer)]