Commit 106664ec4a3cead9d4ef9eaf9b6c247bf7d6e41b
1 parent
aa0bd60b
Setting up the logger
Showing
2 changed files
with
65 additions
and
2 deletions
include/log.h
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | // std | 3 | // std |
| 4 | +#include <mutex> | ||
| 4 | #include <syslog.h> | 5 | #include <syslog.h> |
| 5 | -#include <unistd.h> | ||
| 6 | #include <string> | 6 | #include <string> |
| 7 | +#include <unistd.h> | ||
| 7 | 8 | ||
| 8 | namespace osdev { | 9 | namespace osdev { |
| 9 | namespace components { | 10 | namespace components { |
| @@ -213,6 +214,9 @@ private: | @@ -213,6 +214,9 @@ private: | ||
| 213 | 214 | ||
| 214 | //! The amount of logging | 215 | //! The amount of logging |
| 215 | static LogLevel s_logLevel; | 216 | static LogLevel s_logLevel; |
| 217 | + | ||
| 218 | + //! Mutex to keep the logger thread-safe | ||
| 219 | + static std::mutex m_mutex; | ||
| 216 | }; | 220 | }; |
| 217 | 221 | ||
| 218 | } /* End namespace mqtt */ | 222 | } /* End namespace mqtt */ |
src/log.cpp
| 1 | #include "log.h" | 1 | #include "log.h" |
| 2 | - | ||
| 3 | #include "threadcontext.h" | 2 | #include "threadcontext.h" |
| 4 | 3 | ||
| 5 | // std | 4 | // std |
| 6 | #include <ios> | 5 | #include <ios> |
| 7 | #include <iomanip> | 6 | #include <iomanip> |
| 7 | +#include <lockguard.h> | ||
| 8 | #include <sstream> | 8 | #include <sstream> |
| 9 | #include <syslog.h> | 9 | #include <syslog.h> |
| 10 | #include <sys/types.h> | 10 | #include <sys/types.h> |
| @@ -60,6 +60,8 @@ void Log::terminate() | @@ -60,6 +60,8 @@ void Log::terminate() | ||
| 60 | 60 | ||
| 61 | void Log::log( const std::string &category, const std::string &message, LogLevel level ) | 61 | void Log::log( const std::string &category, const std::string &message, LogLevel level ) |
| 62 | { | 62 | { |
| 63 | + std::lock_guard<std::mutex> lock(m_mutex); | ||
| 64 | + | ||
| 63 | std::string logCategory = s_context + '|' + toString( level ) + '|' + ThreadContext::instance().context() + '|' + category; | 65 | std::string logCategory = s_context + '|' + toString( level ) + '|' + ThreadContext::instance().context() + '|' + category; |
| 64 | std::string logMessage = message; | 66 | std::string logMessage = message; |
| 65 | if( logMessage.empty() ) | 67 | if( logMessage.empty() ) |
| @@ -76,6 +78,13 @@ void Log::log( const std::string &category, const std::string &message, LogLevel | @@ -76,6 +78,13 @@ void Log::log( const std::string &category, const std::string &message, LogLevel | ||
| 76 | writeLog( logCategory, logMessage, level ); | 78 | writeLog( logCategory, logMessage, level ); |
| 77 | } | 79 | } |
| 78 | 80 | ||
| 81 | +std::string Log::fileinfoMessage( const char *file, int line, const std::string &message ) | ||
| 82 | +{ | ||
| 83 | + static const std::string templ("%1:%2| %3"); | ||
| 84 | + QFileInfo fi(file); | ||
| 85 | + return templ.arg( fi.fileName() ).arg(line).arg(message); | ||
| 86 | +} | ||
| 87 | + | ||
| 79 | void Log::writeLog(const std::string &category, const std::string &message, LogLevel level) | 88 | void Log::writeLog(const std::string &category, const std::string &message, LogLevel level) |
| 80 | { | 89 | { |
| 81 | if( s_fileName.empty() ) | 90 | if( s_fileName.empty() ) |
| @@ -95,6 +104,56 @@ void Log::writeLog(const std::string &category, const std::string &message, LogL | @@ -95,6 +104,56 @@ void Log::writeLog(const std::string &category, const std::string &message, LogL | ||
| 95 | } | 104 | } |
| 96 | } | 105 | } |
| 97 | 106 | ||
| 107 | +void Log::trace(const std::string &category, const std::string &message) | ||
| 108 | +{ | ||
| 109 | + log(category, message, LogLevel::Trace); | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +void Log::debug(const std::string& category, const std::string& message) | ||
| 113 | +{ | ||
| 114 | + log( category, message, LogLevel::Debug ); | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +void Log::info(const std::string& category, const std::string& message) | ||
| 118 | +{ | ||
| 119 | + log( category, message, LogLevel::Info ); | ||
| 120 | +} | ||
| 121 | + | ||
| 122 | +void Log::warning(const std::string& category, const std::string& message) | ||
| 123 | +{ | ||
| 124 | + log(category, message, LogLevel::Warning ); | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +void Log::error(const std::string& category, const std::string& message) | ||
| 128 | +{ | ||
| 129 | + log(category, message, LogLevel::Error ); | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +void Log::trace(const char *file, int line, const std::string &category, const std::string &message) | ||
| 133 | +{ | ||
| 134 | + log(category, fileinfoMessage(file, line, message), LogLevel::Trace ); | ||
| 135 | +} | ||
| 136 | + | ||
| 137 | +void Log::debug(const char* file, int line, const std::string& category, const std::string& message) | ||
| 138 | +{ | ||
| 139 | + log( category, fileinfoMessage(file, line, message), LogLevel::Debug ); | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +void Log::info(const char* file, int line, const std::string& category, const std::string& message) | ||
| 143 | +{ | ||
| 144 | + log( category, fileinfoMessage(file, line, message), LogLevel::Info ); | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +void Log::warning(const char* file, int line, const std::string& category, const std::string& message) | ||
| 148 | +{ | ||
| 149 | + log( category, fileinfoMessage(file, line, message), LogLevel::Warning); | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +void Log::error(const char* file, int line, const std::string& category, const std::string& message) | ||
| 153 | +{ | ||
| 154 | + log( category, fileinfoMessage(file, line, message), LogLevel::Error ); | ||
| 155 | +} | ||
| 156 | + | ||
| 98 | void Log::ReplaceAll( std::string &strToReplace, | 157 | void Log::ReplaceAll( std::string &strToReplace, |
| 99 | const std::string& from_chars, | 158 | const std::string& from_chars, |
| 100 | const std::string& to_chars ) | 159 | const std::string& to_chars ) |