base-logging
logging_printf_style.h
Go to the documentation of this file.
1 /*
2  * @file logging_printf_style.h
3  *
4  * @brief printf style logging
5  * @details defines printf like logging functions
6  *
7  * To write a log message, use LOG_<log-level> in the code. E.g.
8  *
9  * LOG_DEBUG("Reached this point")
10  * LOG_INFO("Value is %i", value)
11  *
12  * are valid statements.
13  * if BASE_LONG_NAMES is defined, the statements are prefixed with BASE_ e.g.:
14  *
15  * BASE_LOG_FATAL("Cannot reach device.")
16  */
17 
18 #ifndef _BASE_LOGGING_PRINTF_STYLE_H_
19 #define _BASE_LOGGING_PRINTF_STYLE_H_
20 
21 // Need to map to logging::Priority order
22 // Allowing to set a log level via CFLAGS - method call will not be compiled into the system
23 // Setting the environment variable BASE_LOG_LEVEL does only have any effect if the compiled
24 // level is equal or higher (closer to FATAL) than the one request
25 //
26 #if defined(BASE_LOG_DISABLE)
27 #define BASE_LOG_PRIORITY 0
28 #elif defined(BASE_LOG_FATAL)
29 #define BASE_LOG_PRIORITY 1
30 #undef BASE_LOG_FATAL
31 #elif defined(BASE_LOG_ERROR)
32 #define BASE_LOG_PRIORITY 2
33 #undef BASE_LOG_ERROR
34 #elif defined(BASE_LOG_WARN)
35 #define BASE_LOG_PRIORITY 3
36 #undef BASE_LOG_WARN
37 #elif defined(BASE_LOG_INFO)
38 #define BASE_LOG_PRIORITY 4
39 #undef BASE_LOG_INFO
40 #elif defined(BASE_LOG_DEBUG)
41 #define BASE_LOG_PRIORITY 5
42 #undef BASE_LOG_DEBUG
43 #endif
44 
45 #ifndef BASE_LOG_PRIORITY
46 // Default logging priority that is compiled in, i.e. all log levels
47 // will be accessible at runtime
48 #define BASE_LOG_PRIORITY 6
49 #endif
50 
51 #ifdef BASE_LONG_NAMES
52 // Empty definition of debug statement
53 #define BASE_LOG_DEBUG(FORMAT, ARGS...)
54 #define BASE_LOG_INFO(FORMAT, ARGS...)
55 #define BASE_LOG_WARN(FORMAT, ARGS...)
56 #define BASE_LOG_ERROR(FORMAT, ARGS...)
57 #define BASE_LOG_FATAL(FORMAT, ARGS...)
58 #define BASE_LOG_CONFIGURE(PRIO, STREAM)
59 #else
60 #define LOG_DEBUG(FORMAT, ARGS...)
61 #define LOG_INFO(FORMAT, ARGS...)
62 #define LOG_WARN(FORMAT, ARGS...)
63 #define LOG_ERROR(FORMAT, ARGS...)
64 #define LOG_FATAL(FORMAT, ARGS...)
65 #define LOG_CONFIGURE(PRIO, STREAM)
66 #endif // BASE_LONG_NAMES
67 
68 #ifndef BASE_LOG_NAMESPACE
69 #define BASE_LOG_NAMESPACE ""
70 #endif // BASE_LOG_NAMESPACE
71 // The debug flag BASE_LOG_NAMESPACE needs to be converted to a string
72 // Stringify element
73 #define __STRINGIFY_(X) #X
74 // expand x before being stringified
75 #define __STRINGIFY(X) __STRINGIFY_(X)
76 
77 // Depending on the globally set log level insert log statements by preprocessor
78 //
79 // The namespace represents the library name and should be set via definitions, e.g. in
80 // your CMakeLists.txt -DBASE_LOG_NAMESPACE=yournamespace
81 //
82 // Using __PRETTY_FUNCTION__ when using gcc otherwise __func__ to show current function
83 #ifdef __GNUC__
84 #define __LOG(PRIO, FORMAT, ARGS ...) { ::base::logging::Logger::getInstance()->log(::base::logging::PRIO,__PRETTY_FUNCTION__, __FILE__, __LINE__, __STRINGIFY(BASE_LOG_NAMESPACE), FORMAT, ## ARGS); }
85 #else
86 #define __LOG(PRIO, FORMAT, ARGS ...) { ::base::logging::Logger::getInstance()->log(::base::logging::PRIO,__func__, __FILE__, __LINE__, __STRINGIFY(BASE_LOG_NAMESPACE), FORMAT, ## ARGS); }
87 #endif
88 
89 #ifdef BASE_LONG_NAMES
90 
91 #if BASE_LOG_PRIORITY >= 1
92 #undef BASE_LOG_FATAL
93 #define BASE_LOG_FATAL(FORMAT, ARGS...) __LOG(FATAL_P, FORMAT, ## ARGS)
94 #endif
95 
96 #if BASE_LOG_PRIORITY >= 2
97 #undef BASE_LOG_ERROR
98 #define BASE_LOG_ERROR(FORMAT, ARGS...) __LOG(ERROR_P, FORMAT, ## ARGS)
99 #endif
100 
101 #if BASE_LOG_PRIORITY >= 3
102 #undef BASE_LOG_WARN
103 #define BASE_LOG_WARN(FORMAT, ARGS...) __LOG(WARN_P, FORMAT, ## ARGS)
104 #endif
105 
106 #if BASE_LOG_PRIORITY >= 4
107 #undef BASE_LOG_INFO
108 #define BASE_LOG_INFO(FORMAT, ARGS...) __LOG(INFO_P, FORMAT, ## ARGS)
109 #endif
110 
111 #if BASE_LOG_PRIORITY >= 5
112 #undef BASE_LOG_DEBUG
113 #define BASE_LOG_DEBUG(FORMAT, ARGS...) __LOG(DEBUG_P, FORMAT, ## ARGS)
114 #endif
115 
116 #undef BASE_LOG_CONFIGURE
117 #define BASE_LOG_CONFIGURE(PRIO,STREAM) { ::base::logging::Logger::getInstance()->configure(::base::logging::PRIO, STREAM); }
118 
119 #else // #ifdef BASE_LONG_NAMES
120 
121 // If there should be conflicts with other libraries, switch to long names
122 // Other wise short names will be available as only
123 #undef LOG_CONFIGURE
124 #define LOG_CONFIGURE(PRIO,STREAM) { ::base::logging::Logger::getInstance()->configure(::base::logging::PRIO, STREAM); }
125 
126 #if BASE_LOG_PRIORITY >= 1
127 #undef LOG_FATAL
128 #define LOG_FATAL(FORMAT, ARGS...) __LOG(FATAL_P, FORMAT, ## ARGS)
129 #endif
130 
131 #if BASE_LOG_PRIORITY >= 2
132 #undef LOG_ERROR
133 #define LOG_ERROR(FORMAT, ARGS...) __LOG(ERROR_P, FORMAT, ## ARGS)
134 #endif
135 
136 #if BASE_LOG_PRIORITY >= 3
137 #undef LOG_WARN
138 #define LOG_WARN(FORMAT, ARGS...) __LOG(WARN_P, FORMAT, ## ARGS)
139 #endif
140 
141 #if BASE_LOG_PRIORITY >= 4
142 #undef LOG_INFO
143 #define LOG_INFO(FORMAT, ARGS...) __LOG(INFO_P, FORMAT, ## ARGS)
144 #endif
145 
146 #if BASE_LOG_PRIORITY >= 5
147 #undef LOG_DEBUG
148 #define LOG_DEBUG(FORMAT, ARGS...) __LOG(DEBUG_P, FORMAT, ## ARGS)
149 #endif
150 
151 #endif // BASE_LONG_NAMES
152 
153 
154 
155 
156 #include <string>
157 #include <stdio.h>
158 #include <stdarg.h>
159 #include <vector>
160 #include "../Singleton.hpp"
161 
162 namespace base {
163 
164 namespace logging {
165 
170 #ifdef WIN32
172 #else
173 enum Priority { UNKNOWN = 0, UNKNOWN_P = 0, FATAL = 1, FATAL_P =1, ERROR = 2, ERROR_P = 2, WARN = 3, WARN_P = 3, INFO = 4, INFO_P = 4, DEBUG = 5, DEBUG_P = 5, ENDPRIORITIES };
174 #endif
175 
176 
178 
203 class Logger : public Singleton<Logger>
204 {
205  friend class Singleton<Logger>;
206 
207 protected:
211  Logger();
212 public:
213 
214  virtual ~Logger();
215 
223  void configure(Priority priority, FILE* outputStream);
224 
234  void log(Priority priority, const char* function, const char* filename, int line, const char* name_space, const char* format, ...) const;
235 
240  void logBuffer(Priority priority, const char* function, const char* file, int line, const char* name_space, const char* buffer) const;
241 
242 private:
247  Priority getLogLevelFromEnv() const;
248 
253  bool getLogColorFromEnv() const;
254 
259  LogFormat getLogFormatFromEnv() const;
260 
261  FILE* mStream;
262  std::vector<std::string> mPriorityNames;
263  Priority mPriority;
264 
265  const char* mpLogColor[ENDPRIORITIES];
266  const char* mpColorEnd;
267 
268  std::vector<std::string> mLogFormatNames;
269  LogFormat mLogFormat;
270 };
271 
272 } // end namespace
273 } // end namespace
274 
275 #endif /* _BASE_LOGGING_PRINTF_STYLE_H_ */
276 
Definition: logging_printf_style.h:173
void log(Priority priority, const char *function, const char *filename, int line, const char *name_space, const char *format,...) const
Definition: logging_printf_style.cpp:135
void configure(Priority priority, FILE *outputStream)
Definition: logging_printf_style.cpp:63
Definition: logging_printf_style.h:177
Definition: logging_printf_style.h:173
Definition: Singleton.hpp:7
virtual ~Logger()
Definition: logging_printf_style.cpp:59
Definition: logging_printf_style.h:173
Definition: logging_printf_style.h:173
Definition: logging_printf_style.h:173
void logBuffer(Priority priority, const char *function, const char *file, int line, const char *name_space, const char *buffer) const
Definition: logging_printf_style.cpp:150
Definition: logging_printf_style.h:173
Definition: logging_printf_style.h:177
Definition: logging_iostream_style.h:48
LogFormat
Definition: logging_printf_style.h:177
Definition: logging_printf_style.h:173
Logger()
Definition: logging_printf_style.cpp:20
Definition: logging_printf_style.h:173
Definition: logging_printf_style.h:173
Definition: logging_printf_style.h:177
Definition: logging_printf_style.h:173
Definition: logging_printf_style.h:177
Definition: logging_printf_style.h:173
Definition: logging_printf_style.h:173
Priority
Definition: logging_printf_style.h:173
Definition: logging_printf_style.h:173
Logger is a logger that allows priority based logging with minimal impact on performance and minimal ...
Definition: logging_printf_style.h:203