~bladernr/checkbox/1095713-set-pipefail-on-sleep-jobs

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
#
# This file is part of Checkbox.
#
# Copyright 2008 Canonical Ltd.
#
# Checkbox 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 3 of the License, or
# (at your option) any later version.
#
# Checkbox 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 Checkbox.  If not, see <http://www.gnu.org/licenses/>.
#
import inspect
import logging

from logging import StreamHandler, FileHandler, Formatter


def format_class(cls):
    return "%s.%s" % (cls.__module__, cls.__name__)

def format_object(object, *args, **kwargs):
    """
    Returns a fully-qualified name for the specified object, such as
    'checkbox.log.format_object()'.
    """
    args_string = ""
    if args:
        args_string += ", ".join(str(a) for a in args)
        if kwargs:
            args_string += ", "
    if kwargs:
        args_string += ", ".join(["%s=%s" % (k, v) for k, v in kwargs.iteritems()])

    if inspect.ismethod(object):
        # FIXME If the method is implemented on a base class of
        # object's class, the module name and function name will be
        # from the base class and the method's class name will be from
        # object's class.
        name = repr(object).split(" ")[2]
        return "%s.%s(%s)" % (object.__module__, name, args_string)
    elif inspect.isfunction(object):
        name = repr(object).split(" ")[1]
        return "%s.%s(%s)" % (object.__module__, name, args_string)
    return format_class(object.__class__)

def format_delta(seconds):
    if not seconds:
        seconds = 0.0
    return "%.02fs" % float(seconds)

def set_logging(level, log=None):
    log_level = logging.getLevelName(level.upper())
    log_handlers = []
    if log:
        log_filename = log
        log_handlers.append(FileHandler(log_filename))
    else:
        log_handlers.append(StreamHandler())

    # Log setup
    format = ("%(asctime)s %(levelname)-8s %(message)s")
    if log_handlers:
        for handler in log_handlers:
            handler.setFormatter(Formatter(format))
            logging.getLogger().addHandler(handler)
        if log_level:
            logging.getLogger().setLevel(log_level)
    elif not logging.getLogger().handlers:
        logging.disable(logging.CRITICAL)