2014-04-14 13:21:03 -07:00
|
|
|
# Copyright (C) 2007-2014 various contributors (see AUTHORS)
|
2013-10-24 12:58:09 -07:00
|
|
|
#
|
|
|
|
# This file is part of Python-EFL.
|
|
|
|
#
|
|
|
|
# Python-EFL 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
|
2013-12-07 08:54:58 -08:00
|
|
|
# version 3 of the License, or (at your option) any later version.
|
2013-10-24 12:58:09 -07:00
|
|
|
#
|
|
|
|
# Python-EFL 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 Python-EFL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-04-04 17:13:15 -07:00
|
|
|
from efl.eina cimport Eina_Log_Domain, Eina_Log_Level, \
|
2013-10-24 12:58:09 -07:00
|
|
|
eina_log_print_cb_set, eina_log_domain_register, eina_log_level_set, \
|
2013-10-25 16:38:25 -07:00
|
|
|
eina_log_level_get, eina_log_domain_level_get, eina_log_domain_level_set, \
|
2013-10-30 02:26:14 -07:00
|
|
|
eina_log_print, EINA_LOG_DOM_DBG, EINA_LOG_DOM_INFO, EINA_LOG_DOM_WARN, \
|
|
|
|
EINA_LOG_DOM_ERR, EINA_LOG_DOM_CRIT
|
2014-04-06 13:48:16 -07:00
|
|
|
from cpython cimport PyUnicode_AsUTF8String, PY_VERSION_HEX
|
2013-10-25 17:22:47 -07:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import types
|
2013-10-24 12:58:09 -07:00
|
|
|
|
|
|
|
cdef extern from "stdarg.h":
|
|
|
|
ctypedef struct va_list:
|
|
|
|
pass
|
|
|
|
|
2013-10-30 02:26:14 -07:00
|
|
|
cdef extern from "stdio.h":
|
2014-04-04 17:13:15 -07:00
|
|
|
int vsprintf(char *, const char *fmt, va_list args)
|
2013-10-25 03:43:40 -07:00
|
|
|
|
2013-11-29 13:41:07 -08:00
|
|
|
cdef extern from "Python.h":
|
|
|
|
void PyEval_InitThreads()
|
|
|
|
|
2013-10-24 12:58:09 -07:00
|
|
|
cdef tuple log_levels = (
|
|
|
|
50,
|
|
|
|
40,
|
|
|
|
30,
|
|
|
|
20,
|
|
|
|
10
|
|
|
|
)
|
|
|
|
|
|
|
|
cdef dict loggers = dict()
|
|
|
|
|
2013-11-28 10:56:37 -08:00
|
|
|
cdef char log_buf[1024]
|
|
|
|
|
2013-11-29 13:41:07 -08:00
|
|
|
|
|
|
|
PyEval_InitThreads()
|
|
|
|
|
2014-04-04 17:13:15 -07:00
|
|
|
cdef void py_eina_log_print_cb(const Eina_Log_Domain *d, Eina_Log_Level level,
|
|
|
|
const char *file, const char *fnc, int line,
|
|
|
|
const char *fmt, void *data, va_list args) with gil:
|
2013-11-29 13:41:07 -08:00
|
|
|
|
2013-10-30 02:26:14 -07:00
|
|
|
cdef:
|
2013-11-29 13:41:07 -08:00
|
|
|
unicode msg, name
|
|
|
|
object rec, logger
|
2013-10-30 02:26:14 -07:00
|
|
|
|
2013-11-28 10:56:37 -08:00
|
|
|
vsprintf(log_buf, fmt, args)
|
2013-11-29 13:41:07 -08:00
|
|
|
|
2013-11-28 10:56:37 -08:00
|
|
|
msg = log_buf.decode("utf-8")
|
2013-11-29 13:41:07 -08:00
|
|
|
name = d.name.decode("utf-8")
|
2013-10-30 02:26:14 -07:00
|
|
|
|
2013-11-29 13:41:07 -08:00
|
|
|
rec = logging.LogRecord(
|
|
|
|
name, log_levels[level], file, line, msg, None, None, fnc)
|
2013-10-25 16:38:25 -07:00
|
|
|
logger = loggers.get(name, loggers["efl"])
|
2013-10-24 12:58:09 -07:00
|
|
|
logger.handle(rec)
|
|
|
|
|
|
|
|
eina_log_print_cb_set(py_eina_log_print_cb, NULL)
|
|
|
|
|
2013-10-28 07:54:23 -07:00
|
|
|
def setLevel(self, lvl):
|
|
|
|
cname = self.name
|
2014-04-06 13:48:16 -07:00
|
|
|
if isinstance(cname, unicode): cname = PyUnicode_AsUTF8String(cname)
|
2013-10-28 07:54:23 -07:00
|
|
|
eina_log_domain_level_set(cname, log_levels.index(lvl))
|
|
|
|
logging.Logger.setLevel(self, lvl)
|
|
|
|
|
2013-10-24 12:58:09 -07:00
|
|
|
class PyEFLLogger(logging.Logger):
|
2013-10-28 07:54:23 -07:00
|
|
|
|
2013-10-24 12:58:09 -07:00
|
|
|
def __init__(self, name):
|
2013-10-25 16:38:25 -07:00
|
|
|
cname = name
|
2014-04-06 13:48:16 -07:00
|
|
|
if isinstance(cname, unicode): cname = PyUnicode_AsUTF8String(cname)
|
2013-10-25 16:38:25 -07:00
|
|
|
self.eina_log_domain = eina_log_domain_register(cname, NULL)
|
2013-10-24 12:58:09 -07:00
|
|
|
loggers[name] = self
|
|
|
|
logging.Logger.__init__(self, name)
|
2013-10-28 07:54:23 -07:00
|
|
|
if PY_VERSION_HEX < 0x03000000:
|
|
|
|
self.setLevel = types.MethodType(setLevel, self, type(self))
|
|
|
|
else:
|
|
|
|
self.setLevel = types.MethodType(setLevel, self)
|
2013-10-24 12:58:09 -07:00
|
|
|
|
2013-10-25 17:22:47 -07:00
|
|
|
cdef object add_logger(object name):
|
2013-10-24 12:58:09 -07:00
|
|
|
logging.setLoggerClass(PyEFLLogger)
|
|
|
|
|
|
|
|
log = logging.getLogger(name)
|
2013-10-25 17:22:47 -07:00
|
|
|
if not isinstance(log, PyEFLLogger):
|
|
|
|
# The logger has been instantiated already so lets add our own
|
|
|
|
# initialization for it.
|
|
|
|
cname = name
|
2014-04-06 13:48:16 -07:00
|
|
|
if isinstance(cname, unicode): cname = PyUnicode_AsUTF8String(cname)
|
2013-10-25 17:22:47 -07:00
|
|
|
log.eina_log_domain = eina_log_domain_register(cname, NULL)
|
|
|
|
loggers[name] = log
|
|
|
|
lvl = log.getEffectiveLevel()
|
|
|
|
eina_log_domain_level_set(cname, log_levels.index(lvl))
|
|
|
|
if PY_VERSION_HEX < 0x03000000:
|
2013-10-28 07:54:23 -07:00
|
|
|
log.setLevel = types.MethodType(setLevel, log, type(log))
|
2013-10-25 17:22:47 -07:00
|
|
|
else:
|
2013-10-28 07:54:23 -07:00
|
|
|
log.setLevel = types.MethodType(setLevel, log)
|
2013-10-25 17:22:47 -07:00
|
|
|
else:
|
|
|
|
log.propagate = True
|
|
|
|
log.setLevel(logging.WARNING)
|
2013-12-28 08:26:52 -08:00
|
|
|
|
|
|
|
if PY_VERSION_HEX >= 0x02070000:
|
|
|
|
log.addHandler(logging.NullHandler())
|
2013-10-24 12:58:09 -07:00
|
|
|
|
|
|
|
logging.setLoggerClass(logging.Logger)
|
|
|
|
|
2013-10-25 17:22:47 -07:00
|
|
|
return log
|
|
|
|
|
|
|
|
rootlog = add_logger("efl")
|
|
|
|
rootlog.propagate = False
|
|
|
|
|
|
|
|
cdef public int PY_EFL_LOG_DOMAIN = rootlog.eina_log_domain
|
2013-10-30 02:26:14 -07:00
|
|
|
|
|
|
|
def logger_test_dbg():
|
|
|
|
EINA_LOG_DOM_DBG(PY_EFL_LOG_DOMAIN, "test message", NULL)
|