Skip to content
Snippets Groups Projects
Commit e2de6234 authored by Florian Schmaus's avatar Florian Schmaus
Browse files

Add option to include timestamp in EMPER log messages

This also changes emper_log so that a std::ostringstream is used to
assemble the log message.
parent 56ae984e
No related branches found
No related tags found
No related merge requests found
Pipeline #55777 passed
......@@ -109,6 +109,10 @@ variables:
variables:
EMPER_WORKER_WAKEUP_STRATEGY: "all"
.do-not-log-timestamp:
variables:
EMPER_LOG_TIMESTAMP: "false"
test-gcc:
extends:
- .test
......@@ -184,3 +188,8 @@ test-worker-wakeup-strategy-all:
extends:
- .test
- .worker-wakeup-strategy-all
test-do-not-log-timestamp:
extends:
- .test
- .do-not-log-timestamp
......@@ -2,36 +2,60 @@
// Copyright © 2020 Florian Schmaus
#include "Debug.hpp"
#include <chrono>
#include <ctime> // for localtime
#include <iomanip> // for operator<<, setfill, setw
#include <iostream> // for operator<<, cerr, ostream, basic_ostream
#include <mutex> // for mutex, unique_lock
#include "Common.hpp"
#include "Emper.hpp"
#include "Worker.hpp" // for Runtime
#include "emper-common.h" // for workerid_t
#include "emper-config.h" // for EMPER_LOG_LEVEL
static std::mutex emper_log_mutex;
void emper_add_timestamp_to(std::ostringstream& logMessage) {
auto now = std::chrono::high_resolution_clock::now();
auto now_time_t = std::chrono::high_resolution_clock::to_time_t(now);
auto* now_localtime = std::localtime(&now_time_t);
logMessage << std::put_time(now_localtime, "%H%M.");
auto now_nanos = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
auto time_since_epoch = now_nanos.time_since_epoch();
long time_since_epoch_long = time_since_epoch.count();
long remaining_nanos = time_since_epoch_long % 1000000000;
logMessage << remaining_nanos;
}
void emper_log(const std::string& prefix, const std::string& message) {
Worker* worker = Worker::getCurrentWorker();
std::ostringstream workerPrefix;
std::ostringstream logMessage;
if (likely(worker)) {
workerid_t workerId = worker->getWorkerId();
std::string workerIdAsString = std::to_string(workerId);
workerPrefix << std::setfill('0') << std::setw(3) << workerIdAsString << " ";
logMessage << std::setfill('0') << std::setw(3) << workerIdAsString << " ";
} else {
workerPrefix << " ";
logMessage << " ";
}
if constexpr (emper::LOG_TIMESTAMP) {
emper_add_timestamp_to(logMessage);
}
std::unique_lock<std::mutex> lock(emper_log_mutex);
std::cerr << workerPrefix.str();
if (!prefix.empty()) {
std::cerr << " " << prefix << " ";
logMessage << " " << prefix << " ";
} else {
std::cerr << " ";
logMessage << " ";
}
std::cerr << message << std::endl;
logMessage << message;
std::unique_lock<std::mutex> lock(emper_log_mutex);
std::cerr << logMessage.str() << std::endl;
}
// global log_level which can be changed to control the log output
......
......@@ -82,6 +82,8 @@ namespace emper {
extern enum LogLevel log_level;
}
void emper_add_timestamp_to(std::ostringstream& logMessage);
void emper_log(const std::string& prefix, const std::string& message);
template <LogSubsystem logSubsystem>
......
......@@ -48,5 +48,13 @@ static const bool DEBUG =
#endif
;
static const bool LOG_TIMESTAMP =
#ifdef EMPER_LOG_TIMESTAMP
true
#else
false
#endif
;
auto getFullVersion() -> std::string;
} // namespace emper
......@@ -2,4 +2,5 @@
{ include: ["<bits/getopt_core.h>", "private", "<unistd.h>", "public"] },
{ include: ["@<gtest/.*>", "private", "<gtest/gtest.h>", "public"] },
{ include: ["<urcu/map/urcu-memb.h>", "private", "<urcu.h>", "public"] },
{ include: ["<bits/cxxabi_forced.h>", "private", "<ctime>", "public" ] },
]
......@@ -44,6 +44,7 @@ elif log_level == 'OFF'
conf_data.set('EMPER_LOG_OFF', true)
endif
conf_data.set('EMPER_LOG_LEVEL', log_level)
conf_data.set('EMPER_LOG_TIMESTAMP', get_option('log_timestamp'))
subdir('emper')
subdir('tests')
......
......@@ -10,6 +10,12 @@ option(
choices: ['automatic', 'OFF', 'Error', 'Warning', 'Info', 'Debug', 'FineDebug', 'FinerDebug', 'FinestDebug', 'ALL'],
value: 'automatic',
)
option(
'log_timestamp',
type: 'boolean',
value: true,
description: 'Add a timestamp to the EMPER log messages',
)
option(
'worker_sleep',
type: 'boolean',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment