-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.log_class.cpp
46 lines (43 loc) · 1.07 KB
/
12.log_class.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
#include <iostream>
class Log
{
public:
const int LogLevelError = 0;
const int LogLevelWarning = 1;
const int LogLevelInfo = 2;
// we can use enum instead
// enum Level
// {
// Error, Warning, Info
// };
private:
int m_LogLevel = LogLevelInfo; // m here is like a convention for class private member
// Level m_LogLevel = Info; // using enum
public:
void SetLevel(int level)
{
m_LogLevel = level;
}
void Warn(const char* message)
{
if (m_LogLevel >= LogLevelWarning)
std::cout << "[Warning]:" << message << std::endl;
}
void Error(const char* message)
{
if (m_LogLevel >= LogLevelError)
std::cout << "[Error ]:" << message << std::endl;
}
void Info(const char* message)
{
if (m_LogLevel >= LogLevelInfo)
std::cout << "[Info]:" << message << std::endl;
}
};
int main()
{
Log log;
log.SetLevel(log.LogLevelWarning); // if we use enum we can do this: log.SetLevel(Log::Warning)
log.Warn("Warning!");
std::cin.get();
}