|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <boost/log/core.hpp> |
| 4 | +#include <boost/log/trivial.hpp> |
| 5 | +#include <boost/log/expressions.hpp> |
| 6 | +#include <boost/log/sinks/text_file_backend.hpp> |
| 7 | +#include <boost/log/sinks/text_ostream_backend.hpp> |
| 8 | +#include <boost/log/sources/severity_channel_logger.hpp> |
| 9 | +#include <boost/log/sources/record_ostream.hpp> |
| 10 | +#include <boost/log/utility/setup/file.hpp> |
| 11 | +#include <boost/log/utility/setup/console.hpp> |
| 12 | +#include <boost/log/utility/setup/common_attributes.hpp> |
| 13 | +#include <boost/log/support/date_time.hpp> |
| 14 | +#include <boost/core/null_deleter.hpp> |
| 15 | +#include <boost/shared_ptr.hpp> |
| 16 | +#include <boost/make_shared.hpp> |
| 17 | + |
| 18 | +#include <iostream> |
| 19 | +#include <map> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +namespace logging = boost::log; |
| 23 | +namespace sinks = boost::log::sinks; |
| 24 | +namespace expr = boost::log::expressions; |
| 25 | +namespace attrs = boost::log::attributes; |
| 26 | +namespace keywords = boost::log::keywords; |
| 27 | + |
| 28 | +class AppLoggerWithChannels |
| 29 | +{ |
| 30 | +public: |
| 31 | + // Define severity levels |
| 32 | + enum class Severity |
| 33 | + { |
| 34 | + Debug, |
| 35 | + Info, |
| 36 | + Warning, |
| 37 | + Error, |
| 38 | + Critical |
| 39 | + }; |
| 40 | + |
| 41 | + // Singleton instance getter |
| 42 | + static AppLoggerWithChannels& getInstance(); |
| 43 | + |
| 44 | + // Initialize the AppLoggerWithChannels with default settings |
| 45 | + void init(); |
| 46 | + |
| 47 | + void addChannelSink_working_format( |
| 48 | + const std::string& channel, |
| 49 | + const std::string& filename, |
| 50 | + const Severity minSeverity, |
| 51 | + const std::string& format |
| 52 | + ); |
| 53 | + |
| 54 | + // Add a channel-specific sink with custom filter and format |
| 55 | + void addChannelSink( |
| 56 | + const std::string& channel, |
| 57 | + const std::string& filename, |
| 58 | + const Severity minSeverity, |
| 59 | + const std::string& format |
| 60 | + ); |
| 61 | + |
| 62 | + // Log a message to a specific channel with specified severity |
| 63 | + template<typename T> |
| 64 | + void logToChannel(const std::string& channel, const Severity severity, const T& message) |
| 65 | + { |
| 66 | + auto& slg = getChannelLogger(channel); |
| 67 | + BOOST_LOG_SEV(slg, severity) << message; |
| 68 | + } |
| 69 | + |
| 70 | + // Convenience methods for different severity levels with channel specification |
| 71 | + template<typename T> |
| 72 | + void debug(const std::string& channel, const T& message) |
| 73 | + { |
| 74 | + logToChannel(channel, Severity::Debug, message); |
| 75 | + } |
| 76 | + |
| 77 | + template<typename T> |
| 78 | + void info(const std::string& channel, const T& message) |
| 79 | + { |
| 80 | + logToChannel(channel, Severity::Info, message); |
| 81 | + } |
| 82 | + |
| 83 | + template<typename T> |
| 84 | + void warning(const std::string& channel, const T& message) |
| 85 | + { |
| 86 | + logToChannel(channel, Severity::Warning, message); |
| 87 | + } |
| 88 | + |
| 89 | + template<typename T> |
| 90 | + void error(const std::string& channel, const T& message) |
| 91 | + { |
| 92 | + logToChannel(channel, Severity::Error, message); |
| 93 | + } |
| 94 | + |
| 95 | + template<typename T> |
| 96 | + void critical(const std::string& channel, const T& message) |
| 97 | + { |
| 98 | + logToChannel(channel, Severity::Critical, message); |
| 99 | + } |
| 100 | + |
| 101 | +private: |
| 102 | + AppLoggerWithChannels() = default; |
| 103 | + ~AppLoggerWithChannels() = default; |
| 104 | + AppLoggerWithChannels(const AppLoggerWithChannels&) = delete; |
| 105 | + AppLoggerWithChannels& operator=(const AppLoggerWithChannels&) = delete; |
| 106 | + |
| 107 | + // Store channel-specific sinks |
| 108 | + std::map<std::string, boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>>> channelSinks; |
| 109 | + |
| 110 | + // Store channel-specific loggers |
| 111 | + std::map<std::string, boost::shared_ptr<logging::sources::severity_channel_logger<Severity, std::string>>> channelLoggers; |
| 112 | + |
| 113 | + // Initialize console sink with custom format |
| 114 | + void initConsoleSink(); |
| 115 | + |
| 116 | + // Get or create a channel-specific logger |
| 117 | + logging::sources::severity_channel_logger<Severity, std::string>& getChannelLogger( |
| 118 | + const std::string& channel |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +// Severity level to string conversion |
| 123 | +template<typename CharT, typename TraitsT> |
| 124 | +std::basic_ostream<CharT, TraitsT>& operator<<( |
| 125 | + std::basic_ostream<CharT, TraitsT>& strm, |
| 126 | + AppLoggerWithChannels::Severity lvl) |
| 127 | +{ |
| 128 | + static const char* const str[] = |
| 129 | + { |
| 130 | + "Debug", |
| 131 | + "Info", |
| 132 | + "Warning", |
| 133 | + "Error", |
| 134 | + "Critical" |
| 135 | + }; |
| 136 | + |
| 137 | + if (static_cast<std::size_t>(lvl) < sizeof(str) / sizeof(*str)) |
| 138 | + strm << str[static_cast<std::size_t>(lvl)]; |
| 139 | + else |
| 140 | + strm << static_cast<int>(lvl); |
| 141 | + |
| 142 | + return strm; |
| 143 | +} |
0 commit comments