From f1beed7e6d568c93de531e9cb2d00be3784e9eed Mon Sep 17 00:00:00 2001 From: Alexey Stratulat <alexey.stratulat@demlabs.net> Date: Wed, 28 Aug 2019 11:55:22 +0700 Subject: [PATCH] [+] Added functions logItDebug, logItInfo logItNotice, logItMessage, logItDap, logItWarning, logItAtt, logItError, logItCritical. --- src/libdap-python.c | 131 ++++++++++++++++++++++++++++++++++---------- src/libdap-python.h | 100 ++++++++++++++++++++++++++------- 2 files changed, 182 insertions(+), 49 deletions(-) diff --git a/src/libdap-python.c b/src/libdap-python.c index cef5f78..d63dcb2 100644 --- a/src/libdap-python.c +++ b/src/libdap-python.c @@ -1,6 +1,6 @@ #include "libdap-python.h" -static PyObject *dap_init(PyObject *self, PyObject *args){ +/*static PyObject *dap_init(PyObject *self, PyObject *args){ //const char *data; const char *system_configs_dir; const char *dap_app_name; @@ -33,13 +33,13 @@ static PyObject *dap_deinit(){ log_it(L_DEBUG, "Function dap_config_deinit done."); log_it(L_DEBUG, "Function dap_deinit done."); return PyLong_FromLong(0); -} +}*/ -static PyObject *dap_set_log_level(PyObject *self, PyObject *args){ +PyObject *dap_set_log_level(PyObject *self, PyObject *args){ short int new_log_level; if (!PyArg_ParseTuple(args, "h", &new_log_level)) return NULL; - if (new_log_level < 0 || new_log_level > 5 ) { + if (new_log_level < 0 || new_log_level > 10 ) { return PyLong_FromLong(-1); } else { dap_log_level_set(new_log_level); @@ -47,12 +47,12 @@ static PyObject *dap_set_log_level(PyObject *self, PyObject *args){ } } -static PyObject* dap_log_it(PyObject* self, PyObject* args){ +PyObject* dap_log_it(PyObject* self, PyObject* args){ short int log_level; const char* string_output; if (!PyArg_ParseTuple(args, "h|s", &log_level, &string_output)) return NULL; - if (log_level < 0 || log_level > 5 ) { + if (log_level < 0 || log_level > 10 ) { return PyLong_FromLong(-1); } else { log_it(log_level, string_output); @@ -60,7 +60,80 @@ static PyObject* dap_log_it(PyObject* self, PyObject* args){ } } -static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){ +PyObject* dap_log_it_debug(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_DEBUG, string_output); + return PyLong_FromLong(0); +} +PyObject* dap_log_it_info(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_INFO, string_output); + return PyLong_FromLong(0); +} +PyObject* dap_log_it_notice(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_NOTICE, string_output); + return PyLong_FromLong(0); +} +PyObject* dap_log_it_message(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_MSG, string_output); + return PyLong_FromLong(0); +} +PyObject* dap_log_it_dap(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_DAP, string_output); + return PyLong_FromLong(0); +} +PyObject* dap_log_it_warning(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_WARNING, string_output); + return PyLong_FromLong(0); +} +PyObject* dap_log_it_att(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_ATT, string_output); + return PyLong_FromLong(0); +} +PyObject* dap_log_it_error(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_ERROR, string_output); + return PyLong_FromLong(0); +} +PyObject* dap_log_it_critical(PyObject* self, PyObject* args){ + const char* string_output; + if (!PyArg_ParseTuple(args, "s", &string_output)){ + return NULL; + } + log_it(L_CRITICAL, string_output); + return PyLong_FromLong(0); +} + +PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){ const char *section_path; const char *item_name; if (!PyArg_ParseTuple(args, "s|s", §ion_path, &item_name)) @@ -73,7 +146,7 @@ static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){ return Py_BuildValue("s", res); } -static PyObject* py_m_dap_config_get_item_default(PyObject *self, PyObject *args){ +PyObject* py_m_dap_config_get_item_default(PyObject *self, PyObject *args){ const char *section_path; const char *item_name; const char *def; @@ -83,31 +156,31 @@ static PyObject* py_m_dap_config_get_item_default(PyObject *self, PyObject *args return Py_BuildValue("s", res); } -PyMODINIT_FUNC PyInit_libdap_python_module(void){ +/*PyMODINIT_FUNC PyInit_libdap_python_module(void){ return PyModule_Create(&dapmodule); -} +}*/ -int main(int argc, char **argv) { - wchar_t *program = Py_DecodeLocale(argv[0], NULL); - if (program == NULL) { - fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); - exit(1); - } +//int main(int argc, char **argv) { +// wchar_t *program = Py_DecodeLocale(argv[0], NULL); +// if (program == NULL) { +// fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); +// exit(1); +// } - /* Add a built-in module, before Py_Initialize */ - PyImport_AppendInittab("libdap_python_module", PyInit_libdap_python_module); +// /* Add a built-in module, before Py_Initialize */ +// PyImport_AppendInittab("libdap_python_module", PyInit_libdap_python_module); - /* Pass argv[0] to the Python interpreter */ - Py_SetProgramName(program); +// /* Pass argv[0] to the Python interpreter */ +// Py_SetProgramName(program); - /* Initialize the Python interpreter. Required. */ - Py_Initialize(); +// /* Initialize the Python interpreter. Required. */ +// Py_Initialize(); - /* Optionally import the module; alternatively, - import can be deferred until the embedded script - imports it. */ - PyImport_ImportModule("libdap_python_module"); +// /* Optionally import the module; alternatively, +// import can be deferred until the embedded script +// imports it. */ +// PyImport_ImportModule("libdap_python_module"); - PyMem_RawFree(program); - return 0; -} +// PyMem_RawFree(program); +// return 0; +//} diff --git a/src/libdap-python.h b/src/libdap-python.h index 360f564..00cd688 100644 --- a/src/libdap-python.h +++ b/src/libdap-python.h @@ -1,3 +1,4 @@ +#pragma once #define PY_SSIZE_T_CLEAN #include <Python.h> #include "dap_config.h" @@ -9,38 +10,97 @@ extern "C" { #define LOG_TAG "libdap-python" -static PyObject *dap_init(PyObject *self, PyObject *args); +typedef struct PyDap{ + PyObject_HEAD +}PyDapObject; -static PyObject *dap_deinit(); +//static PyObject *dap_init(PyObject *self, PyObject *args); -static PyObject *dap_set_log_level(PyObject *self, PyObject *args); +//static PyObject *dap_deinit(PyObject *self, PyObject *args); -static PyObject* dap_log_it(PyObject* self, PyObject* args); +PyObject *dap_set_log_level(PyObject *self, PyObject *args); -static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args); +PyObject* dap_log_it(PyObject* self, PyObject* args); -static PyObject* py_m_dap_config_get_item_default(PyObject *self, PyObject *args); +PyObject* dap_log_it_debug(PyObject* self, PyObject* args); +PyObject* dap_log_it_info(PyObject* self, PyObject* args); +PyObject* dap_log_it_notice(PyObject* self, PyObject* args); +PyObject* dap_log_it_message(PyObject* self, PyObject* args); +PyObject* dap_log_it_dap(PyObject* self, PyObject* args); +PyObject* dap_log_it_warning(PyObject* self, PyObject* args); +PyObject* dap_log_it_att(PyObject* self, PyObject* args); +PyObject* dap_log_it_error(PyObject* self, PyObject* args); +PyObject* dap_log_it_critical(PyObject* self, PyObject* args); + +PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args); + +PyObject* py_m_dap_config_get_item_default(PyObject *self, PyObject *args); static PyMethodDef DapMethods[] = { - {"init", dap_init, METH_VARARGS, "Initialization of the DAP (Deus Applicaions Prototypes) library"}, - {"deinit", dap_deinit, METH_NOARGS, "Deinitialization of the DAP (Deus Applicaions Prototypes) library"}, - {"setLogLevel", dap_set_log_level, METH_VARARGS, "Setting the logging level"}, - {"logIt", dap_log_it, METH_VARARGS, "The wrapper of the log_it function for the libdap library"}, - {"configGetItem", py_m_dap_config_get_item, METH_VARARGS, ""}, - {"configGetItemDefault", py_m_dap_config_get_item_default, METH_VARARGS, ""}, + //{"init", dap_init, METH_VARARGS, "Initialization of the DAP (Deus Applicaions Prototypes) library"}, + //{"deinit", dap_deinit, METH_NOARGS, "Deinitialization of the DAP (Deus Applicaions Prototypes) library"}, + //{"setLogLevel", (PyCFunction)dap_set_log_level, METH_STATIC, "Setting the logging level"}, + //{"logIt", (PyCFunction)dap_log_it, METH_STATIC, "The wrapper of the log_it function for the libdap library"}, + //{"configGetItem", (PyCFunction)py_m_dap_config_get_item, METH_STATIC, ""}, + //{"configGetItemDefault", (PyCFunction)py_m_dap_config_get_item_default, METH_STATIC, ""}, {NULL, NULL, 0, NULL} }; -static struct PyModuleDef dapmodule = { - PyModuleDef_HEAD_INIT, - "libdap_python_module", /* name of module */ - NULL, /* module documentation, may be NULL */ - -1, /* size of per-interpreter state of the module, - or -1 if the module keeps state in global variables. */ - DapMethods +static PyTypeObject DapObject_DapObjectType = { + PyVarObject_HEAD_INIT(NULL, 0) + "libCellFrame.Dap", /* tp_name */ + sizeof(PyDapObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0,//(destructor)Noddy_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + "Dap objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DapMethods,//Noddy_methods, /* tp_methods */ + 0,//Noddy_members, /* tp_members */ + 0,//Noddy_getseters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0,//(initproc)PyDapEventsObject_init,//(initproc)Noddy_init, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew,//Noddy_new, /* tp_new */ }; -PyMODINIT_FUNC PyInit_libdap_python_module(void); + + + +//static struct PyModuleDef dapmodule = { +// PyModuleDef_HEAD_INIT, +// "libdap_python_module", /* name of module */ +// NULL, /* module documentation, may be NULL */ +// -1, /* size of per-interpreter state of the module, +// or -1 if the module keeps state in global variables. */ +// DapMethods +//}; + +//PyMODINIT_FUNC PyInit_libdap_python_module(void); #ifdef __cplusplus } -- GitLab