updates.
[silc.git] / lib / silcutil / silclog.c
index 243ce20e8792902746ebbb0622c39f59c86a278e..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[] =
@@ -39,10 +42,10 @@ char *log_info_file;
 char *log_warning_file;
 char *log_error_file;
 char *log_fatal_file;
-unsigned int log_info_size;
-unsigned int log_warning_size;
-unsigned int log_error_size;
-unsigned int log_fatal_size;
+uint32 log_info_size;
+uint32 log_warning_size;
+uint32 log_error_size;
+uint32 log_fatal_size;
 
 /* Log callbacks. If these are set by the application these are used
    instead of the default functions in this file. */
@@ -57,7 +60,7 @@ static SilcDebugHexdumpCb debug_hexdump_cb = NULL;
 
 /* Outputs the log message to what ever log file selected. */
 
-void silc_log_output(const char *filename, unsigned int maxsize,
+void silc_log_output(const char *filename, uint32 maxsize,
                      SilcLogType type, char *string)
 {
   FILE *fp;
@@ -95,38 +98,49 @@ void silc_log_output(const char *filename, unsigned int maxsize,
       break;
     }
 
-  /* 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);
+  if (!filename)
+    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) {
+      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;
     }
   }
 
-  /* 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;
   }
 
   fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), np->name, string);
   fflush(fp);
-  fclose(fp);
+  if (fp != stderr)
+    fclose(fp);
   silc_free(string);
 }
 
@@ -140,14 +154,19 @@ 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:%s:%d: %s\n", file, function, line, string); */
   fprintf(stderr, "%s:%d: %s\n", function, line, string);
   fflush(stderr);
   silc_free(string);
@@ -157,25 +176,30 @@ void silc_log_output_debug(char *file, char *function,
 
 void silc_log_output_hexdump(char *file, char *function, 
                             int line, void *data_in,
-                            unsigned int len, char *string)
+                            uint32 len, char *string)
 {
   int i, k;
   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:%s:%d: %s\n", file, function, line, string); */
   fprintf(stderr, "%s:%d: %s\n", function, line, string);
   silc_free(string);
 
@@ -238,10 +262,10 @@ void silc_log_output_hexdump(char *file, char *function,
 
 /* Sets log files */
 
-void silc_log_set_files(char *info, unsigned int info_size, 
-                       char *warning, unsigned int warning_size,
-                       char *error, unsigned int error_size,
-                       char *fatal, unsigned int fatal_size)
+void silc_log_set_files(char *info, uint32 info_size, 
+                       char *warning, uint32 warning_size,
+                       char *error, uint32 error_size,
+                       char *fatal, uint32 fatal_size)
 {
   log_info_file = info;
   log_warning_file = warning;
@@ -288,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);
+}