4.2.0r3
[enigma2.git] / usr / lib / enigma2 / python / Tools / Log.py
1 from sys import _getframe
2 from LogConfig import LogConfig, LOG_TYPE_DEBUG, LOG_TYPE_INFO, LOG_TYPE_WARNING, LOG_TYPE_ERROR, LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG
3
4 class Log(object):
5         @staticmethod
6         def e(text=""):
7                 #ERROR
8                 LogConfig.init()
9                 if LogConfig.level() >= LOG_LEVEL_ERROR:
10                         callframe = _getframe(1)
11                         Log._log(LOG_TYPE_ERROR, text, callframe)
12
13         @staticmethod
14         def w(text=""):
15                 #WARNING
16                 LogConfig.init()
17                 if LogConfig.level() >= LOG_LEVEL_WARNING:
18                         callframe = _getframe(1)
19                         Log._log(LOG_TYPE_WARNING, text, callframe)
20
21         @staticmethod
22         def i(text=""):
23                 #INFO
24                 LogConfig.init()
25                 if LogConfig.level() >= LOG_LEVEL_INFO:
26                         callframe = _getframe(1)
27                         Log._log(LOG_TYPE_INFO, text, callframe)
28
29         @staticmethod
30         def d(text=""):
31                 #DEBUG
32                 LogConfig.init()
33                 if LogConfig.level >= LOG_LEVEL_DEBUG:
34                         callframe = _getframe(1)
35                         Log._log(LOG_TYPE_DEBUG, text, callframe)
36
37         @staticmethod
38         def _log(type, text, callframe=None):
39                 LogConfig.init()
40                 if callframe is None:
41                         callframe = _getframe(1)
42
43                 func = callframe.f_code.co_name
44                 cls = callframe.f_locals.get('self', None)
45
46                 msg = ""
47                 if not text:
48                         text = "<no detail>"
49                 if cls != None:
50                         cls = cls.__class__.__name__
51                         msg = "%s [%s.%s] :: %s" % (type, cls, func, text)
52                 else:
53                         msg = "%s [%s] :: %s" % (type, func, text)
54
55                 if LogConfig.verbose():
56                         line = callframe.f_lineno
57                         filename = callframe.f_code.co_filename
58                         msg = "%s {%s:%s}" % (msg, filename, line)
59                 if LogConfig.colored():
60                         if type == LOG_TYPE_ERROR:
61                                 msg = "\033[0;31m%s\033[1;m" % msg
62                         elif type == LOG_TYPE_WARNING:
63                                 msg = "\033[1;33m%s\033[1;m" % msg
64                         elif type == LOG_TYPE_DEBUG:
65                                 msg = "\033[0;37m%s\033[1;m" % msg
66
67                 print msg