summaryrefslogtreecommitdiffstats
path: root/src/software/openlmi/software/yumdb/util.py
blob: bd2a0ca50d5b5e7670cfe6dbce8a1a5de180b6df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Software Management Providers
#
# Copyright (C) 2012-2013 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: Michal Minar <miminar@redhat.com>
#

"""
Common utilities meant to be used only be ``yumdb`` subpackage.
"""

from itertools import chain
import inspect
import logging
import os

from openlmi.software.yumdb import errors

class DispatchingFormatter:
    """
    Formatter class for logging module. It allows to predefine different
    format string for paricular module names.
    """
    def __init__(self, formatters, default):
        """
        *format* in parameters description can be either ``string`` or
        another formatter object.

        :param formatters (``dict``) Mapping of module names to *format*.
        :param default Default *format*.
        """
        for k, formatter in formatters.items():
            if isinstance(formatter, basestring):
                formatters[k] = logging.Formatter(formatter)
        self._formatters = formatters
        if isinstance(default, basestring):
            default = logging.Formatter(default)
        self._default_formatter = default

    def format(self, record):
        """
        Interface for logging module.
        """
        formatter = self._formatters.get(record.name, self._default_formatter)
        return formatter.format(record)

# *****************************************************************************
# Decorators
# *****************************************************************************
def trace_function(func):
    """
    Decorator for logging entries and exits of function or method.
    """
    if not inspect.ismethod(func) and not inspect.isfunction(func):
        raise TypeError("func must be a function")

    def _print_value(val):
        """
        Used here for printing function arguments. Shortens the output
        string, if that would be too long.
        """
        if isinstance(val, list):
            if len(val) < 2:
                return str(val)
            else:
                return "[%s, ...]" % _print_value(val[0])
        return str(val)

    logger = logging.getLogger(__name__+'.trace_function')
    module = func.__module__.split('.')[-1]
    lineno = inspect.currentframe().f_back.f_lineno

    def _wrapper(self, *args, **kwargs):
        """
        Wrapper for function or method, that does the logging.
        """
        if logger.isEnabledFor(logging.DEBUG):
            frm = inspect.currentframe()
            logargs = {
                "caller_file" : os.path.basename(os.path.splitext(
                    frm.f_back.f_code.co_filename)[0]),
                "caller_lineno" : frm.f_back.f_lineno,
                "module" : module,
                "func"   : func.__name__,
                "lineno" : lineno,
                "action" : "entering",
                "args"   : ", ".join(chain(
                            (_print_value(a) for a in args),
                            (   "%s=%s"%(k, _print_value(v))
                            for k, v in kwargs.items())))
            }

            if not logargs["args"]:
                logargs["args"] = ""
            else:
                logargs["args"] = " with args=(%s)" % logargs["args"]
            logger.debug("%(caller_file)s:%(caller_lineno)d - %(action)s"
                " %(module)s:%(func)s:%(lineno)d%(args)s" , logargs)
        try:
            result = func(self, *args, **kwargs)
            if logger.isEnabledFor(logging.DEBUG):
                logargs["action"] = "exiting"
                logger.debug("%(caller_file)s:%(caller_lineno)d - %(action)s"
                    " %(module)s:%(func)s:%(lineno)d", logargs)
        except Exception as exc:
            logargs['action'] = 'exiting'
            logargs['error'] = str(exc)
            logger.debug("%(caller_file)s:%(caller_lineno)d - %(action)s"
                    " %(module)s:%(func)s:%(lineno)d with error: %(error)s",
                    logargs)
            raise
        return result

    return _wrapper

def setup_logging(config):
    """
    This is meant to be used by ``YumWorker`` process to setup logging
    independent of what providers are using. Unfortunately ``YumWorker``
    can not use the same facilities as the rest of program, because
    logging is done through *broker*.
    """
    try:
        logging.config.dictConfig(config)
    except Exception:   #pylint: disable=W0703
        # logging is not set up but client expects us to work
        pass