#include "cmd_remote.h" #include "logging_common.h" #include "common_types.h" #include "log_types.h" #include "ulog_in.h" #include "s2j/s2j.h" #define DEFAULT_REMOTE_HOST_PORT 514 #define MAX_REMOTE_HOST_CNT 8 static int logging_remote_json_parse(pointer input, log_remote_host_x_t *config_buff); static int logging_remote_json_parse_array(pointer input, log_remote_host_x_t *config_buff, int *cnt); static int logging_remote_level_json_parse(pointer input, log_remote_level_t *config_buff); static void logging_get_remote_level_print(log_remote_level_t *log_conf); static void logging_get_remote_print(log_remote_host_x_t *log_conf, uint cnt); static log_rfc_t get_rfc_by_name(const char *name) { for (int i = 0; i < (sizeof(rfc_tbl) / sizeof(rfc_key_fmt)); i++) { if (strcasecmp(name, rfc_tbl[i].fmt) == 0) { return rfc_tbl[i].rfc; } } return LOG_RFC_UNKNOWN; } static int conf_remote_by_config_id(cJSON *json_obj, const char *host, const u16 port, const log_rfc_t rfc, uint64 config_id) { log_remote_host_x_t remote = {0}; char *json_output = NULL; int config_type = CM_CONFIG_SET; if (LOG_CONFIG_REMOTE_GET_HOST == config_id) { if (NULL == host || '\0' == host[0]) { config_type = CM_CONFIG_GET_ALL; } else { config_type = CM_CONFIG_GET; strncpy(remote.host, host, sizeof(remote.host)); } remote.port = port; remote.rfc = rfc; } else { config_type = CM_CONFIG_SET; strncpy(remote.host, host, sizeof(remote.host)); remote.port = port; remote.rfc = rfc; } s2j_json_set_basic_element(json_obj, &remote, int, port); s2j_json_set_basic_element(json_obj, &remote, int, rfc); s2j_json_set_basic_element(json_obj, &remote, string, host); s2j_json_set_basic_element(json_obj, &remote, int, level); int ret = set_log_conf(config_type, json_obj, config_id, &json_output); if (ret != 0) { ULOG_ERR(g_log, "Host of log which is configured is failure"); } else { ULOG_DEBUG(g_log, "Host of log whice is configured is success"); } if (LOG_CONFIG_REMOTE_GET_HOST == config_id) { /* get与getall同样适合 */ log_remote_host_x_t remotes_out[MAX_REMOTE_HOST_CNT] = {0}; int cnt = 0; if (logging_remote_json_parse_array(json_output, remotes_out, &cnt) == 0) { logging_get_remote_print(remotes_out, cnt); } } return ret; } int conf_remote(cJSON *json_obj, int argc, char **argv) { int ret = -1; uint64 config_id; if (argc < 3) { ULOG_WARNING(g_log, "Parameter too less"); return ret; } if (strcasecmp(argv[2], "add") == 0) { config_id = LOG_CONFIG_REMOTE_ADD_HOST; } else if (strcasecmp(argv[2], "del") == 0) { config_id = LOG_CONFIG_REMOTE_DEL_HOST; } else if (strcasecmp(argv[2], "get") == 0 || strcasecmp(argv[2], "getall") == 0) { config_id = LOG_CONFIG_REMOTE_GET_HOST; } else { ULOG_WARNING(g_log, "Unknown operation command:%s", argv[2]); return ret; } char *host = NULL; if (argc >= 4) { host = argv[3]; } u16 port = DEFAULT_REMOTE_HOST_PORT; if (LOG_CONFIG_REMOTE_GET_HOST == config_id) { port = 0; } if (argc >= 5) { port= atoi(argv[4]); } log_rfc_t rfc = LOG_RFC_3164; if (LOG_CONFIG_REMOTE_GET_HOST == config_id) { rfc = LOG_RFC_UNKNOWN; } if (argc >= 6) { rfc = get_rfc_by_name(argv[5]); if (rfc == LOG_RFC_UNKNOWN) { ULOG_WARNING(g_log, "Unknown rfc standard:%s of log", argv[5]); return ret; } } return conf_remote_by_config_id(json_obj, host, port, rfc, config_id); } int conf_remote_level(cJSON *json_obj, int argc, char **argv) { int level; log_remote_level_t log_level = {0}; log_remote_level_t log_level_out = {0}; uint config_type = CM_CONFIG_SET; char*json_output = NULL; if (argc < 3) { ULOG_WARNING(g_log, "Not input log level"); return -1; } if (argc == 3 && strstr(argv[2], "get") != NULL) { config_type = CM_CONFIG_GET; } else { config_type = CM_CONFIG_SET; if ((level = log_str_to_level(argv[2])) < 0) { ULOG_WARNING(g_log, "Unknown log level:%s", argv[2]); return -1; } log_level.level = (u8)level; } s2j_json_set_basic_element(json_obj, &log_level, int, level); int ret = set_log_conf(config_type, json_obj, LOG_CONFIG_REMOTE_LEVEL, &json_output); if (ret != 0) { ULOG_ERR(g_log, "Remote level of log which is configured is failure"); } else { ULOG_DEBUG(g_log, "Remote level of log whice is configured is success"); } if (CM_CONFIG_GET == config_type) { logging_remote_level_json_parse(json_output, &log_level_out); logging_get_remote_level_print(&log_level_out); } return ret; } static int logging_remote_json_parse(pointer input, log_remote_host_x_t *config_buff) { cJSON *json_obj; if (NULL == config_buff) { return -1; } json_obj = cJSON_Parse(input); if(!json_obj) { return -1; } // char *print_str = cJSON_Print(json_obj); // ULOG_DEBUG(g_log, "remote json parse, json input:%s \n", print_str); // free(print_str); s2j_create_struct_obj(log_host_x, log_remote_host_x_t); if(log_host_x == NULL) { cJSON_Delete(json_obj); return -1; } s2j_struct_get_basic_element(log_host_x, json_obj, int, port); s2j_struct_get_basic_element(log_host_x, json_obj, int, rfc); S2J_STRUCT_GET_STRING_ELEMENT_N(log_host_x, json_obj, host, LOG_HOST_SZ); s2j_struct_get_basic_element(log_host_x, json_obj, int, level); memcpy(config_buff, log_host_x, sizeof(log_remote_host_x_t)); s2j_delete_struct_obj(log_host_x); cJSON_Delete(json_obj); return 0; } static int logging_remote_json_parse_array(pointer input, log_remote_host_x_t *config_buff, int *cnt) { cJSON *json_obj; int json_cnt = 0, i = 0; int used_cnt = 0; json_obj = cJSON_Parse(input); if(!json_obj) { return -1; } // char *print_str = cJSON_Print(json_obj); // ULOG_DEBUG(g_log, "remote json parse array, json input:%s \n", print_str); // free(print_str); json_cnt = cJSON_GetArraySize(json_obj); if (json_cnt > MAX_REMOTE_HOST_CNT) { used_cnt = MAX_REMOTE_HOST_CNT; } else { used_cnt = json_cnt; } s2j_create_struct_obj(log_host_x, log_remote_host_x_t); if(log_host_x == NULL) { cJSON_Delete(json_obj); return -1; } *cnt = 0; for (i = 0; i < used_cnt; i++) { cJSON* p_array = cJSON_GetArrayItem(json_obj, i); if (p_array) { memset(log_host_x, 0, sizeof(log_remote_host_x_t)); s2j_struct_get_basic_element(log_host_x, p_array, int, port); s2j_struct_get_basic_element(log_host_x, p_array, int, rfc); s2j_struct_get_basic_element(log_host_x, p_array, string, host); s2j_struct_get_basic_element(log_host_x, p_array, int, level); memcpy(config_buff+*cnt, log_host_x, sizeof(log_remote_host_x_t)); (*cnt)++; } } s2j_delete_struct_obj(log_host_x); cJSON_Delete(json_obj); return 0; } static int logging_remote_level_json_parse(pointer input, log_remote_level_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_remote_level, log_remote_level_t); if (log_remote_level == NULL) { cJSON_Delete(json_obj); return -1; } s2j_struct_get_basic_element(log_remote_level, json_obj, int, level); memcpy(config_buff, log_remote_level, sizeof(log_remote_level_t)); s2j_delete_struct_obj(log_remote_level); cJSON_Delete(json_obj); return 0; } static void logging_get_remote_level_print(log_remote_level_t *log_conf) { if (NULL == log_conf) { return; } fprintf(stdout, "--------------------------------------------------------------------------------\n"); fprintf(stdout, " get trap:\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 \n"); fprintf(stdout, "--------------------------------------------------------------------------------\n"); ULOG_DEBUG(g_log, "Log level get: level=%u", log_conf->level); } static void logging_get_remote_print(log_remote_host_x_t *log_conf, uint cnt) { char *tmp_str=NULL; if (NULL == log_conf || 0 == cnt) { return; } if (1 == cnt) { fprintf(stdout, "--------------------------------------------------------------------------------\n"); fprintf(stdout, " get host:\n\n"); if (log_conf->level < sizeof(g_level_array) / sizeof(level_str_t)) tmp_str = &g_level_array[log_conf->level].str; else tmp_str = ""; fprintf(stdout, " level %s\n", tmp_str); fprintf(stdout, " ip_addr %s\n", log_conf->host); fprintf(stdout, " port %u\n", log_conf->port); if (log_conf->rfc < sizeof(rfc_tbl) / sizeof(rfc_key_fmt)) tmp_str = &rfc_tbl[log_conf->rfc].fmt; else tmp_str = ""; fprintf(stdout, " RFCfmt %s\n", tmp_str); fprintf(stdout, "--------------------------------------------------------------------------------\n"); ULOG_DEBUG(g_log, "Log host get: addr=%s, port=%u, log_fmt=%u, level=%u", log_conf->host, log_conf->port, log_conf->rfc, log_conf->level); } else { int i=0; fprintf(stdout, "--------------------------------------------------------------------------------\n"); fprintf(stdout, " get hosts:\n\n"); for (i=0; i