diff --git a/config.c b/config.c index e5eac48..5f7b95d 100644 --- a/config.c +++ b/config.c @@ -1181,7 +1181,7 @@ int cwmp_init(int argc, char** argv,struct cwmp *cwmp) { char *piderr = "PID file creation failed: Quit the daemon!"; fprintf(stderr, "%s\n", piderr); - CWMP_LOG(ERROR, piderr); + CWMP_LOG(ERROR, "%s",piderr); exit(EXIT_FAILURE); } else exit(EXIT_SUCCESS); diff --git a/cwmp.c b/cwmp.c index 64bd593..fa9da67 100644 --- a/cwmp.c +++ b/cwmp.c @@ -190,9 +190,9 @@ void cwmp_schedule_session (struct cwmp *cwmp) CWMP_LOG (INFO,"Start session"); error = cwmp_schedule_rpc (cwmp,session); CWMP_LOG (INFO,"End session"); - run_session_end_func(session); if (session->error == CWMP_RETRY_SESSION && (!list_empty(&(session->head_event_container)) || (list_empty(&(session->head_event_container)) && cwmp->cwmp_cr_event == 0)) ) { + run_session_end_func(session); error = cwmp_move_session_to_session_queue (cwmp, session); CWMP_LOG(INFO,"Retry session, retry count = %d, retry in %ds",cwmp->retry_count_session,cwmp_get_retry_interval(cwmp)); retry = true; @@ -203,6 +203,8 @@ void cwmp_schedule_session (struct cwmp *cwmp) pthread_mutex_unlock (&(cwmp->mutex_session_send)); continue; } + event_remove_all_event_container(session,RPC_SEND); + run_session_end_func(session); cwmp_session_destructor (cwmp, session); cwmp->session_send = NULL; cwmp->retry_count_session = 0; @@ -622,7 +624,7 @@ int main(int argc, char **argv) { return error; } - CWMP_LOG(INFO,"STARTING ICWMP"); + CWMP_LOG(INFO,"STARTING ICWMP with PID :%d", getpid()); cwmp->start_time = time(NULL); if (error = cwmp_init_backup_session(cwmp, NULL, ALL)) diff --git a/inc/log.h b/inc/log.h index 720192d..1237fe2 100644 --- a/inc/log.h +++ b/inc/log.h @@ -24,7 +24,13 @@ enum log_severity_enum { DEBUG }; +enum log_xmlmsg_enum { + XML_MSG_IN, + XML_MSG_OUT +}; + void puts_log(int severity, const char *fmt, ...); +void puts_log_xmlmsg(int severity, char *msg, int msgtype); int log_set_log_file_name (char *value); int log_set_file_max_size(char *value); int log_set_on_console(char *value); @@ -37,9 +43,11 @@ int log_set_severity_idx (char *value); #ifdef WITH_CWMP_DEBUG # ifndef CWMP_LOG # define CWMP_LOG(SEV,MESSAGE,args...) puts_log(SEV,MESSAGE,##args); +# define CWMP_LOG_XML_MSG puts_log_xmlmsg # endif #else # define CWMP_LOG(SEV,MESSAGE,args...) +# define CWMP_LOG_XML_MSG(X, Y, Z) #endif #ifdef WITH_DEV_DEBUG diff --git a/log.c b/log.c index d3dcc5a..da08b3b 100644 --- a/log.c +++ b/log.c @@ -89,7 +89,6 @@ int log_set_on_file(char *value) void puts_log(int severity, const char *fmt, ...) { va_list args; - int buflen = 1024; int i; time_t t; struct tm *Tm; @@ -98,17 +97,15 @@ void puts_log(int severity, const char *fmt, ...) struct stat st; long int size = 0; char log_file_name_bak[256]; + char buf[1024]; + char buf_file[1024]; if (severity>log_severity) { return; } - if (severity == DEBUG) - { - buflen = 512000; - } - char buf[buflen]; - char buf_file[buflen]; + + pthread_mutex_lock (&mutex_log); gettimeofday(&tv, 0); t = time((time_t*)NULL); @@ -127,7 +124,6 @@ void puts_log(int severity, const char *fmt, ...) } if(enable_log_file) { - pthread_mutex_lock (&mutex_log); if (stat(log_file_name, &st) == 0) { size = st.st_size; @@ -147,17 +143,103 @@ void puts_log(int severity, const char *fmt, ...) i += vsprintf(buf+i, fmt, args); if(enable_log_file) { - sprintf(buf_file,"%s\n",buf); + strcpy(buf_file,buf); + strcat(buf_file,"\n"); fputs (buf_file, pLog); } va_end(args); if(enable_log_file) { fclose(pLog); - pthread_mutex_unlock (&mutex_log); } if(enable_log_stdout) { puts(buf); } + pthread_mutex_unlock (&mutex_log); +} + +void puts_log_xmlmsg(int severity, char *msg, int msgtype) +{ + va_list args; + int i; + time_t t; + struct tm *Tm; + struct timeval tv; + FILE *pLog = NULL; + struct stat st; + long int size = 0; + char log_file_name_bak[256]; + char buf[1024]; + char buf_file[1024]; + char *description, *separator; + + if (severity>log_severity) + { + return; + } + + pthread_mutex_lock(&mutex_log); + + gettimeofday(&tv, 0); + t = time((time_t*)NULL); + 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]); + if(strlen(log_file_name) == 0) + { + strcpy(log_file_name,DEFAULT_LOG_FILE_NAME); + } + + if (msgtype == XML_MSG_IN) { + description = "MESSAGE IN\n"; + separator = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"; + + } + else { + description = "MESSAGE OUT\n"; + separator = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"; + } + if(enable_log_file) + { + + if (stat(log_file_name, &st) == 0) + { + size = st.st_size; + } + if(size >= log_max_size) + { + sprintf(log_file_name_bak,"%s.1",log_file_name); + rename(log_file_name,log_file_name_bak); + pLog = fopen(log_file_name,"w"); + } + else + { + pLog = fopen(log_file_name,"a+"); + } + fputs (buf, pLog); + fputs(description, pLog); + fputs(separator, pLog); + fputs (msg, pLog); + fputs ("\n", pLog); + fputs(separator, pLog); + fclose(pLog); + + } + if(enable_log_stdout) + { + puts (buf); + puts(description); + puts(separator); + puts (msg); + puts ("\n"); + puts(separator); + } + pthread_mutex_unlock (&mutex_log); } diff --git a/time.c b/time.c index 5e0ab9f..9034711 100644 --- a/time.c +++ b/time.c @@ -9,7 +9,7 @@ #include -char local_time[26] = {0}; +char local_time[32] = {0}; char * mix_get_time_of(time_t t_time) { diff --git a/ubus.c b/ubus.c index 764862a..eec30d8 100644 --- a/ubus.c +++ b/ubus.c @@ -128,7 +128,7 @@ cwmp_handle_command(struct ubus_context *ctx, struct ubus_object *obj, if(rc) { char *piderr = "PID file unlock failed!"; fprintf(stderr, "%s\n", piderr); - CWMP_LOG(ERROR, piderr); + CWMP_LOG(ERROR, "%s", piderr); } blobmsg_add_u32(&b, "status", 0); if (asprintf(&info, "cwmpd daemon stopped") == -1) diff --git a/xml.c b/xml.c index e14a1dc..787fc70 100644 --- a/xml.c +++ b/xml.c @@ -209,7 +209,7 @@ int xml_send_message(struct cwmp *cwmp, struct session *session, struct rpc *rpc unsigned char *zmsg_out; msg_out = mxmlSaveAllocString(session->tree_out, whitespace_cb); - CWMP_LOG(DEBUG,"Message OUT \n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n%s\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",msg_out); + CWMP_LOG_XML_MSG(DEBUG,msg_out,XML_MSG_OUT); if (cwmp->conf.compression != COMP_NONE) { if (zlib_compress(msg_out, &zmsg_out, &msg_out_len, cwmp->conf.compression)) { return -1; @@ -226,7 +226,7 @@ int xml_send_message(struct cwmp *cwmp, struct session *session, struct rpc *rpc goto error; } if (msg_in) { - CWMP_LOG(DEBUG,"Message IN \n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n%s\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",msg_in); + CWMP_LOG_XML_MSG(DEBUG,msg_in,XML_MSG_IN); if (s = strstr(msg_in, "")) sscanf(s, "%d",&f); if (f) { @@ -878,7 +878,7 @@ error: int cwmp_rpc_acs_destroy_data_inform(struct session *session, struct rpc *rpc) { - event_remove_all_event_container(session,RPC_SEND); + //event_remove_all_event_container(session,RPC_SEND); return 0; }