[tor-commits] [obfsproxy/master] Add timestamps to log messages.

nickm at torproject.org nickm at torproject.org
Sat Feb 11 06:31:37 UTC 2012


commit c2ef1e7028a4b5c6cf33373d41ae8a93e24ef425
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Fri Feb 10 10:08:26 2012 +0100

    Add timestamps to log messages.
    
    Fixes #5069.
---
 src/util.c |   28 ++++++++++------------------
 src/util.h |    4 ++++
 2 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/src/util.c b/src/util.c
index 70bd9b4..9a86ad2 100644
--- a/src/util.c
+++ b/src/util.c
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <assert.h>
+#include <time.h>
 
 #include <event2/dns.h>
 #ifndef _WIN32
@@ -436,21 +437,6 @@ close_obfsproxy_logfile(void)
 }
 
 /**
-   Writes a small prologue in the logfile 'fd' that mentions the
-   obfsproxy version and helps separate log instances.
-
-   Returns 0 on success, -1 on failure.
-*/
-static int
-write_logfile_prologue(int logfile)
-{
-  static const char prologue[] = "\nBrand new obfsproxy log:\n";
-  if (write(logfile, prologue, strlen(prologue)) != strlen(prologue))
-    return -1;
-  return 0;
-}
-
-/**
    Sets the global logging 'method' and also sets and open the logfile
    'filename' in case we want to log into a file.
    It returns 1 on success and -1 on fail.
@@ -461,11 +447,10 @@ log_set_method(int method, const char *filename)
   if (method == LOG_METHOD_FILE) {
     if (open_and_set_obfsproxy_logfile(filename) < 0)
       return -1;
-    if (write_logfile_prologue(logging_logfile) < 0)
-      return -1;
   }
 
   logging_method = method;
+  log_info("\nStarting.");
 
   return 0;
 }
@@ -519,9 +504,16 @@ logv(int severity, const char *format, va_list ap)
   int r=0;
   char buf[MAX_LOG_ENTRY];
 
+  time_t now = time(NULL);
+  struct tm *nowtm = localtime(&now);
+  char ts[ISO_TIME_LEN + 2];
   size_t buflen = MAX_LOG_ENTRY-2;
 
-  r = obfs_snprintf(buf, buflen, "[%s] ", sev_to_string(severity));
+  if (strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S ", nowtm) !=
+      ISO_TIME_LEN + 1)
+    ts[0] = '\0';
+  r = obfs_snprintf(buf, buflen, "%s[%s] ", ts,
+                    sev_to_string(severity));
   if (r < 0)
     n = strlen(buf);
   else
diff --git a/src/util.h b/src/util.h
index 6f3318d..3396abf 100644
--- a/src/util.h
+++ b/src/util.h
@@ -149,6 +149,10 @@ int obfs_snprintf(char *str, size_t size,
 /** We don't want no logs. */
 #define LOG_METHOD_NULL 3
 
+/** Length of the date-time format that we use in log messages, which is
+    "yyyy-mm-dd hh:mm:ss" (without quotes). */
+#define ISO_TIME_LEN 19
+
 /** Set the log method, and open the logfile 'filename' if appropriate. */
 int log_set_method(int method, const char *filename);
 



More information about the tor-commits mailing list