-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs314_p4_log.hpp
143 lines (123 loc) · 3.44 KB
/
cs314_p4_log.hpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#pragma once
#include <string>
#include <sstream>
#include "akj_typedefs.hpp"
#include <cassert>
namespace p4{
#define P4_ASSERT(cond_) {if(!(cond_)){p4::Log::Error("(%s:%s) Failed assertion \"%s\"",__FILE__,__LINE__, #cond_ ); assert(false); } }
#define P4_WARN_IF(cond_) {if(!(cond_)){p4::Log::Warn("(%s:%s) Warning: \"%s\"",__FILE__,__LINE__, #cond_ ); } }
#define P4_TERMINATE(reason) {p4::Log::Error("(%s:%s) Execution terminated: %s",__FILE__,__LINE__, reason ); }
class Log
{
public:
enum eLogLevel
{
LOG_LEVEL_OFF = 0,
LOG_LEVEL_CRITICAL = 1,
LOG_LEVEL_ERROR = 2,
LOG_LEVEL_WARN = 3,
LOG_LEVEL_INFO = 4,
LOG_LEVEL_DEBUG = 5,
LOG_LEVEL_TMI = 6,
LOG_LEVEL_NUMLEVELS
};
~Log(void);
template< typename... Args>
static void TMI( Args... args){
GetInstance()->NewMessage(LOG_LEVEL_TMI).Write(args...);
}
template< typename... Args>
static void Debug( Args... args){
GetInstance()->NewMessage(LOG_LEVEL_DEBUG).Write(args...);
}
template< typename... Args>
static void Critical(Args... args){
GetInstance()->NewMessage(LOG_LEVEL_CRITICAL).Write(args...);
}
template<typename... Args>
static void Warn( Args... args){
GetInstance()->NewMessage(LOG_LEVEL_WARN).Write(args...);
}
template< typename... Args>
static void Info( Args... args){
GetInstance()->NewMessage(LOG_LEVEL_INFO).Write(args...);
}
template<typename... Args>
static void Error(Args... args) {
GetInstance()->NewMessage(LOG_LEVEL_ERROR).Write(args...);
}
Log& SetLogLevel(eLogLevel level){
mLogLevelThreshold = level;
return *this;
}
Log& SetConsoleLogLevel(eLogLevel level){
mConsoleLevelThreshold = level;
return *this;
}
// adapted from http://www.stroustrup.com/C++11FAQ.html#variadic-templates
template<typename T, typename... Args>
void Write(const char* format_string, T first_arg, Args... other_args) {
if(mCurrentLogLevel <= mLogLevelThreshold){
while (format_string && *format_string) {
if (*format_string=='%' && *++format_string!='%') {
mStream.str(std::string());
mStream << first_arg;
mLogBuffer.append(mStream.str());
Write(++format_string, other_args...);
return;
}
mLogBuffer.push_back(*(format_string++));
}
Write("\n\t ... Error in format specifier: too many arguments");
}
}
template<typename T>
void Write(T arg){
if(mCurrentLogLevel <= mLogLevelThreshold){
mStream.str(std::string());
mStream << arg << std::endl;
mLogBuffer.append(mStream.str());
fputs(mLogBuffer.c_str(), mFileHandle);
if(mCurrentLogLevel <= mConsoleLevelThreshold){
printf("%s",mLogBuffer.c_str());
}
fflush(mFileHandle);
}
}
void Write(const char* arg){
if(mCurrentLogLevel <= mLogLevelThreshold){
mLogBuffer.append(arg);
mLogBuffer.push_back('\n');
fputs(mLogBuffer.c_str(), mFileHandle);
if(mCurrentLogLevel <= mConsoleLevelThreshold){
printf("%s",mLogBuffer.c_str());
}
fflush(mFileHandle);
}
}
const char* getLabel(eLogLevel);
Log& NewMessage(eLogLevel level){
mCurrentLogLevel = level;
if(mCurrentLogLevel <= mLogLevelThreshold){
mLogBuffer.clear();
mLogBuffer.append(getLabel(mCurrentLogLevel));
}
return *this;
}
static Log* GetInstance(){
if(sInstance == NULL){
sInstance = new Log();
}
return sInstance;
}
private:
Log(void);
FILE* mFileHandle;
std::ostringstream mStream;
std::string mLogBuffer;
eLogLevel mLogLevelThreshold;
eLogLevel mConsoleLevelThreshold;
eLogLevel mCurrentLogLevel;
static Log* sInstance;
};
}