-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogging.h
More file actions
115 lines (83 loc) · 1.97 KB
/
Copy pathlogging.h
File metadata and controls
115 lines (83 loc) · 1.97 KB
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
#ifndef __YDX_LOGGING_H__
#define __YDX_LOGGING_H__
#include "log_stream.h"
#include "timestamp.h"
namespace ydx
{
class SourceFile
{
public:
template<int N>
inline SourceFile(const char (&arr)[N])
: data_(arr),
size_(N-1)
{
const char* slash = strrchr(data_, '/'); // builtin function
if (slash)
{
data_ = slash + 1;
size_ -= static_cast<int>(data_ - arr);
}
}
explicit SourceFile(const char* filename)
: data_(filename)
{
const char* slash = strrchr(filename, '/');
if (slash)
{
data_ = slash + 1;
}
size_ = static_cast<int>(strlen(data_));
}
const char* data_;
int size_;
};
class Logger
{
public:
enum LogLevel
{
TRACE,
DEBUG,
INFO,
WARN,
ERROR,
FATAL,
NUM_LOG_LEVELS,
};
Logger(SourceFile file, int line);
Logger(SourceFile file, int line, LogLevel level);
Logger(SourceFile file, int line, LogLevel level, const char* func);
Logger(SourceFile file, int line, bool toAbort);
~Logger();
LogStream& stream() { return stream_; }
static LogLevel logLevel();
static void setLogLevel(LogLevel level);
typedef void (*OutputFunc)(const char* msg, int len);
typedef void (*FlushFunc)();
static void setOutput(OutputFunc);
static void setFlush(FlushFunc);
void formatTime();
private:
CTime ctime_;
LogStream stream_;
SourceFile basename_;
LogLevel level_;
int errno_;
int line_;
};
extern Logger::LogLevel g_logLevel;
inline Logger::LogLevel Logger::logLevel()
{
return g_logLevel;
}
#define LOG_INFO if (ydx::Logger::logLevel() <= ydx::Logger::INFO) \
ydx::Logger(__FILE__, __LINE__).stream()
#define LOG_WARN ydx::Logger(__FILE__, __LINE__, ydx::Logger::WARN).stream()
#define LOG_ERROR ydx::Logger(__FILE__, __LINE__, ydx::Logger::ERROR).stream()
#define LOG_FATAL ydx::Logger(__FILE__, __LINE__, ydx::Logger::FATAL).stream()
#define LOG_SYSERR ydx::Logger(__FILE__, __LINE__, false).stream()
#define LOG_SYSFATAL ydx::Logger(__FILE__, __LINE__, true).stream()
const char* strerror_tl(int err_);
}
#endif