diff --git a/server/src/log/consolelogger.cpp b/server/src/log/consolelogger.cpp index 97e583bf..81c2ebe9 100644 --- a/server/src/log/consolelogger.cpp +++ b/server/src/log/consolelogger.cpp @@ -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(time.time_since_epoch()) % 1000000; + std::lock_guard 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() << ' ' diff --git a/server/src/log/consolelogger.hpp b/server/src/log/consolelogger.hpp index a478d8bb..e8cd1f5f 100644 --- a/server/src/log/consolelogger.hpp +++ b/server/src/log/consolelogger.hpp @@ -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 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; diff --git a/server/src/log/filelogger.cpp b/server/src/log/filelogger.cpp index b751ffc8..b0cf9426 100644 --- a/server/src/log/filelogger.cpp +++ b/server/src/log/filelogger.cpp @@ -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(time.time_since_epoch()) % 1000000; + std::lock_guard lock(m_fileMutex); + m_file << std::put_time(std::localtime(&tm), "%F;%T") << '.' << std::setfill('0') << std::setw(6) << us.count() << ';' << objectId << ';' diff --git a/server/src/log/filelogger.hpp b/server/src/log/filelogger.hpp index 06c71670..a6cfba6c 100644 --- a/server/src/log/filelogger.hpp +++ b/server/src/log/filelogger.hpp @@ -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 #include #include @@ -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); diff --git a/server/src/log/memorylogger.cpp b/server/src/log/memorylogger.cpp index d1b31514..ada9c304 100644 --- a/server/src/log/memorylogger.cpp +++ b/server/src/log/memorylogger.cpp @@ -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& args) { - m_logs.emplace_back(time, objectId, message, new std::vector(args)); - changed(*this, 1, cleanUp()); + add(time, std::string{objectId}, message, new std::vector(args)); +} + +void MemoryLogger::add(std::chrono::system_clock::time_point time, std::string objectId, LogMessage message, std::vector* 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() diff --git a/server/src/log/memorylogger.hpp b/server/src/log/memorylogger.hpp index ad530b21..2e827e1c 100644 --- a/server/src/log/memorylogger.hpp +++ b/server/src/log/memorylogger.hpp @@ -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* args; - Log(std::chrono::system_clock::time_point _time, std::string_view _objectId, LogMessage _message, std::vector* _args = nullptr) - : time{_time} - , objectId{_objectId} + Log(std::chrono::system_clock::time_point _time, std::string _objectId, LogMessage _message, std::vector* _args = nullptr) + : time{std::move(_time)} + , objectId{std::move(_objectId)} , message{_message} , args{_args} { @@ -51,6 +51,7 @@ class MemoryLogger : public Logger std::vector m_logs; size_t m_sizeMax; + void add(std::chrono::system_clock::time_point time, std::string objectId, LogMessage message, std::vector* args); uint32_t cleanUp(); public: