log.py :  » Development » Bazaar » bzr-2.2b3 » bzrlib » transport » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Bazaar 
Bazaar » bzr 2.2b3 » bzrlib » transport » log.py
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Transport decorator that logs transport operations to .bzr.log."""


# see also the transportstats plugin, which gives you some summary information
# in a machine-readable dump

import StringIO
import cStringIO
import time
import types

from bzrlib.trace import mutter
from bzrlib.transport import decorator


class TransportLogDecorator(decorator.TransportDecorator):
    """Decorator for Transports that logs interesting operations to .bzr.log.

    In general we want to log things that usually take a network round trip
    and may be slow.

    Not all operations are logged yet.

    See also TransportTraceDecorator, that records a machine-readable log in 
    memory for eg testing.
    """

    def __init__(self, *args, **kw):
        super(TransportLogDecorator, self).__init__(*args, **kw)
        def _make_hook(hookname):
            def _hook(relpath, *args, **kw):
                return self._log_and_call(hookname, relpath, *args, **kw)
            return _hook
        for methodname in (
            'append_bytes',
            'append_file',
            'copy_to',
            'delete',
            'get',
            'has',
            'open_write_stream',
            'mkdir',
            'move',
            'put_bytes', 'put_bytes_non_atomic', 'put_file put_file_non_atomic',
            'list_dir', 'lock_read', 'lock_write',
            'readv', 'rename', 'rmdir',
            'stat',
            'ulock',
            ):
            setattr(self, methodname, _make_hook(methodname))

    @classmethod
    def _get_url_prefix(self):
        return 'log+'

    def iter_files_recursive(self):
        # needs special handling because it does not have a relpath parameter
        mutter("%s %s"
            % ('iter_files_recursive', self._decorated.base))
        return self._call_and_log_result('iter_files_recursive', (), {})

    def _log_and_call(self, methodname, relpath, *args, **kwargs):
        if kwargs:
            kwargs_str = dict(kwargs)
        else:
            kwargs_str = ''
        mutter("%s %s %s %s"
               % (methodname, self._decorated.abspath(relpath),
                  self._shorten(self._strip_tuple_parens(args)),
                  kwargs_str))
        return self._call_and_log_result(methodname, (relpath,) + args, kwargs)

    def _call_and_log_result(self, methodname, args, kwargs):
        before = time.time()
        try:
            result = getattr(self._decorated, methodname)(*args, **kwargs)
        except Exception, e:
            mutter("  --> %s" % e)
            mutter("      %.03fs" % (time.time() - before))
            raise
        return self._show_result(before, methodname, result)

    def _show_result(self, before, methodname, result):
        result_len = None
        if isinstance(result, types.GeneratorType):
            # We now consume everything from the generator so that we can show
            # the results and the time it took to get them.  However, to keep
            # compatibility with callers that may specifically expect a result
            # (see <https://launchpad.net/bugs/340347>) we also return a new
            # generator, reset to the starting position.
            result = list(result)
            return_result = iter(result)
        else:
            return_result = result
        if isinstance(result, (cStringIO.OutputType, StringIO.StringIO)):
            val = repr(result.getvalue())
            result_len = len(val)
            shown_result = "%s(%s) (%d bytes)" % (result.__class__.__name__,
                self._shorten(val), result_len)
        elif methodname == 'readv':
            num_hunks = len(result)
            total_bytes = sum((len(d) for o,d in result))
            shown_result = "readv response, %d hunks, %d total bytes" % (
                num_hunks, total_bytes)
            result_len = total_bytes
        else:
            shown_result = self._shorten(self._strip_tuple_parens(result))
        mutter("  --> %s" % shown_result)
        # The log decorator no longer shows the elapsed time or transfer rate
        # because they're available in the log prefixes and the transport
        # activity display respectively.
        if False:
            elapsed = time.time() - before
            if result_len and elapsed > 0:
                # this is the rate of higher-level data, not the raw network
                # speed using base-10 units (see HACKING.txt).
                mutter("      %9.03fs %8dkB/s"
                       % (elapsed, result_len/elapsed/1000))
            else:
                mutter("      %9.03fs" % (elapsed))
        return return_result

    def _shorten(self, x):
        if len(x) > 70:
            x = x[:67] + '...'
        return x

    def _strip_tuple_parens(self, t):
        t = repr(t)
        if t[0] == '(' and t[-1] == ')':
            t = t[1:-1]
        return t


def get_test_permutations():
    """Return the permutations to be used in testing."""
    from bzrlib.tests import test_server
    return [(TransportLogDecorator, test_server.LogDecoratorServer)]
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.