Using <chrono> to measure time in the LogManager

`clock()`, which was used previously, behaves differently on various platform.
So switching to this lets us use a standard part of the language which
avoids this.

Signed-off-by: B Horn <b@horn.uk>
This commit is contained in:
B Horn 2021-07-20 21:34:42 +02:00 committed by Adam Honse
parent d9de79abfe
commit a1dcf7c1cf
2 changed files with 7 additions and 7 deletions

View file

@ -3,7 +3,7 @@
#include <stdarg.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include "ResourceManager.h"
@ -13,7 +13,7 @@ static const char* log_codes[] = {"FATAL:", "ERROR:", "Warning:", "Info:", "[ver
LogManager::LogManager()
{
base_clock = clock();
base_clock = std::chrono::steady_clock::now();
}
LogManager* LogManager::get()
@ -136,8 +136,8 @@ void LogManager::_flush()
if(temp_messages[msg]->level <= loglevel)
{
// Put the timestamp here
clock_t counter = temp_messages[msg]->counted_second;
log_stream << std::left << std::setw(6) << counter/ ( CLOCKS_PER_SEC / 1000 ) << "|";
std::chrono::milliseconds counter = std::chrono::duration_cast<std::chrono::milliseconds>(temp_messages[msg]->counted_second);
log_stream << std::left << std::setw(6) << counter.count() << "|";
log_stream << std::left << std::setw(9) << log_codes[temp_messages[msg]->level];
log_stream << temp_messages[msg]->buffer;
@ -202,7 +202,7 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con
mes->level = level;
mes->filename = filename;
mes->line = line;
mes->counted_second = clock() - base_clock;
mes->counted_second = std::chrono::steady_clock::now() - base_clock;
/*-------------------------------------------------*\
| If the message is within the current verbosity, |

View file

@ -27,7 +27,7 @@ struct LogMessage
unsigned int level;
const char* filename;
int line;
clock_t counted_second;
std::chrono::duration<double> counted_second;
// int timestamp or float time_offset? TBD
};
typedef std::shared_ptr<LogMessage> PLogMessage;
@ -60,7 +60,7 @@ private:
unsigned int verbosity = LL_WARNING;
//Clock from LogManager creation
clock_t base_clock;
std::chrono::time_point<std::chrono::steady_clock> base_clock;
// A non-guarded append()
void _append(const char* filename, int line, unsigned int level, const char* fmt, va_list va);