-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathChain.cpp
103 lines (76 loc) · 2.43 KB
/
Chain.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//compile using g++ Chain.cpp -Wc++11-extensions -std=c++11
#include <string>
#include <memory>
#include <iostream>
class AbstractLogger{
public:
const static int INFO = 1;
const static int DEBUG = 2;
const static int ERROR = 3;
void logMessage(int level,const std::string &message){
if(this->level <= level){
write(message);
}
if(nextLogger != nullptr){
nextLogger->logMessage(level,message);
}
}
void setNextLogger(std::shared_ptr<AbstractLogger> nextLogger){
this->nextLogger = nextLogger;
}
protected:
int level;
std::shared_ptr<AbstractLogger> nextLogger;
virtual void write(const std::string &message) = 0;
};
const int AbstractLogger::INFO;
const int AbstractLogger::DEBUG;
const int AbstractLogger::ERROR;
class ConsoleLogger:public AbstractLogger {
public:
ConsoleLogger(int level){
this->level = level;
}
protected:
void write(const std::string &message) override {
std::cout<<"Standard Console::Logger: "<<message<<std::endl;
}
};
class ErrorLogger:public AbstractLogger {
public:
ErrorLogger(int level){
this->level = level;
}
protected:
void write(const std::string &message) override {
std::cout<<"Error Console::Logger: "<<message<<std::endl;
}
};
class FileLogger:public AbstractLogger {
public:
FileLogger(int level){
this->level = level;
}
protected:
void write(const std::string &message) override {
std::cout<<"File::Logger: "<<message<<std::endl;
}
};
std::shared_ptr<AbstractLogger> getChainOfLoggers(){
std::shared_ptr<AbstractLogger> errorLogger = std::make_shared<ErrorLogger>(ConsoleLogger::ERROR);
std::shared_ptr<AbstractLogger> fileLogger = std::make_shared<FileLogger>(ConsoleLogger::DEBUG);
std::shared_ptr<AbstractLogger> consoleLogger = std::make_shared<ConsoleLogger>(ConsoleLogger::INFO);
errorLogger->setNextLogger(fileLogger);
fileLogger->setNextLogger(consoleLogger);
return errorLogger;
}
int main(){
std::shared_ptr<AbstractLogger> loggerChain = getChainOfLoggers();
loggerChain->logMessage(AbstractLogger::INFO,
"This is an information.");
loggerChain->logMessage(AbstractLogger::DEBUG,
"This is an debug level information.");
loggerChain->logMessage(AbstractLogger::ERROR,
"This is an error information.");
return 0;
}