summaryrefslogtreecommitdiff
path: root/efl/utils
diff options
context:
space:
mode:
authorKai Huuhko <kai.huuhko@gmail.com>2013-10-24 22:58:09 +0300
committerKai Huuhko <kai.huuhko@gmail.com>2013-10-24 22:58:09 +0300
commit4771ca5ccb7654119a67de72dc28020e091bc3b5 (patch)
tree7c15896f60b241802fc1fffb7e6c1e00baa680e9 /efl/utils
parent8cbee5c44d706a9fc25bfc7f841d25745fadb40b (diff)
Move logger to its own module.
Diffstat (limited to 'efl/utils')
-rw-r--r--efl/utils/logger.pyx82
1 files changed, 82 insertions, 0 deletions
diff --git a/efl/utils/logger.pyx b/efl/utils/logger.pyx
new file mode 100644
index 0000000..471de9e
--- /dev/null
+++ b/efl/utils/logger.pyx
@@ -0,0 +1,82 @@
1# Copyright (C) 2007-2013 various contributors (see AUTHORS)
2#
3# This file is part of Python-EFL.
4#
5# Python-EFL is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# Python-EFL is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with this Python-EFL. If not, see <http://www.gnu.org/licenses/>.
17
18from cpython cimport PyString_FromFormatV
19from libc.string cimport const_char
20from efl.eina cimport Eina_Log_Domain, const_Eina_Log_Domain, Eina_Log_Level, \
21 eina_log_print_cb_set, eina_log_domain_register, eina_log_level_set, \
22 eina_log_level_get, eina_log_domain_level_get, eina_log_domain_level_set
23
24cdef extern from "stdarg.h":
25 ctypedef struct va_list:
26 pass
27
28cdef tuple log_levels = (
29 50,
30 40,
31 30,
32 20,
33 10
34)
35
36cdef dict loggers = dict()
37
38cdef void py_eina_log_print_cb(const_Eina_Log_Domain *d,
39 Eina_Log_Level level,
40 const_char *file, const_char *fnc, int line,
41 const_char *fmt, void *data, va_list args) with gil:
42 cdef str msg = PyString_FromFormatV(fmt, args)
43 rec = logging.LogRecord(d.name, log_levels[level], file, line, msg, None, None, fnc)
44 logger = loggers.get(d.name, loggers["efl"])
45 logger.handle(rec)
46
47import logging
48
49eina_log_print_cb_set(py_eina_log_print_cb, NULL)
50
51class PyEFLLogger(logging.Logger):
52 def __init__(self, name):
53 self.eina_log_domain = eina_log_domain_register(name, NULL)
54 loggers[name] = self
55 logging.Logger.__init__(self, name)
56
57 def setLevel(self, lvl):
58 eina_log_domain_level_set(self.name, log_levels.index(lvl))
59 logging.Logger.setLevel(self, lvl)
60
61logging.setLoggerClass(PyEFLLogger)
62
63rootlog = logging.getLogger("efl")
64rootlog.propagate = False
65rootlog.setLevel(logging.WARNING)
66rootlog.addHandler(logging.NullHandler())
67
68logging.setLoggerClass(logging.Logger)
69
70cdef public int PY_EFL_LOG_DOMAIN = rootlog.eina_log_domain
71
72cdef int add_logger(char *name):
73 logging.setLoggerClass(PyEFLLogger)
74
75 log = logging.getLogger(name)
76 log.propagate = True
77 log.setLevel(logging.WARNING)
78 log.addHandler(logging.NullHandler())
79
80 logging.setLoggerClass(logging.Logger)
81
82 return log.eina_log_domain