From 106664ec4a3cead9d4ef9eaf9b6c247bf7d6e41b Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Thu, 7 Apr 2022 01:02:06 +0200 Subject: [PATCH] Setting up the logger --- include/log.h | 6 +++++- src/log.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/include/log.h b/include/log.h index c3d18a2..6c95166 100644 --- a/include/log.h +++ b/include/log.h @@ -1,9 +1,10 @@ #pragma once // std +#include #include -#include #include +#include namespace osdev { namespace components { @@ -213,6 +214,9 @@ private: //! The amount of logging static LogLevel s_logLevel; + + //! Mutex to keep the logger thread-safe + static std::mutex m_mutex; }; } /* End namespace mqtt */ diff --git a/src/log.cpp b/src/log.cpp index 0bb4d0a..ca78b3f 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,10 +1,10 @@ #include "log.h" - #include "threadcontext.h" // std #include #include +#include #include #include #include @@ -60,6 +60,8 @@ void Log::terminate() void Log::log( const std::string &category, const std::string &message, LogLevel level ) { + std::lock_guard lock(m_mutex); + std::string logCategory = s_context + '|' + toString( level ) + '|' + ThreadContext::instance().context() + '|' + category; std::string logMessage = message; if( logMessage.empty() ) @@ -76,6 +78,13 @@ void Log::log( const std::string &category, const std::string &message, LogLevel writeLog( logCategory, logMessage, level ); } +std::string Log::fileinfoMessage( const char *file, int line, const std::string &message ) +{ + static const std::string templ("%1:%2| %3"); + QFileInfo fi(file); + return templ.arg( fi.fileName() ).arg(line).arg(message); +} + void Log::writeLog(const std::string &category, const std::string &message, LogLevel level) { if( s_fileName.empty() ) @@ -95,6 +104,56 @@ void Log::writeLog(const std::string &category, const std::string &message, LogL } } +void Log::trace(const std::string &category, const std::string &message) +{ + log(category, message, LogLevel::Trace); +} + +void Log::debug(const std::string& category, const std::string& message) +{ + log( category, message, LogLevel::Debug ); +} + +void Log::info(const std::string& category, const std::string& message) +{ + log( category, message, LogLevel::Info ); +} + +void Log::warning(const std::string& category, const std::string& message) +{ + log(category, message, LogLevel::Warning ); +} + +void Log::error(const std::string& category, const std::string& message) +{ + log(category, message, LogLevel::Error ); +} + +void Log::trace(const char *file, int line, const std::string &category, const std::string &message) +{ + log(category, fileinfoMessage(file, line, message), LogLevel::Trace ); +} + +void Log::debug(const char* file, int line, const std::string& category, const std::string& message) +{ + log( category, fileinfoMessage(file, line, message), LogLevel::Debug ); +} + +void Log::info(const char* file, int line, const std::string& category, const std::string& message) +{ + log( category, fileinfoMessage(file, line, message), LogLevel::Info ); +} + +void Log::warning(const char* file, int line, const std::string& category, const std::string& message) +{ + log( category, fileinfoMessage(file, line, message), LogLevel::Warning); +} + +void Log::error(const char* file, int line, const std::string& category, const std::string& message) +{ + log( category, fileinfoMessage(file, line, message), LogLevel::Error ); +} + void Log::ReplaceAll( std::string &strToReplace, const std::string& from_chars, const std::string& to_chars ) -- libgit2 0.21.4