secgateway/Platform/user/logging/cmd_console.c

75 lines
2.0 KiB
C
Raw Normal View History

#include "cmd_console.h"
#include "ulog_api.h"
#include "log_types.h"
#include "configm.h"
#include "configmapi.h"
#include "logging_common.h"
#include "ulog/ulog_in.h"
#include "s2j/s2j.h"
static int __conf_console(cJSON *json_obj, const char *str_level, const log_sw_t on, const char *module_name)
{
log_console_t console = {0};
char *output;
int output_len;
int ret = -1;
u8 level;
if ((level = log_str_to_level(str_level)) < 0) {
ULOG_WARNING(g_log, "Unknown log level:%s", str_level);
return -1;
}
console.level = level;
console.on = on;
if (module_name != NULL) {
strncpy(console.module_name, module_name, sizeof(console.module_name));
}
s2j_json_set_basic_element(json_obj, &console, int, level);
s2j_json_set_basic_element(json_obj, &console, int, on);
s2j_json_set_basic_element(json_obj, &console, string, module_name);
char *json = cJSON_PrintUnformatted(json_obj);
if (json == NULL) {
ULOG_ERR(g_log, "Converting struct to json is failure");
return -1;
}
ULOG_DEBUG(g_log, "Console json is %s", json);
ret_code ret_c = web_config_exec_sync(CM_CONFIG_SET, LOG_CONFIG_CONSOLE,
json, strlen(json),
&output, &output_len);
if (ret_c != RET_OK) {
ULOG_ERR(g_log, "Console of Web config is failure:%d", ret_c);
} else {
ret = 0;
}
if (json != NULL) {
free(json);
}
return ret;
}
int conf_console(cJSON *json_obj, int argc, char **argv)
{
log_sw_t on = LOG_ON;
char *level = DEFAULT_LOG_LEVEL;
if (argc >= 3) {
if (strcasecmp(argv[2], "off") == 0) {
on = LOG_OFF;
} else {
level = argv[2];
}
}
char *module = NULL;
if (argc >= 4) {
module = argv[3];
}
return __conf_console(json_obj, level, on, module);
}