diff options
author | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2010-06-10 07:58:14 +0000 |
---|---|---|
committer | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2010-06-10 07:58:14 +0000 |
commit | 72361f16b3493de7508519a5173672887401e7c7 (patch) | |
tree | ebc2ace605e86cf73b2b9849be79647632937a87 /bindings | |
parent | 2c0ea4d64791369f890774c1d7e3003930513384 (diff) | |
download | lasso-72361f16b3493de7508519a5173672887401e7c7.tar.gz lasso-72361f16b3493de7508519a5173672887401e7c7.tar.xz lasso-72361f16b3493de7508519a5173672887401e7c7.zip |
Binding python: if lasso.logger exists use it for logging
* There is now two paths to get a logger in the python binding:
- first try to get an objet from lasso.logger
- if it doesn't exist or is None, the try logging.getLogger('lasso')
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/python/wrapper_top.c | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index 99f5fd9f..7a2d308c 100644 --- a/bindings/python/wrapper_top.c +++ b/bindings/python/wrapper_top.c @@ -599,14 +599,23 @@ set_object_field(GObject **a_gobject_ptr, PyGObjectPtr *a_pygobject) { } } -static PyObject *_logger_object = NULL; static PyObject *get_logger_object() { - if (_logger_object == NULL) { - PyObject *logging_module = PyImport_ImportModule("logging"); - _logger_object = PyObject_CallMethod(logging_module, "getLogger", - "s#", "lasso", sizeof("lasso")-1); - Py_DECREF(logging_module); + static PyObject *_logger_object = NULL; + + PyObject *logging_module = PyImport_ImportModule("lasso"); + _logger_object = PyObject_GetAttrString(logging_module, "logger"); + if (_logger_object) + goto exit; + Py_DECREF(logging_module); + logging_module = PyImport_ImportModule("logging"); + _logger_object = PyObject_CallMethod(logging_module, "getLogger", + "s#", "lasso", sizeof("lasso")-1); +exit: + Py_DECREF(logging_module); + if (_logger_object == Py_None) { + Py_DECREF(_logger_object); + _logger_object = NULL; } return _logger_object; } @@ -615,9 +624,14 @@ static void lasso_python_log(G_GNUC_UNUSED const char *domain, GLogLevelFlags log_level, const gchar *message, G_GNUC_UNUSED gpointer user_data) { - PyObject *logger_object = get_logger_object(); + PyObject *logger_object = get_logger_object(), *result; char *method = NULL; + if (! logger_object) { + PyErr_SetString(PyExc_RuntimeError, "both lasso.logger and " + "loggin.getLogger('lasso') did not return a logger"); + return; + } switch (log_level) { case G_LOG_LEVEL_DEBUG: method = "debug"; @@ -638,5 +652,11 @@ lasso_python_log(G_GNUC_UNUSED const char *domain, GLogLevelFlags log_level, con default: return; } - PyObject_CallMethod(logger_object, method, "s#s", "%s", 2, message); + result = PyObject_CallMethod(logger_object, method, "s#s", "%s", 2, message); + if (result) { + Py_DECREF(result); + } else { + PyErr_Format(PyExc_RuntimeError, "lasso could not call method %s on its logger", + method); + } } |