]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - convlog.c
Strip suffix length is a global variable
[thirdparty/sarg.git] / convlog.c
index 6fefe3844a92179ed6b75b93bb5fc371fdbf02c2..d87efee88fc95d3189c001b7a4c7c0cd573477c8 100644 (file)
--- a/convlog.c
+++ b/convlog.c
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2008
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
  */
 
 #include "include/conf.h"
+#include "include/defs.h"
 
-void convlog(char *arq, char *df, int dfrom, int duntil)
+void convlog(const char *arq, char df, int dfrom, int duntil)
 {
+       FileObject *fp_in;
+       char *buf;
+       char data[30];
+       char dia[11];
+       time_t tt;
+       int idata=0;
+       struct tm *t;
+       struct getwordstruct gwarea;
+       longline line;
 
-   FILE *fp_in;
-   char buf[MAXLEN];
-   char data[30];
-   char dia[11];
-   char hora[9];
-   char wdata[20];
-   time_t tt;
-   int idata=0;
-   struct tm *t;
+       if(arq[0] == '\0')
+               arq="/var/log/squid/access.log";
 
-   if(arq[0] == '\0')
-      strcpy(arq,"/usr/local/squid/logs/access.log");
+       if((fp_in=FileObject_Open(arq))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,FileObject_GetLastOpenError());
+               exit(EXIT_FAILURE);
+       }
 
-   if((fp_in=fopen(arq,"r"))==NULL) {
-      fprintf(stderr, "SARG: (convlog) %s: %s\n",text[8],arq);
-      exit(1);
-   }
+       if ((line=longline_create())==NULL) {
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),arq);
+               exit(EXIT_FAILURE);
+       }
 
-   while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-      if (getword(data,sizeof(data),buf,' ')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-         exit(1);
-      }
-      tt=atoi(data);
-      t=localtime(&tt);
+       while((buf=longline_read(fp_in,line))!=NULL) {
+               getword_start(&gwarea,buf);
+               if (getword(data,sizeof(data),&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),arq);
+                       exit(EXIT_FAILURE);
+               }
+               tt=atoi(data);
+               t=localtime(&tt);
 
-      if(dfrom) {
-         strftime(wdata, 127, "%Y%m%d", t);
-         idata=atoi(wdata);
-        if(idata < dfrom || idata > duntil)
-         continue;
-      }
+               if(dfrom) {
+                       idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday;
+                       if(idata < dfrom || idata > duntil)
+                               continue;
+               }
 
-      if(strncmp(df,"e",1) == 0)
-         strftime(dia, 127, "%d/%m/%Y", t);
-       else
-         strftime(dia, 127, "%m/%d/%Y", t);
-      sprintf(hora,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
+               if (df=='e')
+                       strftime(dia, sizeof(dia), "%d/%m/%Y", t);
+               else if (df=='u')
+                       strftime(dia, sizeof(dia), "%m/%d/%Y", t);
+               else //if (df=='w')
+                       strftime(dia, sizeof(dia), "%Y.%U", t);
 
-      printf("%s %s %s",dia,hora,buf);
-   }
+               printf("%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current);
+       }
 
-   fclose(fp_in);
+       longline_destroy(&line);
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
 }