-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator_example.cpp
More file actions
57 lines (49 loc) · 1.87 KB
/
Copy pathdecorator_example.cpp
File metadata and controls
57 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "streamlog.hpp"
#include <iostream>
/**
* Custom logger demonstrating inheritance and override capabilities.
*
* This example shows how to create a custom logger by inheriting from StreamLog
* and overriding virtual methods to customize timestamp and log formatting.
*
* Note: This creates a direct instance (not a singleton). The base StreamLog
* singleton is still available via log() for general use.
*/
class CustomLogger : public StreamLog {
public:
CustomLogger() : StreamLog("custom_output.log", true) {}
~CustomLogger() {}
// Rule of Five - delete copy and move operations
CustomLogger(const CustomLogger&) = delete;
CustomLogger(CustomLogger&&) = delete;
CustomLogger& operator=(const CustomLogger&) = delete;
CustomLogger& operator=(CustomLogger&&) = delete;
/**
* Override timestamp to use a custom format instead of Unix epoch.
*/
std::string getTimestamp() const override {
return "[NOW]";
}
/**
* Override log formatting to use custom decorators.
*/
std::stringstream buildLog(const std::string& message) const override {
std::stringstream decorated_stream;
decorated_stream << getTimestamp() << " ";
decorated_stream << getColor();
decorated_stream << "{{" << levelToString(m_level) << "}} ";
decorated_stream << StreamColor::reset;
decorated_stream << message << std::endl;
return decorated_stream;
}
};
int main() {
// Create a custom logger instance (not using singleton pattern)
CustomLogger custom_logger;
// Use the custom logger
custom_logger.getLogStatement(ERROR) << "ERROR FROM CUSTOM LOGGER";
custom_logger.getLogStatement(INFO) << "Info message with custom formatting";
// The base singleton is still available for general logging
log(WARN) << "This uses the default singleton logger";
return 0;
}