diff --git a/config.c b/config.c index 82bfb01..59e0ead 100644 --- a/config.c +++ b/config.c @@ -283,6 +283,13 @@ int get_global_config(struct config *conf) } else { return error; } + if ((error = uci_get_value(UCI_CPE_ENABLE_SYSLOG, &value)) == CWMP_OK) { + if (value != NULL) { + log_set_on_syslog(value); + free(value); + value = NULL; + } + } if ((error = uci_get_value(UCI_CPE_PORT_PATH, &value)) == CWMP_OK) { int a = 0; diff --git a/inc/cwmp_uci.h b/inc/cwmp_uci.h index 3e21ef5..a66e649 100644 --- a/inc/cwmp_uci.h +++ b/inc/cwmp_uci.h @@ -42,6 +42,7 @@ #define UCI_CPE_LOG_MAX_SIZE "cwmp.cpe.log_max_size" #define UCI_CPE_ENABLE_STDOUT_LOG "cwmp.cpe.log_to_console" #define UCI_CPE_ENABLE_FILE_LOG "cwmp.cpe.log_to_file" +#define UCI_CPE_ENABLE_SYSLOG "cwmp.cpe.log_to_syslog" #define UCI_CPE_AMD_VERSION "cwmp.cpe.amd_version" #define UCI_CPE_INSTANCE_MODE "cwmp.cpe.instance_mode" #define UCI_CPE_SESSION_TIMEOUT "cwmp.cpe.session_timeout" diff --git a/inc/log.h b/inc/log.h index 0cffc49..01797aa 100644 --- a/inc/log.h +++ b/inc/log.h @@ -37,6 +37,7 @@ int log_set_log_file_name(char *value); int log_set_file_max_size(char *value); int log_set_on_console(char *value); int log_set_on_file(char *value); +int log_set_on_syslog(char *value); int log_set_severity_idx(char *value); #define DEFAULT_LOG_FILE_SIZE 10240 #define DEFAULT_LOG_FILE_NAME "/var/log/icwmpd.log" diff --git a/log.c b/log.c index 679fb02..a3a19e6 100644 --- a/log.c +++ b/log.c @@ -12,6 +12,9 @@ #include #include +#include +#include +#include #include "common.h" #include "log.h" @@ -21,6 +24,7 @@ static long int log_max_size = DEFAULT_LOG_FILE_SIZE; static char log_file_name[256]; static bool enable_log_file = true; static bool enable_log_stdout = false; +static bool enable_log_syslog = true; static pthread_mutex_t mutex_log = PTHREAD_MUTEX_INITIALIZER; int log_set_severity_idx(char *value) @@ -72,6 +76,27 @@ int log_set_on_file(char *value) return 1; } +extern char *__progname; + +int log_set_on_syslog(char *value) +{ + if (strcmp(value, "enable") == 0) { + char ident[256]; + + enable_log_syslog = true; + + setlogmask(LOG_UPTO(log_severity)); + snprintf(ident, sizeof(ident), "%s[%d]", __progname, getpid()); + ident[sizeof(ident) - 1] = '\0'; + openlog(ident, LOG_NDELAY, LOG_LOCAL1); + } + + if (strcmp(value, "disable") == 0) { + enable_log_syslog = false; + } + return 1; +} + void puts_log(int severity, const char *fmt, ...) { va_list args; @@ -85,12 +110,12 @@ void puts_log(int severity, const char *fmt, ...) char buf[1024]; char buf_file[1024]; - if (severity > log_severity) { - return; - } - pthread_mutex_lock(&mutex_log); + if (severity > log_severity) { + goto syslog; + } + gettimeofday(&tv, 0); Tm = localtime(&tv.tv_sec); i = sprintf(buf, "%02d-%02d-%4d, %02d:%02d:%02d %s ", Tm->tm_mday, Tm->tm_mon + 1, Tm->tm_year + 1900, Tm->tm_hour, Tm->tm_min, Tm->tm_sec, SEVERITY_NAMES[severity]); @@ -123,6 +148,17 @@ void puts_log(int severity, const char *fmt, ...) if (enable_log_stdout) { puts(buf); } + +syslog: + if (enable_log_syslog) { + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + buf[sizeof(buf) - 1] = '\0'; + va_end(args); + + syslog(severity, "%s", buf); + } + pthread_mutex_unlock(&mutex_log); } @@ -137,12 +173,12 @@ void puts_log_xmlmsg(int severity, char *msg, int msgtype) char buf[1024]; char *description, *separator; - if (severity > log_severity) { - return; - } - pthread_mutex_lock(&mutex_log); + if (severity > log_severity) { + goto xml_syslog; + } + gettimeofday(&tv, 0); Tm = localtime(&tv.tv_sec); sprintf(buf, "%02d-%02d-%4d, %02d:%02d:%02d %s ", Tm->tm_mday, Tm->tm_mon + 1, Tm->tm_year + 1900, Tm->tm_hour, Tm->tm_min, Tm->tm_sec, SEVERITY_NAMES[severity]); @@ -185,5 +221,13 @@ void puts_log_xmlmsg(int severity, char *msg, int msgtype) puts("\n"); puts(separator); } + +xml_syslog: + if (enable_log_syslog) { + syslog(severity, "%s: %s", ((msgtype == XML_MSG_IN) ? "IN" : "OUT"), msg); + if (sizeof(buf) < strlen(msg)) + syslog(severity, "Truncated message at %d characters", strlen(msg)); + } + pthread_mutex_unlock(&mutex_log); }