fixed windows timestamp (#200)

* fixed windows timestamp

* ficing linux time

* Fix to_system_time

* fix last review issue.

* fixed formatting.

* Comment added to to_system_time() functiuon.:wq
This commit is contained in:
Andreas Schönle 2017-06-04 15:47:29 +02:00 committed by Kjell Hedström
parent 82df2168aa
commit f42611d2a1
3 changed files with 23 additions and 4 deletions

View File

@ -87,7 +87,7 @@ namespace g3 {
// Complete access to the raw data in case the helper functions above
// are not enough.
//
g3::system_time_point _timestamp;
g3::high_resolution_time_point _timestamp;
std::thread::id _call_thread_id;
std::string _file;
std::string _file_path;

View File

@ -22,7 +22,8 @@
// std::string put_time(const struct tm* tmb, const char* c_time_format)
namespace g3 {
typedef std::chrono::time_point<std::chrono::system_clock> system_time_point;
typedef std::chrono::time_point<std::chrono::system_clock> system_time_point;
typedef std::chrono::time_point<std::chrono::high_resolution_clock> high_resolution_time_point;
typedef std::chrono::milliseconds milliseconds;
typedef std::chrono::microseconds microseconds;
@ -55,6 +56,24 @@ namespace g3 {
* std::put_time. A possible fix if your c++11 library is not updated is to
* modify this to use std::strftime instead */
std::string localtime_formatted(const system_time_point& ts, const std::string& time_format) ;
inline system_time_point to_system_time(const high_resolution_time_point& ts)
{
// On some (windows) systems, the system_clock does not provide the highest possible time
// resolution. Thus g3log uses high_resolution_clock for message time stamps. However,
// unlike system_clock, high_resolution_clock cannot be converted to a time and date as
// it usually measures reflects the time since power-up.
// Thus, hrs_now and sys_now are recorded once when the program starts to be able to convert
// timestamps to dime and date using to_system_time(). The precision of the absolute time is
// of course that of system_clock() with some error added due to the non-simultaneous initialization
// of the two static variables but relative times within one log will be as precise as
// high_resolution_clock.
using namespace std::chrono;
static const auto hrs_now = high_resolution_clock::now();
static const auto sys_now = system_clock::now();
return time_point_cast<system_clock::duration>(sys_now + (ts - hrs_now));
}
}

View File

@ -111,7 +111,7 @@ namespace g3 {
std::string LogMessage::timestamp(const std::string& time_look) const {
return g3::localtime_formatted(_timestamp, time_look);
return g3::localtime_formatted(to_system_time(_timestamp), time_look);
}
@ -125,7 +125,7 @@ namespace g3 {
LogMessage::LogMessage(const std::string& file, const int line,
const std::string& function, const LEVELS& level)
: _timestamp(std::chrono::system_clock::now())
: _timestamp(std::chrono::high_resolution_clock::now())
, _call_thread_id(std::this_thread::get_id())
, _file(splitFileName(file))
, _file_path(file)