-
Notifications
You must be signed in to change notification settings - Fork 10
/
logging.hpp
122 lines (102 loc) · 3.27 KB
/
logging.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
/**
* The logging utilities used in libRDMA.
*/
#pragma once
#include <iostream>
#include <sstream>
namespace rdmaio {
/**
* \def FATAL
* Used for fatal and probably irrecoverable conditions
* \def ERROR
* Used for errors which are recoverable within the scope of the function
* \def WARNING
* Logs interesting conditions which are probably not fatal
* \def EMPH
* Outputs as INFO, but in WARNING colors. Useful for
* outputting information you want to emphasize.
* \def INFO
* Used for providing general useful information
* \def DEBUG
* Debugging purposes only
* \def EVERYTHING
* Log everything
*/
enum loglevel {
NONE = 7,
FATAL = 6,
ERROR = 5,
WARNING = 4,
EMPH = 3,
INFO = 2,
DEBUG = 1,
EVERYTHING = 0
};
#define unlikely(x) __builtin_expect(!!(x), 0)
#ifndef RDMA_LOG_LEVEL
#define RDMA_LOG_LEVEL ::rdmaio::INFO
#endif
// logging macro definiations
// default log
#define RDMA_LOG(n) \
if (n >= RDMA_LOG_LEVEL) \
::rdmaio::MessageLogger((char*)__FILE__, __LINE__, n).stream()
// log with tag
#define RDMA_TLOG(n,t) \
if(n >= RDMA_LOG_LEVEL) \
::rdmaio::MessageLogger((char*)__FILE__, __LINE__, n).stream() \
<< "[" << (t) << "]"
#define RDMA_LOG_IF(n,condition) \
if(n >= RDMA_LOG_LEVEL && (condition)) \
::rdmaio::MessageLogger((char*)__FILE__, __LINE__, n).stream()
#define RDMA_ASSERT(condition) \
if(unlikely(!(condition))) \
::rdmaio::MessageLogger((char*)__FILE__, __LINE__, ::rdmaio::FATAL + 1).stream() << "Assertion! "
#define RDMA_VERIFY(n,condition) RDMA_LOG_IF(n,(!(condition)))
class MessageLogger {
public:
MessageLogger(const char *file, int line, int level) :level_(level) {
if(level_ < RDMA_LOG_LEVEL)
return;
stream_ << "[" << StripBasename(std::string(file)) << ":" << line << "] ";
}
~MessageLogger() {
if(level_ >= RDMA_LOG_LEVEL) {
stream_ << "\n";
std::cout << "\033[" << RDMA_DEBUG_LEVEL_COLOR[std::min(level_,6)] << "m"
<< stream_.str() << EndcolorFlag();
if(level_ >= ::rdmaio::FATAL)
abort();
}
}
// Return the stream associated with the logger object.
std::stringstream &stream() { return stream_; }
private:
std::stringstream stream_;
int level_;
// control flags for color
#define R_BLACK 39
#define R_RED 31
#define R_GREEN 32
#define R_YELLOW 33
#define R_BLUE 34
#define R_MAGENTA 35
#define R_CYAN 36
#define R_WHITE 37
const int RDMA_DEBUG_LEVEL_COLOR[7] = {R_BLACK,R_YELLOW,R_BLACK,R_GREEN,R_MAGENTA,R_RED,R_RED};
static std::string StripBasename(const std::string &full_path) {
const char kSeparator = '/';
size_t pos = full_path.rfind(kSeparator);
if (pos != std::string::npos) {
return full_path.substr(pos + 1, std::string::npos);
} else {
return full_path;
}
}
static std::string EndcolorFlag() {
char flag[7];
snprintf(flag,7, "%c[0m", 0x1B);
return std::string(flag);
}
};
};