* Added global variable silc_log_timestamp that tells silclog
[silc.git] / lib / silcutil / silclog.c
index a28e9f4f8c15e073b7bfeb07ed8adee5c8be7c68..5f770769103d9f085b6d5dca501ad511d4c6f3d2 100644 (file)
 /* Our working struct -- at the moment we keep it private, but this could
  * change in the future */
 struct SilcLogStruct {
-  char *filename;
+  char filename[256];
   FILE *fp;
   SilcUInt32 maxsize;
-  char *typename;
+  const char *typename;
   SilcLogType type;
   SilcLogCb cb;
   void *context;
@@ -43,12 +43,15 @@ typedef struct SilcLogStruct *SilcLog;
 /* These are the known logging channels.  We initialize this struct with most
  * of the fields set to NULL, because we'll fill in those values at runtime. */
 static struct SilcLogStruct silclogs[SILC_LOG_MAX] = {
-  {NULL, NULL, 0, "Info", SILC_LOG_INFO, NULL, NULL},
-  {NULL, NULL, 0, "Warning", SILC_LOG_WARNING, NULL, NULL},
-  {NULL, NULL, 0, "Error", SILC_LOG_ERROR, NULL, NULL},
-  {NULL, NULL, 0, "Fatal", SILC_LOG_FATAL, NULL, NULL},
+  {"", NULL, 0, "Info", SILC_LOG_INFO, NULL, NULL},
+  {"", NULL, 0, "Warning", SILC_LOG_WARNING, NULL, NULL},
+  {"", NULL, 0, "Error", SILC_LOG_ERROR, NULL, NULL},
+  {"", NULL, 0, "Fatal", SILC_LOG_FATAL, NULL, NULL},
 };
 
+/* Causes logging output to contain timestamps */
+bool silc_log_timestamp = TRUE;
+
 /* If TRUE, log files will be flushed for each log input */
 bool silc_log_quick = FALSE;
 
@@ -143,7 +146,7 @@ static bool silc_log_reset(SilcLog log)
     fflush(log->fp);
     fclose(log->fp);
   }
-  if (!log->filename) return FALSE;
+  if (!log->filename[0]) return FALSE;
   if (!(log->fp = fopen(log->filename, "a+"))) {
     SILC_LOG_WARNING(("Couldn't reset logfile %s for type \"%s\": %s",
                      log->filename, log->typename, strerror(errno)));
@@ -178,7 +181,7 @@ SILC_TASK_CALLBACK(silc_log_fflush_callback)
 
 void silc_log_output(SilcLogType type, char *string)
 {
-  char *typename = NULL;
+  const char *typename = NULL;
   FILE *fp;
   SilcLog log;
 
@@ -220,7 +223,12 @@ void silc_log_output(SilcLogType type, char *string)
   goto end;
 
  found:
-  fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), typename, string);
+  /* writes the logging string to the selected channel */
+  if (silc_log_timestamp)
+    fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), typename, string);
+  else
+    fprintf(fp, "[%s] %s\n", typename, string);
+
   if (silc_log_quick || silc_log_starting) {
     fflush(fp);
     if (log) /* we may have been redirected to stderr */
@@ -272,16 +280,18 @@ bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
   }
 
   /* clean the logging channel */
-  if (log->filename) {
+  if (strlen(log->filename)) {
     if (log->fp)
       fclose(log->fp);
-    silc_free(log->filename);
-    log->filename = NULL;
+    memset(log->filename, 0, sizeof(log->filename));
     log->fp = NULL;
   }
 
   if (fp) {
-    log->filename = strdup(filename);
+    memset(log->filename, 0, sizeof(log->filename));
+    strncpy(log->filename, filename,
+           strlen(filename) < sizeof(log->filename) ? strlen(filename) :
+           sizeof(log->filename) - 1);
     log->fp = fp;
     log->maxsize = maxsize;
   }
@@ -486,11 +496,16 @@ void silc_log_reset_debug_callbacks()
 void silc_log_set_debug_string(const char *debug_string)
 {
   char *string;
+  int len;
   if ((strchr(debug_string, '(') && strchr(debug_string, ')')) ||
       strchr(debug_string, '$'))
     string = strdup(debug_string);
   else
     string = silc_string_regexify(debug_string);
-  strncpy(silc_log_debug_string, string, strlen(string));
+  len = strlen(string);
+  if (len >= sizeof(silc_log_debug_string))
+    len = sizeof(silc_log_debug_string) - 1;
+  memset(silc_log_debug_string, 0, sizeof(silc_log_debug_string));
+  strncpy(silc_log_debug_string, string, len);
   silc_free(string);
 }