Skip to content

Commit 0ebdb4b

Browse files
committed
Logger factory interface implementation done.
1 parent 8ed04ec commit 0ebdb4b

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/log/DefaultLogger.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <string>
2+
3+
#include "Logger.h"
4+
#include "Log.h"
5+
6+
class DefaultLogger : public Logger{
7+
private:
8+
bool enable;
9+
10+
public:
11+
void EnableLog(bool enable) {
12+
this->enable = enable;
13+
}
14+
15+
bool IsEnabled() {
16+
return this->enable;
17+
}
18+
19+
template <typename... Object>
20+
void Print(Object... objects){
21+
if (this->enable){
22+
Print(object...);
23+
}
24+
}
25+
26+
template <typename... Object>
27+
void Print(std::string format, Object... objects){
28+
if (this->enable){
29+
Printf(format, object...);
30+
}
31+
}
32+
};

src/log/Log.h

Whitespace-only changes.

src/log/LogUtil.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <string>
2+
3+
#include "DefaultLogger.h"
4+
#include "Logger.h"
5+
6+
class LogUtil{
7+
private:
8+
static Logger logger;
9+
public:
10+
11+
// SetLogger sets the current logger.
12+
static void SetLogger(Logger l){
13+
logger = l;
14+
}
15+
16+
// GetLogger returns the current logger.
17+
static Logger GetLogger() {
18+
return logger;
19+
}
20+
21+
// LogPrint prints the log.
22+
template <typename... Object>
23+
static void LogPrint(Object... objects) {
24+
logger.Print(objects...);
25+
}
26+
27+
// LogPrintf prints the log with the format.
28+
template <typename... Object>
29+
static void LogPrintf(std::string format, Object... objects) {
30+
logger.Printf(format, objects...)
31+
}
32+
};

src/log/Logger.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <string>
2+
3+
class Logger{
4+
public:
5+
//EnableLog controls whether print the message.
6+
void EnableLog(bool enable);
7+
8+
//IsEnabled returns if logger is enabled.
9+
bool IsEnabled();
10+
11+
//Print formats using the default formats for its operands and logs the message.
12+
template <typename... Object>
13+
void Print(Object... objects);
14+
15+
//Printf formats according to a format specifier and logs the message.
16+
template <typename... Object>
17+
void Printf(std::string, Object... objects);
18+
};

0 commit comments

Comments
 (0)