--------------------- PatchSet 4549 Date: 2007/05/15 13:10:33 Author: adri Branch: squid3_logdaemon Tag: (none) Log: * remember that String is the Squid string thingy, not std::string; so be specific * flesh out a very basic Blocking logfile writer. Members: src/LogFile.cc:1.1.2.1->1.1.2.2 src/LogFile.h:1.1.2.1->1.1.2.2 src/LogFileBlocking.cc:1.1.2.1->1.1.2.2 src/LogFileBlocking.h:1.1.2.1->1.1.2.2 src/LogFileSyslog.cc:1.1.2.1->1.1.2.2 src/LogFileSyslog.h:1.1.2.1->1.1.2.2 Index: squid3/src/LogFile.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/LogFile.cc,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/LogFile.cc 15 May 2007 12:22:45 -0000 1.1.2.1 +++ squid3/src/LogFile.cc 15 May 2007 13:10:33 -0000 1.1.2.2 @@ -1,5 +1,5 @@ /* - * $Id: LogFile.cc,v 1.1.2.1 2007/05/15 12:22:45 adri Exp $ + * $Id: LogFile.cc,v 1.1.2.2 2007/05/15 13:10:33 adri Exp $ * * DEBUG: section 50 Log file handling * AUTHOR: Adrian Chadd @@ -59,19 +59,34 @@ void LogFile::BeginLine(void) { - fatal("LogFile::BeginLine: called, but I should be overridden!\n"); + logline.clear(); } void -LogFile::AppendLine(const char *buf) +LogFile::AppendLine(const char *buf, int len) { - fatal("LogFile::AppendLine: called, but I should be overridden!\n"); + logline.append(buf); } void LogFile::PrintLine(const char *fmt, ...) { - fatal("LogFile::PrintLine: called, but I should be overridden!\n"); + va_list args; + char buf[8192]; + int s; + va_start(args, fmt); + + s = vsnprintf(buf, 8192, fmt, args); + + if (s > 8192) { + s = 8192; + + if (fmt[strlen(fmt) - 1] == '\n') + buf[8191] = '\n'; + } + + AppendLine(buf, s); + va_end(args); } void Index: squid3/src/LogFile.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/LogFile.h,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/LogFile.h 15 May 2007 12:22:54 -0000 1.1.2.1 +++ squid3/src/LogFile.h 15 May 2007 13:10:33 -0000 1.1.2.2 @@ -34,7 +34,7 @@ /* begin line */ virtual void BeginLine(); /* write to a line */ - virtual void AppendLine(const char *buf); + virtual void AppendLine(const char *buf, int len); /* printf */ virtual void PrintLine(const char *fmt, ...); /* end of the current line */ @@ -42,9 +42,8 @@ /* stream? */ - private: - String logpath; - + std::string logline; + bool do_fatal; }; }; Index: squid3/src/LogFileBlocking.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/LogFileBlocking.cc,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/LogFileBlocking.cc 15 May 2007 12:22:52 -0000 1.1.2.1 +++ squid3/src/LogFileBlocking.cc 15 May 2007 13:10:33 -0000 1.1.2.2 @@ -1,5 +1,5 @@ /* - * $Id: LogFileBlocking.cc,v 1.1.2.1 2007/05/15 12:22:52 adri Exp $ + * $Id: LogFileBlocking.cc,v 1.1.2.2 2007/05/15 13:10:33 adri Exp $ * * DEBUG: section 50 Log file handling * AUTHOR: Adrian Chadd @@ -24,61 +24,102 @@ LogFileBlocking::LogFileBlocking(void) { - fatal("LogFileBlocking::LogFileBlocking: called, but I should be overridden!\n"); + fp = NULL; } LogFileBlocking::~LogFileBlocking(void) { - fatal("LogFileBlocking::~LogFileBlocking: called, but I should be overridden!\n"); + // We should've already closed the logfile! + assert(fp == NULL); + logpath.clear(); } bool LogFileBlocking::Open(const char *path, int buf_sz, bool fatal_flag) { - fatal("LogFileBlocking::Open: called, but I should be overridden!\n"); - return false; + logpath.clear(); + logpath = path; + + do_fatal = fatal_flag; + + fp = fopen(logpath.c_str(), "a"); + + if (fp == NULL && do_fatal) { + debugs(50, 1, "logfileBlocking::Open: " << logpath << ": " << xstrerror()); + fatalf("Cannot open %s: %s", logpath.c_str(), xstrerror()); + } else { + debugs(50, 1, "logfileBlocking::Open: trying to open " << logpath << "; error " << xstrerror()); + return false; + } + return true; } void LogFileBlocking::Close(void) { - fatal("LogFileBlocking::Close: called, but I should be overridden!\n"); + debugs(50, 1, "LogFileBlocking::Close: called, closing " << logpath); + Flush(); + fclose(fp); fp = NULL; } void LogFileBlocking::Rotate(void) { - fatal("LogFileBlocking::Rotate: called, but I should be overridden!\n"); +#ifdef S_ISREG + struct stat sb; +#endif + + int i; + char from[MAXPATHLEN]; + char to[MAXPATHLEN]; + assert(fp != NULL); + +#ifdef S_ISREG + if (stat(logpath.c_str(), &sb) == 0) + if (S_ISREG(sb.st_mode) == 0) + return; + +#endif + + debugs(0, 1, "LogFileBlocking::Rotate: " << logpath); + + /* Rotate numbers 0 through N up one */ + for (i = Config.Log.rotateNumber; i > 1;) { + i--; + snprintf(from, MAXPATHLEN, "%s.%d", logpath.c_str(), i - 1); + snprintf(to, MAXPATHLEN, "%s.%d", logpath.c_str(), i); + xrename(from, to); + } + + /* Rotate the current log to .0 */ + Close(); + + if (Config.Log.rotateNumber > 0) { + snprintf(to, MAXPATHLEN, "%s.%d", logpath.c_str(), 0); + xrename(logpath.c_str(), to); + } + + /* Reopen the log. It may have been renamed "manually" */ + fp = fopen(logpath.c_str(), "a"); + + if (fp == NULL && do_fatal) { + debugs(50, 1, "logfileRotate: " << logpath << ": " << xstrerror()); + fatalf("Cannot open %s: %s", logpath.c_str(), xstrerror()); + } } void LogFileBlocking::Flush(void) { - fatal("LogFileBlocking::Flush: called, but I should be overridden!\n"); -} - -void -LogFileBlocking::BeginLine(void) -{ - fatal("LogFileBlocking::BeginLine: called, but I should be overridden!\n"); -} - -void -LogFileBlocking::AppendLine(const char *buf) -{ - fatal("LogFileBlocking::AppendLine: called, but I should be overridden!\n"); -} - -void -LogFileBlocking::PrintLine(const char *fmt, ...) -{ - fatal("LogFileBlocking::PrintLine: called, but I should be overridden!\n"); + assert(fp != NULL); + fflush(fp); } void LogFileBlocking::EndLine(void) { - fatal("LogFileBlocking::EndLine: called, but I should be overridden!\n"); + assert(fp != NULL); + (void) fprintf(fp, "%s", logline.c_str()); } } Index: squid3/src/LogFileBlocking.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/LogFileBlocking.h,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/LogFileBlocking.h 15 May 2007 12:22:54 -0000 1.1.2.1 +++ squid3/src/LogFileBlocking.h 15 May 2007 13:10:33 -0000 1.1.2.2 @@ -31,20 +31,13 @@ /* flush */ virtual void Flush(); - /* begin line */ - virtual void BeginLine(); - /* write to a line */ - virtual void AppendLine(const char *buf); - /* printf */ - virtual void PrintLine(const char *fmt, ...); - /* end of the current line */ virtual void EndLine(); /* stream? */ private: - String logpath; - + std::string logpath; + FILE *fp; }; }; Index: squid3/src/LogFileSyslog.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/LogFileSyslog.cc,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/LogFileSyslog.cc 15 May 2007 12:22:53 -0000 1.1.2.1 +++ squid3/src/LogFileSyslog.cc 15 May 2007 13:10:33 -0000 1.1.2.2 @@ -1,5 +1,5 @@ /* - * $Id: LogFileSyslog.cc,v 1.1.2.1 2007/05/15 12:22:53 adri Exp $ + * $Id: LogFileSyslog.cc,v 1.1.2.2 2007/05/15 13:10:33 adri Exp $ * * DEBUG: section 50 Log file handling * AUTHOR: Adrian Chadd @@ -64,7 +64,7 @@ } void -LogFileSyslog::AppendLine(const char *buf) +LogFileSyslog::AppendLine(const char *buf, int len) { fatal("LogFileSyslog::AppendLine: called, but I should be overridden!\n"); } Index: squid3/src/LogFileSyslog.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/LogFileSyslog.h,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/LogFileSyslog.h 15 May 2007 12:22:54 -0000 1.1.2.1 +++ squid3/src/LogFileSyslog.h 15 May 2007 13:10:33 -0000 1.1.2.2 @@ -34,7 +34,7 @@ /* begin line */ virtual void BeginLine(); /* write to a line */ - virtual void AppendLine(const char *buf); + virtual void AppendLine(const char *buf, int line); /* printf */ virtual void PrintLine(const char *fmt, ...); /* end of the current line */