Added thread info to logging

This commit is contained in:
Daniel Wolf 2016-06-27 20:24:59 +02:00
parent 75407dab54
commit f13449f810
2 changed files with 9 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#include "logging.h" #include "logging.h"
#include <tools.h> #include <tools.h>
#include <iostream> #include <iostream>
#include <atomic>
using namespace logging; using namespace logging;
using std::string; using std::string;
@ -41,6 +42,10 @@ Entry::Entry(Level level, const string& message) :
message(message) message(message)
{ {
time(&timestamp); time(&timestamp);
static std::atomic<int> lastThreadId = 0;
thread_local int threadCounter = ++lastThreadId;
this->threadCounter = threadCounter;
} }
string SimpleConsoleFormatter::format(const Entry& entry) { string SimpleConsoleFormatter::format(const Entry& entry) {
@ -48,7 +53,7 @@ string SimpleConsoleFormatter::format(const Entry& entry) {
} }
string SimpleFileFormatter::format(const Entry& entry) { string SimpleFileFormatter::format(const Entry& entry) {
return fmt::format("[{0}] {1}", formatTime(entry.timestamp, "%F %H:%M:%S"), consoleFormatter.format(entry)); return fmt::format("[{0}] {1} {2}", formatTime(entry.timestamp, "%F %H:%M:%S"), entry.threadCounter, consoleFormatter.format(entry));
} }
LevelFilter::LevelFilter(shared_ptr<Sink> innerSink, Level minLevel) : LevelFilter::LevelFilter(shared_ptr<Sink> innerSink, Level minLevel) :
@ -121,7 +126,8 @@ void logging::addSink(shared_ptr<Sink> sink) {
void logging::log(Level level, const string& message) { void logging::log(Level level, const string& message) {
lock_guard<std::mutex> lock(getLogMutex()); lock_guard<std::mutex> lock(getLogMutex());
const Entry entry = Entry(level, message);
for (auto& sink : getSinks()) { for (auto& sink : getSinks()) {
sink->receive(Entry(level, message)); sink->receive(entry);
} }
} }

View File

@ -35,6 +35,7 @@ namespace logging {
Entry(Level level, const std::string& message); Entry(Level level, const std::string& message);
time_t timestamp; time_t timestamp;
int threadCounter;
Level level; Level level;
std::string message; std::string message;
}; };