37 #ifndef _ARPA2_EXCEPT_H
47 #pragma clang diagnostic push
48 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
65 #define except_if(lab,nok) do if (nok) { log_error ("Exceptional outcome for %s at %s:%d", #nok, __FILE__, __LINE__); goto lab; } while (0)
79 #define except_ifnot(lab,ok) do if (!(ok)) { log_error ("Exceptional outcome for %s at %s:%d", #ok , __FILE__, __LINE__); goto lab; } while (0)
88 #define assertxt(test,fmt,...) do if (!(test)) { log_critical ("\nAssertion Failed on %s at %s:%d (" fmt ")\n", #test, __FILE__, __LINE__ , ##__VA_ARGS__); exit (-6); } while (0)
96 #define except_if(lab,nok) while ( nok ) goto lab
102 #define except_ifnot(lab,ok) while (!(ok)) goto lab
109 #define assertxt(test,fmt,...) do if (!(test)) { log_critical ("\nAssertion Failed (" fmt ")\n" , ##__VA_ARGS__); exit (-6); } while (0)
120 #define LOG_STYLE_DEFAULT LOG_STYLE_STDERR
123 #define LOG_STYLE_DEFAULT LOG_STYLE_STDERR
125 #define LOG_STYLE_DEFAULT LOG_STYLE_SYSLOG
135 #define LOG_STYLE_NULL 0
136 #define LOG_STYLE_STDOUT 1
137 #define LOG_STYLE_STDERR 2
138 #define LOG_STYLE_SYSLOG 3
141 #define LOG_STYLE LOG_STYLE_DEFAULT
152 #if LOG_STYLE == LOG_STYLE_NULL
158 #define log_critical(fmt,...) do { ; } while (0)
159 #define log_warning(fmt,...) do { ; } while (0)
160 #define log_error(fmt,...) do { ; } while (0)
161 #define log_info(fmt,...) do { ; } while (0)
162 #define log_notice(fmt,...) do { ; } while (0)
163 #define log_debug(fmt,...) do { ; } while (0)
165 #elif LOG_STYLE == LOG_STYLE_STDOUT
173 #define log_critical(fmt,...) do { fprintf (stdout, "##\n## CRITICAL: " fmt "\n##\n" , ##__VA_ARGS__); fflush (stdout); } while (0)
174 #define log_error(fmt,...) do { fprintf (stdout, "Error: " fmt "\n" , ##__VA_ARGS__); fflush (stdout); } while (0)
175 #define log_warning(fmt,...) do { fprintf (stdout, "Warning: " fmt "\n" , ##__VA_ARGS__); fflush (stdout); } while (0)
176 #define log_info(fmt,...) do { fprintf (stdout, fmt "\n" , ##__VA_ARGS__); fflush (stdout); } while (0)
177 #define log_notice(fmt,...) do { fprintf (stdout, fmt "\n" , ##__VA_ARGS__); fflush (stdout); } while (0)
178 #define log_debug(fmt,...) do { fprintf (stdout, "DEBUG: " fmt "\n" , ##__VA_ARGS__); fflush (stdout); } while (0)
180 #elif LOG_STYLE == LOG_STYLE_STDERR
188 #define log_critical(fmt,...) do { fprintf (stderr, "##\n## CRITICAL: " fmt "\n##\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
189 #define log_error(fmt,...) do { fprintf (stderr, "Error: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
190 #define log_warning(fmt,...) do { fprintf (stderr, "Warning: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
191 #define log_info(fmt,...) do { fprintf (stderr, fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
192 #define log_notice(fmt,...) do { fprintf (stderr, fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
193 #define log_debug(fmt,...) do { fprintf (stderr, "DEBUG: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
195 #elif LOG_STYLE == LOG_STYLE_SYSLOG
204 #error "No Windows syslogging yet"
208 #define log_critical(fmt,...) syslog (LOG_CRIT, fmt , ##__VA_ARGS__)
209 #define log_error(fmt,...) syslog (LOG_ERR, fmt , ##__VA_ARGS__)
210 #define log_warning(fmt,...) syslog (LOG_WARNING, fmt , ##__VA_ARGS__)
211 #define log_info(fmt,...) syslog (LOG_INFO, fmt , ##__VA_ARGS__)
212 #define log_notice(fmt,...) syslog (LOG_NOTICE, fmt , ##__VA_ARGS__)
213 #define log_debug(fmt,...) syslog (LOG_DEBUG, fmt , ##__VA_ARGS__)
219 #error "No recognised LOG_STYLE setting"
235 #if defined(__COM_ERR_H) || defined (__COM_ERR_H__)
236 #define log_errno(fmt,...) log_error ("%s: " fmt, error_message (errno) , ##__VA_ARGS__)
238 #define log_errno(fmt,...) log_error ("%s: " fmt, strerror (errno) , ##__VA_ARGS__)
288 #define log_detail(fmt,...) log_debug (fmt , ##__VA_ARGS__)
290 #define log_detail(fmt,...) do { ; } while (0)
310 static inline void log_data (
const char *descr,
const void *blk, uint32_t blklen, uint32_t maxsz) {
311 uint32_t size = blklen;
312 if ((maxsz > 0) && (size > maxsz)) {
315 char buf [strlen (descr) + 3 * size + 50];
316 int count = snprintf (buf,
sizeof(buf),
"%s =", descr);
318 snprintf (buf + count,
sizeof(buf) - count,
"NULL");
322 for (uint32_t i=0; i < blklen; i++) {
323 count += snprintf (buf + count,
sizeof(buf) - count,
" %02x", ((uint8_t *) blk) [i]);
324 if ((size < blklen) && (i > 5) && (i == size - 5)) {
325 count += snprintf (buf + count,
sizeof(buf) - count,
" ...");
329 count += snprintf (buf + count,
sizeof(buf) - count,
" -> #%d", blklen);
334 #define log_data(descr,blk,blklen,skip) do { ; } while (0)
338 #ifdef __UNREALISED_WISH__
363 #if LOG_STYLE == LOG_STYLE_NULL
365 #define nok_critical(test,fmt,...) (test)
366 #define nok_warning(test,fmt,...) (test)
367 #define nok_error(test,fmt,...) (test)
368 #define nok_info(test,fmt,...) (test)
369 #define nok_notice(test,fmt,...) (test)
370 #define nok_debug(test,fmt,...) (test)
371 #define nok_detail(test,fmt,...) (test)
377 #define nok_critical(test,fmt,...) (test || (log_critical (fmt, ##__VA_ARGS__), false))
378 #define nok_warning(test,fmt,...) (test || (log_warning (fmt, ##__VA_ARGS__), false))
379 #define nok_error(test,fmt,...) (test || (log_error (fmt, ##__VA_ARGS__), false))
380 #define nok_info(test,fmt,...) (test || (log_info (fmt, ##__VA_ARGS__), false))
381 #define nok_notice(test,fmt,...) (test || (log_notice (fmt, ##__VA_ARGS__), false))
382 #define nok_debug(test,fmt,...) (test || (log_debug (fmt, ##__VA_ARGS__), false))
383 #define nok_detail(test,fmt,...) (test || (log_detail (fmt, ##__VA_ARGS__), false))
390 #pragma clang diagnostic pop
397 #define _ARPA2_EXCEPT_H
#define log_debug(fmt,...)
static void log_data(const char *descr, const void *blk, uint32_t blklen, uint32_t maxsz)
log_data(descr,ptr,len,maxsz)
Definition: except.h:310