handling replies continuously from server
[oweals/gnunet.git] / src / util / common_logging.c
index ded410e3d69eca8f44f11c03a5e0fb70f30a2900..2c0fd57a817845ca4313d650f36544cc5fc9798e 100644 (file)
@@ -268,9 +268,9 @@ void
 GNUNET_abort ()
 {
 #if WINDOWS
-  DebugBreak();
+  DebugBreak ();
 #endif
-  abort();
+  abort ();
 }
 
 
@@ -292,6 +292,7 @@ add_definition (char *component, char *file, char *function, int from_line,
 {
   struct LogDef n;
   int r;
+
   if (logdefs_size == logdefs_len)
     resize_logdefs ();
   memset (&n, 0, sizeof (n));
@@ -299,27 +300,26 @@ add_definition (char *component, char *file, char *function, int from_line,
     component = (char *) ".*";
   r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
   if (r != 0)
-    {
-      return r;
-    }
+  {
+    return r;
+  }
   if (strlen (file) == 0)
     file = (char *) ".*";
   r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB);
   if (r != 0)
-    {
-      regfree (&n.component_regex);
-      return r;
-    }
-  if ( (NULL == function) ||
-       (strlen (function) == 0))
+  {
+    regfree (&n.component_regex);
+    return r;
+  }
+  if ((NULL == function) || (strlen (function) == 0))
     function = (char *) ".*";
   r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
   if (r != 0)
-    {
-      regfree (&n.component_regex);
-      regfree (&n.file_regex);
-      return r;
-    }
+  {
+    regfree (&n.component_regex);
+    regfree (&n.file_regex);
+    return r;
+  }
   n.from_line = from_line;
   n.to_line = to_line;
   n.level = level;
@@ -364,16 +364,16 @@ GNUNET_get_log_call_status (int caller_level, const char *comp,
   force_only = min_level >= 0;
   for (i = 0; i < logdefs_len; i++)
   {
-      ld = &logdefs[i];
-      if ((!force_only || ld->force) &&
-         (line >= ld->from_line && line <= ld->to_line) &&
-         (regexec (&ld->component_regex, comp, 0, NULL, 0) == 0) &&
-         (regexec (&ld->file_regex, file, 0, NULL, 0) == 0) &&
-         (regexec (&ld->function_regex, function, 0, NULL, 0) == 0))
-       {
-         /* We're finished */
-         return caller_level <= ld->level;
-       }
+    ld = &logdefs[i];
+    if ((!force_only || ld->force) &&
+        (line >= ld->from_line && line <= ld->to_line) &&
+        (regexec (&ld->component_regex, comp, 0, NULL, 0) == 0) &&
+        (regexec (&ld->file_regex, file, 0, NULL, 0) == 0) &&
+        (regexec (&ld->function_regex, function, 0, NULL, 0) == 0))
+    {
+      /* We're finished */
+      return caller_level <= ld->level;
+    }
   }
   /* No matches - use global level, if defined */
   if (min_level >= 0)
@@ -429,90 +429,89 @@ parse_definitions (const char *constname, int force)
   if (tmp == NULL)
     return 0;
   def = GNUNET_strdup (tmp);
-  level = -1;
   from_line = 0;
   to_line = INT_MAX;
   for (p = def, state = 0, start = def; keep_looking; p++)
   {
-      switch (p[0])
-       {
-       case ';':               /* found a field separator */
-         p[0] = '\0';
-         switch (state)
-           {
-           case 0:             /* within a component name */
-             comp = start;
-             break;
-           case 1:             /* within a file name */
-             file = start;
-             break;
-           case 2:             /* within a function name */
-             /* after a file name there must be a function name */
-             function = start;
-             break;
-           case 3:             /* within a from-to line range */
-             if (strlen (start) > 0)
-               {
-                 errno = 0;
-                 from_line = strtol (start, &t, 10);
-                 if (errno != 0 || from_line < 0)
-                   {
-                     free (def);
-                     return counter;
-                   }
-                 if (t < p && t[0] == '-')
-                   {
-                     errno = 0;
-                     start = t + 1;
-                     to_line = strtol (start, &t, 10);
-                     if (errno != 0 || to_line < 0 || t != p)
-                       {
-                         free (def);
-                         return counter;
-                       }
-                   }
-                 else          /* one number means "match this line only" */
-                   to_line = from_line;
-               }
-             else              /* default to 0-max */
-               {
-                 from_line = 0;
-                 to_line = INT_MAX;
-               }
-             break;
-           }
-         start = p + 1;
-         state += 1;
-         break;
-       case '\0':              /* found EOL */
-         keep_looking = 0;
-         /* fall through to '/' */
-       case '/':               /* found a definition separator */
-         switch (state)
-           {
-           case 4:             /* within a log level */
-             p[0] = '\0';
-             state = 0;
-             level = get_type ((const char *) start);
-             if (level == GNUNET_ERROR_TYPE_INVALID
-                 || level == GNUNET_ERROR_TYPE_UNSPECIFIED
-                 || 0 != add_definition (comp, file, function, from_line,
-                                          to_line, level, force))
-               {
-                 free (def);
-                 return counter;
-               }
-             counter += 1;
-             start = p + 1;
-             break;
-           default:
-             break;
-           }
-       default:
-         break;
-       }
+    switch (p[0])
+    {
+    case ';':                  /* found a field separator */
+      p[0] = '\0';
+      switch (state)
+      {
+      case 0:                  /* within a component name */
+        comp = start;
+        break;
+      case 1:                  /* within a file name */
+        file = start;
+        break;
+      case 2:                  /* within a function name */
+        /* after a file name there must be a function name */
+        function = start;
+        break;
+      case 3:                  /* within a from-to line range */
+        if (strlen (start) > 0)
+        {
+          errno = 0;
+          from_line = strtol (start, &t, 10);
+          if (errno != 0 || from_line < 0)
+          {
+            GNUNET_free (def);
+            return counter;
+          }
+          if (t < p && t[0] == '-')
+          {
+            errno = 0;
+            start = t + 1;
+            to_line = strtol (start, &t, 10);
+            if (errno != 0 || to_line < 0 || t != p)
+            {
+              GNUNET_free (def);
+              return counter;
+            }
+          }
+          else                  /* one number means "match this line only" */
+            to_line = from_line;
+        }
+        else                    /* default to 0-max */
+        {
+          from_line = 0;
+          to_line = INT_MAX;
+        }
+        break;
+      }
+      start = p + 1;
+      state += 1;
+      break;
+    case '\0':                 /* found EOL */
+      keep_looking = 0;
+      /* fall through to '/' */
+    case '/':                  /* found a definition separator */
+      switch (state)
+      {
+      case 4:                  /* within a log level */
+        p[0] = '\0';
+        state = 0;
+        level = get_type ((const char *) start);
+        if (level == GNUNET_ERROR_TYPE_INVALID ||
+            level == GNUNET_ERROR_TYPE_UNSPECIFIED ||
+            0 != add_definition (comp, file, function, from_line, to_line,
+                                 level, force))
+        {
+          GNUNET_free (def);
+          return counter;
+        }
+        counter += 1;
+        start = p + 1;
+        break;
+      default:
+        break;
+      }
+    default:
+      break;
+    }
   }
-  free (def);
+  GNUNET_free (def);
   return counter;
 }
 
@@ -531,6 +530,8 @@ parse_all_definitions ()
   gnunet_force_log_parsed = GNUNET_YES;
 }
 #endif
+
+
 /**
  * Setup logging.
  *
@@ -546,6 +547,7 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
   int dirwarn;
   char *fn;
   const char *env_logfile = NULL;
+  int altlog_fd;
 
   min_level = get_type (loglevel);
 #if !defined(GNUNET_CULL_LOGGING)
@@ -560,8 +562,7 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
   component_nopid = GNUNET_strdup (comp);
 
   env_logfile = getenv ("GNUNET_FORCE_LOGFILE");
-  if ( (env_logfile != NULL) &&
-       (strlen (env_logfile) > 0) )
+  if ((env_logfile != NULL) && (strlen (env_logfile) > 0))
     logfile = env_logfile;
 
   if (logfile == NULL)
@@ -570,10 +571,40 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
   if (NULL == fn)
     return GNUNET_SYSERR;
   dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn));
-  altlog = FOPEN (fn, "a");
-  if (altlog == NULL)
+#if WINDOWS
+  altlog_fd = OPEN (fn, O_APPEND |
+                        O_BINARY |
+                        O_WRONLY | O_CREAT,
+                        _S_IREAD | _S_IWRITE);
+#else
+  altlog_fd = OPEN (fn, O_APPEND |
+                        O_WRONLY | O_CREAT,
+                        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+#endif
+  if (altlog_fd != -1)
+  {
+    int dup_return;
+    if (GNUNET_stderr != NULL)
+      fclose (GNUNET_stderr);
+    dup_return = dup2 (altlog_fd, 2);
+    (void) close (altlog_fd);
+    if (dup_return != -1)
+    {
+      altlog = fdopen (2, "ab");
+      if (altlog == NULL)
+      {
+        (void) close (2);
+        altlog_fd = -1;
+      }
+    }
+    else
+    {
+      altlog_fd = -1;
+    }
+  }
+  if (altlog_fd == -1)
   {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn);
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
     if (dirwarn)
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   _("Failed to create or access directory for log file `%s'\n"),
@@ -582,8 +613,6 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
     return GNUNET_SYSERR;
   }
   GNUNET_free (fn);
-  if (GNUNET_stderr != NULL)
-    fclose (GNUNET_stderr);
   GNUNET_stderr = altlog;
   return GNUNET_OK;
 }
@@ -651,7 +680,7 @@ output_message (enum GNUNET_ErrorType kind, const char *comp,
 
   if (GNUNET_stderr != NULL)
   {
-    fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
+    FPRINTF (GNUNET_stderr, "%s %s %s %s", datestr, comp,
              GNUNET_error_type_to_string (kind), msg);
     fflush (GNUNET_stderr);
   }
@@ -920,6 +949,23 @@ GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
   return (const char *) ret.encoding;
 }
 
+/**
+ * Convert a peer identity to a string (for printing debug messages).
+ * This is one of the very few calls in the entire API that is
+ * NOT reentrant!
+ *
+ * @param pid the peer identity
+ * @return string form of the pid; will be overwritten by next
+ *         call to GNUNET_i2s.
+ */
+const char *
+GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
+{
+  static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
+
+  GNUNET_CRYPTO_hash_to_enc (&pid->hashPubKey, &ret);
+  return (const char *) ret.encoding;
+}
 
 
 /**