"Fossies" - the Fresh Open Source Software Archive 
Member "sarg-2.4.0/splitlog.c" (22 Dec 2019, 5351 Bytes) of package /linux/privat/sarg-2.4.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "splitlog.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.3.11_vs_2.4.0.
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2015
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27 #include "include/conf.h"
28 #include "include/defs.h"
29
30 /*
31 Extract a date range from a squid log file and write it into a separate file.
32
33 It can optionally convert the date in human readable format.
34
35 The output can be split by day into separate files.
36
37 \param arq The squid log file to split.
38 \param df The date format if the date is to be converted in human readable form. Only the first
39 character is taken into account. It can be 'e' for European date format or anything else for
40 US date format.
41 \param ReadFilter How to filter out log data.
42 \param convert \c True if the date must be converted into human readable form.
43 \param splitprefix If not empty, the output file is written in separate files (one for each day) and
44 the files are named after the day they contain prefixed with the string contained in this variable.
45 */
46 void splitlog(const char *arq, char df, const struct ReadLogDataStruct *ReadFilter, int convert, const char *splitprefix)
47 {
48 FileObject *fp_in;
49 FILE *fp_ou=NULL;
50 char *buf;
51 char data[30];
52 char dia[11];
53 char output_file[MAXLEN];
54 time_t tt;
55 time_t min_tt;
56 time_t max_tt=0;
57 int idata=0;
58 int autosplit=0;
59 int output_prefix_len=0;
60 int prev_year=0, prev_month=0, prev_day=0;
61 struct tm *t;
62 struct getwordstruct gwarea;
63 longline line;
64
65 if (splitprefix[0]!='\0') {
66 // '/' + '-YYYY-mm-dd' + '\0' == 13
67 output_prefix_len=snprintf(output_file,sizeof(output_file)-12,"%s%s",outdir,splitprefix);
68 if (output_prefix_len>=sizeof(output_file)-12) {
69 debuga(__FILE__,__LINE__,_("Path too long: "));
70 debuga_more("%s%s-YYYY-mm-dd\n",outdir,splitprefix);
71 exit(EXIT_FAILURE);
72 }
73 autosplit=1;
74 } else {
75 fp_ou=stdout;
76 }
77
78 if (arq[0] == '\0')
79 arq="/var/log/squid/access.log";
80
81 if ((fp_in=decomp(arq))==NULL) {
82 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,FileObject_GetLastOpenError());
83 exit(EXIT_FAILURE);
84 }
85
86 if ((line=longline_create())==NULL) {
87 debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),arq);
88 exit(EXIT_FAILURE);
89 }
90 time(&min_tt);
91
92 while((buf=longline_read(fp_in,line))!=NULL) {
93 getword_start(&gwarea,buf);
94 if (getword(data,sizeof(data),&gwarea,' ')<0) {
95 debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),arq);
96 exit(EXIT_FAILURE);
97 }
98 tt=atoi(data);
99 t=localtime(&tt);
100
101 if (ReadFilter->DateRange[0])
102 {
103 idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday;
104 if (idata<ReadFilter->StartDate || idata>ReadFilter->EndDate)
105 continue;
106 }
107 if (ReadFilter->StartTime>=0 || ReadFilter->EndTime>=0)
108 {
109 int hmr=t->tm_hour*100+t->tm_min;
110 if (hmr<ReadFilter->StartTime || hmr>=ReadFilter->EndTime)
111 continue;
112 }
113
114 if (autosplit && (prev_year!=t->tm_year || prev_month!=t->tm_mon || prev_day!=t->tm_mday)) {
115 prev_year=t->tm_year;
116 prev_month=t->tm_mon;
117 prev_day=t->tm_mday;
118 if (fp_ou && fclose(fp_ou)==EOF) {
119 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),output_file,strerror(errno));
120 exit(EXIT_FAILURE);
121 }
122 strftime(output_file+output_prefix_len, sizeof(output_file)-output_prefix_len, "-%Y-%m-%d", t);
123 /*
124 The line must be added to a file we have already created. The file must be created if the date
125 is seen for the first time. The idea is to create the files from scratch if the split is started
126 a second time.
127 */
128 if ((fp_ou=MY_FOPEN(output_file,(tt>=min_tt && tt<=max_tt) ? "a" : "w"))==NULL) {
129 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),output_file,strerror(errno));
130 exit(EXIT_FAILURE);
131 }
132 if (tt<min_tt) min_tt=tt;
133 if (tt>max_tt) max_tt=tt;
134 }
135
136 if (!convert) {
137 fprintf(fp_ou,"%s %s\n",data,gwarea.current);
138 } else {
139 if (df=='e')
140 strftime(dia, sizeof(dia), "%d/%m/%Y", t);
141 else
142 strftime(dia, sizeof(dia), "%m/%d/%Y", t);
143
144 fprintf(fp_ou,"%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current);
145 }
146 }
147
148 longline_destroy(&line);
149 if (FileObject_Close(fp_in)) {
150 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,FileObject_GetLastCloseError());
151 exit(EXIT_FAILURE);
152 }
153 if (autosplit && fp_ou) {
154 if (fclose(fp_ou)==EOF) {
155 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),output_file,strerror(errno));
156 exit(EXIT_FAILURE);
157 }
158 }
159 }