updates.
[silc.git] / lib / silcutil / silclog.c
index 15c64884fec45277afd572fdf4fc32909f6f6070..08805a3887c644aa461dff2ca6ac001360b6b6be 100644 (file)
 #include "silcincludes.h"
 
 /* Set TRUE/FALSE to enable/disable debugging */
-int silc_debug = FALSE;
+bool silc_debug = FALSE;
+bool silc_debug_hexdump = FALSE;
+char *silc_debug_string = NULL;
+bool log_file_open_error = FALSE;
 
 /* SILC Log name strings. These strings are printed to the log file. */
 const SilcLogTypeName silc_log_types[] =
@@ -117,10 +120,13 @@ void silc_log_output(const char *filename, uint32 maxsize,
     
     /* Open the log file */
     if ((fp = fopen(filename, "a+")) == NULL) {
-      fprintf(stderr, "warning: could not open log file "
-             "%s: %s\n", filename, strerror(errno));
-      fprintf(stderr, "warning: log messages will be displayed on "
-             "the screen\n");
+      if (!log_file_open_error) {
+       fprintf(stderr, "warning: could not open log file "
+               "%s: %s\n", filename, strerror(errno));
+       fprintf(stderr, "warning: log messages will be displayed on "
+               "the screen\n");
+      }
+      log_file_open_error = TRUE;
       fp = stderr;
     }
   }
@@ -148,12 +154,18 @@ void silc_log_output_debug(char *file, char *function,
     return;
   }
 
-  if (debug_cb)
-    {
-      (*debug_cb)(file, function, line, string);
-      silc_free(string);
-      return;
-    }
+  if (silc_debug_string && 
+      (!silc_string_regex_match(silc_debug_string, file) &&
+       !silc_string_regex_match(silc_debug_string, function))) {
+    silc_free(string);
+    return;
+  }
+
+  if (debug_cb) {
+    (*debug_cb)(file, function, line, string);
+    silc_free(string);
+    return;
+  }
 
   fprintf(stderr, "%s:%d: %s\n", function, line, string);
   fflush(stderr);
@@ -170,17 +182,23 @@ void silc_log_output_hexdump(char *file, char *function,
   int off, pos, count;
   unsigned char *data = (unsigned char *)data_in;
 
-  if (!silc_debug) {
+  if (!silc_debug_hexdump) {
     silc_free(string);
     return;
   }
 
-  if (debug_hexdump_cb)
-    {
-      (*debug_hexdump_cb)(file, function, line, data_in, len, string);
-      silc_free(string);
-      return;
-    }
+  if (silc_debug_string && 
+      (!silc_string_regex_match(silc_debug_string, file) &&
+       !silc_string_regex_match(silc_debug_string, function))) {
+    silc_free(string);
+    return;
+  }
+
+  if (debug_hexdump_cb) {
+    (*debug_hexdump_cb)(file, function, line, data_in, len, string);
+    silc_free(string);
+    return;
+  }
 
   fprintf(stderr, "%s:%d: %s\n", function, line, string);
   silc_free(string);
@@ -294,3 +312,16 @@ void silc_log_reset_debug_callbacks()
   debug_cb = NULL;
   debug_hexdump_cb = NULL;
 }
+
+/* Set current debug string */
+
+void silc_log_set_debug_string(const char *debug_string)
+{
+  silc_free(silc_debug_string);
+  if ((strchr(debug_string, '(') &&
+       strchr(debug_string, ')')) ||
+      strchr(debug_string, '$'))
+    silc_debug_string = strdup(debug_string);
+  else
+    silc_debug_string = silc_string_regexify(debug_string);
+}