瀏覽代碼

2008-09-23 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Log microseconds.
	* src/SimpleLogger.cc (SimpleLogger::writeLog)
Tatsuhiro Tsujikawa 17 年之前
父節點
當前提交
2522175ff1
共有 2 個文件被更改,包括 15 次插入4 次删除
  1. 5 0
      ChangeLog
  2. 10 4
      src/SimpleLogger.cc

+ 5 - 0
ChangeLog

@@ -1,3 +1,8 @@
+2008-09-23  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Log microseconds.
+	* src/SimpleLogger.cc (SimpleLogger::writeLog)
+
 2008-09-22  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Replaced HelpItem.cc with OptionHandler.cc.

+ 10 - 4
src/SimpleLogger.cc

@@ -44,6 +44,7 @@
 #include <cstring>
 #include <iostream>
 #include <cstdlib>
+#include <cassert>
 
 namespace aria2 {
 
@@ -142,11 +143,16 @@ void SimpleLogger::writeLog(std::ostream& o, Logger::LEVEL level,
   default:
     levelStr = INFO;
   }
-  time_t now = time(NULL);
-  char datestr[20];
+  struct timeval tv;
+  gettimeofday(&tv, 0);
+  char datestr[27]; // 'YYYY-MM-DD hh:mm:ss.uuuuuu'+'\0' = 27 bytes
   struct tm tm;
-  localtime_r(&now, &tm);
-  strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
+  localtime_r(&tv.tv_sec, &tm);
+  size_t dateLength =
+    strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
+  assert(dateLength <= (size_t)20);
+  snprintf(datestr+dateLength, sizeof(datestr)-dateLength,
+	   ".%06ld", tv.tv_usec);
 
   // TODO a quick hack not to print header in console
   if(printHeader) {