Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10989


Ignore:
Timestamp:
Dec 29, 2015, 11:35:49 AM (8 years ago)
Author:
landauf
Message:

using the c++11 chrono library to add milliseconds to the timestamp in the logfile.

Location:
code/branches/cpp11_v2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/src/libraries/util/output/LogWriter.cc

    r9550 r10989  
    3535
    3636#include <ctime>
     37#include <chrono>
    3738#include <cstdlib>
    3839
     
    180181            return;
    181182
     183        // get the milliseconds
     184        std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
     185        std::chrono::system_clock::duration timeSinceEpoch = now.time_since_epoch();
     186        std::chrono::milliseconds millisSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(timeSinceEpoch);
     187        unsigned int millis = (millisSinceEpoch.count() % 1000);
     188
    182189        // get the current time
    183         time_t rawtime;
    184         struct tm* timeinfo;
    185         time(&rawtime);
    186         timeinfo = localtime(&rawtime);
     190        time_t rawtime = std::chrono::system_clock::to_time_t(now);
     191        struct tm* timeinfo = localtime(&rawtime);
     192
     193        // format time: hh:mm:ss:xxx
     194        char buffer[13];
     195        snprintf(buffer, sizeof(buffer), "%.2i:%.2i:%.2i:%.3i", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, millis);
    187196
    188197        // print timestamp and output line to the log file
    189         this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' <<
    190                        (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' <<
    191                        (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl;
     198        this->file_ << buffer << ' ' << line << std::endl;
    192199    }
    193200}
  • code/branches/cpp11_v2/test/util/output/LogWriterTest.cc

    r10846 r10989  
    137137        std::string line = getLineWhichContains(path, "myothertestoutput");
    138138        EXPECT_FALSE(line.empty());
     139        ASSERT_TRUE(line.length() > 12);
     140
    139141        EXPECT_TRUE(isdigit(line[0]));
    140142        EXPECT_TRUE(isdigit(line[1]));
     
    145147        EXPECT_TRUE(isdigit(line[6]));
    146148        EXPECT_TRUE(isdigit(line[7]));
    147         EXPECT_EQ(' ', line[8]);
     149        EXPECT_EQ(':', line[8]);
     150        EXPECT_TRUE(isdigit(line[9]));
     151        EXPECT_TRUE(isdigit(line[10]));
     152        EXPECT_TRUE(isdigit(line[11]));
     153        EXPECT_EQ(' ', line[12]);
    148154    }
    149155
Note: See TracChangeset for help on using the changeset viewer.