]> git.ipfire.org Git - thirdparty/sarg.git/blame - splitlog.c
Work around the lack of %lld in msvcrt
[thirdparty/sarg.git] / splitlog.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
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"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
9b179eb0 30void splitlog(const char *arq, char *df, int dfrom, int duntil, int convert)
25697a35 31{
25697a35 32 FILE *fp_in;
9b179eb0 33 char *buf;
25697a35
GS
34 char data[30];
35 char dia[11];
25697a35
GS
36 time_t tt;
37 int idata=0;
38 struct tm *t;
9c7c6346 39 struct getwordstruct gwarea;
9b179eb0 40 longline line;
25697a35
GS
41
42 if(arq[0] == '\0')
a96e796d 43 arq="/var/log/squid/access.log";
25697a35 44
936c9905 45 if((fp_in=MY_FOPEN(arq,"r"))==NULL) {
9b179eb0
FM
46 debuga(_("(splitlog) Cannot open log file %s - %s\n"),arq,strerror(errno));
47 exit(EXIT_FAILURE);
48 }
49
50 if ((line=longline_create())==NULL) {
51 debuga(_("Not enough memory to read the log file %s\n"),arq);
06b39c87 52 exit(EXIT_FAILURE);
25697a35
GS
53 }
54
9b179eb0 55 while((buf=longline_read(fp_in,line))!=NULL) {
9c7c6346
FM
56 getword_start(&gwarea,buf);
57 if (getword(data,sizeof(data),&gwarea,' ')<0) {
9b179eb0 58 debuga(_("Invalid date found in file %s\n"),arq);
06b39c87 59 exit(EXIT_FAILURE);
4bcb77cf 60 }
25697a35
GS
61 tt=atoi(data);
62 t=localtime(&tt);
63
64 if(dfrom) {
9b179eb0 65 idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday;
120d768c
FM
66 if(idata < dfrom || idata > duntil)
67 continue;
25697a35
GS
68 }
69
9b179eb0
FM
70 if(!convert) {
71 printf("%s %s\n",data,gwarea.current);
72 } else {
73 if(df[0]=='e')
74 strftime(dia, sizeof(dia), "%d/%m/%Y", t);
75 else
76 strftime(dia, sizeof(dia), "%m/%d/%Y", t);
25697a35 77
9b179eb0
FM
78 printf("%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current);
79 }
25697a35
GS
80 }
81
9b179eb0
FM
82 longline_destroy(&line);
83 if (fclose(fp_in)==EOF) {
84 debuga(_("Failed to close file %s - %s\n"),arq,strerror(errno));
85 }
25697a35 86}