From: <cha...@us...> - 2008-09-26 03:02:07
|
Revision: 542 http://sipp.svn.sourceforge.net/sipp/?rev=542&view=rev Author: charlespwright Date: 2008-09-26 03:02:03 +0000 (Fri, 26 Sep 2008) Log Message: ----------- Enh: Add abstraction for the various log files. Modified Paths: -------------- sipp/trunk/screen.cpp sipp/trunk/sipp.cpp sipp/trunk/sipp.hpp Modified: sipp/trunk/screen.cpp =================================================================== --- sipp/trunk/screen.cpp 2008-09-25 14:27:13 UTC (rev 541) +++ sipp/trunk/screen.cpp 2008-09-26 03:02:03 UTC (rev 542) @@ -168,9 +168,9 @@ "Max file size reached - no more logs\n", CStat::formatTime(¤tTime)); fflush(f); - stop_all_traces(); + stop_all_traces(); print_all_responses = 0; - screen_errorf = 0; + error_lfi.fptr = NULL; } @@ -231,33 +231,33 @@ c+= sprintf(c, ".\n"); screen_errors++; - if(screen_inited && !screen_errorf && print_all_responses) { + if(screen_inited && !error_lfi.fptr && print_all_responses) { rotate_errorf(); - if(!screen_errorf) { + if(!error_lfi.fptr) { c += sprintf(c, "%s: Unable to create '%s': %s.\n", screen_exename, screen_logfile, strerror(errno)); screen_exit(EXIT_FATAL_ERROR); } else { - fprintf(screen_errorf, "%s: The following events occured:\n", + fprintf(error_lfi.fptr, "%s: The following events occured:\n", screen_exename); - fflush(screen_errorf); + fflush(error_lfi.fptr); } } - if(screen_errorf) { - count += fprintf(screen_errorf, "%s", screen_last_error); - fflush(screen_errorf); + if(error_lfi.fptr) { + count += fprintf(error_lfi.fptr, "%s", screen_last_error); + fflush(error_lfi.fptr); if (ringbuffer_size && count > ringbuffer_size) { rotate_errorf(); count = 0; } if (max_log_size && count > max_log_size) { print_all_responses = 0; - if (screen_errorf) { - fflush(screen_errorf); - fclose(screen_errorf); - screen_errorf = NULL; - errorf_overwrite = false; + if (error_lfi.fptr) { + fflush(error_lfi.fptr); + fclose(error_lfi.fptr); + error_lfi.fptr = NULL; + error_lfi.overwrite = false; } } } else if (fatal) { Modified: sipp/trunk/sipp.cpp =================================================================== --- sipp/trunk/sipp.cpp 2008-09-25 14:27:13 UTC (rev 541) +++ sipp/trunk/sipp.cpp 2008-09-26 03:02:03 UTC (rev 542) @@ -40,6 +40,11 @@ void sipp_usleep(unsigned long usec); +void rotate_messagef(); +void rotate_calldebugf(); +void rotate_shortmessagef(); +void rotate_logfile(); + #ifdef _USE_OPENSSL SSL_CTX *sip_trp_ssl_ctx = NULL; /* For SSL cserver context */ SSL_CTX *sip_trp_ssl_ctx_client = NULL; /* For SSL cserver context */ @@ -1667,6 +1672,15 @@ } } +void log_off(struct logfile_info *lfi) { + if (lfi->fptr) { + fflush(lfi->fptr); + fclose(lfi->fptr); + lfi->fptr = NULL; + lfi->overwrite = false; + } +} + void process_trace(char *what) { bool on = false; char *rest = strchr(what, ' '); @@ -1703,15 +1717,10 @@ print_all_responses = 1; } else { print_all_responses = 0; - if (screen_errorf) { - fflush(screen_errorf); - fclose(screen_errorf); - screen_errorf = NULL; - errorf_overwrite = false; - } + log_off(&error_lfi); } } else if (!strcmp(what, "logs")) { - if (on == !!logfile) { + if (on == !!log_lfi.fptr) { return; } if (on) { @@ -1719,13 +1728,10 @@ rotate_logfile(); } else { useLogf = 0; - fflush(logfile); - fclose(logfile); - logfile = NULL; - logfile_overwrite = false; + log_off(&log_lfi); } } else if (!strcmp(what, "messages")) { - if (on == !!messagef) { + if (on == !!message_lfi.fptr) { return; } if (on) { @@ -1733,13 +1739,10 @@ rotate_logfile(); } else { useMessagef = 0; - fflush(messagef); - fclose(messagef); - messagef = NULL; - messagef_overwrite = false; + log_off(&message_lfi); } } else if (!strcmp(what, "shortmessages")) { - if (on == !!shortmessagef) { + if (on == !!shortmessage_lfi.fptr) { return; } @@ -1748,10 +1751,7 @@ rotate_shortmessagef(); } else { useShortMessagef = 0; - fflush(shortmessagef); - fclose(shortmessagef); - shortmessagef = NULL; - shortmessagef_overwrite = false; + log_off(&shortmessage_lfi); } } else { WARNING("Unknown log file: %s", what); @@ -3716,12 +3716,10 @@ void stop_all_traces() { - if(messagef) messagef = NULL; - if(logfile) logfile = NULL; - // if(timeoutf) timeoutf = NULL; TODO: finish the -trace_timeout option implementation + message_lfi.fptr = NULL; + log_lfi.fptr = NULL; if(dumpInRtt) dumpInRtt = 0; if(dumpInFile) dumpInFile = 0; - } char* remove_pattern(char* P_buffer, char* P_extensionPattern) { @@ -5583,214 +5581,152 @@ } } -#ifdef __cplusplus -extern "C" { -#endif -int TRACE_MSG(char *fmt, ...) { - int ret = 0; - static unsigned long long count = 0; - if(messagef) { - va_list ap; - va_start(ap, fmt); - ret = vfprintf(messagef, fmt, ap); - va_end(ap); - fflush(messagef); +void rotatef(struct logfile_info *lfi) { + char L_rotate_file_name [MAX_PATH]; - count += ret; + sprintf (lfi->file_name, "%s_%d_%s.log", scenario_file, getpid(), lfi->name); - if (max_log_size && count > max_log_size) { - fclose(messagef); - messagef = NULL; + if (ringbuffer_files > 0) { + if (!lfi->ftimes) { + lfi->ftimes = (struct logfile_id *)calloc(ringbuffer_files, sizeof(struct logfile_id)); } - - if (ringbuffer_size && count > ringbuffer_size) { - rotate_messagef(); - count = 0; + /* We need to rotate away an existing file. */ + if (lfi->nfiles == ringbuffer_files) { + if ((lfi->ftimes)[0].n) { + sprintf(L_rotate_file_name, "%s_%d_%s_%lu.%d.log", scenario_file, getpid(), lfi->name, (lfi->ftimes)[0].start, (lfi->ftimes)[0].n); + } else { + sprintf(L_rotate_file_name, "%s_%d_%s_%lu.log", scenario_file, getpid(), lfi->name, (lfi->ftimes)[0].start); + } + unlink(L_rotate_file_name); + lfi->nfiles--; + memmove(lfi->ftimes, &((lfi->ftimes)[1]), sizeof(struct logfile_id) * (lfi->nfiles)); } + if (lfi->starttime) { + (lfi->ftimes)[lfi->nfiles].start = lfi->starttime; + (lfi->ftimes)[lfi->nfiles].n = 0; + /* If we have the same time, then we need to append an identifier. */ + if (lfi->nfiles && ((lfi->ftimes)[lfi->nfiles].start == (lfi->ftimes)[lfi->nfiles - 1].start)) { + (lfi->ftimes)[lfi->nfiles].n = (lfi->ftimes)[lfi->nfiles - 1].n + 1; + } + if ((lfi->ftimes)[lfi->nfiles].n) { + sprintf(L_rotate_file_name, "%s_%d_%s_%lu.%d.log", scenario_file, getpid(), lfi->name, (lfi->ftimes)[lfi->nfiles].start, (lfi->ftimes)[lfi->nfiles].n); + } else { + sprintf(L_rotate_file_name, "%s_%d_%s_%lu.log", scenario_file, getpid(), lfi->name, (lfi->ftimes)[lfi->nfiles].start); + } + lfi->nfiles++; + fflush(lfi->fptr); + fclose(lfi->fptr); + lfi->fptr = NULL; + rename(lfi->file_name, L_rotate_file_name); + } } - return ret; + + time(&lfi->starttime); + if (lfi->overwrite) { + lfi->fptr = fopen(lfi->file_name, "w"); + } else { + lfi->fptr = fopen(lfi->file_name, "a"); + lfi->overwrite = true; + } + if(lfi->check && !lfi->fptr) { + /* We can not use the error functions from this function, as we may be rotating the error log itself! */ + ERROR("Unable to create '%s'", lfi->file_name); + } } -int TRACE_SHORTMSG(char *fmt, ...) { - int ret = 0; - static unsigned long long count = 0; - if(shortmessagef) { - va_list ap; - va_start(ap, fmt); - ret = vfprintf(shortmessagef, fmt, ap); - va_end(ap); - fflush(shortmessagef); +void rotate_calldebugf() { + rotatef(&calldebug_lfi); +} - count += ret; +void rotate_messagef() { + rotatef(&message_lfi); +} - if (max_log_size && count > max_log_size) { - fclose(shortmessagef); - shortmessagef = NULL; - } - if (ringbuffer_size && count > ringbuffer_size) { - rotate_shortmessagef(); - count = 0; - } - } - return ret; +void rotate_shortmessagef() { + rotatef(&shortmessage_lfi); } -int LOG_MSG(char *fmt, ...) { - int ret = 0; - static unsigned long long count = 0; - if(logfile) { - va_list ap; - va_start(ap, fmt); - ret = vfprintf(logfile, fmt, ap); - va_end(ap); - fflush(logfile); - count += ret; +void rotate_logfile() { + rotatef(&log_lfi); +} - if (max_log_size && count > max_log_size) { - fclose(logfile); - logfile = NULL; - } - - if (ringbuffer_size && count > ringbuffer_size) { - rotate_messagef(); - count = 0; - } - } - return ret; +void rotate_errorf() { + rotatef(&error_lfi); + strcpy(screen_logfile, error_lfi.file_name); } -int TRACE_CALLDEBUG(char *fmt, ...) { +#ifdef __cplusplus +extern "C" { +#endif +int _trace (struct logfile_info *lfi, char *fmt, va_list ap) { int ret = 0; - static unsigned long long count = 0; - if(calldebugf) { - va_list ap; - va_start(ap, fmt); - ret = vfprintf(calldebugf, fmt, ap); - va_end(ap); - fflush(calldebugf); + if(lfi->fptr) { + ret = vfprintf(lfi->fptr, fmt, ap); + fflush(lfi->fptr); - count += ret; + lfi->count += ret; - if (max_log_size && count > max_log_size) { - fclose(calldebugf); - calldebugf = NULL; + if (max_log_size && lfi->count > max_log_size) { + fclose(lfi->fptr); + lfi->fptr = NULL; } - if (ringbuffer_size && count > ringbuffer_size) { - rotate_calldebugf(); - count = 0; + if (ringbuffer_size && lfi->count > ringbuffer_size) { + rotatef(lfi); + lfi->count = 0; } } return ret; } -// TODO: finish the -trace_timeout option implementation -/* int TRACE_TIMEOUT(char *fmt, ...) */ -#ifdef __cplusplus -} -#endif +int TRACE_MSG(char *fmt, ...) { + int ret; + va_list ap; -struct logfile_id { - time_t start; - int n; -}; + va_start(ap, fmt); + ret = _trace(&message_lfi, fmt, ap); + va_end(ap); -/* We can not use the error functions from this file, as we may be rotating the error log itself! */ -void rotatef(char *name, FILE **fptr, time_t *starttime, int *nfiles, struct logfile_id **ftimes, bool check, bool *overwrite) { - char L_file_name [MAX_PATH]; - char L_rotate_file_name [MAX_PATH]; + return ret; +} - sprintf (L_file_name, "%s_%d_%s.log", scenario_file, getpid(), name); +int TRACE_SHORTMSG(char *fmt, ...) { + int ret; + va_list ap; - if (ringbuffer_files > 0) { - if (!*ftimes) { - *ftimes = (struct logfile_id *)calloc(ringbuffer_files, sizeof(struct logfile_id)); - } - /* We need to rotate away an existing file. */ - if (*nfiles == ringbuffer_files) { - if ((*ftimes)[0].n) { - sprintf(L_rotate_file_name, "%s_%d_%s_%lu.%d.log", scenario_file, getpid(), name, (*ftimes)[0].start, (*ftimes)[0].n); - } else { - sprintf(L_rotate_file_name, "%s_%d_%s_%lu.log", scenario_file, getpid(), name, (*ftimes)[0].start); - } - unlink(L_rotate_file_name); - (*nfiles)--; - memmove(*ftimes, &((*ftimes)[1]), sizeof(struct logfile_id) * (*nfiles)); - } - if (*starttime) { - (*ftimes)[*nfiles].start = *starttime; - (*ftimes)[*nfiles].n = 0; - /* If we have the same time, then we need to append an identifier. */ - if (*nfiles && ((*ftimes)[*nfiles].start == (*ftimes)[*nfiles - 1].start)) { - (*ftimes)[*nfiles].n = (*ftimes)[*nfiles - 1].n + 1; - } - if ((*ftimes)[*nfiles].n) { - sprintf(L_rotate_file_name, "%s_%d_%s_%lu.%d.log", scenario_file, getpid(), name, (*ftimes)[*nfiles].start, (*ftimes)[*nfiles].n); - } else { - sprintf(L_rotate_file_name, "%s_%d_%s_%lu.log", scenario_file, getpid(), name, (*ftimes)[*nfiles].start); - } - (*nfiles)++; - fflush(*fptr); - fclose(*fptr); - *fptr = NULL; - rename(L_file_name, L_rotate_file_name); - } - } + va_start(ap, fmt); + ret = _trace(&shortmessage_lfi, fmt, ap); + va_end(ap); - time(starttime); - if (*overwrite) { - *fptr = fopen(L_file_name, "w"); - } else { - *fptr = fopen(L_file_name, "a"); - *overwrite = true; - } - if(check && !*fptr) { - ERROR("Unable to create '%s'", L_file_name); - } + return ret; } -int calldebugf_nfiles = 0; -struct logfile_id *calldebugf_times = NULL; +int LOG_MSG(char *fmt, ...) { + int ret; + va_list ap; -void rotate_calldebugf() { - static time_t starttime = 0; - rotatef("calldebug", &calldebugf, &starttime, &calldebugf_nfiles, &calldebugf_times, true, &calldebugf_overwrite); -} + va_start(ap, fmt); + ret = _trace(&log_lfi, fmt, ap); + va_end(ap); - -int messagef_nfiles = 0; -struct logfile_id *messagef_times = NULL; - -void rotate_messagef() { - static time_t starttime = 0; - rotatef("messages", &messagef, &starttime, &messagef_nfiles, &messagef_times, true, &messagef_overwrite); + return ret; } -int shortmessagef_nfiles = 0; -struct logfile_id *shortmessagef_times = NULL; +int TRACE_CALLDEBUG(char *fmt, ...) { + int ret; + va_list ap; -void rotate_shortmessagef() { - static time_t starttime = 0; - rotatef("shortmessages", &shortmessagef, &starttime, &shortmessagef_nfiles, &shortmessagef_times, true, &shortmessagef_overwrite); -} + va_start(ap, fmt); + ret = _trace(&calldebug_lfi, fmt, ap); + va_end(ap); -int logfile_nfiles = 0; -struct logfile_id *logfile_times = NULL; - -void rotate_logfile() { - static time_t starttime = 0; - rotatef("logs", &logfile, &starttime, &logfile_nfiles, &logfile_times, true, &logfile_overwrite); + return ret; } -int errorf_nfiles = 0; -struct logfile_id *errorf_times = NULL; - -void rotate_errorf() { - static time_t starttime = 0; - rotatef("errors", &screen_errorf, &starttime, &errorf_nfiles, &errorf_times, false, &errorf_overwrite); - /* If rotatef is changed, this must be changed as well. */ - sprintf (screen_logfile, "%s_%d_errors.log", scenario_file, getpid()); +#ifdef __cplusplus } +#endif + Modified: sipp/trunk/sipp.hpp =================================================================== --- sipp/trunk/sipp.hpp 2008-09-25 14:27:13 UTC (rev 541) +++ sipp/trunk/sipp.hpp 2008-09-26 03:02:03 UTC (rev 542) @@ -439,10 +439,6 @@ /************************** Trace Files ***********************/ extern FILE * screenf _DEFVAL(0); -extern FILE * logfile _DEFVAL(0); -extern FILE * messagef _DEFVAL(0); -extern FILE * calldebugf _DEFVAL(0); -extern FILE * shortmessagef _DEFVAL(0); extern FILE * countf _DEFVAL(0); // extern FILE * timeoutf _DEFVAL(0); extern bool useMessagef _DEFVAL(0); @@ -450,12 +446,6 @@ extern bool useShortMessagef _DEFVAL(0); extern bool useScreenf _DEFVAL(0); extern bool useLogf _DEFVAL(0); -// should we overwrite the existing files? -extern bool messagef_overwrite _DEFVAL(true); -extern bool calldebugf_overwrite _DEFVAL(true); -extern bool shortmessagef_overwrite _DEFVAL(true); -extern bool errorf_overwrite _DEFVAL(true); -extern bool logfile_overwrite _DEFVAL(true); //extern bool useTimeoutf _DEFVAL(0); extern bool dumpInFile _DEFVAL(0); extern bool dumpInRtt _DEFVAL(0); @@ -469,13 +459,39 @@ extern char screen_last_error[32768]; extern char screen_logfile[MAX_PATH] _DEFVAL(""); -extern FILE * screen_errorf _DEFVAL(NULL); /* Log Rotation Functions. */ -void rotate_messagef(); -void rotate_calldebugf(); -void rotate_shortmessagef(); -void rotate_logfile(); +struct logfile_id { + time_t start; + int n; +}; + +struct logfile_info { + char *name; + bool check; + FILE *fptr; + int nfiles; + struct logfile_id *ftimes; + char file_name[MAX_PATH]; + bool overwrite; + bool fixedname; + time_t starttime; + unsigned int count; +}; + +#ifdef GLOBALS_FULL_DEFINITION +#define LOGFILE(name, s, check) \ + struct logfile_info name = { s, check, NULL, 0, NULL, "", true, false, 0, 0}; +#else +#define LOGFILE(name, s, check) \ + extern struct logfile_info name; +#endif +LOGFILE(calldebug_lfi, "calldebug", true); +LOGFILE(message_lfi, "messages", true); +LOGFILE(shortmessage_lfi, "shortmessages", true); +LOGFILE(log_lfi, "logs", true); +LOGFILE(error_lfi, "errors", false); + void rotate_errorf(); /* Screen/Statistics Printing Functions. */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |