-LRN: fix socket_close logging
[oweals/gnunet.git] / src / util / common_logging.c
index 0b7d06bab91299b725b0fb9ad9ee03b930f0800f..22901bf268bc44b3358b231b9514df55f0b5e0a3 100644 (file)
@@ -30,6 +30,8 @@
 #include "gnunet_strings_lib.h"
 #include "gnunet_time_lib.h"
 
+#include <regex.h>
+
 /**
  * After how many milliseconds do we always print
  * that "message X was repeated N times"?  Use 12h.
@@ -145,24 +147,19 @@ static FILE *GNUNET_stderr;
 struct LogDef
 {
   /**
-   * Component name. NULL means that this definition matches any component
-   */
-  char *component;
-
-  /**
-   * File name. NULL means that this definition matches any file
+   * Component name regex
    */
-  char *file;
+  regex_t component_regex;
 
   /**
-   * Stores strlen(file)
+   * File name regex
    */
-  int strlen_file;
+  regex_t file_regex;
 
   /**
-   * Function name. NULL means that this definition matches any function
+   * Function name regex
    */
-  char *function;
+  regex_t function_regex;
 
   /**
    * Lowest line at which this definition matches.
@@ -263,6 +260,20 @@ resize_logdefs ()
   logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef));
 }
 
+
+/**
+ * Abort the process, generate a core dump if possible.
+ */
+void
+GNUNET_abort ()
+{
+#if WINDOWS
+  DebugBreak ();
+#endif
+  abort ();
+}
+
+
 /**
  * Utility function - adds a parsed definition to logdefs array.
  *
@@ -273,30 +284,48 @@ resize_logdefs ()
  * @param to_line see struct LogDef
  * @param level see struct LogDef, must be >= 0
  * @param force see struct LogDef
+ * @return 0 on success, regex-specific error otherwise
  */
-static void
+static int
 add_definition (char *component, char *file, char *function, int from_line,
                 int to_line, int level, int force)
 {
-  if (logdefs_size == logdefs_len)
-    resize_logdefs ();
   struct LogDef n;
+  int r;
 
+  if (logdefs_size == logdefs_len)
+    resize_logdefs ();
   memset (&n, 0, sizeof (n));
-  if (strlen (component) > 0 && component[0] != '*')
-    n.component = GNUNET_strdup (component);
-  if (strlen (file) > 0 && file[0] != '*')
+  if (strlen (component) == 0)
+    component = (char *) ".*";
+  r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
+  if (r != 0)
+  {
+    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))
+    function = (char *) ".*";
+  r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
+  if (r != 0)
   {
-    n.file = GNUNET_strdup (file);
-    n.strlen_file = strlen (file);
+    regfree (&n.component_regex);
+    regfree (&n.file_regex);
+    return r;
   }
-  if ((NULL != function) && (strlen (function) > 0) && (function[0] != '*'))
-    n.function = GNUNET_strdup (function);
   n.from_line = from_line;
   n.to_line = to_line;
   n.level = level;
   n.force = force;
   logdefs[logdefs_len++] = n;
+  return 0;
 }
 
 
@@ -320,7 +349,6 @@ GNUNET_get_log_call_status (int caller_level, const char *comp,
   struct LogDef *ld;
   int i;
   int force_only;
-  size_t strlen_file;
 
   if (comp == NULL)
     /* Use default component */
@@ -334,19 +362,14 @@ GNUNET_get_log_call_status (int caller_level, const char *comp,
 
   /* Only look for forced definitions? */
   force_only = min_level >= 0;
-  strlen_file = strlen (file);
   for (i = 0; i < logdefs_len; i++)
   {
     ld = &logdefs[i];
     if ((!force_only || ld->force) &&
-        (line >= ld->from_line && line <= ld->to_line) && (ld->component == NULL
-                                                           || strcmp (comp,
-                                                                      ld->component)
-                                                           == 0) &&
-        (ld->file == NULL ||
-         (ld->strlen_file <= strlen_file &&
-          strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
-        (ld->function == NULL || strcmp (function, ld->function) == 0))
+        (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;
@@ -382,7 +405,7 @@ GNUNET_get_log_call_status (int caller_level, const char *comp,
  *
  * @param constname name of the environment variable from which to get the
  *   string to be parsed
- * @param force 1 if definitions found in @constname are to be forced
+ * @param force 1 if definitions found in constname are to be forced
  * @return number of added definitions
  */
 static int
@@ -472,12 +495,13 @@ parse_definitions (const char *constname, int force)
         state = 0;
         level = get_type ((const char *) start);
         if (level == GNUNET_ERROR_TYPE_INVALID ||
-            level == GNUNET_ERROR_TYPE_UNSPECIFIED)
+            level == GNUNET_ERROR_TYPE_UNSPECIFIED ||
+            0 != add_definition (comp, file, function, from_line, to_line,
+                                 level, force))
         {
           free (def);
           return counter;
         }
-        add_definition (comp, file, function, from_line, to_line, level, force);
         counter += 1;
         start = p + 1;
         break;
@@ -536,7 +560,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)
+  if ((env_logfile != NULL) && (strlen (env_logfile) > 0))
     logfile = env_logfile;
 
   if (logfile == NULL)