-
Notifications
You must be signed in to change notification settings - Fork 33
added section for using Python logging module #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ This describes reliable patterns of coding Python Extensions in C. It covers the | |
canonical_function | ||
parsing_arguments | ||
module_globals | ||
logging | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
|
||
Indices and tables | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
.. highlight:: python | ||
:linenothreshold: 10 | ||
|
||
.. toctree:: | ||
:maxdepth: 3 | ||
|
||
================================= | ||
Logging | ||
================================= | ||
|
||
This presents a recipe for using the Python logging module in C. | ||
|
||
We import the module and define C equivalent logging functions that are | ||
compatible with the `*printf` family. | ||
|
||
.. code-block:: c | ||
|
||
#include <Python.h> | ||
#include <stdarg.h> | ||
|
||
/* logging levels defined by logging module */ | ||
enum { INFO, WARNING, ERROR, DEBUG, EXCEPTION }; | ||
|
||
/* module globals */ | ||
static PyObject *logging_import = NULL; | ||
static PyObject *logger = NULL; | ||
|
||
/* Get a logger object from the logging module. */ | ||
static PyObject *py_get_logger(char *logger_name) | ||
{ | ||
PyObject *logger = NULL; | ||
PyObject *ret = NULL; | ||
|
||
logger = PyObject_CallMethod(logging_import, "getLogger", "s", logger_name); | ||
if (logger == NULL) | ||
{ | ||
const char *err_msg = "failed to call logging.getLogger"; | ||
PyErr_SetString(PyExc_RuntimeError, err_msg); | ||
} | ||
|
||
return logger; | ||
} | ||
|
||
/* main interface to logging function */ | ||
static void py_log_msg(int log_level, char *printf_fmt, ...) | ||
{ | ||
PyObject *log_msg = NULL; | ||
va_list fmt_args; | ||
|
||
va_start(fmt_args, printf_fmt); | ||
log_msg = PyUnicode_FromFormatV(printf_fmt, fmt_args); | ||
va_end(fmt_args); | ||
|
||
if (log_msg == NULL) | ||
{ | ||
/* fail silently. */ | ||
return; | ||
} | ||
|
||
/* call function depending on loglevel */ | ||
switch (log_level) | ||
{ | ||
case INFO: | ||
PyObject_CallMethod(PyLogger, "info", "O", log_msg); | ||
break; | ||
|
||
case WARNING: | ||
PyObject_CallMethod(PyLogger, "warn", "O", log_msg); | ||
break; | ||
|
||
case ERROR: | ||
PyObject_CallMethod(PyLogger, "error", "O", log_msg); | ||
break; | ||
|
||
case DEBUG: | ||
PyObject_CallMethod(PyLogger, "debug", "O", log_msg); | ||
break; | ||
|
||
case EXCEPTION: | ||
PyObject_CallMethod(PyLogger, "exception", "O", log_msg); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
Py_DECREF(log_msg); | ||
} | ||
|
||
/* module initialization function. */ | ||
PyMODINIT_FUNC PyInit_interface(void) | ||
{ | ||
/* ... define local variables ... */ | ||
|
||
try: | ||
|
||
/* ... code to initialise module ... */ | ||
|
||
logging_import = PyImport_ImportModule("logging"); | ||
|
||
if (!logging_import) | ||
{ | ||
const char *err_msg = "failed to import 'logging'"; | ||
PyErr_SetString(PyExc_ImportError, err_msg); | ||
goto except; | ||
} | ||
|
||
logger = py_get_logger("my.module.name"); | ||
|
||
if (!logger) | ||
{ | ||
goto except; | ||
} | ||
|
||
/* ... more fabulous things ... */ | ||
|
||
except: | ||
|
||
/* abnormal cleanup */ | ||
|
||
/* cleanup logger references */ | ||
Py_XDECREF(logging_import); | ||
Py_XDECREF(logger); | ||
ret = NULL; | ||
|
||
finally: | ||
|
||
/* ... clean up under normal conditions ... */ | ||
} | ||
|
||
To simply use the interface defined in the above function, use it like the `printf` family of functions: | ||
|
||
.. code-block:: c | ||
|
||
py_log_msg(WARNING, "error code: %d", 10); | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.