Show milliseconds in SILC_LOG_DEBUG and family outputs
[runtime.git] / lib / silcutil / silclog.c
index 2955a5dd761e2203ebb3196dc945871487eb5f4b..e857bac29789cf85c96cd5fe7e794b18c5913faa 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2005 Pekka Riikonen
+  Copyright (C) 1997 - 2008 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -16,9 +16,8 @@
   GNU General Public License for more details.
 
 */
-/* $Id$ */
 
-#include "silcincludes.h"
+#include "silcruntime.h"
 
 /* SilcLogSettings context */
 typedef struct {
@@ -50,6 +49,8 @@ typedef struct {
   void *context;
 } *SilcLog, SilcLogStruct;
 
+#ifndef SILC_SYMBIAN
+
 /* Default settings */
 static SilcLogSettingsStruct silclog =
 {
@@ -66,8 +67,14 @@ static SilcLogSettingsStruct silclog =
   TRUE,
 };
 
+#endif /* !SILC_SYMBIAN */
+
 /* Default log contexts */
+#ifndef SILC_SYMBIAN
 static SilcLogStruct silclogs[4] =
+#else
+const SilcLogStruct silclogs[4] =
+#endif /* !SILC_SYMBIAN */
 {
   {"", NULL, 0, "Info", SILC_LOG_INFO, NULL, NULL},
   {"", NULL, 0, "Warning", SILC_LOG_WARNING, NULL, NULL},
@@ -81,7 +88,7 @@ static SilcLog silc_log_get_context(SilcLogType type)
 {
   if (type < 1 || type > 4)
     return NULL;
-  return &silclogs[(int)type - 1];
+  return (SilcLog)&silclogs[(int)type - 1];
 }
 
 /* Check log file site and cycle log file if it is over max size. */
@@ -106,12 +113,13 @@ static void silc_log_checksize(SilcLog log)
   /* Cycle log file */
   fprintf(log->fp,
          "[%s] [%s] Cycling log file, over max log size (%lu kilobytes)\n",
-         silc_get_time(0), log->typename, (unsigned long)log->maxsize / 1024);
+         silc_time_string(0), log->typename,
+         (unsigned long)log->maxsize / 1024);
   fflush(log->fp);
   fclose(log->fp);
 
   memset(newname, 0, sizeof(newname));
-  snprintf(newname, sizeof(newname) - 1, "%s.old", log->filename);
+  silc_snprintf(newname, sizeof(newname) - 1, "%s.old", log->filename);
   unlink(newname);
   rename(log->filename, newname);
 
@@ -128,6 +136,7 @@ static void silc_log_checksize(SilcLog log)
 
 SILC_TASK_CALLBACK(silc_log_fflush_callback)
 {
+#ifndef SILC_SYMBIAN
   SilcLog log;
 
   if (!silclog.quick) {
@@ -146,9 +155,9 @@ SILC_TASK_CALLBACK(silc_log_fflush_callback)
 
   if (silclog.flushdelay < 2)
     silclog.flushdelay = 2;
-  silc_schedule_task_add(context, 0, silc_log_fflush_callback, context,
-                        silclog.flushdelay, 0, SILC_TASK_TIMEOUT,
-                        SILC_TASK_PRI_NORMAL);
+  silc_schedule_task_add_timeout(context, silc_log_fflush_callback, context,
+                                silclog.flushdelay, 0);
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Output log message to log file */
@@ -169,10 +178,11 @@ void silc_log_output(SilcLogType type, char *string)
 
   typename = log->typename;
 
+#ifndef SILC_SYMBIAN
   if (!silclog.scheduled) {
     if (silclog.no_init == FALSE) {
       fprintf(stderr,
-             "Warning, trying to output without log files initialization, "
+             "Warning, log files not initialized, "
              "log output is going to stderr\n");
       silclog.no_init = TRUE;
     }
@@ -181,6 +191,7 @@ void silc_log_output(SilcLogType type, char *string)
     log = NULL;
     goto found;
   }
+#endif /* !SILC_SYMBIAN */
 
   /* Find open log file */
   while (log) {
@@ -194,9 +205,10 @@ void silc_log_output(SilcLogType type, char *string)
   if (!log || !log->fp)
     goto end;
 
+#ifndef SILC_SYMBIAN
  found:
   if (silclog.timestamp)
-    fprintf(fp, "[%s] [%s] %s\n", silc_get_time(0), typename, string);
+    fprintf(fp, "[%s] [%s] %s\n", silc_time_string(0), typename, string);
   else
     fprintf(fp, "[%s] %s\n", typename, string);
 
@@ -205,24 +217,34 @@ void silc_log_output(SilcLogType type, char *string)
     if (log)
       silc_log_checksize(log);
   }
+#endif /* !SILC_SYMBIAN */
 
  end:
+#ifndef SILC_SYMBIAN
   /* Output log to stderr if debugging */
   if (typename && silclog.debug) {
     fprintf(stderr, "[Logging] [%s] %s\n", typename, string);
     fflush(stderr);
   }
+#else
+  fprintf(stderr, "[Logging] [%s] %s\n", typename, string);
+#endif /* !SILC_SYMBIAN */
+
   silc_free(string);
 }
 
 /* Set and initialize the specified log file. */
 
-bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
-                      SilcSchedule scheduler)
+SilcBool silc_log_set_file(SilcLogType type, char *filename,
+                          SilcUInt32 maxsize, SilcSchedule scheduler)
 {
+#ifndef SILC_SYMBIAN
   FILE *fp = NULL;
   SilcLog log;
 
+  if (!scheduler)
+    scheduler = silc_schedule_get_global();
+
   log = silc_log_get_context(type);
   if (!log)
     return FALSE;
@@ -264,11 +286,12 @@ bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
   /* Add flush timeout */
   if (scheduler) {
     silc_schedule_task_del_by_callback(scheduler, silc_log_fflush_callback);
-    silc_schedule_task_add(scheduler, 0, silc_log_fflush_callback, scheduler,
-                          10, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+    silc_schedule_task_add_timeout(scheduler, silc_log_fflush_callback,
+                                  scheduler, 10, 0);
     silclog.scheduled = TRUE;
   }
 
+#endif /* !SILC_SYMBIAN */
   return TRUE;
 }
 
@@ -284,17 +307,20 @@ char *silc_log_get_file(SilcLogType type)
 
 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context)
 {
+#ifndef SILC_SYMBIAN
   SilcLog log = silc_log_get_context(type);
   if (log) {
     log->cb = cb;
     log->context = context;
   }
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Reset log callbacks */
 
 void silc_log_reset_callbacks(void)
 {
+#ifndef SILC_SYMBIAN
   SilcLog log;
   log = silc_log_get_context(SILC_LOG_INFO);
   log->cb = log->context = NULL;
@@ -304,6 +330,7 @@ void silc_log_reset_callbacks(void)
   log->cb = log->context = NULL;
   log = silc_log_get_context(SILC_LOG_FATAL);
   log->cb = log->context = NULL;
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Flush all log files */
@@ -370,31 +397,36 @@ void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
                                  SilcLogHexdumpCb hexdump_cb,
                                  void *hexdump_context)
 {
+#ifndef SILC_SYMBIAN
   silclog.debug_cb = debug_cb;
   silclog.debug_context = debug_context;
   silclog.hexdump_cb = hexdump_cb;
   silclog.hexdump_context = hexdump_context;
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Resets debug callbacks */
 
 void silc_log_reset_debug_callbacks()
 {
+#ifndef SILC_SYMBIAN
   silclog.debug_cb = NULL;
   silclog.debug_context = NULL;
   silclog.hexdump_cb = NULL;
   silclog.hexdump_context = NULL;
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Set current debug string */
 
 void silc_log_set_debug_string(const char *debug_string)
 {
+#ifndef SILC_SYMBIAN
   char *string;
   int len;
   if ((strchr(debug_string, '(') && strchr(debug_string, ')')) ||
       strchr(debug_string, '$'))
-    string = strdup(debug_string);
+    string = silc_strdup(debug_string);
   else
     string = silc_string_regexify(debug_string);
   len = strlen(string);
@@ -403,41 +435,52 @@ void silc_log_set_debug_string(const char *debug_string)
   memset(silclog.debug_string, 0, sizeof(silclog.debug_string));
   strncpy(silclog.debug_string, string, len);
   silc_free(string);
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Set timestamp */
 
-void silc_log_timestamp(bool enable)
+void silc_log_timestamp(SilcBool enable)
 {
+#ifndef SILC_SYMBIAN
   silclog.timestamp = enable;
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Set flushdelay */
 
 void silc_log_flushdelay(SilcUInt32 flushdelay)
 {
+#ifndef SILC_SYMBIAN
   silclog.flushdelay = flushdelay;
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Set quick logging */
 
-void silc_log_quick(bool enable)
+void silc_log_quick(SilcBool enable)
 {
+#ifndef SILC_SYMBIAN
   silclog.quick = enable;
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Set debugging */
 
-void silc_log_debug(bool enable)
+void silc_log_debug(SilcBool enable)
 {
+#ifndef SILC_SYMBIAN
   silclog.debug = enable;
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Set debug hexdump */
 
-void silc_log_debug_hexdump(bool enable)
+void silc_log_debug_hexdump(SilcBool enable)
 {
+#ifndef SILC_SYMBIAN
   silclog.debug_hexdump = enable;
+#endif /* !SILC_SYMBIAN */
 }
 
 /* Outputs the debug message to stderr. */
@@ -445,6 +488,9 @@ void silc_log_debug_hexdump(bool enable)
 void silc_log_output_debug(char *file, const char *function,
                           int line, char *string)
 {
+  SilcTimeStruct curtime;
+
+#ifndef SILC_SYMBIAN
   if (!silclog.debug)
     goto end;
 
@@ -457,9 +503,23 @@ void silc_log_output_debug(char *file, const char *function,
                            silclog.debug_context))
       goto end;
   }
+#endif /* !SILC_SYMBIAN */
 
-  fprintf(stderr, "%s:%d: %s\n", function, line, string);
+  silc_time_value(0, &curtime);
+
+#ifdef SILC_WIN32
+  if (strrchr(function, '\\'))
+    fprintf(stderr, "%s:%d: %s\n", strrchr(function, '\\') + 1, line, string);
+  else
+#endif /* SILC_WIN32 */
+#ifdef SILC_SYMBIAN
+  silc_symbian_debug(function, line, string);
+#else
+  fprintf(stderr, "%02d:%02d:%02d.%03d %s:%d: %s\n", curtime.hour,
+         curtime.minute, curtime.second, curtime.msecond, function, line,
+         string);
   fflush(stderr);
+#endif /* SILC_SYMBIAN */
 
  end:
   silc_free(string);
@@ -471,10 +531,7 @@ void silc_log_output_hexdump(char *file, const char *function,
                             int line, void *data_in,
                             SilcUInt32 len, char *string)
 {
-  int i, k;
-  int off, pos, count;
-  unsigned char *data = (unsigned char *)data_in;
-
+#ifndef SILC_SYMBIAN
   if (!silclog.debug_hexdump)
     goto end;
 
@@ -487,63 +544,11 @@ void silc_log_output_hexdump(char *file, const char *function,
                              data_in, len, string, silclog.hexdump_context))
       goto end;
   }
+#endif /* !SILC_SYMBIAN */
 
   fprintf(stderr, "%s:%d: %s\n", function, line, string);
 
-  k = 0;
-  pos = 0;
-  count = 16;
-  off = len % 16;
-  while (1) {
-    if (off) {
-      if ((len - pos) < 16 && (len - pos <= len - off))
-       count = off;
-    } else {
-      if (pos == len)
-       count = 0;
-    }
-    if (off == len)
-      count = len;
-
-    if (count)
-      fprintf(stderr, "%08X  ", k++ * 16);
-
-    for (i = 0; i < count; i++) {
-      fprintf(stderr, "%02X ", data[pos + i]);
-
-      if ((i + 1) % 4 == 0)
-       fprintf(stderr, " ");
-    }
-
-    if (count && count < 16) {
-      int j;
-
-      for (j = 0; j < 16 - count; j++) {
-       fprintf(stderr, "   ");
-
-       if ((j + count + 1) % 4 == 0)
-         fprintf(stderr, " ");
-      }
-    }
-
-    for (i = 0; i < count; i++) {
-      char ch;
-
-      if (data[pos] < 32 || data[pos] >= 127)
-       ch = '.';
-      else
-       ch = data[pos];
-
-      fprintf(stderr, "%c", ch);
-      pos++;
-    }
-
-    if (count)
-      fprintf(stderr, "\n");
-
-    if (count < 16)
-      break;
-  }
+  silc_hexdump(data_in, len, stderr);
 
  end:
   silc_free(string);