2019-08-14 09:17:18 +00:00
|
|
|
#include "cmd_file.h"
|
|
|
|
#include "ulog_api.h"
|
|
|
|
#include "log_types.h"
|
|
|
|
#include "logging_common.h"
|
|
|
|
#include "ulog/ulog_in.h"
|
|
|
|
#include "s2j/s2j.h"
|
|
|
|
|
2019-08-27 00:49:47 +00:00
|
|
|
|
2019-09-04 07:33:56 +00:00
|
|
|
void logging_get_file_print(log_file_t *log_file);
|
2019-08-27 00:49:47 +00:00
|
|
|
static int logging_file_json_parse(pointer input, log_file_t *config_buff);
|
|
|
|
|
|
|
|
|
2019-08-14 09:17:18 +00:00
|
|
|
int conf_file(cJSON *json_obj, int argc, char **argv)
|
|
|
|
{
|
2019-08-27 00:49:47 +00:00
|
|
|
log_file_t log_file = {0};
|
|
|
|
log_file_t log_file_out = {0};
|
2019-08-14 09:17:18 +00:00
|
|
|
int level = LOG_INFO;
|
2019-08-27 00:49:47 +00:00
|
|
|
uint config_type = CM_CONFIG_SET;
|
2019-08-27 08:45:48 +00:00
|
|
|
char *json_output = NULL;
|
2019-08-27 00:49:47 +00:00
|
|
|
|
|
|
|
log_file.is_compress = LOG_UNCOMPRESS;
|
2019-08-14 09:17:18 +00:00
|
|
|
|
2019-08-27 00:49:47 +00:00
|
|
|
if (argc == 3 && strstr(argv[2], "get") != NULL)
|
|
|
|
{
|
|
|
|
config_type = CM_CONFIG_GET;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
config_type = CM_CONFIG_SET;
|
|
|
|
if (argc >= 3) {
|
|
|
|
if ((level = log_str_to_level(argv[2])) < 0) {
|
|
|
|
ULOG_WARNING(g_log, "Unknown log level:%s", argv[2]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log_file.level = level;
|
2019-08-14 09:17:18 +00:00
|
|
|
|
2019-08-27 00:49:47 +00:00
|
|
|
if (argc >= 4) {
|
|
|
|
strncpy(log_file.path, argv[3], sizeof(log_file.path));
|
|
|
|
}
|
2019-08-14 09:17:18 +00:00
|
|
|
|
2019-08-27 00:49:47 +00:00
|
|
|
if (argc >= 5) {
|
|
|
|
if (atoi(argv[4]) > 0) {
|
|
|
|
log_file.is_compress = LOG_COMPRESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc >= 6) {
|
|
|
|
int day = atoi(argv[5]);
|
|
|
|
if (day > 0) {
|
|
|
|
log_file.del_over_days = day;
|
|
|
|
} else if (day < 0) {
|
|
|
|
ULOG_WARNING(g_log, "Deleting day(%d) mustn't be less than 0", day);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc >= 7) {
|
|
|
|
if (snprintf(log_file.del_over_size, sizeof(log_file.del_over_size), "%s", argv[6]) < 0) {
|
|
|
|
ULOG_ERR(g_log, "Building del max size:%s is filure", argv[6]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 09:17:18 +00:00
|
|
|
|
|
|
|
s2j_json_set_basic_element(json_obj, &log_file, int, level);
|
|
|
|
s2j_json_set_basic_element(json_obj, &log_file, string, path);
|
|
|
|
s2j_json_set_basic_element(json_obj, &log_file, int, is_compress);
|
|
|
|
s2j_json_set_basic_element(json_obj, &log_file, int, del_over_days);
|
2019-08-21 02:17:41 +00:00
|
|
|
s2j_json_set_basic_element(json_obj, &log_file, string, del_over_size);
|
2019-08-14 09:17:18 +00:00
|
|
|
|
2019-08-27 00:49:47 +00:00
|
|
|
int ret = set_log_conf(config_type, json_obj, LOG_CONFIG_FILE, &json_output);
|
|
|
|
if (ret != 0) {
|
|
|
|
ULOG_ERR(g_log, "File log which is configured is failure");
|
|
|
|
} else {
|
|
|
|
ULOG_DEBUG(g_log, "File log whice is configured is success");
|
2019-08-14 09:17:18 +00:00
|
|
|
}
|
2019-08-27 00:49:47 +00:00
|
|
|
|
|
|
|
if (CM_CONFIG_GET == config_type)
|
|
|
|
{
|
|
|
|
logging_file_json_parse(json_output, &log_file_out);
|
2019-09-04 07:33:56 +00:00
|
|
|
|
|
|
|
logging_get_file_print(&log_file_out);
|
2019-08-27 00:49:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 09:17:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:49:47 +00:00
|
|
|
|
|
|
|
static int logging_file_json_parse(pointer input, log_file_t *config_buff)
|
|
|
|
{
|
|
|
|
cJSON *json_obj;
|
|
|
|
|
|
|
|
if (NULL == config_buff)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_obj = cJSON_Parse(input);
|
|
|
|
if (!json_obj)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s2j_create_struct_obj(log_file, log_file_t);
|
|
|
|
|
|
|
|
if (log_file == NULL)
|
|
|
|
{
|
|
|
|
cJSON_Delete(json_obj);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s2j_struct_get_basic_element(log_file, json_obj, int, level);
|
|
|
|
S2J_STRUCT_GET_STRING_ELEMENT_N(log_file, json_obj, path, MAX_PATH_SZ);
|
|
|
|
s2j_struct_get_basic_element(log_file, json_obj, int, is_compress);
|
|
|
|
s2j_struct_get_basic_element(log_file, json_obj, int, del_over_days);
|
|
|
|
S2J_STRUCT_GET_STRING_ELEMENT_N(log_file, json_obj, del_over_size, MAX_U64_SZ);
|
|
|
|
|
|
|
|
memcpy(config_buff, log_file, sizeof(log_file_t));
|
|
|
|
|
|
|
|
s2j_delete_struct_obj(json_obj);
|
|
|
|
cJSON_Delete(json_obj);
|
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
}
|
|
|
|
|
2019-09-04 07:33:56 +00:00
|
|
|
void logging_get_file_print(log_file_t *log_conf)
|
|
|
|
{
|
|
|
|
if (NULL == log_conf)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stdout, "--------------------------------------------------------------------------------\n");
|
|
|
|
fprintf(stdout, " get file:\n\n");
|
|
|
|
if (log_conf->level < sizeof(g_level_array) / sizeof(level_str_t))
|
|
|
|
fprintf(stdout, " level %s\n", g_level_array[log_conf->level].str);
|
|
|
|
else
|
|
|
|
fprintf(stdout, " level <invalid_value>\n");
|
|
|
|
fprintf(stdout, " file_path %s\n", log_conf->path);
|
|
|
|
fprintf(stdout, " is_compress %s\n", log_conf->is_compress==LOG_UNCOMPRESS?"no":"yes");
|
|
|
|
fprintf(stdout, " del_over_days %u\n", log_conf->del_over_days);
|
|
|
|
fprintf(stdout, " del_over_size %s\n", log_conf->del_over_size);
|
|
|
|
fprintf(stdout, "--------------------------------------------------------------------------------\n");
|
|
|
|
|
|
|
|
ULOG_DEBUG(g_log, "Log file get: level=%u, path=%s, is_compress=%d, del_over_days=%u, del_over_size=%s",
|
|
|
|
log_conf->level,
|
|
|
|
log_conf->path,
|
|
|
|
log_conf->is_compress,
|
|
|
|
log_conf->del_over_days, log_conf->del_over_size);
|
|
|
|
}
|