]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/debug.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / debug.cc
index bbd9da8c6dadb29bc19be091bc682eab2e825633..a2eb7d2c4f3794d9102da41f1a790dbdae193a5f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $Id$
  *
- * DEBUG: section 0     Debug Routines
+ * DEBUG: section 00    Debug Routines
  * AUTHOR: Harvest Derived
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
  *
  */
 
-#include "config.h"
+#include "squid.h"
 #include "Debug.h"
 #include "SquidTime.h"
+#include "util.h"
+#include "ipc/Kids.h"
 
-/* for Config */
-#include "structs.h"
+/* for shutting_down flag in xassert() */
+#include "globals.h"
 
+char *Debug::debugOptions = NULL;
+int Debug::override_X = 0;
+int Debug::log_stderr = -1;
+bool Debug::log_syslog = false;
 int Debug::Levels[MAX_DEBUG_SECTIONS];
 int Debug::level;
+char *Debug::cache_log = NULL;
+int Debug::rotateNumber = -1;
 FILE *debug_log = NULL;
 static char *debug_log_file = NULL;
 static int Ctx_Lock = 0;
 static const char *debugLogTime(void);
+static const char *debugLogKid(void);
 static void ctx_print(void);
 #if HAVE_SYSLOG
 #ifdef LOG_LOCAL4
@@ -55,7 +64,7 @@ static void _db_print_syslog(const char *format, va_list args);
 static void _db_print_stderr(const char *format, va_list args);
 static void _db_print_file(const char *format, va_list args);
 
-#ifdef _SQUID_MSWIN_
+#if _SQUID_MSWIN_
 SQUIDCEXTERN LPCRITICAL_SECTION dbg_mutex;
 typedef BOOL (WINAPI * PFInitializeCriticalSectionAndSpinCount) (LPCRITICAL_SECTION, DWORD);
 #endif
@@ -63,12 +72,13 @@ typedef BOOL (WINAPI * PFInitializeCriticalSectionAndSpinCount) (LPCRITICAL_SECT
 void
 _db_print(const char *format,...)
 {
-    LOCAL_ARRAY(char, f, BUFSIZ);
+    char f[BUFSIZ];
+    f[0]='\0';
     va_list args1;
     va_list args2;
     va_list args3;
 
-#ifdef _SQUID_MSWIN_
+#if _SQUID_MSWIN_
     /* Multiple WIN32 threads may call this simultaneously */
 
     if (!dbg_mutex) {
@@ -109,8 +119,9 @@ _db_print(const char *format,...)
     va_start(args2, format);
     va_start(args3, format);
 
-    snprintf(f, BUFSIZ, "%s| %s",
+    snprintf(f, BUFSIZ, "%s%s| %s",
              debugLogTime(),
+             debugLogKid(),
              format);
 
     _db_print_file(f, args1);
@@ -120,7 +131,7 @@ _db_print(const char *format,...)
     _db_print_syslog(format, args3);
 #endif
 
-#ifdef _SQUID_MSWIN_
+#if _SQUID_MSWIN_
     LeaveCriticalSection(dbg_mutex);
 #endif
 
@@ -130,7 +141,8 @@ _db_print(const char *format,...)
 }
 
 static void
-_db_print_file(const char *format, va_list args) {
+_db_print_file(const char *format, va_list args)
+{
     if (debug_log == NULL)
         return;
 
@@ -139,14 +151,13 @@ _db_print_file(const char *format, va_list args) {
         ctx_print();
 
     vfprintf(debug_log, format, args);
-
-//*AYJ:*/    if (!Config.onoff.buffered_logs)
     fflush(debug_log);
 }
 
 static void
-_db_print_stderr(const char *format, va_list args) {
-    if (opt_debug_stderr < Debug::level)
+_db_print_stderr(const char *format, va_list args)
+{
+    if (Debug::log_stderr < Debug::level)
         return;
 
     if (debug_log == stderr)
@@ -157,16 +168,17 @@ _db_print_stderr(const char *format, va_list args) {
 
 #if HAVE_SYSLOG
 static void
-_db_print_syslog(const char *format, va_list args) {
-    LOCAL_ARRAY(char, tmpbuf, BUFSIZ);
+_db_print_syslog(const char *format, va_list args)
+{
     /* level 0,1 go to syslog */
 
     if (Debug::level > 1)
         return;
 
-    if (0 == opt_syslog_enable)
+    if (!Debug::log_syslog)
         return;
 
+    char tmpbuf[BUFSIZ];
     tmpbuf[0] = '\0';
 
     vsnprintf(tmpbuf, BUFSIZ, format, args);
@@ -175,21 +187,24 @@ _db_print_syslog(const char *format, va_list args) {
 
     syslog(Debug::level == 0 ? LOG_WARNING : LOG_NOTICE, "%s", tmpbuf);
 }
-
 #endif /* HAVE_SYSLOG */
 
 static void
-debugArg(const char *arg) {
+debugArg(const char *arg)
+{
     int s = 0;
     int l = 0;
     int i;
 
-    if (!strncasecmp(arg, "ALL", 3)) {
+    if (!strncasecmp(arg, "rotate=", 7)) {
+        arg += 7;
+        Debug::rotateNumber = atoi(arg);
+        return;
+    } else if (!strncasecmp(arg, "ALL", 3)) {
         s = -1;
         arg += 4;
     } else {
         s = atoi(arg);
-
         while (*arg && *arg++ != ',');
     }
 
@@ -215,7 +230,8 @@ debugArg(const char *arg) {
 }
 
 static void
-debugOpenLog(const char *logfile) {
+debugOpenLog(const char *logfile)
+{
     if (logfile == NULL) {
         debug_log = stderr;
         return;
@@ -239,9 +255,8 @@ debugOpenLog(const char *logfile) {
         debug_log = stderr;
     }
 
-#ifdef _SQUID_WIN32_
+#if _SQUID_WINDOWS_
     setmode(fileno(debug_log), O_TEXT);
-
 #endif
 }
 
@@ -363,8 +378,10 @@ syslog_facility_names[] = {
 #endif
 
 void
-_db_set_syslog(const char *facility) {
-    opt_syslog_enable = 1;
+_db_set_syslog(const char *facility)
+{
+    Debug::log_syslog = true;
+
 #ifdef LOG_LOCAL4
 #ifdef LOG_DAEMON
 
@@ -372,7 +389,7 @@ _db_set_syslog(const char *facility) {
 #else
 
     syslog_facility = LOG_LOCAL4;
-#endif
+#endif /* LOG_DAEMON */
 
     if (facility) {
 
@@ -393,18 +410,19 @@ _db_set_syslog(const char *facility) {
     if (facility)
         fprintf(stderr, "syslog facility type not supported on your system\n");
 
-#endif
+#endif /* LOG_LOCAL4 */
 }
 
 #endif
 
 void
-Debug::parseOptions(char const *options) {
+Debug::parseOptions(char const *options)
+{
     int i;
     char *p = NULL;
     char *s = NULL;
 
-    if (Config.onoff.debug_override_X) {
+    if (override_X) {
         debugs(0, 9, "command-line -X overrides: " << options);
         return;
     }
@@ -423,58 +441,56 @@ Debug::parseOptions(char const *options) {
 }
 
 void
-_db_init(const char *logfile, const char *options) {
+_db_init(const char *logfile, const char *options)
+{
     Debug::parseOptions(options);
 
     debugOpenLog(logfile);
 
 #if HAVE_SYSLOG && defined(LOG_LOCAL4)
 
-    if (opt_syslog_enable)
+    if (Debug::log_syslog)
         openlog(APP_SHORTNAME, LOG_PID | LOG_NDELAY | LOG_CONS, syslog_facility);
 
 #endif /* HAVE_SYSLOG */
 
+    /* Pre-Init TZ env, see bug #2656 */
+    tzset();
 }
 
 void
-_db_rotate_log(void) {
-    int i;
-    LOCAL_ARRAY(char, from, MAXPATHLEN);
-    LOCAL_ARRAY(char, to, MAXPATHLEN);
-#ifdef S_ISREG
-
-    struct stat sb;
-#endif
-
+_db_rotate_log(void)
+{
     if (debug_log_file == NULL)
         return;
 
 #ifdef S_ISREG
-
+    struct stat sb;
     if (stat(debug_log_file, &sb) == 0)
         if (S_ISREG(sb.st_mode) == 0)
             return;
-
 #endif
 
+    char from[MAXPATHLEN];
+    from[0] = '\0';
+
+    char to[MAXPATHLEN];
+    to[0] = '\0';
+
     /*
      * NOTE: we cannot use xrename here without having it in a
      * separate file -- tools.c has too many dependencies to be
      * used everywhere debug.c is used.
      */
     /* Rotate numbers 0 through N up one */
-    for (i = Config.Log.rotateNumber; i > 1;) {
+    for (int i = Debug::rotateNumber; i > 1;) {
         i--;
         snprintf(from, MAXPATHLEN, "%s.%d", debug_log_file, i - 1);
         snprintf(to, MAXPATHLEN, "%s.%d", debug_log_file, i);
-#ifdef _SQUID_MSWIN_
-
+#if _SQUID_MSWIN_
         remove
         (to);
-
 #endif
-
         rename(from, to);
     }
 
@@ -482,32 +498,29 @@ _db_rotate_log(void) {
      * You can't rename open files on Microsoft "operating systems"
      * so we close before renaming.
      */
-#ifdef _SQUID_MSWIN_
+#if _SQUID_MSWIN_
     if (debug_log != stderr)
         fclose(debug_log);
-
 #endif
     /* Rotate the current log to .0 */
-    if (Config.Log.rotateNumber > 0) {
+    if (Debug::rotateNumber > 0) {
         snprintf(to, MAXPATHLEN, "%s.%d", debug_log_file, 0);
-#ifdef _SQUID_MSWIN_
-
+#if _SQUID_MSWIN_
         remove
         (to);
-
 #endif
-
         rename(debug_log_file, to);
     }
 
     /* Close and reopen the log.  It may have been renamed "manually"
      * before HUP'ing us. */
     if (debug_log != stderr)
-        debugOpenLog(Config.Log.log);
+        debugOpenLog(Debug::cache_log);
 }
 
 static const char *
-debugLogTime(void) {
+debugLogTime(void)
+{
 
     time_t t = getCurrentTime();
 
@@ -532,8 +545,22 @@ debugLogTime(void) {
     return buf;
 }
 
+static const char *
+debugLogKid(void)
+{
+    if (KidIdentifier != 0) {
+        static char buf[16];
+        if (!*buf) // optimization: fill only once after KidIdentifier is set
+            snprintf(buf, sizeof(buf), " kid%d", KidIdentifier);
+        return buf;
+    }
+
+    return "";
+}
+
 void
-xassert(const char *msg, const char *file, int line) {
+xassert(const char *msg, const char *file, int line)
+{
     debugs(0, 0, "assertion failed: " << file << ":" << line << ": \"" << msg << "\"");
 
     if (!shutting_down)
@@ -635,7 +662,8 @@ static const char *ctx_get_descr(Ctx ctx);
 
 
 Ctx
-ctx_enter(const char *descr) {
+ctx_enter(const char *descr)
+{
     Ctx_Current_Level++;
 
     if (Ctx_Current_Level <= CTX_MAX_LEVEL)
@@ -650,7 +678,8 @@ ctx_enter(const char *descr) {
 }
 
 void
-ctx_exit(Ctx ctx) {
+ctx_exit(Ctx ctx)
+{
     assert(ctx >= 0);
     Ctx_Current_Level = (ctx >= 0) ? ctx - 1 : -1;
 
@@ -663,7 +692,8 @@ ctx_exit(Ctx ctx) {
  * info for deducing the current execution stack
  */
 static void
-ctx_print(void) {
+ctx_print(void)
+{
     /* lock so _db_print will not call us recursively */
     Ctx_Lock++;
     /* ok, user saw [0,Ctx_Reported_Level] descriptions */
@@ -693,7 +723,8 @@ ctx_print(void) {
 
 /* checks for nulls and overflows */
 static const char *
-ctx_get_descr(Ctx ctx) {
+ctx_get_descr(Ctx ctx)
+{
     if (ctx < 0 || ctx > CTX_MAX_LEVEL)
         return "<lost>";
 
@@ -703,7 +734,8 @@ ctx_get_descr(Ctx ctx) {
 int Debug::TheDepth = 0;
 
 std::ostream &
-Debug::getDebugOut() {
+Debug::getDebugOut()
+{
     assert(TheDepth >= 0);
     ++TheDepth;
     if (TheDepth > 1) {
@@ -720,7 +752,8 @@ Debug::getDebugOut() {
 }
 
 void
-Debug::finishDebug() {
+Debug::finishDebug()
+{
     assert(TheDepth >= 0);
     assert(CurrentDebug);
     if (TheDepth > 1) {
@@ -737,7 +770,8 @@ Debug::finishDebug() {
 // Hack: replaces global ::xassert() to debug debugging assertions
 // Relies on assert macro calling xassert() without a specific scope.
 void
-Debug::xassert(const char *msg, const char *file, int line) {
+Debug::xassert(const char *msg, const char *file, int line)
+{
 
     if (CurrentDebug) {
         *CurrentDebug << "assertion failed: " << file << ":" << line <<
@@ -748,19 +782,18 @@ Debug::xassert(const char *msg, const char *file, int line) {
 
 std::ostringstream (*Debug::CurrentDebug)(NULL);
 
-const size_t
+size_t
 BuildPrefixInit()
 {
-
-    /* Anyone moving the src/Debug.cc file MUST record its new location here,
-     * or there will be issues in the debugging statements, possibly fatal!
-     */
-
+    // XXX: This must be kept in sync with the actual debug.cc location
     const char *ThisFileNameTail = "src/debug.cc";
 
-    //leave immediately if misconfigured. Unfortunately not possible to detect at build
     const char *file=__FILE__;
-    assert(strstr(file,ThisFileNameTail)!=NULL);
+
+    // Disable heuristic if it does not work.
+    if (!strstr(file, ThisFileNameTail))
+        return 0;
+
     return strlen(file)-strlen(ThisFileNameTail);
 }