[or-cvs] the logs now include a timestamp and severity

Roger Dingledine arma at seul.org
Wed Sep 4 00:39:36 UTC 2002


Update of /home/or/cvsroot/src/common
In directory moria.seul.org:/home/arma/work/onion/cvs/src/common

Modified Files:
	log.c 
Log Message:
the logs now include a timestamp and severity

the implementation is sort of a kludge..you're welcome to fix it up


Index: log.c
===================================================================
RCS file: /home/or/cvsroot/src/common/log.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- log.c	12 Jul 2002 18:14:16 -0000	1.2
+++ log.c	4 Sep 2002 00:39:33 -0000	1.3
@@ -8,6 +8,11 @@
 /*
  * Changes :
  * $Log$
+ * Revision 1.3  2002/09/04 00:39:33  arma
+ * the logs now include a timestamp and severity
+ *
+ * the implementation is sort of a kludge..you're welcome to fix it up
+ *
  * Revision 1.2  2002/07/12 18:14:16  montrose
  * removed loglevel from global namespace. severity level is set using log() with a NULL format argument now. example: log(LOG_ERR,NULL);
  *
@@ -55,12 +60,53 @@
 #include <syslog.h>
 #include <string.h>
 #include <errno.h>
+#include <assert.h>
+#include <time.h>
 #include "log.h"
 
+/* FIXME this whole thing is hacked together. feel free to make it clean. */
+size_t sev_to_string(char *buf, int max, int severity) {
+  assert(max > 20);
+
+  switch(severity) {
+    case LOG_DEBUG:
+      strcpy(buf,"debug");  
+      break;
+    case LOG_INFO:
+      strcpy(buf,"info");
+      break;
+    case LOG_NOTICE:
+      strcpy(buf,"notice");
+      break;
+    case LOG_WARNING:
+      strcpy(buf,"warn");
+      break;
+    case LOG_ERR:
+      strcpy(buf,"err");
+      break;
+    case LOG_CRIT:
+      strcpy(buf,"crit");
+      break;
+    case LOG_ALERT:
+      strcpy(buf,"alert");
+      break;
+    case LOG_EMERG: 
+      strcpy(buf,"emerg");
+      break;
+    default:
+      strcpy(buf,"UNKNOWN");
+      break;
+  }
+
+  return strlen(buf)+1;
+}
+
 /* Outputs a message to stdout */
 void log(int severity, const char *format, ...)
 {
   static int loglevel = LOG_DEBUG;
+  char buf[201];
+  time_t t;
   va_list ap;
 
   if ( format )
@@ -70,6 +116,11 @@
   
     if (severity <= loglevel)
     {
+      t = time(NULL);
+      strftime(buf, 200, "%b %d %H:%M:%S", localtime(&t));
+      printf("%s ", buf);
+      sev_to_string(buf, 200, severity);
+      printf("[%s] ", buf);
       vprintf(format,ap);
       printf("\n");
     }
@@ -79,3 +130,4 @@
   else
     loglevel = severity;
 }
+



More information about the tor-commits mailing list