29 #ifndef Common_Logger_h
30 #define Common_Logger_h
102 m_file = fdopen(fd,
"wt");
126 void log(
int priority,
const char *
format, ...);
135 void log_string(
int priority,
const char *message);
169 #define HT_LOG_BUFSZ 1024
173 # define HT_ABORT abort()
175 #define HT_ABORT raise(SIGABRT)
179 #define HT_LOG(priority, msg) do { \
180 if (Logger::get()->is_enabled(priority)) { \
181 if (Logger::get()->show_line_numbers()) \
182 Logger::get()->log(priority, Hypertable::format( \
183 "(%s:%d) %s", __FILE__, __LINE__, msg)); \
185 Logger::get()->log(priority, msg); \
189 #define HT_LOGF(priority, fmt, ...) do { \
190 if (Logger::get()->is_enabled(priority)) { \
191 if (Logger::get()->show_line_numbers()) \
192 Logger::get()->log(priority, Hypertable::format( \
193 "(%s:%d) " fmt, __FILE__, __LINE__, __VA_ARGS__)); \
195 Logger::get()->log(priority, Hypertable::format( \
196 fmt, __VA_ARGS__)); \
201 #define HT_LOG_BUF_SIZE 4096
203 #define HT_OUT(priority) do { if (Logger::get()->is_enabled(priority)) { \
204 char logbuf[HT_LOG_BUF_SIZE]; \
205 int _priority_ = Logger::get()->get_level(); \
206 FixedOstream _out_(logbuf, sizeof(logbuf)); \
207 if (Logger::get()->show_line_numbers()) \
208 _out_ <<"("<< __FILE__ <<':'<< __LINE__ <<") "; \
211 #define HT_OUT2(priority) do { if (Logger::get()->is_enabled(priority)) { \
212 char logbuf[HT_LOG_BUF_SIZE]; \
213 int _priority_ = priority; \
214 FixedOstream _out_(logbuf, sizeof(logbuf)); \
216 if (Logger::get()->show_line_numbers()) \
217 _out_ << " ("<< __FILE__ <<':'<< __LINE__ <<")"; \
220 #define HT_END ""; Logger::get()->log(_priority_, _out_.str()); \
221 if (_priority_ == Logger::Priority::FATAL) HT_ABORT; \
224 #define HT_OUT_DISABLED do { if (0) {
227 #define HT_DUMP_CSTR(_os_, _label_, _str_) do { \
228 if (!_str_) _os_ <<" " #_label_ "=[NULL]"; \
229 else _os_ <<" " #_label_ "='"<< (_str_) << "'"; \
232 #define HT_DUMP_CSTR_FIELD(_os_, _obj_, _field_) \
233 HT_DUMP_CSTR(_os_, _field_, _obj_._field_)
237 #ifndef HT_DISABLE_LOG_ALL
239 #ifndef HT_DISABLE_LOG_DEBUG
241 #define HT_LOG_ENTER do { \
242 if (Logger::get()->is_enabled(Logger::Priority::DEBUG)) {\
243 if (Logger::get()->show_line_numbers()) \
244 Logger::get()->debug("(%s:%d) %s() ENTER", __FILE__, __LINE__, HT_FUNC);\
246 Logger::get()->debug("%s() ENTER", HT_FUNC); \
250 #define HT_LOG_EXIT do { \
251 if (Logger::get()->is_enabled(Logger::Priority::DEBUG)) { \
252 if (Logger::get()->show_line_numbers()) \
253 Logger::get()->debug("(%s:%d) %s() EXIT", __FILE__, __LINE__, HT_FUNC); \
255 Logger::get()->debug("%s() EXIT", HT_FUNC); \
259 #define HT_DEBUG(msg) HT_LOG(Logger::Priority::DEBUG, msg)
260 #define HT_DEBUGF(msg, ...) HT_LOGF(Logger::Priority::DEBUG, msg, __VA_ARGS__)
261 #define HT_DEBUG_OUT HT_OUT2(Logger::Priority::DEBUG)
265 #define HT_DEBUG(msg)
266 #define HT_DEBUGF(msg, ...)
267 #define HT_DEBUG_OUT HT_OUT_DISABLED
270 #ifndef HT_DISABLE_LOG_INFO
271 #define HT_INFO(msg) HT_LOG(Logger::Priority::INFO, msg)
272 #define HT_INFOF(msg, ...) HT_LOGF(Logger::Priority::INFO, msg, __VA_ARGS__)
275 #define HT_INFOF(msg, ...)
278 #ifndef HT_DISABLE_LOG_NOTICE
279 #define HT_NOTICE(msg) HT_LOG(Logger::Priority::NOTICE, msg)
280 #define HT_NOTICEF(msg, ...) HT_LOGF(Logger::Priority::NOTICE, msg, __VA_ARGS__)
281 #define HT_NOTICE_OUT HT_OUT(Logger::Priority::NOTICE)
283 #define HT_NOTICE(msg)
284 #define HT_NOTICEF(msg, ...)
285 #define HT_NOTICE_OUT HT_OUT_DISABLED
288 #ifndef HT_DISABLE_LOG_WARN
289 #define HT_WARN(msg) HT_LOG(Logger::Priority::WARN, msg)
290 #define HT_WARNF(msg, ...) HT_LOGF(Logger::Priority::WARN, msg, __VA_ARGS__)
291 #define HT_WARN_OUT HT_OUT2(Logger::Priority::WARN)
294 #define HT_WARNF(msg, ...)
295 #define HT_WARN_OUT HT_OUT_DISABLED
298 #ifndef HT_DISABLE_LOG_ERROR
299 #define HT_ERROR(msg) HT_LOG(Logger::Priority::ERROR, msg)
300 #define HT_ERRORF(msg, ...) HT_LOGF(Logger::Priority::ERROR, msg, __VA_ARGS__)
301 #define HT_ERROR_OUT HT_OUT2(Logger::Priority::ERROR)
303 #define HT_ERROR(msg)
304 #define HT_ERRORF(msg, ...)
305 #define HT_ERROR_OUT HT_OUT_DISABLED
308 #ifndef HT_DISABLE_LOG_CRIT
309 #define HT_CRIT(msg) HT_LOG(Logger::Priority::CRIT, msg)
310 #define HT_CRITF(msg, ...) HT_LOGF(Logger::Priority::CRIT, msg, __VA_ARGS__)
311 #define HT_CRIT_OUT HT_OUT2(Logger::Priority::CRIT)
314 #define HT_CRITF(msg, ...)
315 #define HT_CRIT_OUT HT_OUT_DISABLED
318 #ifndef HT_DISABLE_LOG_ALERT
319 #define HT_ALERT(msg) HT_LOG(Logger::Priority::ALERT, msg)
320 #define HT_ALERTF(msg, ...) HT_LOGF(Logger::Priority::ALERT, msg, __VA_ARGS__)
321 #define HT_ALERT_OUT HT_OUT2(Logger::Priority::ALERT)
323 #define HT_ALERT(msg)
324 #define HT_ALERTF(msg, ...)
325 #define HT_ALERT_OUT HT_OUT_DISABLED
328 #ifndef HT_DISABLE_LOG_EMERG
329 #define HT_EMERG(msg) HT_LOG(Logger::Priority::EMERG, msg)
330 #define HT_EMERGF(msg, ...) HT_LOGF(Logger::Priority::EMERG, msg, __VA_ARGS__)
331 #define HT_EMERG_OUT HT_OUT2(Logger::Priority::EMERG)
333 #define HT_EMERG(msg)
334 #define HT_EMERGF(msg, ...)
335 #define HT_EMERG_OUT HT_OUT_DISABLED
338 #ifndef HT_DISABLE_LOG_FATAL
339 #define HT_FATAL(msg) do { \
340 HT_LOG(Logger::Priority::FATAL, msg); \
343 #define HT_FATALF(msg, ...) do { \
344 HT_LOGF(Logger::Priority::FATAL, msg, __VA_ARGS__); \
347 #define HT_FATAL_OUT HT_OUT2(Logger::Priority::FATAL)
349 #define HT_FATAL(msg)
350 #define HT_FATALF(msg, ...)
351 #define HT_FATAL_OUT HT_OUT_DISABLED
354 #else // HT_DISABLE_LOGGING
356 #define HT_DEBUG(msg)
357 #define HT_DEBUGF(msg, ...)
359 #define HT_INFOF(msg, ...)
360 #define HT_NOTICE(msg)
361 #define HT_NOTICEF(msg, ...)
363 #define HT_WARNF(msg, ...)
364 #define HT_ERROR(msg)
365 #define HT_ERRORF(msg, ...)
367 #define HT_CRITF(msg, ...)
368 #define HT_ALERT(msg)
369 #define HT_ALERTF(msg, ...)
370 #define HT_EMERG(msg)
371 #define HT_EMERGF(msg, ...)
372 #define HT_FATAL(msg)
373 #define HT_FATALF(msg, ...)
376 #define HT_DEBUG_OUT HT_OUT_DISABLED
377 #define HT_NOTICE_OUT HT_OUT_DISABLED
378 #define HT_WARN_OUT HT_OUT_DISABLED
379 #define HT_ERROR_OUT HT_OUT_DISABLED
380 #define HT_CRIT_OUT HT_OUT_DISABLED
381 #define HT_ALERT_OUT HT_OUT_DISABLED
382 #define HT_EMERG_OUT HT_OUT_DISABLED
383 #define HT_FATAL_OUT HT_OUT_DISABLED
385 #endif // HT_DISABLE_LOGGING
388 #define HT_EXPECT(_e_, _code_) do { if (_e_); else { \
389 if (_code_ == Error::FAILED_EXPECTATION) \
390 HT_FATAL("failed expectation: " #_e_); \
391 HT_THROW(_code_, "failed expectation: " #_e_); } \
396 #define HT_ASSERT(_e_) HT_EXPECT(_e_, Error::FAILED_EXPECTATION)
398 #endif // Common_Logger_h
int get_level() const
Returns the message level.
void log_string(int priority, const char *message)
Appends a string message to the log.
void initialize(const String &name)
Public initialization function - creates a singleton instance of LogWriter.
std::string String
A String is simply a typedef to std::string.
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
void debug(const char *format,...)
Prints a debug message with variable arguments (similar to printf)
void log_varargs(int priority, const char *format, va_list ap)
Appends a string message with variable arguments to the log.
String m_name
The name of the application.
int m_priority
The current priority (everything above is filtered)
void set_test_mode(int fd=-1)
The test mode disables line numbers and timestamps and can redirect the output to a separate file des...
Fixed size string buffers.
void flush()
Flushes the log file.
FILE * m_file
The output file handle.
The LogWriter class writes to stdout.
void log(int priority, const char *format,...)
Prints a message with variable arguments.
void debug(const String &message)
Prints a debug message.
void log(int priority, const String &message)
Prints a message.
bool is_enabled(int level) const
Returns true if a message with this level is not discarded.
bool m_show_line_numbers
True if line numbers are shown.
bool show_line_numbers() const
Returns true if line numbers are printed.
A String class based on std::string.
void set_level(int level)
Sets the message level; all messages with a higher level are discarded.
LogWriter(const String &name)
Constructor.
Error codes, Exception handling, error logging.
bool m_test_mode
True if this log is in test mode.