[Orxonox-commit 6332] r10989 - in code/branches/cpp11_v2: src/libraries/util/output test/util/output

landauf at orxonox.net landauf at orxonox.net
Tue Dec 29 11:35:49 CET 2015


Author: landauf
Date: 2015-12-29 11:35:49 +0100 (Tue, 29 Dec 2015)
New Revision: 10989

Modified:
   code/branches/cpp11_v2/src/libraries/util/output/LogWriter.cc
   code/branches/cpp11_v2/test/util/output/LogWriterTest.cc
Log:
using the c++11 chrono library to add milliseconds to the timestamp in the logfile.

Modified: code/branches/cpp11_v2/src/libraries/util/output/LogWriter.cc
===================================================================
--- code/branches/cpp11_v2/src/libraries/util/output/LogWriter.cc	2015-12-28 22:22:21 UTC (rev 10988)
+++ code/branches/cpp11_v2/src/libraries/util/output/LogWriter.cc	2015-12-29 10:35:49 UTC (rev 10989)
@@ -34,6 +34,7 @@
 #include "LogWriter.h"
 
 #include <ctime>
+#include <chrono>
 #include <cstdlib>
 
 #include "OutputManager.h"
@@ -179,15 +180,21 @@
         if (!this->file_.is_open())
             return;
 
+        // get the milliseconds
+        std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
+        std::chrono::system_clock::duration timeSinceEpoch = now.time_since_epoch();
+        std::chrono::milliseconds millisSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(timeSinceEpoch);
+        unsigned int millis = (millisSinceEpoch.count() % 1000);
+
         // get the current time
-        time_t rawtime;
-        struct tm* timeinfo;
-        time(&rawtime);
-        timeinfo = localtime(&rawtime);
+        time_t rawtime = std::chrono::system_clock::to_time_t(now);
+        struct tm* timeinfo = localtime(&rawtime);
 
+        // format time: hh:mm:ss:xxx
+        char buffer[13];
+        snprintf(buffer, sizeof(buffer), "%.2i:%.2i:%.2i:%.3i", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, millis);
+
         // print timestamp and output line to the log file
-        this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' <<
-                       (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' <<
-                       (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl;
+        this->file_ << buffer << ' ' << line << std::endl;
     }
 }

Modified: code/branches/cpp11_v2/test/util/output/LogWriterTest.cc
===================================================================
--- code/branches/cpp11_v2/test/util/output/LogWriterTest.cc	2015-12-28 22:22:21 UTC (rev 10988)
+++ code/branches/cpp11_v2/test/util/output/LogWriterTest.cc	2015-12-29 10:35:49 UTC (rev 10989)
@@ -136,6 +136,8 @@
 
         std::string line = getLineWhichContains(path, "myothertestoutput");
         EXPECT_FALSE(line.empty());
+        ASSERT_TRUE(line.length() > 12);
+
         EXPECT_TRUE(isdigit(line[0]));
         EXPECT_TRUE(isdigit(line[1]));
         EXPECT_EQ(':', line[2]);
@@ -144,7 +146,11 @@
         EXPECT_EQ(':', line[5]);
         EXPECT_TRUE(isdigit(line[6]));
         EXPECT_TRUE(isdigit(line[7]));
-        EXPECT_EQ(' ', line[8]);
+        EXPECT_EQ(':', line[8]);
+        EXPECT_TRUE(isdigit(line[9]));
+        EXPECT_TRUE(isdigit(line[10]));
+        EXPECT_TRUE(isdigit(line[11]));
+        EXPECT_EQ(' ', line[12]);
     }
 
     void deleteAllLogFiles()




More information about the Orxonox-commit mailing list