Good day!

I have patched Squid-2.5.STABLE10 to write logs into pipe.
Write in config file:
cache_access_log |<some script>
and access log would be catched with script.
During logrotate pipe is reopened.

I think this is useful for realtime log analize.

Patch included.

Andrey Chichak.
diff -ru squid-2.5.STABLE10/src/logfile.c squid-2.5.STABLE10-pl1/src/logfile.c
--- squid-2.5.STABLE10/src/logfile.c	Tue Jan 21 00:57:50 2003
+++ squid-2.5.STABLE10-pl1/src/logfile.c	Mon Jul 11 18:11:02 2005
@@ -20,12 +20,12 @@
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
- *  
+ *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
- *  
+ *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
@@ -41,23 +41,34 @@
 {
     int fd;
     Logfile *lf;
-    fd = file_open(path, O_WRONLY | O_CREAT | O_TEXT);
-    if (DISK_ERROR == fd) {
-	if (ENOENT == errno && fatal_flag) {
-	    fatalf("Cannot open '%s' because\n"
+    const char *args[2];
+    lf = xcalloc(1, sizeof(*lf));
+    if (path[0] != '|') {
+	fd = file_open(path, O_WRONLY | O_CREAT | O_TEXT);
+	if (DISK_ERROR == fd) {
+	    if (ENOENT == errno && fatal_flag) {
+		fatalf("Cannot open '%s' because\n"
 		"\tthe parent directory does not exist.\n"
 		"\tPlease create the directory.\n", path);
-	} else if (EACCES == errno && fatal_flag) {
-	    fatalf("Cannot open '%s' for writing.\n"
+	    } else if (EACCES == errno && fatal_flag) {
+		fatalf("Cannot open '%s' for writing.\n"
 		"\tThe parent directory must be writeable by the\n"
 		"\tuser '%s', which is the cache_effective_user\n"
 		"\tset in squid.conf.", path, Config.effectiveUser);
-	} else {
-	    debug(50, 1) ("logfileOpen: %s: %s\n", path, xstrerror());
-	    return NULL;
+	    } else {
+		debug(50, 1) ("logfileOpen: %s: %s\n", path, xstrerror());
+		return NULL;
+	    }
+	}
+	lf->flags.isipc = 0;
+    } else {
+	args[0] = "(logger)";
+	args[1] = NULL;
+	if ( ipcCreate(IPC_FIFO,&path[1],args,NULL,NULL,&fd) <= 0 ) {
+	    fatalf("Cannot start log listener %s :s", path, xstrerror());
 	}
+	lf->flags.isipc = 1;
     }
-    lf = xcalloc(1, sizeof(*lf));
     lf->fd = fd;
     if (fatal_flag)
 	lf->flags.fatal = 1;
@@ -88,32 +99,44 @@
     int i;
     char from[MAXPATHLEN];
     char to[MAXPATHLEN];
-    assert(lf->path);
+
+    const char *args[2];
+
+    if (lf->flags.isipc) {
+	file_close(lf->fd);
+	args[0] = "(logger)";
+	args[1] = NULL;
+	if ( ipcCreate(IPC_FIFO,&(lf->path[1]),args,NULL,NULL,&(lf->fd)) <= 0 ) {
+	    fatalf("Cannot start log listener %s :s", lf->path, xstrerror());
+	}
+    } else {
+        assert(lf->path);
 #ifdef S_ISREG
-    if (stat(lf->path, &sb) == 0)
-	if (S_ISREG(sb.st_mode) == 0)
-	    return;
+	if (stat(lf->path, &sb) == 0)
+	    if (S_ISREG(sb.st_mode) == 0)
+		return;
 #endif
-    debug(0, 1) ("logfileRotate: %s\n", lf->path);
-    /* Rotate numbers 0 through N up one */
-    for (i = Config.Log.rotateNumber; i > 1;) {
-	i--;
-	snprintf(from, MAXPATHLEN, "%s.%d", lf->path, i - 1);
-	snprintf(to, MAXPATHLEN, "%s.%d", lf->path, i);
-	xrename(from, to);
-    }
-    /* Rotate the current log to .0 */
-    logfileFlush(lf);
-    file_close(lf->fd);		/* always close */
-    if (Config.Log.rotateNumber > 0) {
-	snprintf(to, MAXPATHLEN, "%s.%d", lf->path, 0);
-	xrename(lf->path, to);
-    }
-    /* Reopen the log.  It may have been renamed "manually" */
-    lf->fd = file_open(lf->path, O_WRONLY | O_CREAT | O_TEXT);
-    if (DISK_ERROR == lf->fd && lf->flags.fatal) {
-	debug(50, 1) ("logfileRotate: %s: %s\n", lf->path, xstrerror());
-	fatalf("Cannot open %s: %s", lf->path, xstrerror());
+	debug(0, 1) ("logfileRotate: %s\n", lf->path);
+	/* Rotate numbers 0 through N up one */
+	for (i = Config.Log.rotateNumber; i > 1;) {
+	    i--;
+	    snprintf(from, MAXPATHLEN, "%s.%d", lf->path, i - 1);
+	    snprintf(to, MAXPATHLEN, "%s.%d", lf->path, i);
+	    xrename(from, to);
+	}
+	/* Rotate the current log to .0 */
+	logfileFlush(lf);
+	file_close(lf->fd);		/* always close */
+	if (Config.Log.rotateNumber > 0) {
+	    snprintf(to, MAXPATHLEN, "%s.%d", lf->path, 0);
+	    xrename(lf->path, to);
+	}
+	/* Reopen the log.  It may have been renamed "manually" */
+	lf->fd = file_open(lf->path, O_WRONLY | O_CREAT | O_TEXT);
+	if (DISK_ERROR == lf->fd && lf->flags.fatal) {
+	    debug(50, 1) ("logfileRotate: %s: %s\n", lf->path, xstrerror());
+	    fatalf("Cannot open %s: %s", lf->path, xstrerror());
+	}
     }
 }
 
diff -ru squid-2.5.STABLE10/src/structs.h squid-2.5.STABLE10-pl1/src/structs.h
--- squid-2.5.STABLE10/src/structs.h	Thu May  5 01:03:47 2005
+++ squid-2.5.STABLE10-pl1/src/structs.h	Mon Jul 11 18:12:56 2005
@@ -2201,6 +2201,7 @@
     ssize_t offset;
     struct {
 	unsigned int fatal:1;
+	unsigned int isipc:1;
     } flags;
 };
 

Reply via email to