"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "splitlog.c" between
sarg-2.3.11.tar.gz and sarg-2.4.0.tar.gz

About: SARG ia a Squid Analysis Report Generator.

splitlog.c  (sarg-2.3.11):splitlog.c  (sarg-2.4.0)
/* /*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
* 1998, 2013 * 1998, 2015
* *
* SARG donations: * SARG donations:
* please look at http://sarg.sourceforge.net/donations.php * please look at http://sarg.sourceforge.net/donations.php
* Support: * Support:
* http://sourceforge.net/projects/sarg/forums/forum/363374 * http://sourceforge.net/projects/sarg/forums/forum/363374
* --------------------------------------------------------------------- * ---------------------------------------------------------------------
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
skipping to change at line 41 skipping to change at line 41
Extract a date range from a squid log file and write it into a separate file. Extract a date range from a squid log file and write it into a separate file.
It can optionally convert the date in human readable format. It can optionally convert the date in human readable format.
The output can be split by day into separate files. The output can be split by day into separate files.
\param arq The squid log file to split. \param arq The squid log file to split.
\param df The date format if the date is to be converted in human readable form. Only the first \param df The date format if the date is to be converted in human readable form. Only the first
character is taken into account. It can be 'e' for European date format or anyth ing else for character is taken into account. It can be 'e' for European date format or anyth ing else for
US date format. US date format.
\param dfrom The first date to output in the form (Year*10000+Month*100+Day). \param ReadFilter How to filter out log data.
\param duntil The last date to output in the form (Year*10000+Month*100+Day).
\param convert \c True if the date must be converted into human readable form. \param convert \c True if the date must be converted into human readable form.
\param splitprefix If not empty, the output file is written in separate files (o ne for each day) and \param splitprefix If not empty, the output file is written in separate files (o ne for each day) and
the files are named after the day they contain prefixed with the string containe d in this variable. the files are named after the day they contain prefixed with the string containe d in this variable.
*/ */
void splitlog(const char *arq, const char *df, int dfrom, int duntil, int conver t, const char *splitprefix) void splitlog(const char *arq, char df, const struct ReadLogDataStruct *ReadFilt er, int convert, const char *splitprefix)
{ {
FILE *fp_in; FileObject *fp_in;
FILE *fp_ou=NULL; FILE *fp_ou=NULL;
char *buf; char *buf;
char data[30]; char data[30];
char dia[11]; char dia[11];
char output_file[MAXLEN]; char output_file[MAXLEN];
time_t tt; time_t tt;
time_t min_tt; time_t min_tt;
time_t max_tt=0; time_t max_tt=0;
int idata=0; int idata=0;
int autosplit=0; int autosplit=0;
int output_prefix_len=0; int output_prefix_len=0;
int prev_year=0, prev_month=0, prev_day=0; int prev_year=0, prev_month=0, prev_day=0;
struct tm *t; struct tm *t;
struct getwordstruct gwarea; struct getwordstruct gwarea;
longline line; longline line;
if (splitprefix[0]!='\0') { if (splitprefix[0]!='\0') {
// '/' + '-YYYY-mm-dd' + '\0' == 13 // '/' + '-YYYY-mm-dd' + '\0' == 13
output_prefix_len=snprintf(output_file,sizeof(output_file)-12,"%s %s",outdir,splitprefix); output_prefix_len=snprintf(output_file,sizeof(output_file)-12,"%s %s",outdir,splitprefix);
if (output_prefix_len>=sizeof(output_file)-12) { if (output_prefix_len>=sizeof(output_file)-12) {
debugapos("splitlog",_("Path too long: ")); debuga(__FILE__,__LINE__,_("Path too long: "));
debuga_more("%s%s-YYYY-mm-dd\n",outdir,splitprefix); debuga_more("%s%s-YYYY-mm-dd\n",outdir,splitprefix);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
autosplit=1; autosplit=1;
} else { } else {
fp_ou=stdout; fp_ou=stdout;
} }
if(arq[0] == '\0') if (arq[0] == '\0')
arq="/var/log/squid/access.log"; arq="/var/log/squid/access.log";
if (arq[0]=='-' && arq[1]=='\0') { if ((fp_in=decomp(arq))==NULL) {
fp_in=stdin; debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,F
} else if((fp_in=MY_FOPEN(arq,"r"))==NULL) { ileObject_GetLastOpenError());
debugapos("splitlog",_("Cannot open file \"%s\": %s\n"),arq,strer
ror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if ((line=longline_create())==NULL) { if ((line=longline_create())==NULL) {
debuga(_("Not enough memory to read file \"%s\"\n"),arq); debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\" \n"),arq);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
time(&min_tt); time(&min_tt);
while((buf=longline_read(fp_in,line))!=NULL) { while((buf=longline_read(fp_in,line))!=NULL) {
getword_start(&gwarea,buf); getword_start(&gwarea,buf);
if (getword(data,sizeof(data),&gwarea,' ')<0) { if (getword(data,sizeof(data),&gwarea,' ')<0) {
debuga(_("Invalid date in file \"%s\"\n"),arq); debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n "),arq);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
tt=atoi(data); tt=atoi(data);
t=localtime(&tt); t=localtime(&tt);
if(dfrom) { if (ReadFilter->DateRange[0])
{
idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mda y; idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mda y;
if(idata < dfrom || idata > duntil) if (idata<ReadFilter->StartDate || idata>ReadFilter->EndD
ate)
continue;
}
if (ReadFilter->StartTime>=0 || ReadFilter->EndTime>=0)
{
int hmr=t->tm_hour*100+t->tm_min;
if (hmr<ReadFilter->StartTime || hmr>=ReadFilter->EndTime
)
continue; continue;
} }
if (autosplit && (prev_year!=t->tm_year || prev_month!=t->tm_mon || prev_day!=t->tm_mday)) { if (autosplit && (prev_year!=t->tm_year || prev_month!=t->tm_mon || prev_day!=t->tm_mday)) {
prev_year=t->tm_year; prev_year=t->tm_year;
prev_month=t->tm_mon; prev_month=t->tm_mon;
prev_day=t->tm_mday; prev_day=t->tm_mday;
if (fp_ou && fclose(fp_ou)==EOF) { if (fp_ou && fclose(fp_ou)==EOF) {
debuga(_("Failed to close file \"%s\": %s\n"),out put_file,strerror(errno)); debuga(__FILE__,__LINE__,_("Write error in \"%s\" : %s\n"),output_file,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strftime(output_file+output_prefix_len, sizeof(output_fil e)-output_prefix_len, "-%Y-%m-%d", t); strftime(output_file+output_prefix_len, sizeof(output_fil e)-output_prefix_len, "-%Y-%m-%d", t);
/* /*
The line must be added to a file we have already created. The file must be created if the date The line must be added to a file we have already created. The file must be created if the date
is seen for the first time. The idea is to create the fil es from scratch if the split is started is seen for the first time. The idea is to create the fil es from scratch if the split is started
a second time. a second time.
*/ */
if ((fp_ou=MY_FOPEN(output_file,(tt>=min_tt && tt<=max_tt ) ? "a" : "w"))==NULL) { if ((fp_ou=MY_FOPEN(output_file,(tt>=min_tt && tt<=max_tt ) ? "a" : "w"))==NULL) {
debugapos("splitlog",_("Cannot open file \"%s\": %s\n"),output_file,strerror(errno)); debuga(__FILE__,__LINE__,_("Cannot open file \"%s \": %s\n"),output_file,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (tt<min_tt) min_tt=tt; if (tt<min_tt) min_tt=tt;
if (tt>max_tt) max_tt=tt; if (tt>max_tt) max_tt=tt;
} }
if(!convert) { if (!convert) {
fprintf(fp_ou,"%s %s\n",data,gwarea.current); fprintf(fp_ou,"%s %s\n",data,gwarea.current);
} else { } else {
if(df[0]=='e') if (df=='e')
strftime(dia, sizeof(dia), "%d/%m/%Y", t); strftime(dia, sizeof(dia), "%d/%m/%Y", t);
else else
strftime(dia, sizeof(dia), "%m/%d/%Y", t); strftime(dia, sizeof(dia), "%m/%d/%Y", t);
fprintf(fp_ou,"%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t-> tm_min,t->tm_sec,gwarea.current); fprintf(fp_ou,"%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t-> tm_min,t->tm_sec,gwarea.current);
} }
} }
longline_destroy(&line); longline_destroy(&line);
if (fp_in!=stdin && fclose(fp_in)==EOF) { if (FileObject_Close(fp_in)) {
debuga(_("Failed to close file \"%s\": %s\n"),arq,strerror(errno) debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,File
); Object_GetLastCloseError());
exit(EXIT_FAILURE);
} }
if (autosplit && fp_ou) { if (autosplit && fp_ou) {
if (fclose(fp_ou)==EOF) { if (fclose(fp_ou)==EOF) {
debuga(_("Failed to close file \"%s\": %s\n"),output_file ,strerror(errno)); debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n") ,output_file,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
} }
 End of changes. 17 change blocks. 
24 lines changed or deleted 31 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)