浏览代码

ZOOKEEPER-222. print C client log message timestamp in human readable form. (pat via mahadev)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@726862 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 16 年之前
父节点
当前提交
790028113f
共有 2 个文件被更改,包括 23 次插入2 次删除
  1. 3 0
      CHANGES.txt
  2. 20 2
      src/c/src/zk_log.c

+ 3 - 0
CHANGES.txt

@@ -78,6 +78,9 @@ IMPROVEMENTS:
    ZOOKEEPER-225. c client should log an info message in zookeeper_init
 detailing connection parameters. (pat via mahadev)
 
+   ZOOKEEPER-222.  print C client log message timestamp in human readable
+form. (pat via mahadev) 
+
 Release 3.0.0 - 2008-10-21
 
 Non-backward compatible changes:

+ 20 - 2
src/c/src/zk_log.c

@@ -23,8 +23,9 @@
 #include "zk_log.h"
 #include <unistd.h>
 #include <stdarg.h>
+#include <time.h>
 
-#define TIME_NOW_BUF_SIZE 128
+#define TIME_NOW_BUF_SIZE 1024
 #define FORMAT_LOG_BUF_SIZE 2048
 
 #ifdef THREADED
@@ -95,7 +96,24 @@ static const char* time_now(){
         return "time_now(): Failed to allocate memory buffer";
     
     gettimeofday(&tv,0);
-    sprintf(now_str,"%ld.%03d.%03d",tv.tv_sec,(int)(tv.tv_usec/1000),(int)(tv.tv_usec%1000));
+
+    const time_t now = tv.tv_sec;
+    struct tm lt;
+    localtime_r(&now, &lt);
+
+    // clone the format used by log4j ISO8601DateFormat
+    // specifically: "yyyy-MM-dd HH:mm:ss,SSS"
+
+    size_t len = strftime(now_str,
+                          TIME_NOW_BUF_SIZE,
+                          "%F %H:%M:%S",
+                          &lt);
+
+    len += snprintf(now_str + len,
+                    TIME_NOW_BUF_SIZE - len,
+                    ",%03d",
+                    (int)(tv.tv_usec/1000));
+
     return now_str;
 }