add AdcOstreamLogger class (std::basic_stream based multithread-safe

simple logger)
This commit is contained in:
2024-11-12 18:07:00 +03:00
parent 9769c24005
commit 7251f95459
2 changed files with 97 additions and 2 deletions

View File

@@ -2,7 +2,9 @@
#include <algorithm>
#include <charconv>
#include <iostream>
#include <limits>
#include <mutex>
#include <ranges>
#include <regex>
#include <utility>
@@ -674,5 +676,85 @@ static constexpr size_t AdcFNV1aHash(const R& r)
return hash;
}
/* std::basic_ostream based multithread-safe simple logger */
template <typename CharT = char, typename CharTraitsT = std::char_traits<CharT>>
class AdcOstreamLogger
{
public:
typedef CharT char_t;
typedef CharTraitsT char_traits_t;
enum loglevel_t { NULL_LEVEL, ERROR_LEVEL, INFO_LEVEL, DEBUG_LEVEL };
static constexpr std::array logLevelMark{"null", "error", "info", "debug"};
AdcOstreamLogger(std::basic_ostream<CharT, CharTraitsT>& stream = std::cout, loglevel_t log_level = INFO_LEVEL)
: _logStream(stream), _currentLogLevel(log_level)
{
}
AdcOstreamLogger(loglevel_t log_level) : _logStream(std::cout), _currentLogLevel(log_level) {}
virtual ~AdcOstreamLogger() = default;
void setLogLevel(loglevel_t log_level)
{
std::lock_guard<std::mutex> lock(_logMutex);
_currentLogLevel = log_level;
}
loglevel_t getLogLevel() const
{
return _currentLogLevel;
}
template <traits::formattable... Ts>
void logMessage(loglevel_t level, std::string_view fmt, Ts&&... args)
{
std::lock_guard<std::mutex> lock(_logMutex);
if (_currentLogLevel < level)
return;
std::string s;
std::format_to(std::back_inserter(s), fmt, std::forward<Ts>(args)...);
const std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
// format log-message in form:
// [YYYY-MM-DD HH:MM:SS][level] log-message
//
_logStream << std::put_time(std::localtime(&now), "[%F %T]") << "[" << logLevelMark[_currentLogLevel] << "] "
<< s;
}
template <traits::formattable... Ts>
void logError(std::string_view fmt, Ts&&... args)
{
logMessage(ERROR_LEVEL, fmt, std::forward<Ts>(args)...);
}
template <traits::formattable... Ts>
void logInfo(std::string_view fmt, Ts&&... args)
{
logMessage(INFO_LEVEL, fmt, std::forward<Ts>(args)...);
}
template <traits::formattable... Ts>
void logDebug(std::string_view fmt, Ts&&... args)
{
logMessage(DEBUG_LEVEL, fmt, std::forward<Ts>(args)...);
}
protected:
std::basic_ostream<CharT, CharTraitsT>& _logStream;
loglevel_t _currentLogLevel;
std::mutex _logMutex;
};
} // namespace adc::utils