|
| 1 | +#include <os/log.h> |
| 2 | +#include <os/object.h> |
| 3 | + |
| 4 | +#include "Python.h" |
| 5 | + |
| 6 | +PyDoc_STRVAR(_pyoslog_doc, |
| 7 | + "Send messages to the macOS unified logging system. See: " |
| 8 | + "https://developer.apple.com/documentation/os/os_log"); |
| 9 | + |
| 10 | +/* -------------------------------------------------------------------------- */ |
| 11 | + |
| 12 | +PyDoc_STRVAR(os_log_with_type_doc, |
| 13 | + "Sends a message at a specific logging level, such as default, " |
| 14 | + "info, debug, error, or fault, to the logging system. See: " |
| 15 | + "https://developer.apple.com/documentation/os/os_log_with_type"); |
| 16 | + |
| 17 | +static PyObject *py_os_log_with_type(PyObject *self, PyObject *args) { |
| 18 | + PyObject *py_log; |
| 19 | + const uint8_t *type; |
| 20 | + const char *log_message; |
| 21 | + |
| 22 | + if (!PyArg_ParseTuple(args, "Obs", &py_log, &type, &log_message)) { |
| 23 | + return NULL; |
| 24 | + } |
| 25 | + |
| 26 | + os_log_t *log; |
| 27 | + int use_default = 0; |
| 28 | + if (PyCapsule_IsValid(py_log, "os_log_t")) { |
| 29 | + if (!(log = (os_log_t *)PyCapsule_GetPointer(py_log, "os_log_t"))) { |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + } else { |
| 33 | + // a hack - treat capsule errors as selecting the default log, |
| 34 | + // meaning we don't have to provide a constant os_log_t object |
| 35 | + use_default = 1; |
| 36 | + } |
| 37 | + |
| 38 | + // TODO: can we support custom formats here? (must be constant strings) |
| 39 | + os_log_with_type(use_default ? OS_LOG_DEFAULT : *log, (os_log_type_t)type, |
| 40 | + "%{public}s", log_message); |
| 41 | + Py_RETURN_NONE; |
| 42 | +} |
| 43 | + |
| 44 | +static void os_log_release(PyObject *args) { |
| 45 | + os_log_t *log; |
| 46 | + if (!(log = (os_log_t *)PyCapsule_GetPointer(args, "os_log_t"))) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + os_release(*log); |
| 51 | + PyMem_Free(log); |
| 52 | +} |
| 53 | + |
| 54 | +PyDoc_STRVAR( |
| 55 | + os_log_create_doc, |
| 56 | + "Creates a custom log object. See: " |
| 57 | + "https://developer.apple.com/documentation/os/1643744-os_log_create"); |
| 58 | + |
| 59 | +static PyObject *py_os_log_create(PyObject *self, PyObject *args) { |
| 60 | + const char *subsystem; |
| 61 | + const char *category; |
| 62 | + if (!PyArg_ParseTuple(args, "ss", &subsystem, &category)) { |
| 63 | + return NULL; |
| 64 | + } |
| 65 | + |
| 66 | + os_log_t *log = (os_log_t *)PyMem_Malloc(sizeof(os_log_t *)); |
| 67 | + if (log == NULL) { |
| 68 | + return NULL; |
| 69 | + } |
| 70 | + |
| 71 | + *log = os_log_create(subsystem, category); |
| 72 | + return PyCapsule_New(log, "os_log_t", os_log_release); |
| 73 | +} |
| 74 | + |
| 75 | +// TODO: are there any additional methods worth implementing? |
| 76 | +// https://opensource.apple.com/source/xnu/xnu-3789.21.4/libkern/os/log.h.auto.html |
| 77 | +static PyMethodDef module_methods[] = { |
| 78 | + {.ml_name = "os_log_with_type", |
| 79 | + .ml_meth = (PyCFunction)py_os_log_with_type, |
| 80 | + .ml_flags = METH_VARARGS, |
| 81 | + .ml_doc = os_log_with_type_doc}, |
| 82 | + {.ml_name = "os_log_create", |
| 83 | + .ml_meth = (PyCFunction)py_os_log_create, |
| 84 | + .ml_flags = METH_VARARGS, |
| 85 | + .ml_doc = os_log_create_doc}, |
| 86 | + {.ml_name = NULL} /* sentinel */ |
| 87 | +}; |
| 88 | + |
| 89 | +static struct PyModuleDef module_definition = {.m_base = PyModuleDef_HEAD_INIT, |
| 90 | + .m_name = "_pyoslog", |
| 91 | + .m_doc = _pyoslog_doc, |
| 92 | + .m_size = -1, |
| 93 | + .m_methods = module_methods, |
| 94 | + /* m_reload = */ NULL, |
| 95 | + .m_traverse = NULL, |
| 96 | + .m_clear = NULL, |
| 97 | + .m_free = NULL}; |
| 98 | + |
| 99 | +PyObject *PyInit__pyoslog(void) { |
| 100 | + PyObject *module = PyModule_Create(&module_definition); |
| 101 | + if (module == NULL) { |
| 102 | + return NULL; |
| 103 | + } |
| 104 | + |
| 105 | + // the default log type (no named subsystem or facility) |
| 106 | + // see note above for why we don't use the actual value |
| 107 | + PyModule_AddIntConstant(module, "OS_LOG_DEFAULT", 0); |
| 108 | + |
| 109 | + // standard log types |
| 110 | + PyModule_AddIntConstant(module, "OS_LOG_TYPE_DEFAULT", OS_LOG_TYPE_DEFAULT); |
| 111 | + PyModule_AddIntConstant(module, "OS_LOG_TYPE_INFO", OS_LOG_TYPE_DEFAULT); |
| 112 | + PyModule_AddIntConstant(module, "OS_LOG_TYPE_DEBUG", OS_LOG_TYPE_DEBUG); |
| 113 | + PyModule_AddIntConstant(module, "OS_LOG_TYPE_ERROR", OS_LOG_TYPE_ERROR); |
| 114 | + PyModule_AddIntConstant(module, "OS_LOG_TYPE_FAULT", OS_LOG_TYPE_FAULT); |
| 115 | + |
| 116 | + return module; |
| 117 | +} |
0 commit comments