Skip to content

Commit 25a0866

Browse files
committed
Adds two versions of app logger class - with and without channels.
1 parent 895b17b commit 25a0866

File tree

4 files changed

+615
-0
lines changed

4 files changed

+615
-0
lines changed

include/applogger/applogger.hxx

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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_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+
#include <iostream>
18+
#include <string>
19+
20+
namespace logging = boost::log;
21+
namespace sinks = boost::log::sinks;
22+
namespace expr = boost::log::expressions;
23+
namespace attrs = boost::log::attributes;
24+
namespace keywords = boost::log::keywords;
25+
26+
class AppLogger
27+
{
28+
public:
29+
// Define severity levels
30+
enum class Severity {
31+
Debug,
32+
Info,
33+
Warning,
34+
Error,
35+
Critical
36+
};
37+
38+
// Singleton instance getter
39+
static AppLogger& getInstance();
40+
41+
// Initialize the AppLogger with default settings
42+
void init();
43+
44+
// Add a custom file sink with specific severity filter and format
45+
void addFileSink(
46+
const std::string& filename,
47+
const Severity minSeverity,
48+
const std::string& format
49+
);
50+
51+
// Log a message with specified severity
52+
template<typename T>
53+
void log(const Severity severity, const T& message) {
54+
auto& slg = getLogger();
55+
BOOST_LOG_SEV(slg, severity) << message;
56+
}
57+
58+
// Convenience methods for different severity levels
59+
template<typename T> void debug(const T& message) { log(Severity::Debug, message); }
60+
template<typename T> void info(const T& message) { log(Severity::Info, message); }
61+
template<typename T> void warning(const T& message) { log(Severity::Warning, message); }
62+
template<typename T> void error(const T& message) { log(Severity::Error, message); }
63+
template<typename T> void critical(const T& message) { log(Severity::Critical, message); }
64+
65+
private:
66+
AppLogger() = default;
67+
~AppLogger() = default;
68+
AppLogger(const AppLogger&) = delete;
69+
AppLogger& operator=(const AppLogger&) = delete;
70+
71+
// Initialize console sink with custom format
72+
void initConsoleSink();
73+
74+
// Initialize default file sink
75+
void initDefaultFileSink();
76+
77+
// Get the severity AppLogger instance
78+
logging::sources::severity_logger<Severity>& getLogger();
79+
};
80+
81+
// Severity level to string conversion
82+
template<typename CharT, typename TraitsT>
83+
std::basic_ostream<CharT, TraitsT>& operator<<(
84+
std::basic_ostream<CharT, TraitsT>& strm,
85+
AppLogger::Severity lvl)
86+
{
87+
static const char* const str[] = {
88+
"Debug",
89+
"Info",
90+
"Warning",
91+
"Error",
92+
"Critical"
93+
};
94+
95+
if (static_cast<std::size_t>(lvl) < sizeof(str) / sizeof(*str))
96+
strm << str[static_cast<std::size_t>(lvl)];
97+
else
98+
strm << static_cast<int>(lvl);
99+
100+
return strm;
101+
}
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)