summaryrefslogtreecommitdiff
path: root/insanity/log.py
blob: 19b5041e97015a517289bc329987735eb5bef049 (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
# GStreamer QA system
#
#       client.py
#
# Copyright (c) 2007, Edward Hervey <bilboed@bilboed.com>
#
# This program 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 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.

"""
Logging features

Outputs information on stderr

Set INSANITY_DEBUG env variable to the required level

INSANITY_DEBUG   LEVEL
-------------------------
1               CRITICAL
2               ERROR
3               WARNING
4               INFO
5               DEBUG
"""

from logging import critical, debug, error, exception, info, log, warning, \
     ERROR, CRITICAL, WARNING, INFO, DEBUG, basicConfig
import sys
import os


##
## LOGGING
##
## TODO :
##  Should improve this to have categories
##  Add convenience functions for objects, maybe an interface
##     This might be tricky, need to go up some frames to get proper
##     filename and lineno.

__logging_setup__ = False

def initLogging():
    """
    Setup the logging system according to environment variables
    """
    global __logging_setup__
    if __logging_setup__ == True:
        info("Logging was already setup, returning")
        return

    majv, minv = sys.version_info[:2]

    debugformat = "%(asctime)s  0x%(thread)x  %(levelname)10s  %(filename)s:%(lineno)d:"
    if (majv, minv) >= (2, 5):
        debugformat += "%(funcName)s: %(message)s"
    else:
        debugformat += ": %(message)s"
    debuglevel = ERROR

    # get default level INSANITY_DEBUG
    if os.getenv("INSANITY_DEBUG"):
        insdeb = int(os.getenv("INSANITY_DEBUG"))
        if insdeb > 0 and insdeb <= 5:
            levels = [CRITICAL, ERROR,
                      WARNING, INFO,
                      DEBUG]
            debuglevel = levels[insdeb - 1]

    # basicConfig
    basicConfig(level = debuglevel, format = debugformat)
    info("Logging is now properly setup")
    __logging_setup__ = True