Skip to content
Snippets Groups Projects
Commit df9a9422 authored by alexey.stratulat's avatar alexey.stratulat
Browse files

Corrected. Now the log_it function accepts not JSON as input, but two...

Corrected. Now the log_it function accepts not JSON as input, but two parameters — the level of logging and the displayed string.
Corrected. The setLogLevel function does not accept a string with a level name, but a parameter from the LogLevel enumeration.
Accordingly, the LogLevel enumeration has been added, which contains logging levels from debug to critical.
parent c9a2d0a3
No related branches found
No related tags found
No related merge requests found
...@@ -32,11 +32,10 @@ static PyObject *dap_deinit(){ ...@@ -32,11 +32,10 @@ static PyObject *dap_deinit(){
} }
static PyObject *dap_set_log_level(PyObject *self, PyObject *args){ static PyObject *dap_set_log_level(PyObject *self, PyObject *args){
const char *data; short int new_log_level;
if (!PyArg_ParseTuple(args, "s", &data)) if (!PyArg_ParseTuple(args, "h", &new_log_level))
return NULL; return NULL;
dap_log_level_t new_log_level = convert_const_char_to_dap_log_level(data); if (new_log_level < 0 || new_log_level > 5 ) {
if (new_log_level == -1) {
return PyLong_FromLong(-1); return PyLong_FromLong(-1);
} else { } else {
dap_log_level_set(new_log_level); 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){ ...@@ -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){ static PyObject* dap_log_it(PyObject* self, PyObject* args){
const char* dap_log_leve_char; short int log_level;
const char* string_output; 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; return NULL;
dap_log_level_t log_level = convert_const_char_to_dap_log_level(dap_log_leve_char); if (log_level < 0 || log_level > 5 ) {
if (log_level == -1)
return PyLong_FromLong(-1); return PyLong_FromLong(-1);
log_it(log_level, string_output); } else {
dap_log_level_set(log_level);
return PyLong_FromLong(0); return PyLong_FromLong(0);
}
} }
static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){ static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){
......
import json import json
from enum import Enum
import libdap_python_module import libdap_python_module
class DapIniException(Exception): class DapIniException(Exception):
pass 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): def init(data):
res = json.loads(data) res = json.loads(data)
modules=res['modules'] modules=res['modules']
...@@ -13,18 +22,16 @@ def init(data): ...@@ -13,18 +22,16 @@ def init(data):
res_init = libdap_python_module.init(config_dir, application_name) res_init = libdap_python_module.init(config_dir, application_name)
if res_init == -1 or res_init == -2: if res_init == -1 or res_init == -2:
raise DapIniException("Initialization erorr") raise DapIniException("Initialization erorr")
if log_level != "NOTICE": if log_level != LogLevel.L_NOTICE.name:
setLogLevel(log_level) setLogLevel(LogLevel[log_level])
def deInit(): def deInit():
libdap_python_module.deinit() libdap_python_module.deinit()
def setLogLevel(data): def setLogLevel(logLevel):
log_level=data res_setLogLevel = libdap_python_module.setLogLevel(logLevel.value)
res_setLogLevel = libdap_python_module.setLogLevel(data)
if res_setLogLevel == -1: if res_setLogLevel == -1:
raise DapIniException("Failed to set the logging level, perhaps you did not 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(data): def logIt(logLevel, data):
parse_data = json.loads(data) res_log_it = libdap_python_module.logIt(logLevel.value, data)
res_log_it = libdap_python_module.logIt(parse_data['level'], parse_data['data'])
if res_log_it == -1: 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") 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): def configGetItem(section_path, item_name):
......
...@@ -4,19 +4,16 @@ json_string = """{ ...@@ -4,19 +4,16 @@ json_string = """{
"modules": "libdap", "modules": "libdap",
"dap": { "dap": {
"config_dir": "/opt/dap/etc", "config_dir": "/opt/dap/etc",
"log_level": "DEBUG", "log_level": \""""+libdapConnector.LogLevel.L_DEBUG.name+"""\",
"application_name": "TestAPP" "application_name": "TestAPP"
} }
}""" }"""
print("Standard Configuration \n"+json_string) print("Standard Configuration \n"+json_string)
libdapConnector.init(json_string) libdapConnector.init(json_string)
print("Initialization of the DAP") print("Initialization of the DAP")
libdapConnector.setLogLevel("DEBUG") libdapConnector.setLogLevel(libdapConnector.LogLevel.L_DEBUG)
print("Level logging ""DEBUG"" done") print("Level logging ""DEBUG"" done")
libdapConnector.logIt("""{ libdapConnector.logIt(libdapConnector.LogLevel.L_DEBUG, "Test. Outputting a string using the log_it function in the libdap library")
"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") print("Outputting a string using the log_it function done")
res1 = libdapConnector.configGetItem("server", "listen_address") res1 = libdapConnector.configGetItem("server", "listen_address")
print("Output [server] 'listen_address' = "+res1+"\n") print("Output [server] 'listen_address' = "+res1+"\n")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment