/ logging.h
logging.h
  1  #ifndef __LOGGING_H__
  2  #define __LOGGING_H__
  3  
  4  #include "config.h"
  5  #include <stdbool.h>
  6  #include <stdarg.h>
  7  
  8  #ifdef HAVE_SYSLOG_H
  9  #include <syslog.h>
 10  #else
 11  enum {
 12  	LOG_ERR,
 13  	LOG_WARNING,
 14  	LOG_NOTICE,
 15  	LOG_INFO,
 16  	LOG_DEBUG,
 17  };
 18  #endif
 19  
 20  /* debug flags */
 21  extern bool opt_debug;
 22  extern bool opt_decode;
 23  extern bool opt_log_output;
 24  extern bool opt_realquiet;
 25  extern bool want_per_device_stats;
 26  
 27  /* global log_level, messages with lower or equal prio are logged */
 28  extern int opt_log_level;
 29  
 30  #define LOGBUFSIZ 256
 31  
 32  extern void _applog(int prio, const char *str, bool force);
 33  extern void _simplelog(int prio, const char *str, bool force);
 34  
 35  #define IN_FMT_FFL " in %s %s():%d"
 36  
 37  #define applog(prio, fmt, ...) do { \
 38  	if (opt_debug || prio != LOG_DEBUG) { \
 39  		if (use_syslog || opt_log_output || prio <= opt_log_level) { \
 40  			char tmp42[LOGBUFSIZ]; \
 41  			snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
 42  			_applog(prio, tmp42, false); \
 43  		} \
 44  	} \
 45  } while (0)
 46  
 47  #define simplelog(prio, fmt, ...) do { \
 48  	if (opt_debug || prio != LOG_DEBUG) { \
 49  		if (use_syslog || opt_log_output || prio <= opt_log_level) { \
 50  			char tmp42[LOGBUFSIZ]; \
 51  			snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
 52  			_simplelog(prio, tmp42, false); \
 53  		} \
 54  	} \
 55  } while (0)
 56  
 57  #define applogsiz(prio, _SIZ, fmt, ...) do { \
 58  	if (opt_debug || prio != LOG_DEBUG) { \
 59  		if (use_syslog || opt_log_output || prio <= opt_log_level) { \
 60  			char tmp42[_SIZ]; \
 61  			snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
 62  			_applog(prio, tmp42, false); \
 63  		} \
 64  	} \
 65  } while (0)
 66  
 67  #define forcelog(prio, fmt, ...) do { \
 68  	if (opt_debug || prio != LOG_DEBUG) { \
 69  		if (use_syslog || opt_log_output || prio <= opt_log_level) { \
 70  			char tmp42[LOGBUFSIZ]; \
 71  			snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
 72  			_applog(prio, tmp42, true); \
 73  		} \
 74  	} \
 75  } while (0)
 76  
 77  #define quit(status, fmt, ...) do { \
 78  	if (fmt) { \
 79  		char tmp42[LOGBUFSIZ]; \
 80  		snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
 81  		_applog(LOG_ERR, tmp42, true); \
 82  	} \
 83  	_quit(status); \
 84  } while (0)
 85  
 86  #define early_quit(status, fmt, ...) do { \
 87  	if (fmt) { \
 88  		char tmp42[LOGBUFSIZ]; \
 89  		snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
 90  		_applog(LOG_ERR, tmp42, true); \
 91  	} \
 92  	__quit(status, false); \
 93  } while (0)
 94  
 95  #define quithere(status, fmt, ...) do { \
 96  	if (fmt) { \
 97  		char tmp42[LOGBUFSIZ]; \
 98  		snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
 99  				##__VA_ARGS__, __FILE__, __func__, __LINE__); \
100  		_applog(LOG_ERR, tmp42, true); \
101  	} \
102  	_quit(status); \
103  } while (0)
104  
105  #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
106  	if (fmt) { \
107  		char tmp42[LOGBUFSIZ]; \
108  		snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
109  				##__VA_ARGS__, _file, _func, _line); \
110  		_applog(LOG_ERR, tmp42, true); \
111  	} \
112  	_quit(status); \
113  } while (0)
114  
115  #ifdef HAVE_CURSES
116  
117  #define wlog(fmt, ...) do { \
118  	char tmp42[LOGBUFSIZ]; \
119  	snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
120  	_wlog(tmp42); \
121  } while (0)
122  
123  #define wlogprint(fmt, ...) do { \
124  	char tmp42[LOGBUFSIZ]; \
125  	snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
126  	_wlogprint(tmp42); \
127  } while (0)
128  
129  #endif
130  
131  #endif /* __LOGGING_H__ */