-
Notifications
You must be signed in to change notification settings - Fork 36
/
Logger.h
151 lines (116 loc) · 4.9 KB
/
Logger.h
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
143
144
145
146
147
148
149
150
151
#ifndef KRISLIBRARY_LOGGER_H
#define KRISLIBRARY_LOGGER_H
/** @file Logger.h
* @brief The logging system used in KrisLibrary.
*
* Log4cxx will be used if present. Otherwise, LOG4CXX macros will be defined just to print to stdout
* (INFO and WARN) or stderr
*/
#if HAVE_LOG4CXX
#include <log4cxx/logger.h>
namespace KrisLibrary {
typedef log4cxx::LoggerPtr LoggerType;
/** @brief Retrieves the base logger.
*
* On first call, will attempt to load a configuration from log4cxx.xml in the current directory.
* If this doesn't exist, then a default configuration printing to stderr will be used.
*
* To log, call LOG4CXX_INFO(KrisLibrary::logger(),msg...), or LOG4CXX_WARN, LOG4CXX_ERROR, etc.
*/
extern LoggerType logger();
/** @brief Retrieves a named logger.
*
* On first call, will attempt to load a configuration from log4cxx.xml in the current directory.
* If this doesn't exist, then a default configuration printing to stderr will be used.
*
* It can be used by calling LOG4CXX_INFO(KrisLibrary::logger(name),msg...), or LOG4CXX_WARN, LOG4CXX_ERROR, etc.
* However, this is not the most efficient method, since logger lookup is performed for each message. Instead
* you should save the LoggerPtr and use it multiple times. The DEFINE_LOGGER and GET_LOGGER macros are
* better for this, since they only perform lookup of the logger on the first call.
*/
extern LoggerType logger(const char* name);
/** @brief If the root logger is enabled for debug level, this will cause a getchar() to be called.
*/
extern void loggerWait();
/** @brief If logger is enabled for debug level, this will cause a getchar() to be called.
*/
extern void loggerWait(LoggerType logger);
///Use this inside a cpp file to define a fast logger
#define DEFINE_LOGGER(name) \
DECLARE_LOGGER(name) \
namespace KrisLibrary { \
LoggerType _logger_##name; \
}
///Use to declare that you will use a fast logger (only needed if you will share a fast logger between cpp files)
#define DECLARE_LOGGER(name) \
namespace KrisLibrary { \
extern LoggerType _logger_##name; \
inline LoggerType _get_logger_##name() { \
if (_logger_##name == NULL) \
_logger_##name = logger(#name); \
return _logger_##name; \
} \
}
///Use this to retrieve a fast logger
#define GET_LOGGER(name) KrisLibrary::_get_logger_##name()
} //namespace KrisLibrary
#else
#include <iostream>
#include <stdlib.h>
namespace KrisLibrary {
typedef const char* LoggerType;
/** @brief Retrieves the base logger.
*
* To log, call LOG4CXX_INFO(KrisLibrary::logger(),msg...), or LOG4CXX_WARN, LOG4CXX_ERROR, etc.
*/
inline LoggerType logger() { return NULL; }
/** @brief Retrieves a named logger.
*
* It can be used by calling LOG4CXX_INFO(KrisLibrary::logger(name),msg...), or LOG4CXX_WARN, LOG4CXX_ERROR, etc.
* However, this is not the most efficient method, since logger lookup is performed for each message. Instead
* you should save the LoggerPtr and use it multiple times. The DEFINE_LOGGER and GET_LOGGER macros are
* better for this, since they only perform lookup of the logger on the first call.
*/
inline LoggerType logger(const char* name) { return name; }
/** @brief If the root logger is enabled for debug level, this will cause a getchar() to be called.
*/
inline void loggerWait() { printf("Press enter to continue...\n"); getchar(); }
/** @brief If logger is enabled for debug level, this will cause a getchar() to be called.
*/
inline void loggerWait(LoggerType logger) { printf("Press enter to continue...\n"); getchar(); }
#define LOG4CXX_DEBUG(logger,data) { \
if(logger) std::cout<<logger<<": "<<data<<std::endl; \
else std::cout<<data<<std::endl; }
#define LOG4CXX_INFO(logger,data) { \
if(logger) std::cout<<logger<<": "<<data<<std::endl; \
else std::cout<<data<<std::endl; }
#define LOG4CXX_WARN(logger,data) { \
if(logger) std::cout<<logger<<": "<<data<<std::endl; \
else std::cout<<data<<std::endl; }
#define LOG4CXX_ERROR(logger,data) { \
if(logger) std::cerr<<logger<<": "<<data<<std::endl; \
else std::cerr<<data<<std::endl; }
#define LOG4CXX_FATAL(logger,data) { \
if(logger) std::cerr<<logger<<": "<<data<<std::endl; \
else std::cerr<<data<<std::endl; }
///Use this inside a cpp file to define a fast logger
#define DEFINE_LOGGER(name) \
DECLARE_LOGGER(name) \
namespace KrisLibrary { \
LoggerType _logger_##name; \
}
///Use to declare that you will use a fast logger (only needed if you will share a fast logger between cpp files)
#define DECLARE_LOGGER(name) \
namespace KrisLibrary { \
extern LoggerType _logger_##name; \
inline LoggerType _get_logger_##name() { \
if (_logger_##name == NULL) \
_logger_##name = logger(#name); \
return _logger_##name; \
} \
}
///Use this to retrieve a fast logger
#define GET_LOGGER(name) KrisLibrary::_get_logger_##name()
} //namespace KrisLibrary
#endif // HAVE_LOG4CXX
#endif // KRISLIBRARY_LOGGER_H