diff --git a/src/libdap-python.c b/src/libdap-python.c index 75eb61f6183239581ea2f8891cb32b8a4e10f9e7..5806859daf13bc4550bf75d360bb6e0366f2804a 100644 --- a/src/libdap-python.c +++ b/src/libdap-python.c @@ -32,11 +32,10 @@ static PyObject *dap_deinit(){ } static PyObject *dap_set_log_level(PyObject *self, PyObject *args){ - const char *data; - if (!PyArg_ParseTuple(args, "s", &data)) + short int new_log_level; + if (!PyArg_ParseTuple(args, "h", &new_log_level)) return NULL; - dap_log_level_t new_log_level = convert_const_char_to_dap_log_level(data); - if (new_log_level == -1) { + if (new_log_level < 0 || new_log_level > 5 ) { return PyLong_FromLong(-1); } else { dap_log_level_set(new_log_level); @@ -67,16 +66,16 @@ static dap_log_level_t convert_const_char_to_dap_log_level(const char* string){ } static PyObject* dap_log_it(PyObject* self, PyObject* args){ - const char* dap_log_leve_char; + short int log_level; const char* string_output; - if (!PyArg_ParseTuple(args, "s|s", &dap_log_leve_char, &string_output)) + if (!PyArg_ParseTuple(args, "h|s", &log_level, &string_output)) return NULL; - dap_log_level_t log_level = convert_const_char_to_dap_log_level(dap_log_leve_char); - if (log_level == -1) + if (log_level < 0 || log_level > 5 ) { return PyLong_FromLong(-1); - log_it(log_level, string_output); - - return PyLong_FromLong(0); + } else { + dap_log_level_set(log_level); + return PyLong_FromLong(0); + } } static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){ diff --git a/src/libdapConnector.py b/src/libdapConnector.py index 3aca29eff9ae82347b542e977d4c051cdd99fc19..b0165a72056c03606edd2b1540f051f42cc6eb01 100644 --- a/src/libdapConnector.py +++ b/src/libdapConnector.py @@ -1,9 +1,18 @@ import json +from enum import Enum import libdap_python_module class DapIniException(Exception): pass +class LogLevel(Enum): + L_CRITICAL=5 + L_ERROR=4 + L_WARNING=3 + L_NOTICE=2 + L_INFO=1 + L_DEBUG=0 + def init(data): res = json.loads(data) modules=res['modules'] @@ -13,18 +22,16 @@ def init(data): res_init = libdap_python_module.init(config_dir, application_name) if res_init == -1 or res_init == -2: raise DapIniException("Initialization erorr") - if log_level != "NOTICE": - setLogLevel(log_level) + if log_level != LogLevel.L_NOTICE.name: + setLogLevel(LogLevel[log_level]) def deInit(): libdap_python_module.deinit() -def setLogLevel(data): - log_level=data - res_setLogLevel = libdap_python_module.setLogLevel(data) +def setLogLevel(logLevel): + res_setLogLevel = libdap_python_module.setLogLevel(logLevel.value) if res_setLogLevel == -1: raise DapIniException("Failed to set the logging level, perhaps you did not correctly specify the name of the level") -def logIt(data): - parse_data = json.loads(data) - res_log_it = libdap_python_module.logIt(parse_data['level'], parse_data['data']) +def logIt(logLevel, data): + res_log_it = libdap_python_module.logIt(logLevel.value, 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") def configGetItem(section_path, item_name): diff --git a/test/main_test.py b/test/main_test.py index 6a3980ecd83387ab9154b13a512f6a412bc480b9..b1f545be1fb40958a2fc450c81b74252d0eaca68 100644 --- a/test/main_test.py +++ b/test/main_test.py @@ -4,19 +4,16 @@ json_string = """{ "modules": "libdap", "dap": { "config_dir": "/opt/dap/etc", - "log_level": "DEBUG", + "log_level": \""""+libdapConnector.LogLevel.L_DEBUG.name+"""\", "application_name": "TestAPP" } }""" print("Standard Configuration \n"+json_string) libdapConnector.init(json_string) print("Initialization of the DAP") -libdapConnector.setLogLevel("DEBUG") +libdapConnector.setLogLevel(libdapConnector.LogLevel.L_DEBUG) print("Level logging ""DEBUG"" done") -libdapConnector.logIt("""{ - "level": "DEBUG", - "data": "Test. Outputting a string using the log_it function in the libdap library" -}""") +libdapConnector.logIt(libdapConnector.LogLevel.L_DEBUG, "Test. Outputting a string using the log_it function in the libdap library") print("Outputting a string using the log_it function done") res1 = libdapConnector.configGetItem("server", "listen_address") print("Output [server] 'listen_address' = "+res1+"\n")