logger: made loggers thread safe

Dieser Commit ist enthalten in:
Reinder Feenstra 2022-05-22 11:17:59 +02:00
Ursprung 644e95385e
Commit 95b4d88693
6 geänderte Dateien mit 40 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2021 Reinder Feenstra
* Copyright (C) 2021-2022 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -39,6 +39,8 @@ void ConsoleLogger::write(const std::chrono::system_clock::time_point& time, std
const auto tm = std::chrono::system_clock::to_time_t(time);
const auto us = std::chrono::duration_cast<std::chrono::microseconds>(time.time_since_epoch()) % 1000000;
std::lock_guard<std::mutex> lock(m_streamMutex);
std::ostream& ss = (isErrorLogMessage(code) || isCriticalLogMessage(code) || isFatalLogMessage(code)) ? std::cerr : std::cout;
ss
<< std::put_time(std::localtime(&tm), "%F %T") << '.' << std::setfill('0') << std::setw(6) << us.count() << ' '

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2021 Reinder Feenstra
* Copyright (C) 2021-2022 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -24,11 +24,14 @@
#define TRAINTASTIC_SERVER_LOG_CONSOLELOGGER_HPP
#include "logger.hpp"
#include <mutex>
class ConsoleLogger : public Logger
{
private:
static void write(const std::chrono::system_clock::time_point& time, std::string_view objectId, LogMessage code, std::string_view message);
std::mutex m_streamMutex;
void write(const std::chrono::system_clock::time_point& time, std::string_view objectId, LogMessage code, std::string_view message);
public:
ConsoleLogger() = default;

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2021 Reinder Feenstra
* Copyright (C) 2021-2022 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -52,6 +52,8 @@ void FileLogger::write(const std::chrono::system_clock::time_point& time, std::s
const auto tm = std::chrono::system_clock::to_time_t(time);
const auto us = std::chrono::duration_cast<std::chrono::microseconds>(time.time_since_epoch()) % 1000000;
std::lock_guard<std::mutex> lock(m_fileMutex);
m_file
<< std::put_time(std::localtime(&tm), "%F;%T") << '.' << std::setfill('0') << std::setw(6) << us.count() << ';'
<< objectId << ';'

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2021 Reinder Feenstra
* Copyright (C) 2021-2022 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -24,6 +24,7 @@
#define TRAINTASTIC_SERVER_LOG_FILELOGGER_HPP
#include "logger.hpp"
#include <mutex>
#include <fstream>
#include <traintastic/utils/stdfilesystem.hpp>
@ -31,6 +32,7 @@ class FileLogger : public Logger
{
private:
std::ofstream m_file;
std::mutex m_fileMutex;
void write(const std::chrono::system_clock::time_point& time, std::string_view objectId, LogMessage code, std::string_view message);

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2021 Reinder Feenstra
* Copyright (C) 2021-2022 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -21,6 +21,7 @@
*/
#include "memorylogger.hpp"
#include "../core/eventloop.hpp"
MemoryLogger::MemoryLogger(uint32_t sizeMax)
: m_sizeMax{sizeMax}
@ -29,14 +30,29 @@ MemoryLogger::MemoryLogger(uint32_t sizeMax)
void MemoryLogger::log(const std::chrono::system_clock::time_point& time, std::string_view objectId, LogMessage message)
{
m_logs.emplace_back(time, objectId, message);
changed(*this, 1, cleanUp());
add(time, std::string{objectId}, message, nullptr);
}
void MemoryLogger::log(const std::chrono::system_clock::time_point& time, std::string_view objectId, LogMessage message, const std::vector<std::string>& args)
{
m_logs.emplace_back(time, objectId, message, new std::vector<std::string>(args));
changed(*this, 1, cleanUp());
add(time, std::string{objectId}, message, new std::vector<std::string>(args));
}
void MemoryLogger::add(std::chrono::system_clock::time_point time, std::string objectId, LogMessage message, std::vector<std::string>* args)
{
if(isEventLoopThread())
{
m_logs.emplace_back(std::move(time), std::move(objectId), message, args);
changed(*this, 1, cleanUp());
}
else
{
EventLoop::call(
[this, time=std::move(time), objectId=std::move(objectId), message, args]()
{
add(std::move(time), std::move(objectId), message, args);
});
}
}
uint32_t MemoryLogger::cleanUp()

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2021 Reinder Feenstra
* Copyright (C) 2021-2022 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -38,9 +38,9 @@ class MemoryLogger : public Logger
LogMessage message;
std::vector<std::string>* args;
Log(std::chrono::system_clock::time_point _time, std::string_view _objectId, LogMessage _message, std::vector<std::string>* _args = nullptr)
: time{_time}
, objectId{_objectId}
Log(std::chrono::system_clock::time_point _time, std::string _objectId, LogMessage _message, std::vector<std::string>* _args = nullptr)
: time{std::move(_time)}
, objectId{std::move(_objectId)}
, message{_message}
, args{_args}
{
@ -51,6 +51,7 @@ class MemoryLogger : public Logger
std::vector<Log> m_logs;
size_t m_sizeMax;
void add(std::chrono::system_clock::time_point time, std::string objectId, LogMessage message, std::vector<std::string>* args);
uint32_t cleanUp();
public: