From b7453f87db3726602ea0a61a0d1c986e3ccd53de Mon Sep 17 00:00:00 2001 From: Alexey Stratulat <alexey.stratulat@demlabs.net> Date: Mon, 24 Jun 2019 22:32:24 +0700 Subject: [PATCH] The wrapping of the log_it function has been added, a test has also been added for the fact that it works --- src/libdap-python.c | 75 ++++++++++++++++++++++++++++++++---------- src/libdap-python.h | 5 +++ src/libdapConnector.py | 8 ++++- test/main_test.py | 5 +++ 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/src/libdap-python.c b/src/libdap-python.c index 218465a..79bb87e 100644 --- a/src/libdap-python.c +++ b/src/libdap-python.c @@ -53,31 +53,70 @@ static PyObject *dap_set_log_level(PyObject *self, PyObject *args){ const char *data; if (!PyArg_ParseTuple(args, "s", &data)) return NULL; - if (strcmp(data,"DEBUG") == 0){ - dap_log_level_set(L_DEBUG); + dap_log_level_t new_log_level = convert_const_char_to_dap_log_level(data); + if (new_log_level == -1) { + return PyLong_FromLong(-1); + } else { + dap_log_level_set(new_log_level); return PyLong_FromLong(0); } - if (strcmp(data, "INFO") == 0){ - dap_log_level_set(L_INFO); - return PyLong_FromLong(0); +} + +static dap_log_level_t convert_const_char_to_dap_log_level(const char* string){ + if (strcmp(string,"DEBUG") == 0){ + return L_DEBUG; } - if (strcmp(data, "NOTICE") == 0){ - dap_log_level_set(L_NOTICE); - return PyLong_FromLong(0); + if (strcmp(string, "INFO") == 0){ + return L_INFO; } - if (strcmp(data, "WARNING") == 0){ - dap_log_level_set(L_WARNING); - return PyLong_FromLong(0); + if (strcmp(string, "NOTICE") == 0){ + return L_NOTICE; } - if (strcmp(data, "ERROR") == 0){ - dap_log_level_set(L_ERROR); - return PyLong_FromLong(0); + if (strcmp(string, "WARNING") == 0){ + return L_WARNING; } - if (strcmp(data, "CRITICAL") == 0){ - dap_log_level_set(L_CRITICAL); - return PyLong_FromLong(0); + if (strcmp(string, "ERROR") == 0){ + return L_ERROR; } - return PyLong_FromLong(-1); + if (strcmp(string, "CRITICAL") == 0){ + return L_CRITICAL; + } + return -1; +} + +static PyObject* dap_log_it(PyObject* self, PyObject* args){ + const char *data; + if (!PyArg_ParseTuple(args, "s", &data)) + return NULL; + char* dap_log_leve_char; + char* string_output; + int len_log_level_char=0; + int len_string_output=0; + int countSeparators=0; + int lenMassives = 0; + while (*(data+lenMassives) != '\0'){ + if (*(data+lenMassives)=='\n'){ + countSeparators += 1; + }else { + if (countSeparators == 0) + len_log_level_char++; + if (countSeparators == 1) + len_string_output++; + } + lenMassives++; + } + if (len_log_level_char == 0 || len_string_output == 0) + return PyLong_FromLong(-1); + dap_log_leve_char = calloc(len_log_level_char, sizeof(char)); + string_output = calloc(len_string_output, sizeof(char)); + memcpy(dap_log_leve_char, data, len_log_level_char); + memcpy(string_output, data+len_log_level_char+1, len_string_output); + dap_log_level_t log_level = convert_const_char_to_dap_log_level(dap_log_leve_char); + if (log_level == -1) + return PyLong_FromLong(-1); + log_it(log_level, string_output); + + return PyLong_FromLong(0); } PyMODINIT_FUNC PyInit_libdap_python_module(void){ diff --git a/src/libdap-python.h b/src/libdap-python.h index 93c1400..f43a70d 100644 --- a/src/libdap-python.h +++ b/src/libdap-python.h @@ -15,10 +15,15 @@ static PyObject *dap_deinit(PyObject *self); static PyObject *dap_set_log_level(PyObject *self, PyObject *args); +static PyObject* dap_log_it(PyObject* self, PyObject* args); + +static dap_log_level_t convert_const_char_to_dap_log_level(const char* string); + 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"}, {NULL, NULL, 0, NULL} }; diff --git a/src/libdapConnector.py b/src/libdapConnector.py index 4450054..ddcd081 100644 --- a/src/libdapConnector.py +++ b/src/libdapConnector.py @@ -20,5 +20,11 @@ class Dap: self.log_level=data res_setLogLevel = libdap_python_module.setLogLevel(data) if res_setLogLevel == -1: - raise DapIniException("Failed to set the logging level, perhaps you did correctly specify the name of the level") + raise DapIniException("Failed to set the logging level, perhaps you did not correctly specify the name of the level") + def logIt(self, data): + parse_data = json.loads(data) + res_log_it = libdap_python_module.logIt(parse_data['level']+"\n"+parse_data['data']) + if res_log_it == -1: + raise DapIniException("Could not execute log_it function. Perhaps you did not correctly specify the name of the logging level or did not leave the information that needs to be displayed") + diff --git a/test/main_test.py b/test/main_test.py index 334bc5c..7fbb113 100644 --- a/test/main_test.py +++ b/test/main_test.py @@ -13,3 +13,8 @@ daptest = libdapConnector.Dap(json_string) print("Initialization of the DAP") daptest.setLogLevel("DEBUG") print("Level logging ""DEBUG"" done") +daptest.logIt("""{ + "level": "DEBUG", + "data": "Test. Outputting a string using the log_it function in the libdap library" +}""") +print("Outputting a string using the log_it function done") -- GitLab