From: Frédéric Marchal Date: Sun, 26 Aug 2012 09:18:52 +0000 (+0200) Subject: Store the entry time in a structure instead of a pointer X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cb53374b032dbd087a136caa58de300768cc2527;p=thirdparty%2Fsarg.git Store the entry time in a structure instead of a pointer Instead of requiring that the module keeps track of the entry time on behalf of the main loop, the entry time is stored in the entry structure. Therefore, there is no need to keep a static variable inside the module and pass its pointer to the caller. --- diff --git a/authfail.c b/authfail.c index 7741e14..5250796 100644 --- a/authfail.c +++ b/authfail.c @@ -69,7 +69,7 @@ void authfail_write(const struct ReadLogStruct *log_entry) char date[80]; if (fp_authfail && (strstr(log_entry->HttpCode,"DENIED/401") != 0 || strstr(log_entry->HttpCode,"DENIED/407") != 0)) { - strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",log_entry->EntryTime); + strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->EntryTime); fprintf(fp_authfail, "%s\t%s\t%s\t%s\n",date,log_entry->User,log_entry->Ip,log_entry->Url); authfail_exists=true; } diff --git a/denied.c b/denied.c index e483afe..67fd177 100644 --- a/denied.c +++ b/denied.c @@ -69,7 +69,7 @@ void denied_write(const struct ReadLogStruct *log_entry) char date[80]; if (fp_denied && strstr(log_entry->HttpCode,"DENIED/403") != 0) { - strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",log_entry->EntryTime); + strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->EntryTime); fprintf(fp_denied, "%s\t%s\t%s\t%s\n",date,log_entry->User,log_entry->Ip,log_entry->Url); denied_exists=true; } diff --git a/include/readlog.h b/include/readlog.h index f895064..8e18b5b 100644 --- a/include/readlog.h +++ b/include/readlog.h @@ -24,7 +24,7 @@ enum ReadLogReturnCodeEnum struct ReadLogStruct { //! The time corresponding to the entry. - struct tm *EntryTime; + struct tm EntryTime; //! The IP address connecting to internet. char *Ip; //! The user's name. diff --git a/readlog.c b/readlog.c index 1de337d..fcd5171 100644 --- a/readlog.c +++ b/readlog.c @@ -669,10 +669,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) log_entry.EntryTime=&tt; } #endif - if (log_entry.EntryTime==NULL) { - debuga(_("Unknown input log file format: no time\n")); - break; - } if (log_entry.Ip==NULL) { debuga(_("Unknown input log file format: no IP addresses\n")); break; @@ -694,10 +690,10 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) } // Record only hours usage which is required - if( bsearch( &( log_entry.EntryTime->tm_wday ), weekdays.list, weekdays.len, sizeof( int ), compar ) == NULL ) + if( bsearch( &( log_entry.EntryTime.tm_wday ), weekdays.list, weekdays.len, sizeof( int ), compar ) == NULL ) continue; - if( bsearch( &( log_entry.EntryTime->tm_hour ), hours.list, hours.len, sizeof( int ), compar ) == NULL ) + if( bsearch( &( log_entry.EntryTime.tm_hour ), hours.list, hours.len, sizeof( int ), compar ) == NULL ) continue; @@ -757,7 +753,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) } if(Filter->StartTime >= 0 && Filter->EndTime >= 0) { - hmr=log_entry.EntryTime->tm_hour*100+log_entry.EntryTime->tm_min; + hmr=log_entry.EntryTime.tm_hour*100+log_entry.EntryTime.tm_min; if(hmr < Filter->StartTime || hmr > Filter->EndTime) continue; } @@ -879,8 +875,8 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) } } - strftime(dia, sizeof(dia), "%d/%m/%Y", log_entry.EntryTime); - strftime(hora,sizeof(hora),"%H:%M:%S",log_entry.EntryTime); + strftime(dia, sizeof(dia), "%d/%m/%Y",&log_entry.EntryTime); + strftime(hora,sizeof(hora),"%H:%M:%S",&log_entry.EntryTime); if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora, log_entry.Ip,url,(uint64_t)log_entry.DataSize, @@ -916,12 +912,12 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) if (current_format!=&ReadSargLog) { if(!totper || idatamaxdate) { maxdate=idata; - memcpy(&period.end,log_entry.EntryTime,sizeof(*log_entry.EntryTime)); + memcpy(&period.end,&log_entry.EntryTime,sizeof(log_entry.EntryTime)); } totper=true; } diff --git a/readlog_common.c b/readlog_common.c index 2a53130..5cd1074 100644 --- a/readlog_common.c +++ b/readlog_common.c @@ -59,7 +59,6 @@ static enum ReadLogReturnCodeEnum Common_ReadEntry(char *Line,struct ReadLogStru int Hour; int Minute; int Second; - static struct tm tt; // get IP address Entry->Ip=Line; @@ -123,13 +122,12 @@ static enum ReadLogReturnCodeEnum Common_ReadEntry(char *Line,struct ReadLogStru while (*Line && *Line!=']') Line++; if (*Line!=']') return(RLRC_Unknown); - tt.tm_year=Year-1900; - tt.tm_mon=Month; - tt.tm_mday=Day; - tt.tm_hour=Hour; - tt.tm_min=Minute; - tt.tm_sec=Second; - Entry->EntryTime=&tt; + Entry->EntryTime.tm_year=Year-1900; + Entry->EntryTime.tm_mon=Month; + Entry->EntryTime.tm_mday=Day; + Entry->EntryTime.tm_hour=Hour; + Entry->EntryTime.tm_min=Minute; + Entry->EntryTime.tm_sec=Second; // the URL is enclosed between double qhotes ++Line; diff --git a/readlog_sarg.c b/readlog_sarg.c index 7eda5b4..015b2d6 100644 --- a/readlog_sarg.c +++ b/readlog_sarg.c @@ -61,7 +61,6 @@ static enum ReadLogReturnCodeEnum Sarg_ReadEntry(char *Line,struct ReadLogStruct int Hour; int Minute; int Second; - static struct tm tt; if (strncmp(Line,"*** SARG Log ***",16)==0) { InSargLog=true; @@ -99,13 +98,12 @@ static enum ReadLogReturnCodeEnum Sarg_ReadEntry(char *Line,struct ReadLogStruct while (isdigit(*Line)) Second=Second*10+(*Line++-'0'); if (*Line!='\t' || Second>60) return(RLRC_Unknown); //second can be 60 due to a leap second - tt.tm_year=Year-1900; - tt.tm_mon=Month-1; - tt.tm_mday=Day; - tt.tm_hour=Hour; - tt.tm_min=Minute; - tt.tm_sec=Second; - Entry->EntryTime=&tt; + Entry->EntryTime.tm_year=Year-1900; + Entry->EntryTime.tm_mon=Month-1; + Entry->EntryTime.tm_mday=Day; + Entry->EntryTime.tm_hour=Hour; + Entry->EntryTime.tm_min=Minute; + Entry->EntryTime.tm_sec=Second; // the ID of the user Entry->User=++Line; diff --git a/readlog_squid.c b/readlog_squid.c index a83ecd6..a7ac4b7 100644 --- a/readlog_squid.c +++ b/readlog_squid.c @@ -52,6 +52,7 @@ static enum ReadLogReturnCodeEnum Squid_ReadEntry(char *Line,struct ReadLogStruc int HttpCodeLen; int UrlLen; int UserLen; + struct tm *tt; // get log time. Begin=Line; @@ -106,11 +107,12 @@ static enum ReadLogReturnCodeEnum Squid_ReadEntry(char *Line,struct ReadLogStruc if (*Line!=' ' || UserLen==0) return(RLRC_Unknown); // now, the format is known with a good confidence. If the time doesn't decode, it is an error. - Entry->EntryTime=localtime(&log_time); - if (Entry->EntryTime == NULL) { + tt=localtime(&log_time); + if (tt==NULL) { debuga(_("Cannot convert the timestamp from the squid log file\n")); return(RLRC_InternalError); } + memcpy(&Entry->EntryTime,tt,sizeof(struct tm)); // it is safe to alter the line buffer now that we are returning a valid entry Entry->Ip[IpLen]='\0';