updates.
[silc.git] / lib / silcutil / silclog.c
index a0c9181a7f72a93a8f2b80931aa3ea11b05f057c..0ffb369432c31a890c9a75dc6b15e61aad114776 100644 (file)
@@ -22,7 +22,9 @@
 #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;
 
 /* SILC Log name strings. These strings are printed to the log file. */
 const SilcLogTypeName silc_log_types[] =
@@ -96,35 +98,37 @@ void silc_log_output(const char *filename, uint32 maxsize,
     }
 
   if (!filename)
-    filename = " ";
-
-  /* Purge the log file if the max size is defined. */
-  if (maxsize) {
-    fp = fopen(filename, "r");
-    if (fp) {
-      int filelen;
-      
-      filelen = fseek(fp, (off_t)0L, SEEK_END);
-      fseek(fp, (off_t)0L, SEEK_SET);  
-      
-      /* Purge? */
-      if (filelen >= maxsize)
-       unlink(filename);
-
-      fclose(fp);
+    fp = stderr;
+  else {
+    /* Purge the log file if the max size is defined. */
+    if (maxsize) {
+      fp = fopen(filename, "r");
+      if (fp) {
+       int filelen;
+       
+       filelen = fseek(fp, (off_t)0L, SEEK_END);
+       fseek(fp, (off_t)0L, SEEK_SET);  
+       
+       /* Purge? */
+       if (filelen >= maxsize)
+         unlink(filename);
+       
+       fclose(fp);
+      }
+    }
+    
+    /* 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");
+      fp = stderr;
     }
   }
 
-  /* 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");
-    fp = stderr;
-  }
   /* Get the log type name */
-  for(np = silc_log_types; np->name; np++) {
+  for (np = silc_log_types; np->name; np++) {
     if (np->type == type)
       break;
   }
@@ -146,6 +150,13 @@ void silc_log_output_debug(char *file, char *function,
     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);
@@ -168,7 +179,14 @@ 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 (silc_debug_string && 
+      (!silc_string_regex_match(silc_debug_string, file) &&
+       !silc_string_regex_match(silc_debug_string, function))) {
     silc_free(string);
     return;
   }
@@ -292,3 +310,15 @@ 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, ')'))
+    silc_debug_string = strdup(debug_string);
+  else
+    silc_debug_string = silc_string_regexify(debug_string);
+}