diff --git a/Platform/build/user.ulog.log_sched.Makefile b/Platform/build/user.ulog.log_sched.Makefile index fd936cd80..70fe33360 100755 --- a/Platform/build/user.ulog.log_sched.Makefile +++ b/Platform/build/user.ulog.log_sched.Makefile @@ -27,7 +27,7 @@ VPATH = ../user/ulog/log-sched # set the source file, don't used .o because of ... -COMMON_SRCS = log_file.c log_console.c log_tty.c log_common.c log_sched.c +COMMON_SRCS = log_file.c log_console.c log_tty.c log_remote.c log_common.c log_sched.c # MRS Board Source Files PLAT_LINUX_SRCS = $(COMMON_SRCS) diff --git a/Platform/user/database/database.c b/Platform/user/database/database.c index ed60c9169..4fee34266 100644 --- a/Platform/user/database/database.c +++ b/Platform/user/database/database.c @@ -242,9 +242,9 @@ int update_database(int module_id, void * db_handle, int op_type, char * tabl int type; int len; SQLLEN ind_name = SQL_NTS; - long long int_temp; + long long int_temp[DB_ROWS + 1]; va_list ap; - double double_temp; + double double_temp[DB_ROWS + 1]; SQLPrepare(hstmt, sql_str, SQL_NTS); @@ -258,8 +258,8 @@ int update_database(int module_id, void * db_handle, int op_type, char * tabl if (DB_DATA_INT_TYPE == type) { - int_temp = va_arg(ap, long long); - ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_UBIGINT, SQL_C_UBIGINT, len, 0, &int_temp, len, &ind_name); + int_temp[i] = va_arg(ap, long long); + ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_UBIGINT, SQL_C_UBIGINT, len, 0, &int_temp[i], len, &ind_name); } else if (DB_DATA_STRING_TYPE == type) { @@ -267,8 +267,8 @@ int update_database(int module_id, void * db_handle, int op_type, char * tabl } else if (DB_DATA_FLOAT_TYPE == type) { - double_temp = va_arg(ap, double); - ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, len, 0, &double_temp, len, &ind_name); + double_temp[i] = va_arg(ap, double); + ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, len, 0, &double_temp[i], len, &ind_name); } else { @@ -400,9 +400,9 @@ void * select_datebase_by_number(int module_id, void * db_handle, char * table_ int type; int len; SQLLEN ind_name = SQL_NTS; - long long int_temp; + long long int_temp[DB_ROWS + 1]; va_list ap; - double double_temp; + double double_temp[DB_ROWS + 1]; SQLPrepare(hstmt, sql_str, SQL_NTS); @@ -415,8 +415,8 @@ void * select_datebase_by_number(int module_id, void * db_handle, char * table_ if (DB_DATA_INT_TYPE == type) { - int_temp = va_arg(ap, long long); - ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_UBIGINT, SQL_C_UBIGINT, len, 0, &int_temp, len, &ind_name); + int_temp[i] = va_arg(ap, long long); + ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_UBIGINT, SQL_C_UBIGINT, len, 0, &int_temp[i], len, &ind_name); } else if (DB_DATA_STRING_TYPE == type) { @@ -424,8 +424,8 @@ void * select_datebase_by_number(int module_id, void * db_handle, char * table_ } else if (DB_DATA_FLOAT_TYPE == type) { - double_temp = va_arg(ap, double); - ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, len, 0, &double_temp, len, &ind_name); + double_temp[i] = va_arg(ap, double); + ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, len, 0, &double_temp[i], len, &ind_name); } else { @@ -674,9 +674,9 @@ int get_select_datebase_number(int module_id, void * db_handle, char * table_n int type; int len; SQLLEN ind_name = SQL_NTS; - long long int_temp; + long long int_temp[DB_ROWS + 1]; va_list ap; - double double_temp; + double double_temp[DB_ROWS + 1]; SQLPrepare(hstmt, sql_str, SQL_NTS); @@ -689,8 +689,8 @@ int get_select_datebase_number(int module_id, void * db_handle, char * table_n if (DB_DATA_INT_TYPE == type) { - int_temp = va_arg(ap, long long); - ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_UBIGINT, SQL_C_UBIGINT, len, 0, &int_temp, len, &ind_name); + int_temp[i] = va_arg(ap, long long); + ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_UBIGINT, SQL_C_UBIGINT, len, 0, &int_temp[i], len, &ind_name); } else if (DB_DATA_STRING_TYPE == type) { @@ -698,8 +698,8 @@ int get_select_datebase_number(int module_id, void * db_handle, char * table_n } else if (DB_DATA_FLOAT_TYPE == type) { - double_temp = va_arg(ap, double); - ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, len, 0, &double_temp, len, &ind_name); + double_temp[i] = va_arg(ap, double); + ret = SQLBindParameter(hstmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, len, 0, &double_temp[i], len, &ind_name); } else { diff --git a/Platform/user/ulog/log-sched/log_common.c b/Platform/user/ulog/log-sched/log_common.c index b97c3cc76..c0350fd48 100755 --- a/Platform/user/ulog/log-sched/log_common.c +++ b/Platform/user/ulog/log-sched/log_common.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "ulog.h" @@ -8,9 +9,49 @@ #define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n" -#define BAK_FILE "/tmp/%s" -int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, + +static int copy_file(const char *src, const char *dst) +{ + int ret = 1; + FILE *src_fp = NULL,*dst_fp = NULL; + src_fp = fopen(src, "r"); + if (src_fp == NULL) { + ULOG_ERR(g_log, "Opening src file:%s is failure:%s", src, strerror(errno)); + goto END; + } + dst_fp = fopen(dst, "w"); + if (dst_fp == NULL) { + ULOG_ERR(g_log, "Opening dst file:%s is failure:%s", src, strerror(errno)); + goto END; + } + + char buff[1024]; + int len; + int n; + while(feof(src_fp) > 0) + { + len = fread(buff, 1, sizeof(buff), src_fp); + n = fwrite(buff, 1, len ,dst_fp); + if (n != len) { + ULOG_ERR(g_log, "Configure file which is written[length:%d, actual len:%n] is failure:%s", n, len, strerror(errno)); + goto END; + } + } + + ret = 0; +END: + if (src_fp != NULL) { + fclose(src_fp); + } + if (dst_fp != NULL) { + fclose(dst_fp); + } + + return ret; +} + +static int __log_conf(const char *mode, const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg) { FILE *fp = NULL; @@ -23,17 +64,18 @@ int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, char bak_file[MAX_PATH_SZ]; snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file); - if (rename(conf_path_file, bak_file) < 0) { + //if (rename(conf_path_file, bak_file) < 0) + if (copy_file(conf_path_file, bak_file) != 0) { if (errno == ENOENT) { exist_backup = 0; - ULOG_INFO(g_log, "Been not exist, Configure file:%s is need to backup"); + ULOG_INFO(g_log, "Been not exist, Configure file:%s don't need to backup", conf_path_file); } else { - ULOG_ERR(g_log, "Baking configure file:%s is failure:%s %d", conf_path_file, strerror(errno), errno); + ULOG_ERR(g_log, "Baking configure file:%s is failure:%s", conf_path_file, strerror(errno)); return ret; } } - fp = fopen(conf_path_file, "w"); + fp = fopen(conf_path_file, mode); if (fp == NULL) { ULOG_ERR(g_log, "Opening log configure file:%s is failure:%s", conf_path_file, strerror(errno)); goto END; @@ -56,11 +98,27 @@ END: if (rename(bak_file, conf_path_file) < 0) { ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno)); } + } else { + if (unlink(bak_file) < 0) { + ULOG_WARNING(g_log, "Delete back file:%s is failure", bak_file); + } } return ret; } +int log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, + int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg) +{ + return __log_conf("w", level, conf_path, conf_file, filter_mod, cb_content, arg); +} + +int log_conf_append(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, + int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg) +{ + return __log_conf("a", level, conf_path, conf_file, filter_mod, cb_content, arg); +} + int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) { int i; @@ -89,13 +147,15 @@ int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *a strcat(line, ";"); } } - strcat(line, " "); + strcat(line, REDIRECT_SEPERATE); strcat(line, (const char *)arg); strcat(line, "\n"); - if (fputs(line, fp) == EOF) { - ULOG_ERR(g_log, "Configure file which is written is failure:%s", strerror(errno)); + //if (fputs(line, fp) == EOF) { + int n = fwrite(line, 1, strlen(line), fp); + if (n != strlen(line)) { + ULOG_ERR(g_log, "Configure file which is written[length:%d] is failure:%s", n, strerror(errno)); goto END; } diff --git a/Platform/user/ulog/log-sched/log_common.h b/Platform/user/ulog/log-sched/log_common.h index 891f8866b..67eb51606 100755 --- a/Platform/user/ulog/log-sched/log_common.h +++ b/Platform/user/ulog/log-sched/log_common.h @@ -8,6 +8,13 @@ #define LOG_CONF_PATH "/etc/rsyslog.d/" +#define LOG_DEV_DIR "/dev/" +#define BAK_FILE "/tmp/%s" + + +#define REDIRECT_SEPERATE " " + + typedef struct _level_str { u32 level; char str[10]; @@ -26,7 +33,9 @@ static level_str_t g_level_array[] = { {LOG_DEBUG, "debug"} }; -int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, +int log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, + int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg); +int log_conf_append(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg); int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg); int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_mod, void *arg); diff --git a/Platform/user/ulog/log-sched/log_console.c b/Platform/user/ulog/log-sched/log_console.c index a0bd060b1..d72bb137c 100755 --- a/Platform/user/ulog/log-sched/log_console.c +++ b/Platform/user/ulog/log-sched/log_console.c @@ -1,3 +1,7 @@ +#include +#include +#include + #include "log_console.h" #include "log_common.h" @@ -5,11 +9,51 @@ #define LOG_REDIRECT_CONSOLE "/dev/console" +static int write_console_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) +{ + DIR *dir; + + if ((dir = opendir(LOG_DEV_DIR)) == NULL) { + ULOG_ERR(g_log, "Open dir:[%s] is failure:%d\n", LOG_DEV_DIR, strerror(errno)); + return 1; + } + + struct dirent *ptr; + char path[MAX_PATH_SZ]; + while ((ptr = readdir(dir)) != NULL) { + if ((strcmp(ptr->d_name, ".") == 0) + || (strcmp(ptr->d_name, "..") == 0) + || (ptr->d_type == DT_DIR)) { ///current dir OR parrent dir + ULOG_DEBUG(g_log,"The file:[%s] or directory jump over\n", ptr->d_name); + continue; + } + + if ((strstr(ptr->d_name, "ttyS") == NULL)) { + ULOG_DEBUG(g_log,"The file:[%s] isn't redirected\n", ptr->d_name); + continue; + } + + ULOG_DEBUG(g_log, "ttyS name:%s", ptr->d_name); + if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name) < 0) { + ULOG_ERR(g_log, "Setting %s of log console is failure", ptr->d_name); + return 1; + } + if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) { + ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod); + return 1; + } + + } + + return 0; +} + + static int config_log_console(const log_console_t *conf) { - if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module_name, - write_conf_content_authorizing, (void *)LOG_REDIRECT_CONSOLE) != 0) { - ULOG_ERR(g_log, "Log configure which is written is failure"); + if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module_name, + write_console_content, NULL) != 0) { + ULOG_ERR(g_log, "Log's console configure which is written is failure"); return 1; } diff --git a/Platform/user/ulog/log-sched/log_file.c b/Platform/user/ulog/log-sched/log_file.c index d2bfd4a74..eeedb78a2 100755 --- a/Platform/user/ulog/log-sched/log_file.c +++ b/Platform/user/ulog/log-sched/log_file.c @@ -104,7 +104,7 @@ static int conf_log_file(const log_file_t *conf) } ULOG_DEBUG(g_log, "write_conf_content:%x", write_conf_content); - if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, NULL, + if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, NULL, write_conf_content, (void *)path) != 0) { ULOG_ERR(g_log, "Log configure which is written is failure"); return 1; diff --git a/Platform/user/ulog/log-sched/log_remote.c b/Platform/user/ulog/log-sched/log_remote.c new file mode 100755 index 000000000..dda147bcc --- /dev/null +++ b/Platform/user/ulog/log-sched/log_remote.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include + + +#include "log_remote.h" +#include "log_common.h" + +#define LOG_CONF_REMOTE_FILE_NAME "log-remote.conf" + +typedef struct _rfc_key_fmt { + log_rfc_t rfc; + char fmt[20]; +} rfc_key_fmt; + +static rfc_key_fmt rfc_tbl[] = { + {LOG_RFC_3164, "RFC3164fmt"}, + {LOG_RFC_5424, "RFC5424fmt"} +}; + +static int match_line(const log_remote_host_t *conf) +{ + +} + +static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) +{ + int ret = 1; + FILE *bak_fp = NULL; + char path[MAX_PATH_SZ]; + snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME); + bak_fp = fopen(bak_fp, "r"); + if (bak_fp == NULL) { + ULOG_ERR(g_log, "Opening remote back file:%s is failure:%s", path, strerror(errno)); + return 1; + } + + char *line = NULL; + ssize_t n, n1; + char *pad, *host, *rfc_fmt; + u16 port; + while ((n = getline(&line, &n, bak_fp)) != -1) { + n1 = sscanf(line, "%s"REDIRECT_SEPERATE"%s:%u:%s", &pad, &host, &port, &rfc_fmt); + if (n1 == 4) { + // 匹配到 + } if (errno != 0) { + // 错误发生 + } else { + // 未知行,跳过 + continue; + } + + if (write_conf_content(fp, level, filter_mod, arg) != 0) { + ULOG_ERR(g_log, "Write remote configure is failure"); + goto END; + } + } + + + ret = 0; +END: + if (line != NULL) { + free(line); + } + if (bak_fp != NULL) { + fclose(bak_fp); + } + + return ret; +} + +static int add_remote_host(const log_remote_host_t *conf) +{ + char prefix[1] = ""; + if (conf->rfc == LOG_RFC_5424) { + strcat(prefix, "@"); + }; + + char redirect[MAX_LINE_SZ]; + if (snprintf(redirect, sizeof(redirect), "%s@%s:%u:%s", + prefix, conf->host, conf->port, rfc_tbl[conf->rfc].fmt) < 0) { + ULOG_ERR(g_log, "Setting remote redirect[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); + return 1; + } + if (log_conf_append(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL, + write_conf_content, (void *)redirect) != 0) { + ULOG_ERR(g_log, "Adding remote server[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); + return 1; + } + + return 0; +} + +static int del_remote_host(const log_remote_host_t *conf) +{ +} + +static int config_log_remote_host(const log_remote_host_t *conf) +{ + if ((sizeof(rfc_tbl) / sizeof(rfc_key_fmt)) < conf->rfc) { + ULOG_WARNING(g_log, "Unknown rfc format key:%u", conf->rfc); + return 1; + } + + + return 0; +} + +void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer data) +{ + u32 need_len = sizeof(log_remote_host_t); + if (input_len < need_len) { + ULOG_WARNING(g_log, + "The input paramter of rpc log remote host is needed length of %u, but the actual length is %u", + need_len, input_len); + return; + } + + config_log_remote_host((const log_remote_host_t *)input); +} + diff --git a/Platform/user/ulog/log-sched/log_remote.h b/Platform/user/ulog/log-sched/log_remote.h new file mode 100755 index 000000000..c813fce19 --- /dev/null +++ b/Platform/user/ulog/log-sched/log_remote.h @@ -0,0 +1,27 @@ +#ifndef _LOG_REMOTE_H +#define _LOG_REMOTE_H + +#include "ulog_api.h" +#include "common_types.h" +#include "rpc_common.h" + +typedef enum { + LOG_REMOTE_OP_ADD = 0, + LOG_REMOTE_OP_DEL +} log_op_t; + +typedef enum { + LOG_RFC_3164 = 0, + LOG_RFC_5424 +} log_rfc_t; + +typedef struct _log_remote_host { + log_op_t op; + log_rfc_t rfc; + char host[256]; + u16 port; +} log_remote_host_t; + +void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer data); + +#endif \ No newline at end of file diff --git a/Platform/user/ulog/log-sched/log_sched.c b/Platform/user/ulog/log-sched/log_sched.c index 24d9f1867..9057d1449 100755 --- a/Platform/user/ulog/log-sched/log_sched.c +++ b/Platform/user/ulog/log-sched/log_sched.c @@ -8,11 +8,15 @@ #include "log_file.h" #include "log_console.h" #include "log_tty.h" +#include "log_remote.h" #include "rpc_module.h" -#define LOG_SCHED_MODULE_NAME "log-sched" -#define SERVICE_LOG_FILE_NAME "log-file" +#define LOG_SCHED_MODULE_NAME "log-sched" + +#define SERVICE_LOG_FILE_NAME "log-file" +#define SERIVCE_LOG_CONSOLE_NAME "log-console" +#define SERVICE_LOG_TTY_NAME "log-tty" ulog_t *g_log = NULL; @@ -55,6 +59,8 @@ int main(int argc, char **argv) /* 注册配置处理函数 */ rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", rpc_conf_log_file); + rpc_server_regservice(server, SERIVCE_LOG_CONSOLE_NAME, "conf_log_console", rpc_conf_log_console); + rpc_server_regservice(server, SERVICE_LOG_TTY_NAME, "conf_log_tty", rpc_conf_log_tty); log_file_t log = {0}; log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS; @@ -70,10 +76,16 @@ int main(int argc, char **argv) log_tty_t tty = {0}; - strcpy(tty.module_name, "111"); + //strcpy(tty.module_name, "111"); tty.level = LOG_DEBUG; tty.on = 1; rpc_conf_log_tty(NULL, &tty, sizeof(tty), NULL); + + log_remote_host_t remote = {0}; + strcpy(remote.host, "192.168.3.1"); + remote.rfc = LOG_RFC_5424; + remote.port = 514; + rpc_conf_log_remote(NULL, &remote, sizeof(remote), NULL); END: ulog_close(g_log); return 0; diff --git a/Platform/user/ulog/log-sched/log_tty.c b/Platform/user/ulog/log-sched/log_tty.c index ded33b003..35e40c1d3 100755 --- a/Platform/user/ulog/log-sched/log_tty.c +++ b/Platform/user/ulog/log-sched/log_tty.c @@ -7,7 +7,7 @@ #include "log_common.h" #define LOG_CONF_TTY_FILE_NAME "log-tty.conf" -#define LOG_DEV_DIR "/dev/" + static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) { @@ -37,7 +37,10 @@ static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, v } ULOG_DEBUG(g_log, "tty name:%s", ptr->d_name); - snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name); + if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name) < 0) { + ULOG_ERR(g_log, "Setting tty of log[%s] is failure", ptr->d_name); + return 1; + } if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) { ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod); return 1; @@ -50,7 +53,7 @@ static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, v static int config_log_tty(const log_tty_t *conf) { - if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_TTY_FILE_NAME, conf->module_name, + if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_TTY_FILE_NAME, conf->module_name, write_tty_content, NULL) != 0) { ULOG_ERR(g_log, "Log's tty configure which is written is failure"); return 1; @@ -64,7 +67,7 @@ void rpc_conf_log_tty(rpc_conn *conn, pointer input, int input_len, pointer data u32 need_len = sizeof(log_tty_t); if (input_len < need_len) { ULOG_WARNING(g_log, - "The input paramter of rpc log console is needed length of %u, but the actual length is %u", + "The input paramter of rpc log tty is needed length of %u, but the actual length is %u", need_len, input_len); return; }