Add another static NOBUG_ANN flag for logging annotations, refine loglevels
[nobug.git] / src / nobug.h
blobbafab12bf20030041d2e8eb65f2af514ea166cb6
1 /*
2 This file is part of the NoBug debugging library.
4 Copyright (C)
5 2007, 2008, 2009 Christian Thaeter <ct@pipapo.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, contact Christian Thaeter <ct@pipapo.org>.
20 #ifndef NOBUG_H
21 #define NOBUG_H
23 #ifndef NOBUG_LIBNOBUG_C /* not when building libnobug */
24 #ifdef NDEBUG
25 #ifdef EBUG_ALPHA
26 #error NDEBUG incompatible with -DEBUG_ALPHA
27 #endif
28 #ifdef EBUG_BETA
29 #error NDEBUG incompatible with -DEBUG_BETA
30 #endif
31 #endif
33 #if defined(EBUG_ALPHA)
34 # define NOBUG_MODE_ALPHA 1
35 # define NOBUG_MODE_BETA 0
36 # define NOBUG_MODE_RELEASE 0
37 #elif defined(EBUG_BETA)
38 # define NOBUG_MODE_ALPHA 0
39 # define NOBUG_MODE_BETA 1
40 # define NOBUG_MODE_RELEASE 0
41 #elif defined(NDEBUG)
42 # define NOBUG_MODE_ALPHA 0
43 # define NOBUG_MODE_BETA 0
44 # define NOBUG_MODE_RELEASE 1
45 #else
46 #error no debug level and no NDEBUG defined
47 #endif
48 #endif /* NOBUG_LIBNOBUG_C */
51 HEADERS
54 #include <syslog.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <stdio.h>
58 #include <stdarg.h>
60 #ifdef HAVE_EXECINFO_H
61 # ifndef NOBUG_USE_EXECINFO
62 # define NOBUG_USE_EXECINFO 1
63 # endif
64 #else
65 # undef NOBUG_USE_EXECINFO
66 # define NOBUG_USE_EXECINFO 0
67 #endif
69 #if NOBUG_USE_EXECINFO
70 #include <execinfo.h>
71 #endif
73 #if defined(HAVE_VALGRIND_H) && !defined(NVALGRIND)
74 # ifndef NOBUG_USE_VALGRIND
75 # define NOBUG_USE_VALGRIND 1
76 # endif
77 #else
78 # undef NOBUG_USE_VALGRIND
79 # define NOBUG_USE_VALGRIND 0
80 #endif
82 #if NOBUG_USE_VALGRIND
83 #include <valgrind.h>
84 #endif
86 #ifdef HAVE_PTHREAD
87 # ifndef NOBUG_USE_PTHREAD
88 # define NOBUG_USE_PTHREAD 1
89 # endif
90 #else
91 # ifdef NOBUG_USE_PTHREAD
92 # undef NOBUG_USE_PTHREAD
93 # endif
94 # define NOBUG_USE_PTHREAD 0
95 #endif
97 #if NOBUG_USE_PTHREAD
98 #include <pthread.h>
99 #endif
101 #if NOBUG_MODE_RELEASE==0
103 //index.assert xref:assert[assert]:: C standard assertion
104 //assertions [[assert]]
105 //assertions .assert
106 //assertions assert(expr)
107 //assertions
108 //assertions NoBug overrides the standard assert macro in *ALPHA* and *BETA* builds.
109 //assertions This is just a compatibility feature, its use is not suggested.
110 //assertions
112 #undef assert
113 #define assert(expr) NOBUG_ASSERT(expr)
114 #endif
118 ASSERTIONS
122 //index.require xref:REQUIRE[REQUIRE]:: preconditions (incoming parameters)
123 //assertions [[REQUIRE]]
124 //assertions .REQUIRE
125 //assertions REQUIRE(expr, ...)
126 //assertions REQUIRE_DBG(expr, ...)
127 //assertions REQUIRE_IF(when, expr, ...)
128 //assertions REQUIRE_IF_DBG(when, expr, ...)
129 //assertions
130 //assertions Precondition (input) check. Use these macros to validate input a
131 //assertions function receives. The checks are enabled in *ALPHA* and *BETA* builds and
132 //assertions optimized out in *RELEASE* builds.
133 //assertions
135 #define NOBUG_REQUIRE(expr, ...) \
136 NOBUG_IF_NOT_RELEASE(NOBUG_ASSERT_(expr, "PRECONDITION", \
137 NOBUG_LOCATION_INFO, ## __VA_ARGS__))
140 #define NOBUG_REQUIRE_DBG(expr, ...) \
141 NOBUG_IF_NOT_RELEASE(NOBUG_ASSERT_DBG_(expr, "PRECONDITION", \
142 NOBUG_LOCATION_INFO, ## __VA_ARGS__))
145 #define NOBUG_REQUIRE_IF(when, expr, ...) \
146 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_REQUIRE(expr, ## __VA_ARGS__)))
148 #define NOBUG_REQUIRE_IF_DBG(when, expr, ...) \
149 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_REQUIRE_DBG(expr, ## __VA_ARGS__)))
153 //index.ensure xref:ENSURE[ENSURE]:: postconditions (computation outcomes)
154 //assertions [[ENSURE]]
155 //assertions .ENSURE
156 //assertions ENSURE(expr, ...)
157 //assertions ENSURE_DBG(expr, ...)
158 //assertions ENSURE_IF(when, expr, ...)
159 //assertions ENSURE_IF_DBG(when, expr, ...)
160 //assertions
161 //assertions Postcondition (progress/output) check. Use these macros to validate the
162 //assertions data a function produces (example: return value). The checks enabled
163 //assertions unconditionally in *ALPHA* builds and optimized out in *BETA* builds for
164 //assertions scopes which are tagged as `CHECKED`. In *RELEASE* builds this checks are
165 //assertions always optimized out, but scopes tagged as `UNCHECKED` are not permitted.
166 //assertions
168 #define NOBUG_ENSURE(expr, ...) \
169 NOBUG_IF_ALPHA (NOBUG_ASSERT_(expr, "POSTCONDITION", \
170 NOBUG_LOCATION_INFO, ## __VA_ARGS__)) \
171 NOBUG_IF_BETA ( \
172 NOBUG_WHEN (NOBUG_SCOPE_UNCHECKED, \
173 NOBUG_ASSERT_(expr, "POSTCONDITION", \
174 NOBUG_LOCATION_INFO, ## __VA_ARGS__))\
177 #define NOBUG_ENSURE_DBG(expr, ...) \
178 NOBUG_IF_ALPHA (NOBUG_ASSERT_DBG_ (expr, "POSTCONDITION", \
179 NOBUG_LOCATION_INFO, ## __VA_ARGS__)) \
180 NOBUG_IF_BETA ( \
181 NOBUG_WHEN (NOBUG_SCOPE_UNCHECKED, \
182 NOBUG_ASSERT_DBG_(expr, "POSTCONDITION", \
183 NOBUG_LOCATION_INFO, ## __VA_ARGS__)) \
187 #define NOBUG_ENSURE_IF(when, expr, ...) \
188 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_ENSURE(expr, ## __VA_ARGS__)))
190 #define NOBUG_ENSURE_IF_DBG(when, expr, ...) \
191 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_ENSURE_DBG(expr, ## __VA_ARGS__)))
195 generic assertion
197 //index.assert xref:ASSERT[ASSERT]:: generic assertion
198 //assertions [[ASSERT]]
199 //assertions .ASSERT
200 //assertions ASSERT(expr, ...)
201 //assertions ASSERT_DBG(expr, ...)
202 //assertions ASSERT_IF(when, expr, ...)
203 //assertions ASSERT_IF_DBG(when, expr, ...)
204 //assertions
205 //assertions Generic check. Use these macros when you want to validate something
206 //assertions which doesn't fall into one of the above categories. A example is when
207 //assertions a library function can return a unexpected result (scanf with syntax
208 //assertions error in the formatstring, when a constant/literal formatstring is
209 //assertions expected). The checks are enabled in *ALPHA* and *BETA* builds and
210 //assertions optimized out in *RELEASE* builds.
211 //assertions
213 #define NOBUG_ASSERT(expr, ...) \
214 NOBUG_IF_NOT_RELEASE( \
215 NOBUG_ASSERT_(expr, "ASSERTION", \
216 NOBUG_LOCATION_INFO, ## __VA_ARGS__) \
219 #define NOBUG_ASSERT_DBG(expr, ...) \
220 NOBUG_IF_NOT_RELEASE( \
221 NOBUG_ASSERT_DBG_(expr, "ASSERTION", \
222 NOBUG_LOCATION_INFO, ## __VA_ARGS__) \
225 #define NOBUG_ASSERT_IF(when, expr, ...) \
226 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_ASSERT(expr, ## __VA_ARGS__)))
228 #define NOBUG_ASSERT_IF_DBG(when, expr, ...) \
229 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_ASSERT_DBG(expr, ## __VA_ARGS__)))
234 //index.check xref:CHECK[CHECK]:: assertion for testsuites
235 //assertions [[CHECK]]
236 //assertions .CHECK
237 //assertions CHECK(expr, ...)
238 //assertions CHECK_IF(when, expr, ...)
239 //assertions
240 //assertions This assertion is never optimized out. Its main purpose is for implementing
241 //assertions testsuites where one want to assert tests independent of the build level
242 //assertions
244 #define NOBUG_CHECK(expr, ...) \
245 NOBUG_ASSERT_(expr, "CHECK", NOBUG_LOCATION_INFO, ## __VA_ARGS__)
247 #define NOBUG_CHECK_IF(when, expr, ...) \
248 NOBUG_WHEN(when, NOBUG_CHECK(expr, ## __VA_ARGS__))
253 low level assert
255 #define NOBUG_ASSERT_(expr, what, location, ...) \
256 NOBUG_WHEN (!(expr), \
257 NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \
258 location, what, "(%s) " \
259 NOBUG_HEAD(__VA_ARGS__), \
260 #expr NOBUG_TAIL(__VA_ARGS__)); \
261 NOBUG_BACKTRACE; \
262 NOBUG_ABORT \
265 #define NOBUG_ASSERT_DBG_(expr, what, location, ...) \
266 NOBUG_WHEN (NOBUG_ACTIVE_DBG && !(expr), \
267 NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \
268 location, what, "(%s) " \
269 NOBUG_HEAD(__VA_ARGS__), \
270 #expr NOBUG_TAIL(__VA_ARGS__)); \
271 NOBUG_BACKTRACE; \
272 NOBUG_ABORT \
275 #define NOBUG_ASSERTN_(expr, what, location, ...) \
276 NOBUG_WHEN (!(expr), \
277 NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \
278 location, what, \
279 ## __VA_ARGS__); \
280 NOBUG_BACKTRACE; \
281 NOBUG_ABORT \
286 //index.invariant xref:INVARIANT[INVARIANT]:: validate the state
287 //assertions [[INVARIANT]]
288 //assertions .INVARIANT
289 //assertions INVARIANT(type, pointer, depth)
290 //assertions INVARIANT_DBG(type, pointer, depth)
291 //assertions INVARIANT_IF(when,type, pointer, depth)
292 //assertions INVARIANT_IF_DBG(when, type, pointer, depth)
293 //assertions
294 //assertions Checking invariants. You can provide more complex checking functions
295 //assertions which test the validity of datastructures. Invariants are only enabled
296 //assertions in *ALPHA* builds for scopes which are not tagged as `CHECKED` and
297 //assertions otherwise optimized out.
298 //assertions
299 //assertions TODO: describe how to create invariant checks
300 //assertions
302 #define NOBUG_INVARIANT(type, pointer, depth) \
303 NOBUG_IF_ALPHA( \
304 NOBUG_WHEN (NOBUG_SCOPE_UNCHECKED, \
305 NOBUG_CAT(type,_invariant)(pointer, depth, \
306 NOBUG_LOCATION_ARGS) \
310 #define NOBUG_INVARIANT_DBG(type, pointer, ...) \
311 NOBUG_IF_ALPHA( \
312 NOBUG_WHEN (NOBUG_SCOPE_UNCHECKED && NOBUG_ACTIVE_DBG, \
313 NOBUG_CAT(type,_invariant)(pointer, depth, \
314 NOBUG_LOCATION_ARGS) \
318 #define NOBUG_INVARIANT_IF(when, type, pointer, ...) \
319 NOBUG_IF_ALPHA (NOBUG_WHEN (when, \
320 NOBUG_INVARIANT(type, pointer, ## __VA_ARGS__)) \
323 #define NOBUG_INVARIANT_IF_DBG(when, type, pointer, ...) \
324 NOBUG_IF_ALPHA (NOBUG_WHEN (when, \
325 NOBUG_INVARIANT_DBG(type, pointer, ## __VA_ARGS__))\
328 #define NOBUG_INVARIANT_ASSERT(expr, ...) \
329 NOBUG_ASSERT_(expr, "INVARIANT", (file, line, func), ## __VA_ARGS__)
333 checked/unchecked tagged scopes
335 #define NOBUG_SCOPE_UNCHECKED NOBUG_CHECKED_VALUE == 0
337 #define NOBUG_CHECKED NOBUG_IF_NOT_RELEASE(enum {NOBUG_CHECKED_VALUE=1})
339 #define NOBUG_UNCHECKED \
340 NOBUG_IF_NOT_RELEASE(enum {NOBUG_CHECKED_VALUE=0}) \
341 NOBUG_IF_RELEASE(NOBUG_UNCHECKED_NOT_ALLOWED_IN_RELEASE_BUILD)
346 dumping datastructures
348 /*TODO dump-level for flags instead limits[0]*/
350 //index.dump xref:DUMP[DUMP]:: dumping datastructures
351 //dumpmacros [[DUMP]]
352 //dumpmacros * DUMP(flag, type, pointer, depth)
353 //dumpmacros + Dump a datastructure
354 //dumpmacros DUMP_IF(expr, flag, type, pointer, depth)
355 //dumpmacros + Dump datastructure if expr is true
356 //dumpmacros
357 //dumpmacros This macros call a datastructure dump of the object (`pointer`) in question.
358 //dumpmacros `DUMP_IF` is the only enabled dumping macro for the RELEASE build level.
359 //dumpmacros
361 #define NOBUG_DUMP(flag, type, pointer, depth) \
362 NOBUG_IF_NOT_RELEASE( \
363 NOBUG_WHEN (NOBUG_DUMP_LEVEL <= nobug_flag_##flag.limits[0], \
364 NOBUG_CAT3 (nobug_, type, _dump)(pointer, depth, \
365 NOBUG_LOCATION_ARGS) \
369 #define NOBUG_DUMP_DBG(flag, type, pointer, depth) \
370 NOBUG_IF_NOT_RELEASE( \
371 NOBUG_WHEN (NOBUG_ACTIVE_DBG && NOBUG_DUMP_LEVEL <= nobug_flag_##flag.limits[0], \
372 NOBUG_CAT3 (nobug_, type, _dump)(pointer, depth, \
373 NOBUG_LOCATION_ARGS) \
377 #define NOBUG_DUMP_IF(when, flag, type, pointer, depth) \
378 NOBUG_WHEN (when && NOBUG_DUMP_LEVEL <= nobug_flag_##flag.limits[0], \
379 NOBUG_CAT3 (nobug_, type, _dump)(pointer, depth, \
380 NOBUG_LOCATION_ARGS) \
383 #define NOBUG_DUMP_IF_DBG(when, flag, type, pointer, depth) \
384 NOBUG_IF_NOT_RELEASE( \
385 NOBUG_WHEN (NOBUG_ACTIVE_DBG && when && NOBUG_DUMP_LEVEL <= nobug_flag_##flag.limits[0],\
386 NOBUG_CAT3 (nobug_, type, _dump)(pointer, depth, \
387 NOBUG_LOCATION_ARGS) \
393 //index.dump_log xref:DUMP_LOG[DUMP_LOG]:: Logging functions for dumping
394 //dumpmacros [[DUMP_LOG]]
395 //dumpmacros .DUMP_LOG
396 //dumpmacros DUMP_LOG(fmt, ...)
397 //dumpmacros DUMP_LOG_DBG(fmt, ...)
398 //dumpmacros DUMP_LOG_IF(when, fmt, ...)
399 //dumpmacros DUMP_LOG_IF_DBG(when, fmt, ...)
400 //dumpmacros
401 //dumpmacros Any output from `DUMP` handlers should be done by these macros.
402 //dumpmacros
403 //dumpmacros Dumping is by default done on level `LOG_DEBUG`, this can be overridden by
404 //dumpmacros defining `NOBUG_DUMP_LEVEL` to some other level.
405 //dumpmacros
407 #define NOBUG_DUMP_LOG(fmt, ...) \
408 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_DUMP_LEVEL, \
409 (file, line, func), "DUMP", \
410 fmt, ## __VA_ARGS__)
413 #define NOBUG_DUMP_LOG_DBG(fmt, ...) \
414 NOBUG_WHEN (NOBUG_ACTIVE_DBG, \
415 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_DUMP_LEVEL, \
416 (file, line, func), "DUMP", \
417 fmt, ## __VA_ARGS__) \
420 #define NOBUG_DUMP_LOG_IF(expr, fmt, ...) \
421 NOBUG_WHEN (expr, \
422 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_DUMP_LEVEL, \
423 (file, line, func), "DUMP", \
424 fmt, ## __VA_ARGS__) \
427 #define NOBUG_DUMP_LOG_IF_DBG(expr, fmt, ...) \
428 NOBUG_WHEN (NOBUG_ACTIVE_DBG && expr, \
429 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_DUMP_LEVEL, \
430 (file, line, func), "DUMP", \
431 fmt, ## __VA_ARGS__) \
435 #ifndef NOBUG_DUMP_LEVEL
436 #define NOBUG_DUMP_LEVEL LOG_DEBUG
437 #endif
440 Logging
444 //index.echo xref:ECHO[ECHO]:: logging for tests
445 //logmacros [[ECHO]]
446 //logmacros .ECHO
447 //logmacros ECHO(...)
448 //logmacros
449 //logmacros Never optimized out, logs at LOG_NOTICE level. Its main purpose is for implementing
450 //logmacros testsuites where one want to print and log messages independent of the build level
451 //logmacros
453 #define NOBUG_ECHO(...) \
454 NOBUG_LOG_(&nobug_flag_NOBUG_ON, LOG_NOTICE, NOBUG_LOCATION_INFO, "ECHO", ## __VA_ARGS__)
457 //index.alert xref:ALERT[ALERT]:: about to die
458 //logmacros [[ALERT]]
459 //logmacros .ALERT
460 //logmacros ALERT(flag, ...)
461 //logmacros ALERT_DBG(flag, ...)
462 //logmacros ALERT_IF(when, flag, ...)
463 //logmacros ALERT_IF_DBG(when, flag, ...)
464 //logmacros
465 //logmacros This is the most critical condition an application might log. This might be used
466 //logmacros if an error occurs which can not be handled except a safe shutdown for example.
467 //logmacros
469 #define NOBUG_ALERT(flag, ...) \
470 NOBUG_LOG(flag, LOG_ALERT, ## __VA_ARGS__)
471 #define NOBUG_ALERT_IF(expr, flag, ...) \
472 NOBUG_LOG_IF(expr, flag, LOG_ALERT, ## __VA_ARGS__)
473 #define NOBUG_ALERT_DBG(flag, ...) \
474 NOBUG_LOG_DBG(flag, LOG_ALERT, ## __VA_ARGS__)
475 #define NOBUG_ALERT_IF_DBG(expr, flag, ...) \
476 NOBUG_LOG_IF_DBG(expr, flag, LOG_ALERT, ## __VA_ARGS__)
479 //index.critical xref:CRITICAL[CRITICAL]:: can not continue
480 //logmacros [[CRITICAL]]
481 //logmacros .CRITICAL
482 //logmacros CRITICAL(flag, ...)
483 //logmacros CRITICAL_DBG(flag, ...)
484 //logmacros CRITICAL_IF(when, flag, ...)
485 //logmacros CRITICAL_IF_DBG(when, flag, ...)
486 //logmacros
487 //logmacros An error which can not be handled occured but the application does not need to be
488 //logmacros shutdowen, perhaps waiting for an operator to fix the cause.
489 //logmacros
491 #define NOBUG_CRITICAL(flag, ...) \
492 NOBUG_LOG(flag, LOG_CRIT, ## __VA_ARGS__)
493 #define NOBUG_CRITICAL_IF(expr, flag, ...) \
494 NOBUG_LOG_IF(expr, flag, LOG_CRIT, ## __VA_ARGS__)
495 #define NOBUG_CRITICAL_DBG(flag, ...) \
496 NOBUG_LOG_DBG(flag, LOG_CRIT, ## __VA_ARGS__)
497 #define NOBUG_CRITICAL_IF_DBG(expr, flag, ...) \
498 NOBUG_LOG_IF_DBG(expr, flag, LOG_CRIT, ## __VA_ARGS__)
501 //index.error xref:ERROR[ERROR]:: something gone wrong
502 //logmacros [[ERROR]]
503 //logmacros .ERROR
504 //logmacros ERROR(flag, fmt, ...)
505 //logmacros ERROR_DBG(flag, fmt, ...)
506 //logmacros ERROR_IF(when, flag, fmt, ...)
507 //logmacros ERROR_IF_DBG(when, flag, fmt, ...)
508 //logmacros
509 //logmacros Application takes a error handling brach
510 //logmacros
512 #define NOBUG_ERROR(flag, ...) \
513 NOBUG_LOG(flag, LOG_ERR, ## __VA_ARGS__)
514 #define NOBUG_ERROR_IF(expr, flag, ...) \
515 NOBUG_LOG_IF(expr, flag, LOG_ERR, ## __VA_ARGS__)
516 #define NOBUG_ERROR_DBG(flag, ...) \
517 NOBUG_LOG_DBG(flag, LOG_ERR, ## __VA_ARGS__)
518 #define NOBUG_ERROR_IF_DBG(expr, flag, ...) \
519 NOBUG_LOG_IF_DBG(expr, flag, LOG_ERR, ## __VA_ARGS__)
522 //index.warn xref:WARN[WARN]:: unexpected fixable error
523 //logmacros [[WARN]]
524 //logmacros .WARN
525 //logmacros WARN(flag, fmt, ...)
526 //logmacros WARN_DBG(flag, fmt, ...)
527 //logmacros WARN_IF(when, flag, fmt, ...)
528 //logmacros WARN_IF_DBG(when, flag, fmt, ...)
529 //logmacros
530 //logmacros Rare, handled but unexpected branch
531 //logmacros
533 #define NOBUG_WARN(flag, ...) \
534 NOBUG_LOG(flag, LOG_WARNING, ## __VA_ARGS__)
535 #define NOBUG_WARN_IF(expr, flag, ...) \
536 NOBUG_LOG_IF(expr, flag, LOG_WARNING, ## __VA_ARGS__)
537 #define NOBUG_WARN_DBG(flag, ...) \
538 NOBUG_LOG_DBG(flag, LOG_WARNING, ## __VA_ARGS__)
539 #define NOBUG_WARN_IF_DBG(expr, flag, ...) \
540 NOBUG_LOG_IF_DBG(expr, flag, LOG_WARNING, ## __VA_ARGS__)
543 //index.info xref:INFO[INFO]:: progress message
544 //logmacros [[INFO]]
545 //logmacros .INFO
546 //logmacros INFO(flag, fmt, ...)
547 //logmacros INFO_DBG(flag, fmt, ...)
548 //logmacros INFO_IF(when, flag, fmt, ...)
549 //logmacros INFO_IF_DBG(when, flag, fmt, ...)
550 //logmacros
551 //logmacros Message about program progress
552 //logmacros
554 #define NOBUG_INFO(flag, ...) \
555 NOBUG_LOG(flag, LOG_INFO, ## __VA_ARGS__)
556 #define NOBUG_INFO_IF(expr, flag, ...) \
557 NOBUG_LOG_IF(expr, flag, LOG_INFO, ## __VA_ARGS__)
558 #define NOBUG_INFO_DBG(flag, ...) \
559 NOBUG_LOG_DBG(flag, LOG_INFO, ## __VA_ARGS__)
560 #define NOBUG_INFO_IF_DBG(expr, flag, ...) \
561 NOBUG_LOG_IF_DBG(expr, flag, LOG_INFO, ## __VA_ARGS__)
564 //index.notice xref:NOTICE[NOTICE]:: detailed progress message
565 //logmacros [[NOTICE]]
566 //logmacros .NOTICE
567 //logmacros NOTICE(flag, fmt, ...)
568 //logmacros NOTICE_DBG(flag, fmt, ...)
569 //logmacros NOTICE_IF(when, flag, fmt, ...)
570 //logmacros NOTICE_IF_DBG(when, flag, fmt, ...)
571 //logmacros
572 //logmacros More detailed progress message
573 //logmacros
575 #define NOBUG_NOTICE(flag, ...) \
576 NOBUG_LOG(flag, LOG_NOTICE, ## __VA_ARGS__)
577 #define NOBUG_NOTICE_IF(expr, flag, ...) \
578 NOBUG_LOG_IF(expr, flag, LOG_NOTICE, ## __VA_ARGS__)
579 #define NOBUG_NOTICE_DBG(flag, ...) \
580 NOBUG_LOG_DBG(flag, LOG_NOTICE, ## __VA_ARGS__)
581 #define NOBUG_NOTICE_IF_DBG(expr, flag, ...) \
582 NOBUG_LOG_IF_DBG(expr, flag, LOG_NOTICE, ## __VA_ARGS__)
585 //index.trace xref:TRACE[TRACE]:: debugging level message
586 //logmacros [[TRACE]]
587 //logmacros .TRACE
588 //logmacros TRACE(flag, fmt, ...)
589 //logmacros TRACE_DBG(flag, fmt, ...)
590 //logmacros TRACE_IF(when, flag, fmt, ...)
591 //logmacros TRACEIF_DBG(when, flag, fmt, ...)
592 //logmacros
593 //logmacros Very fine grained messages
594 //logmacros
595 //logmacros NOTE: that `TRACE` corresponds to `LOG_DEBUG`, because using `DEBUG` could be ambiguous.
596 //logmacros
598 #define NOBUG_TRACE(flag, ...) \
599 NOBUG_LOG(flag, LOG_DEBUG, ## __VA_ARGS__)
600 #define NOBUG_TRACE_IF(expr, flag, ...) \
601 NOBUG_LOG_IF(expr, flag, LOG_DEBUG, ## __VA_ARGS__)
602 #define NOBUG_TRACE_DBG(flag, ...) \
603 NOBUG_LOG_DBG(flag, LOG_DEBUG, ## __VA_ARGS__)
604 #define NOBUG_TRACE_IF_DBG(expr, flag, ...) \
605 NOBUG_LOG_IF_DBG(expr, flag, LOG_DEBUG, ## __VA_ARGS__)
609 //index.log xref:LOG[LOG]:: generic logging
610 //logmacros [[LOG]]
611 //logmacros .LOG
612 //logmacros LOG(flag, lvl, fmt, ...)
613 //logmacros LOG_DBG(flag, lvl, fmt, ...)
614 //logmacros LOG_IF(when, flag, lvl, fmt, ...)
615 //logmacros LOG_IF_DBG(when, flag, lvl, fmt, ...)
616 //logmacros
617 //logmacros Generic logging macro which takes the level explicitly,
618 //logmacros avoid this, unless you implement your own logging facilities.
619 //logmacros
621 #define NOBUG_LOG(flag, lvl, ...) \
622 NOBUG_LOG_(&NOBUG_FLAG(flag), lvl, NOBUG_LOCATION_INFO, \
623 NOBUG_LVL(lvl), ## __VA_ARGS__)
625 #define NOBUG_LOG_DBG(flag, lvl, ...) \
626 NOBUG_WHEN (NOBUG_ACTIVE_DBG, \
627 NOBUG_LOG_(&NOBUG_FLAG(flag), lvl, NOBUG_LOCATION_INFO, \
628 NOBUG_LVL(lvl), ## __VA_ARGS__) \
631 #define NOBUG_LOG_IF(expr, flag, lvl, ...) \
632 NOBUG_WHEN (expr, \
633 NOBUG_LOG_(&NOBUG_FLAG(flag), lvl, NOBUG_LOCATION_INFO, \
634 NOBUG_LVL(lvl), ## __VA_ARGS__) \
637 #define NOBUG_LOG_IF_DBG(expr, flag, lvl, ...) \
638 NOBUG_WHEN (NOBUG_ACTIVE_DBG && expr, \
639 NOBUG_LOG_(&NOBUG_FLAG(flag), lvl, NOBUG_LOCATION_INFO, \
640 NOBUG_LVL(lvl), ## __VA_ARGS__) \
645 low level logging handler
647 #define NOBUG_LOG_(flag, lvl, location, what, ...) \
648 NOBUG_WHEN (lvl <= NOBUG_LOG_BASELIMIT && lvl <= (flag)->limits[NOBUG_TARGET_RINGBUFFER], \
649 nobug_log (flag, lvl, \
650 "%0.10llu: %s: %s:%d:"NOBUG_THREAD_ID_FMT(" ",":")" %s: " \
651 NOBUG_HEAD(__VA_ARGS__), ++nobug_counter, what, \
652 NOBUG_LOCATION_FILE location, \
653 NOBUG_LOCATION_LINE location NOBUG_THREAD_ID_COMMA, \
654 NOBUG_LOCATION_FUNC location NOBUG_TAIL(__VA_ARGS__)) \
657 #define NOBUG_LVL(lvl) NOBUG_LVL_##lvl
658 #define NOBUG_LVL_0 "EMERG"
659 #define NOBUG_LVL_1 "ALERT"
660 #define NOBUG_LVL_2 "CRIT"
661 #define NOBUG_LVL_3 "ERR"
662 #define NOBUG_LVL_4 "WARNING"
663 #define NOBUG_LVL_5 "NOTICE"
664 #define NOBUG_LVL_6 "INFO"
665 #define NOBUG_LVL_7 "TRACE"
668 //index.log_baselimit xref:LOG_BASELIMIT[LOG_BASELIMIT]
669 //logmacros [[LOG_BASELIMIT]]
670 //logmacros .LOG_BASELIMIT
671 //logmacros NOBUG_LOG_BASELIMIT_ALPHA
672 //logmacros NOBUG_LOG_BASELIMIT_BETA
673 //logmacros NOBUG_LOG_BASELIMIT_RELEASE
674 //logmacros NOBUG_LOG_BASELIMIT
675 //logmacros
676 //logmacros NoBug defines some logging base limits, anything more detailed
677 //logmacros than this base limits will be optimized out. This is used to
678 //logmacros reduce the logging overhead for *RELEASE* builds.
679 //logmacros By default the limit is set to `LOG_DEBUG` for *ALPHA* and *BETA*
680 //logmacros builds, so all logging is retained and `LOG_NOTICE` in *RELEASE*
681 //logmacros builds to log the application progress only coarsely then.
682 //logmacros
683 //logmacros This macros can be defined before including 'nobug.h' to some other
684 //logmacros log level (as defined in 'syslog.h').
685 //logmacros
687 #ifndef NOBUG_LOG_BASELIMIT_ALPHA
688 #define NOBUG_LOG_BASELIMIT_ALPHA LOG_DEBUG
689 #endif
691 #ifndef NOBUG_LOG_BASELIMIT_BETA
692 #define NOBUG_LOG_BASELIMIT_BETA LOG_DEBUG
693 #endif
695 #ifndef NOBUG_LOG_BASELIMIT_RELEASE
696 #define NOBUG_LOG_BASELIMIT_RELEASE LOG_NOTICE
697 #endif
699 #ifndef NOBUG_LOG_BASELIMIT
700 #define NOBUG_LOG_BASELIMIT \
701 NOBUG_IF_ALPHA(NOBUG_LOG_BASELIMIT_ALPHA) \
702 NOBUG_IF_BETA(NOBUG_LOG_BASELIMIT_BETA) \
703 NOBUG_IF_RELEASE(NOBUG_LOG_BASELIMIT_RELEASE)
704 #endif
707 Annotations
711 alpha beta release
712 DEPRECATED log nothing wont compile
714 //index.deprecated xref:DEPRECATED[DEPRECATED]:: to be discarded in future
715 //annotations [[DEPRECATED]]
716 //annotations * DEPRECATED(...)
717 //annotations + Something which shouldn't be used in future
718 //annotations
720 #define NOBUG_DEPRECATED(msg) \
721 NOBUG_IF_ALPHA(NOBUG_LOG_(&nobug_flag_NOBUG_ANN, LOG_WARN, \
722 NOBUG_LOCATION_INFO, "DEPRECATED", msg) \
724 NOBUG_IF_RELEASE(NOBUG_DEPRECATED_NOT_ALLOWED_IN_RELEASE_BUILD(msg))
728 alpha beta release
729 UNIMPLEMENTED abort abort wont compile
731 //index.unimplemented xref:UNIMPLEMENTED[UNIMPLEMENTED]:: not yet implemented
732 //annotations [[UNIMPLEMENTED]]
733 //annotations * UNIMPLEMENTED(...)
734 //annotations + not yet finished feature
735 //annotations
737 #define NOBUG_UNIMPLEMENTED(msg) \
738 NOBUG_IF_NOT_RELEASE ( do { \
739 NOBUG_LOG_ (&nobug_flag_NOBUG_ANN, LOG_EMERG, NOBUG_LOCATION_INFO, \
740 "UNIMPLEMENTED", msg); \
741 NOBUG_ABORT; \
742 } while (0)) \
743 NOBUG_IF_RELEASE( NOBUG_UNIMPLEMENTED_NOT_ALLOWED_IN_RELEASE_BUILD(msg))
747 alpha beta release
748 FIXME log wont compile wont compile
750 //index.fixme xref:FIXME[FIXME]:: known Bug
751 //annotations [[FIXME]]
752 //annotations * FIXME(...)
753 //annotations + known bug to be fixed later
754 //annotations
756 #define NOBUG_FIXME(msg) \
757 NOBUG_IF_ALPHA( NOBUG_ONCE( NOBUG_LOG_(&nobug_flag_NOBUG_ANN, LOG_ALERT, \
758 NOBUG_LOCATION_INFO, "FIXME", msg))) \
759 NOBUG_IF_BETA( NOBUG_FIXME_NOT_ALLOWED_IN_BETA_BUILD(msg)) \
760 NOBUG_IF_RELEASE( NOBUG_FIXME_NOT_ALLOWED_IN_RELEASE_BUILD(msg))
764 alpha beta release
765 TODO log log wont compile
767 //index.todo xref:TODO[TODO]:: things to be done
768 //annotations [[TODO]]
769 //annotations * TODO(...)
770 //annotations + enhancement to be done soon
771 //annotations
773 #define NOBUG_TODO(msg) \
774 NOBUG_IF_NOT_RELEASE ( \
775 NOBUG_ONCE ( \
776 NOBUG_LOG_( &nobug_flag_NOBUG_ANN, LOG_NOTICE, NOBUG_LOCATION_INFO, \
777 "TODO", msg); \
778 )) \
779 NOBUG_IF_RELEASE(NOBUG_TODO_NOT_ALLOWED_IN_RELEASE_BUILD(msg))
783 alpha beta release
784 PLANNED log nothing nothing
786 //index.planned xref:PLANNED[PLANNED]:: ideas for future
787 //annotations [[PLANNED]]
788 //annotations * PLANNED(...)
789 //annotations + future enhancement
790 //annotations
792 #define NOBUG_PLANNED(msg) \
793 NOBUG_IF_ALPHA( NOBUG_ONCE(NOBUG_LOG_ (&nobug_flag_NOBUG_ANN, LOG_INFO, \
794 NOBUG_LOCATION_INFO, "PLANNED", msg)))
798 alpha beta release
799 NOTREACHED abort abort nothing
801 //index.notreached xref:NOTREACHED[NOTREACHED]:: code path never taken
802 //annotations [[NOTREACHED]]
803 //annotations * NOTREACHED
804 //annotations + used to tag code-path which shall be never executed (defaults in switch
805 //annotations statements, `else NOTREACHED;` after series of if's, etc.)
806 //annotations
808 #define NOBUG_NOTREACHED \
809 NOBUG_IF_NOT_RELEASE( do { \
810 NOBUG_LOG_( &nobug_flag_NOBUG_ANN, LOG_EMERG, \
811 NOBUG_LOCATION_INFO, "NOTREACHED"); \
812 NOBUG_ABORT; \
813 } while (0))
817 Fault injection
819 //faultinjection NoBug has some macros which can be used to simulate errorneous behaviour:
820 //faultinjection
821 //index.inject_goodbad xref:INJECT_GOODBAD[INJECT_GOODBAD]:: fault injection expression
822 //faultinjection [[INJECT_GOODBAD]]
823 //faultinjection * INJECT_GOODBAD(expr, good, bad) substitutes to an expression and returns
824 //faultinjection good when expr is false and bad when expr is true. In BETA and RELEASE
825 //faultinjection builds 'good' is always returned.
826 //faultinjection
828 #define NOBUG_INJECT_GOODBAD(expr, good, bad) \
829 NOBUG_IF_ALPHA((expr)?({NOBUG_INJECT_LOG(#expr": "#bad);}),bad:good) \
830 NOBUG_IF_NOT_ALPHA(good)
834 //index.inject_fault xref:INJECT_FAULT[INJECT_FAULT]:: fault injection statement
835 //faultinjection [[INJECT_FAULT]]
836 //faultinjection * INJECT_FAULT(expr, bad) substitutes to a statement which executes 'bad'
837 //faultinjection when expr is true. Optimitzed out in BETA and RELEASE builds.
838 //faultinjection
840 #define NOBUG_INJECT_FAULT(expr, bad) \
841 NOBUG_IF_ALPHA(NOBUG_WHEN(expr,NOBUG_INJECT_LOG(#expr": "#bad); bad))
845 //faultinjection In both cases, when a fault is injected it will be logged at
846 //faultinjection NOBUG_INJECT_LEVEL (LOG_NOTICE).
847 //faultinjection
849 #define NOBUG_INJECT_LOG(msg) \
850 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_INJECT_LEVEL, \
851 NOBUG_LOCATION_INFO, "INJECT_FAULT", \
852 msg)
855 #ifndef NOBUG_INJECT_LEVEL
856 #define NOBUG_INJECT_LEVEL LOG_NOTICE
857 #endif
861 Flag handling
863 #define NOBUG_FLAG(name) NOBUG_CAT(nobug_flag_,name)
865 #define NOBUG_DEFINE_FLAG(name) struct nobug_flag NOBUG_FLAG(name) = \
866 NOBUG_IF(NOBUG_MODE_ALPHA, {#name, NULL, 0, \
867 {LOG_DEBUG, LOG_INFO, LOG_DEBUG, -1, LOG_INFO}, &nobug_default_ringbuffer, NULL,NULL}) \
868 NOBUG_IF(NOBUG_MODE_BETA, {#name, NULL, 0, \
869 {LOG_INFO, LOG_NOTICE, LOG_NOTICE, LOG_NOTICE, LOG_WARNING}, &nobug_default_ringbuffer, NULL,NULL}) \
870 NOBUG_IF(NOBUG_MODE_RELEASE, {#name, NULL, 0, \
871 {LOG_NOTICE, -1, LOG_WARNING, LOG_WARNING, LOG_ERR}, &nobug_default_ringbuffer, NULL,NULL})
873 #define NOBUG_DEFINE_FLAG_PARENT(name, parent) \
874 NOBUG_DECLARE_FLAG(parent); \
875 struct nobug_flag NOBUG_FLAG(name) = \
876 NOBUG_IF(NOBUG_MODE_ALPHA, {#name, &NOBUG_FLAG(parent), 0, \
877 {LOG_DEBUG, LOG_INFO, LOG_DEBUG, -1, LOG_INFO}, &nobug_default_ringbuffer, NULL,NULL}) \
878 NOBUG_IF(NOBUG_MODE_BETA, {#name, &NOBUG_FLAG(parent), 0, \
879 {LOG_INFO, LOG_NOTICE, LOG_NOTICE, LOG_NOTICE, LOG_WARNING}, &nobug_default_ringbuffer, NULL,NULL}) \
880 NOBUG_IF(NOBUG_MODE_RELEASE, {#name, &NOBUG_FLAG(parent), 0, \
881 {LOG_NOTICE, -1, LOG_WARNING, LOG_WARNING, LOG_ERR}, &nobug_default_ringbuffer, NULL,NULL})
883 #define NOBUG_DEFINE_FLAG_LIMIT(name, limit) struct nobug_flag NOBUG_FLAG(name) = \
884 NOBUG_IF(NOBUG_MODE_ALPHA, {#name, NULL, 0, \
885 {LOG_DEBUG, limit, LOG_DEBUG, -1, LOG_INFO}, &nobug_default_ringbuffer, NULL,NULL}) \
886 NOBUG_IF(NOBUG_MODE_BETA, {#name, NULL, 0, \
887 {LOG_INFO, LOG_NOTICE, limit, LOG_NOTICE, LOG_WARNING}, &nobug_default_ringbuffer, NULL,NULL}) \
888 NOBUG_IF(NOBUG_MODE_RELEASE, {#name, NULL, 0, \
889 {LOG_NOTICE, -1, LOG_WARNING, limit, LOG_ERR}, &nobug_default_ringbuffer, NULL,NULL})
891 #define NOBUG_DEFINE_FLAG_PARENT_LIMIT(name, parent, limit) \
892 NOBUG_DECLARE_FLAG(parent); \
893 struct nobug_flag NOBUG_FLAG(name) = \
894 NOBUG_IF(NOBUG_MODE_ALPHA, {#name, &NOBUG_FLAG(parent), 0, \
895 {LOG_DEBUG, limit, LOG_DEBUG, -1, LOG_INFO}, &nobug_default_ringbuffer, NULL,NULL}) \
896 NOBUG_IF(NOBUG_MODE_BETA, {#name, &NOBUG_FLAG(parent), 0, \
897 {LOG_INFO, LOG_NOTICE, limit, LOG_NOTICE, LOG_WARNING}, &nobug_default_ringbuffer, NULL,NULL}) \
898 NOBUG_IF(NOBUG_MODE_RELEASE, {#name, &NOBUG_FLAG(parent), 0, \
899 {LOG_NOTICE, -1, LOG_WARNING, limit, LOG_ERR}, &nobug_default_ringbuffer, NULL,NULL})
902 /*TODO better FLAG_LIMIT handling, should respect LOG_TARGET */
905 #define NOBUG_DECLARE_FLAG(name) extern struct nobug_flag NOBUG_FLAG(name)
907 #define NOBUG_INIT_FLAG(name) \
908 nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, NOBUG_LOG_LIMIT)
910 #define NOBUG_INIT_FLAG_LIMIT(name, default) \
911 nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default)
913 #ifdef __cplusplus
914 #define NOBUG_CPP_DEFINE_FLAG(name) \
915 NOBUG_DEFINE_FLAG(name); \
916 static int nobug_##name##_cppinit##__LINE__ = NOBUG_INIT_FLAG(name)
918 #define NOBUG_CPP_DEFINE_FLAG_PARENT(name, parent) \
919 NOBUG_DEFINE_FLAG_PARENT(name, parent); \
920 static int nobug_##name##_cppinit##__LINE__ = NOBUG_INIT_FLAG(name)
922 #define NOBUG_CPP_DEFINE_FLAG_LIMIT(name, default) \
923 NOBUG_DEFINE_FLAG_LIMIT(name, default); \
924 static int nobug_##name##_cppinit##__LINE__ = NOBUG_INIT_FLAG_LIMIT(name, default)
926 #define NOBUG_CPP_DEFINE_FLAG_PARENT_LIMIT(name, parent, default) \
927 NOBUG_DEFINE_FLAG_PARENT_LIMIT(name, parent, default); \
928 static int nobug_##name##_cppinit##__LINE__ = NOBUG_INIT_FLAG_LIMIT(name, default)
929 #endif
931 #ifndef NOBUG_LOG_LIMIT_ALPHA
932 # define NOBUG_LOG_LIMIT_ALPHA LOG_INFO
933 #endif
934 #ifndef NOBUG_LOG_LIMIT_BETA
935 # define NOBUG_LOG_LIMIT_BETA LOG_WARNING
936 #endif
937 #ifndef NOBUG_LOG_LIMIT_RELEASE
938 # define NOBUG_LOG_LIMIT_RELEASE LOG_CRIT
939 #endif
941 #ifndef NOBUG_LOG_LIMIT
942 # define NOBUG_LOG_LIMIT \
943 NOBUG_IF_ALPHA( NOBUG_LOG_LIMIT_ALPHA) \
944 NOBUG_IF_BETA( NOBUG_LOG_LIMIT_BETA) \
945 NOBUG_IF_RELEASE( NOBUG_LOG_LIMIT_RELEASE)
946 #endif
948 #ifndef NOBUG_LOG_TARGET_ALPHA
949 # define NOBUG_LOG_TARGET_ALPHA NOBUG_TARGET_CONSOLE
950 #endif
951 #ifndef NOBUG_LOG_TARGET_BETA
952 # define NOBUG_LOG_TARGET_BETA NOBUG_TARGET_FILE
953 #endif
954 #ifndef NOBUG_LOG_TARGET_RELEASE
955 # define NOBUG_LOG_TARGET_RELEASE NOBUG_TARGET_SYSLOG
956 #endif
958 #ifndef NOBUG_LOG_TARGET
959 # define NOBUG_LOG_TARGET \
960 NOBUG_IF_ALPHA( NOBUG_LOG_TARGET_ALPHA) \
961 NOBUG_IF_BETA( NOBUG_LOG_TARGET_BETA) \
962 NOBUG_IF_RELEASE( NOBUG_LOG_TARGET_RELEASE)
963 #endif
965 #define NOBUG_SET_LIMIT(flag, min) \
966 NOBUG_IF_NOT_RELEASE( NOBUG_FLAG(flag) = (min))
970 Resource registry macros
974 //index.resource_logging xref:RESOURCE_LOGGING[RESOURCE_LOGGING]:: switch resource logging on and off
975 //index.resource_log_level xref:RESOURCE_LOGGING[RESOURCE_LOG_LEVEL]:: select the log level for resource logging
976 //resourcemacros [[RESOURCE_LOGGING]]
977 //resourcemacros
978 //resourcemacros Unless the user defines `NOBUG_RESOURCE_LOGGING` to 0 each of the above macros
979 //resourcemacros will emit a log message at `NOBUG_RESOURCE_LOG_LEVEL` which defaults to
980 //resourcemacros `LOG_DEBUG`.
981 //resourcemacros
983 #ifndef NOBUG_RESOURCE_LOGGING
984 #define NOBUG_RESOURCE_LOGGING 1
985 #endif
987 #ifndef NOBUG_RESOURCE_LOG_LEVEL
988 #define NOBUG_RESOURCE_LOG_LEVEL LOG_DEBUG
989 #endif
993 //index.resource_handle xref:RESOURCE_HANDLE[RESOURCE_HANDLE]:: define an resource handle
994 //resourcemacros [[RESOURCE_HANDLE]]
995 //resourcemacros Resources are accessed through handles.
996 //resourcemacros
997 //resourcemacros .RESOURCE_HANDLE
998 //resourcemacros RESOURCE_HANDLE(name)
999 //resourcemacros
1000 //resourcemacros Defines handles to access resources.
1001 //resourcemacros
1002 //resourcemacros `name`::
1003 //resourcemacros identifer to be used for the handle
1004 //resourcemacros
1005 //resourcemacros This macro takes care that the declaration is optimized out in the same manner
1006 //resourcemacros the rest of the resource tracker would be disabled. You can still instantiate
1007 //resourcemacros handles as `struct nobug_resource_record*` in structures which must have a
1008 //resourcemacros constant size unconditional of the build level.
1009 //resourcemacros
1011 #define NOBUG_RESOURCE_HANDLE(handle) \
1012 NOBUG_IF_ALPHA(struct nobug_resource_record* handle)
1014 #define NOBUG_RESOURCE_HANDLE_INIT(handle) NOBUG_IF_ALPHA(handle = NULL)
1018 //index.resource_announce xref:RESOURCE_ANNOUNCE[RESOURCE_ANNOUNCE]:: publish new resources
1019 //resourcemacros [[RESOURCE_ANNOUNCE]]
1020 //resourcemacros
1021 //resourcemacros .RESOURCE_ANNOUNCE
1022 //resourcemacros RESOURCE_ANNOUNCE(flag, type, name, identifier, handle)
1023 //resourcemacros
1024 //resourcemacros Publishes resources.
1025 //resourcemacros
1026 //resourcemacros `flag`::
1027 //resourcemacros the NoBug flag which turns logging on for this macro
1028 //resourcemacros `type`::
1029 //resourcemacros a string which should denote the domain of the resource,
1030 //resourcemacros examples are "file", "mutex", "lock", "database" and so on
1031 //resourcemacros `name`::
1032 //resourcemacros the actual name of a named resource this as string which
1033 //resourcemacros together with type forms a unique identifier of the resource. `type` and
1034 //resourcemacros `name` must be available through the entire lifetime of the resource, using
1035 //resourcemacros literal strings is recommended
1036 //resourcemacros `identifier`::
1037 //resourcemacros a pointer which should be unique for this resource, any
1038 //resourcemacros kind of pointer will suffice, it is only used for identification. In
1039 //resourcemacros multithreaded applications the thread identifier becomes an additional
1040 //resourcemacros identifier
1041 //resourcemacros `handle`::
1042 //resourcemacros a `nobug_resource_record*` which will be initialized to point to
1043 //resourcemacros the newly created resource.
1044 //resourcemacros
1045 //resourcemacros Resources must be unique, it is a fatal error when a resource it tried to be
1046 //resourcemacros announced more than one time.
1047 //resourcemacros
1049 #define NOBUG_RESOURCE_ANNOUNCE(flag, type, name, ptr, handle) \
1050 NOBUG_RESOURCE_ANNOUNCE_RAW(&NOBUG_FLAG(flag), type, name, ptr, handle)
1052 #define NOBUG_RESOURCE_ANNOUNCE_RAW(flag, type, name, ptr, handle) \
1053 NOBUG_IF_ALPHA( do { \
1054 NOBUG_REQUIRE(!handle, "Announced resource handle not initialized"); \
1055 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1056 NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, \
1057 NOBUG_LOCATION_INFO, "RESOURCE_ANNOUNCE", \
1058 "%s: %s@%p", type, name, ptr);) \
1059 NOBUG_ASSERTN_(handle = nobug_resource_announce (type, name, ptr, \
1060 NOBUG_BASENAME(__FILE__ ":" NOBUG_STRINGIZE(__LINE__))), \
1061 "RESOURCE_ANNOUNCE", NOBUG_LOCATION_INFO, \
1062 "%s: %s@%p %s", type, name, ptr, nobug_resource_error); \
1063 } while (0))
1067 //index.resource_forget xref:RESOURCE_FORGET[RESOURCE_FORGET]:: remove resources
1068 //resourcemacros [[RESOURCE_FORGET]]
1069 //resourcemacros
1070 //resourcemacros .RESOURCE_FORGET
1071 //resourcemacros RESOURCE_FORGET(flag, handle)
1072 //resourcemacros
1073 //resourcemacros Removes resources that have become unavailable from the registry.
1074 //resourcemacros
1075 //resourcemacros `flag`::
1076 //resourcemacros the NoBug flag which turns logging on for this macro
1077 //resourcemacros `handle`::
1078 //resourcemacros the `nobug_resource_record*` used to track this resource
1079 //resourcemacros
1080 //resourcemacros The resource must still exist and no users must be attached to it, else a fatal
1081 //resourcemacros error is raised.
1082 //resourcemacros
1084 #define NOBUG_RESOURCE_FORGET(flag, handle) \
1085 NOBUG_RESOURCE_FORGET_RAW(&NOBUG_FLAG(flag), handle)
1087 #define NOBUG_RESOURCE_FORGET_RAW(flag, handle) \
1088 NOBUG_IF_ALPHA( do { \
1089 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1090 NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \
1091 "RESOURCE_FORGET", "%s: %s@%p", handle?handle->type:"", \
1092 handle?handle->name:"", handle?handle->object_id:NULL);) \
1093 NOBUG_ASSERTN_(nobug_resource_forget (handle), \
1094 "RESOURCE_FORGET", NOBUG_LOCATION_INFO, "%s: %s@%p: %s", \
1095 handle?handle->type:"", handle?handle->name:"", \
1096 handle?handle->object_id:NULL, nobug_resource_error); \
1097 handle = NULL; \
1098 } while (0))
1102 //index.resource_enter xref:RESOURCE_ENTER[RESOURCE_ENTER]:: claim a resource
1103 //resourcemacros [[RESOURCE_ENTER]]
1104 //resourcemacros
1105 //resourcemacros .RESOURCE_ENTER
1106 //resourcemacros RESOURCE_ENTER(flag, announced, name, identifier, state, handle)
1107 //resourcemacros
1108 //resourcemacros Attach a resource to code.
1109 //resourcemacros
1110 //resourcemacros `flag`::
1111 //resourcemacros nobug flag which turns logging on for this macro
1112 //resourcemacros `announced`::
1113 //resourcemacros the handle set by `RESOURCE_ANNOUNCE`
1114 //resourcemacros `name`::
1115 //resourcemacros a free-form identifier
1116 //resourcemacros `identifier`::
1117 //resourcemacros some pointer which must be unique for the user of the
1118 //resourcemacros resource (see above)
1119 //resourcemacros `state`::
1120 //resourcemacros the initial state, one of `NOBUG_RESOURCE_WAITING`,
1121 //resourcemacros `NOBUG_RESOURCE_EXCLUSIVE` or `NOBUG_RESOURCE_RECURSIVE`
1122 //resourcemacros `handle`::
1123 //resourcemacros a `nobug_resource_record*` which will be initialized to the
1124 //resourcemacros entering node
1125 //resourcemacros
1127 #define NOBUG_RESOURCE_ENTER(flag, resource, user, ptr, state, handle) \
1128 NOBUG_IF_ALPHA( do { \
1129 NOBUG_REQUIRE(resource, "Announced resource handle not initialized"); \
1130 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1131 NOBUG_LOG_(&NOBUG_FLAG(flag), NOBUG_RESOURCE_LOG_LEVEL, \
1132 NOBUG_LOCATION_INFO, "RESOURCE_ENTER", \
1133 "%s: %s@%p: %s@%p: %s", resource->type, resource->name, \
1134 resource->object_id, user, ptr, \
1135 nobug_resource_states[state]);) \
1136 NOBUG_ASSERTN_(handle = nobug_resource_enter (resource, user, ptr, state, \
1137 NOBUG_BASENAME(__FILE__ ":" NOBUG_STRINGIZE(__LINE__))), \
1138 "RESOURCE_ENTER", NOBUG_LOCATION_INFO, \
1139 "%s: %s@%p: %s@%p: %s: %s", \
1140 resource?resource->type:"", resource?resource->name:"", \
1141 resource?resource->object_id:NULL, \
1142 user, ptr, nobug_resource_states[state], \
1143 nobug_resource_error); \
1144 } while (0))
1148 //index.resource_state xref:RESOURCE_STATE[RESOURCE_STATE]:: change the state of a resource
1149 //resourcemacros [[RESOURCE_STATE]]
1150 //resourcemacros
1151 //resourcemacros .NOBUG_RESOURCE_STATE
1152 //resourcemacros NOBUG_RESOURCE_STATE(flag, entered, state)
1153 //resourcemacros
1154 //resourcemacros Changes resource's state.
1155 //resourcemacros
1156 //resourcemacros `flag`::
1157 //resourcemacros is nobug flag which turns logging on for this macro
1158 //resourcemacros `entered`::
1159 //resourcemacros the handle set by `RESOURCE_ENTER`
1160 //resourcemacros `state`::
1161 //resourcemacros the new state Note that only certain state transitions are
1162 //resourcemacros allowed, see discussion/diagram above
1163 //resourcemacros
1165 #define NOBUG_RESOURCE_STATE(flag, resource, state) \
1166 NOBUG_RESOURCE_STATE_RAW(&NOBUG_FLAG(flag), resource, state)
1168 #define NOBUG_RESOURCE_STATE_RAW(flag, resource, state) \
1169 NOBUG_IF_ALPHA( do { \
1170 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1171 NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \
1172 "RESOURCE_STATE", "%s: %s@%p: %s@%p: %s->%s", \
1173 resource?((struct nobug_resource_record*)resource->data)->type:"", \
1174 resource?((struct nobug_resource_record*)resource->data)->name:"", \
1175 resource?((struct nobug_resource_record*)resource->data)->object_id:"", \
1176 resource?resource->name:NULL, \
1177 resource?resource->object_id:NULL, \
1178 resource?resource->type:"", nobug_resource_states[state]); \
1180 NOBUG_ASSERTN_(nobug_resource_state (resource, state), \
1181 "RESOURCE_STATE", NOBUG_LOCATION_INFO, "%s: %s@%p: %s@%p: %s->%s: %s", \
1182 resource?((struct nobug_resource_record*)resource->data)->type:"", \
1183 resource?((struct nobug_resource_record*)resource->data)->name:"", \
1184 resource?((struct nobug_resource_record*)resource->data)->object_id:"", \
1185 resource?resource->name:NULL, \
1186 resource?resource->object_id:NULL, \
1187 resource?resource->type:"", nobug_resource_states[state], \
1188 nobug_resource_error); \
1189 } while (0))
1193 //index.resource_leave xref:RESOURCE_LEAVE[RESOURCE_LEAVE]:: relinquish a claimed resource
1194 //resourcemacros [[RESOURCE_LEAVE]]
1195 //resourcemacros
1196 //resourcemacros .RESOURCE_LEAVE
1197 //resourcemacros RESOURCE_LEAVE(flag, handle)
1198 //resourcemacros
1199 //resourcemacros Disconnect from a resource identified with its handle.
1200 //resourcemacros
1201 //resourcemacros `flag`::
1202 //resourcemacros nobug flag which turns logging on for this macro
1203 //resourcemacros `handle`::
1204 //resourcemacros the handle you got while entering the resource
1205 //resourcemacros
1207 #define NOBUG_RESOURCE_LEAVE(flag, handle) \
1208 NOBUG_RESOURCE_LEAVE_RAW(&NOBUG_FLAG(flag), handle)
1210 #define NOBUG_RESOURCE_LEAVE_RAW(flag, handle) \
1211 NOBUG_IF_ALPHA( do { \
1212 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1213 NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \
1214 "RESOURCE_LEAVE", "%s: %s@%p: %s@%p: %s", \
1215 handle?((struct nobug_resource_record*)handle->data)->type:"", \
1216 handle?((struct nobug_resource_record*)handle->data)->name:"", \
1217 handle?((struct nobug_resource_record*)handle->data)->object_id:"", \
1218 handle?handle->name:"", \
1219 handle?handle->object_id:NULL, \
1220 handle?handle->type:""); \
1222 NOBUG_ASSERTN_(nobug_resource_leave (handle), \
1223 "RESOURCE_LEAVE", NOBUG_LOCATION_INFO, "%s: %s@%p: %s@%p: %s: %s", \
1224 handle?((struct nobug_resource_record*)handle->data)->type:"", \
1225 handle?((struct nobug_resource_record*)handle->data)->name:"", \
1226 handle?((struct nobug_resource_record*)handle->data)->object_id:"", \
1227 handle?handle->name:NULL, \
1228 handle?handle->object_id:NULL, \
1229 handle?handle->type:"", \
1230 nobug_resource_error); \
1231 handle = NULL; \
1232 } while (0))
1236 //index.resource_leave_lookup xref:RESOURCE_LEAVE_LOOKUP[RESOURCE_LEAVE_LOOKUP]:: relinquish a claimed resource w/o handle
1237 //resourcemacros [[RESOURCE_LEAVE_LOOKUP]]
1238 //resourcemacros
1239 //resourcemacros .RESOURCE_LEAVE_LOOKUP
1240 //resourcemacros RESOURCE_LEAVE_LOOKUP(flag, resource, identifier)
1241 //resourcemacros
1242 //resourcemacros Disconnect from a resource identified by its identifier.
1243 //resourcemacros
1244 //resourcemacros `flag`::
1245 //resourcemacros nobug flag which turns logging on for this macro
1246 //resourcemacros `resource`::
1247 //resourcemacros the handle you got from announcing the resource
1248 //resourcemacros `identifier`::
1249 //resourcemacros the pointer you gave it when creating it
1250 //resourcemacros
1251 //resourcemacros Resources keep track of the thread which used them, you cant ANNOUNCE/FORGET
1252 //resourcemacros ENTER/LEAVE the same handle from different threads.
1253 //resourcemacros
1255 #define NOBUG_RESOURCE_LEAVE_LOOKUP(flag, handle, ptr) \
1256 NOBUG_IF_ALPHA( do { \
1257 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1258 NOBUG_LOG_(&NOBUG_FLAG(flag), NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \
1259 "RESOURCE_LEAVE", "%s: %s@%p: @%p", \
1260 handle?handle->type:"", \
1261 handle?handle->name:"", \
1262 handle?handle->object_id:"", \
1263 ptr); \
1265 NOBUG_ASSERTN_(nobug_resource_leave (nobug_resource_find(handle, ptr)), \
1266 "RESOURCE_LEAVE", NOBUG_LOCATION_INFO, "%s: %s@%p: @%p: %s", \
1267 handle?handle->type:"", \
1268 handle?handle->name:"", \
1269 handle?handle->object_id:"", \
1270 ptr, nobug_resource_error); \
1271 } while (0))
1275 //index.resource_dump xref:NOBUG_RESOURCE_DUMP[NOBUG_RESOURCE_DUMP]:: dump the state of a single resource
1276 //resourcemacros [[NOBUG_RESOURCE_DUMP]]
1277 //resourcemacros
1278 //resourcemacros .NOBUG_RESOURCE_DUMP
1279 //resourcemacros NOBUG_RESOURCE_DUMP(flag, handle)
1280 //resourcemacros
1281 //resourcemacros Dump the state of a single resource.
1282 //resourcemacros
1283 //resourcemacros `flag`::
1284 //resourcemacros Nobug flag for the log channel
1285 //resourcemacros `handle`::
1286 //resourcemacros handle of the resource to be dumped
1287 //resourcemacros
1289 #define NOBUG_RESOURCE_DUMP(flag, handle) \
1290 do { NOBUG_RESOURCE_LOCK; \
1291 nobug_resource_dump (handle, &(struct nobug_resource_dump_context) \
1292 {&NOBUG_FLAG(flag), \
1293 NOBUG_RESOURCE_LOG_LEVEL, \
1294 __FILE__, __LINE__, __func__}); \
1295 NOBUG_RESOURCE_UNLOCK; } while (0)
1297 #define NOBUG_RESOURCE_DUMP_IF(when, flag, handle) \
1298 NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \
1299 nobug_resource_dump (handle, &(struct nobug_resource_dump_context) \
1300 {&NOBUG_FLAG(flag), \
1301 NOBUG_RESOURCE_LOG_LEVEL, \
1302 __FILE__, __LINE__, __func__}); \
1303 NOBUG_RESOURCE_UNLOCK)
1307 //index.resource_dumpall xref:NOBUG_RESOURCE_DUMPALL[NOBUG_RESOURCE_DUMPALL]:: dump the state of all resources
1308 //resourcemacros [[NOBUG_RESOURCE_DUMPALL]]
1309 //resourcemacros
1310 //resourcemacros .NOBUG_RESOURCE_DUMPALL
1311 //resourcemacros NOBUG_RESOURCE_DUMPALL(flag)
1312 //resourcemacros
1313 //resourcemacros Dump the state of all resources.
1314 //resourcemacros
1315 //resourcemacros `flag`::
1316 //resourcemacros Nobug flag for the log channel
1317 //resourcemacros
1319 #define NOBUG_RESOURCE_DUMPALL(flag) \
1320 do { NOBUG_RESOURCE_LOCK; \
1321 nobug_resource_dump_all (&(struct nobug_resource_dump_context) \
1322 {&NOBUG_FLAG(flag), \
1323 NOBUG_RESOURCE_LOG_LEVEL, \
1324 __FILE__, __LINE__, __func__}); \
1325 NOBUG_RESOURCE_UNLOCK; } while (0)
1327 #define NOBUG_RESOURCE_DUMPALL_IF(when, flag) \
1328 NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \
1329 nobug_resource_dump_all (&(struct nobug_resource_dump_context) \
1330 {&NOBUG_FLAG(flag), \
1331 NOBUG_RESOURCE_LOG_LEVEL, \
1332 __FILE__, __LINE__, __func__}); \
1333 NOBUG_RESOURCE_UNLOCK)
1337 //index.resource_list xref:NOBUG_RESOURCE_LIST[NOBUG_RESOURCE_LIST]:: enumerate all registered resources
1338 //resourcemacros [[NOBUG_RESOURCE_LIST]]
1339 //resourcemacros
1340 //resourcemacros .NOBUG_RESOURCE_LIST
1341 //resourcemacros NOBUG_RESOURCE_LIST(flag)
1342 //resourcemacros
1343 //resourcemacros List all registered resources.
1344 //resourcemacros
1345 //resourcemacros `flag`::
1346 //resourcemacros Nobug flag for the log channel
1347 //resourcemacros
1349 #define NOBUG_RESOURCE_LIST(flag) \
1350 do { NOBUG_RESOURCE_LOCK; \
1351 nobug_resource_list (&(struct nobug_resource_dump_context) \
1352 {&NOBUG_FLAG(flag), \
1353 NOBUG_RESOURCE_LOG_LEVEL, \
1354 __FILE__, __LINE__, __func__}); \
1355 NOBUG_RESOURCE_UNLOCK; } while (0)
1357 #define NOBUG_RESOURCE_LIST_IF(when, flag) \
1358 NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \
1359 nobug_resource_list (&(struct nobug_resource_dump_context) \
1360 {&NOBUG_FLAG(flag), \
1361 NOBUG_RESOURCE_LOG_LEVEL, \
1362 __FILE__, __LINE__, __func__}); \
1363 NOBUG_RESOURCE_UNLOCK)
1367 threading support
1369 #if NOBUG_USE_PTHREAD
1370 #define NOBUG_THREAD_ID_SET(name) nobug_thread_id_set(name)
1371 #define NOBUG_THREAD_ID_FMT(pre,post) pre "%s" post
1372 #define NOBUG_THREAD_ID_COMMA , NOBUG_THREAD_ID_GET
1373 #define NOBUG_THREAD_ID_GET nobug_thread_id_get()
1375 #else
1376 #define NOBUG_THREAD_ID_SET(name)
1377 #define NOBUG_THREAD_ID_FMT(pre,post) ""
1378 #define NOBUG_THREAD_ID_COMMA
1379 #define NOBUG_THREAD_ID_GET ""
1380 #endif
1384 Source location
1386 #define NOBUG_LOCATION_ARGS NOBUG_BASENAME(__FILE__), __LINE__, __func__
1387 #define NOBUG_LOCATION_INFO (NOBUG_LOCATION_ARGS)
1389 #define NOBUG_LOCATION_FILE(file, _1, _2) file
1390 #define NOBUG_LOCATION_LINE(_1, line, _2) line
1391 #define NOBUG_LOCATION_FUNC(_1, _2, func) func
1395 Debuggers
1398 #define NOBUG_DBG_NONE 0
1399 #define NOBUG_DBG_GDB 1
1400 #define NOBUG_DBG_VALGRIND 2
1402 #define NOBUG_ACTIVE_DBG \
1403 NOBUG_IF(NOBUG_USE_VALGRIND, (RUNNING_ON_VALGRIND?2:0)) \
1404 NOBUG_IFNOT(NOBUG_USE_VALGRIND, 0)
1407 //index.backtrace xref:BACKTRACE[BACKTRACE]:: generate a backtrace
1408 //toolmacros [[BACKTRACE]]
1409 //toolmacros .BACKTRACE
1410 //toolmacros BACKTRACE
1411 //toolmacros
1412 //toolmacros The backtrace macro logs a stacktrace using the NoBug facilities.
1413 //toolmacros This is automatically called when NoBug finds an error and is due
1414 //toolmacros to abort. But one might call it manually too.
1415 //toolmacros
1417 #define NOBUG_BACKTRACE \
1418 NOBUG_IF_ALPHA( \
1419 switch (NOBUG_ACTIVE_DBG) { \
1420 case NOBUG_DBG_VALGRIND: \
1421 NOBUG_BACKTRACE_VALGRIND; \
1422 break; \
1423 default: \
1424 NOBUG_BACKTRACE_GLIBC; \
1425 }) \
1426 NOBUG_IF_NOT_ALPHA (NOBUG_BACKTRACE_GLIBC)
1428 #define NOBUG_BACKTRACE_DBG \
1429 NOBUG_IF_NOT_RELEASE( \
1430 switch (NOBUG_ACTIVE_DBG) { \
1431 case NOBUG_DBG_VALGRIND: \
1432 NOBUG_BACKTRACE_VALGRIND; \
1433 break; \
1436 #define NOBUG_BACKTRACE_GDB fprintf(stderr, "UNIMPLEMENTED : GDB Backtraces\n")
1438 #define NOBUG_BACKTRACE_VALGRIND \
1439 NOBUG_IF(NOBUG_USE_VALGRIND, \
1440 VALGRIND_PRINTF_BACKTRACE("BACKTRACE: %s@%d %s", \
1441 NOBUG_LOCATION_ARGS))
1444 #ifndef NOBUG_BACKTRACE_DEPTH
1445 #define NOBUG_BACKTRACE_DEPTH 256
1446 #endif
1448 #define NOBUG_BACKTRACE_GLIBC \
1449 NOBUG_IF_NOT_RELEASE( \
1450 NOBUG_IF(NOBUG_USE_EXECINFO, do { \
1451 NOBUG_LOG_(&nobug_flag_NOBUG_ON, LOG_NOTICE, NOBUG_LOCATION_INFO, "BACKTRACE"); \
1452 nobug_backtrace_glibc (); \
1453 } while (0)))
1457 init and other function wrapers
1459 #define NOBUG_INIT nobug_init()
1460 #define NOBUG_BASENAME(name) (strrchr((name), '/')?strrchr((name), '/') + 1 : (name))
1461 #define NOBUG_ABORT abort()
1465 short macros without NOBUG_
1467 #ifndef NOBUG_DISABLE_SHORTNAMES
1468 #ifndef REQUIRE
1469 #define REQUIRE NOBUG_REQUIRE
1470 #endif
1471 #ifndef REQUIRE_DBG
1472 #define REQUIRE_DBG NOBUG_REQUIRE_DBG
1473 #endif
1474 #ifndef REQUIRE_IF
1475 #define REQUIRE_IF NOBUG_REQUIRE_IF
1476 #endif
1477 #ifndef REQUIRE_IF_DBG
1478 #define REQUIRE_IF_DBG NOBUG_REQUIRE_IF_DBG
1479 #endif
1480 #ifndef ENSURE
1481 #define ENSURE NOBUG_ENSURE
1482 #endif
1483 #ifndef ENSURE_DBG
1484 #define ENSURE_DBG NOBUG_ENSURE_DBG
1485 #endif
1486 #ifndef ENSURE_IF
1487 #define ENSURE_IF NOBUG_ENSURE_IF
1488 #endif
1489 #ifndef ENSURE_IF_DBG
1490 #define ENSURE_IF_DBG NOBUG_ENSURE_IF_DBG
1491 #endif
1492 #ifndef ASSERT
1493 #define ASSERT NOBUG_ASSERT
1494 #endif
1495 #ifndef ASSERT_DBG
1496 #define ASSERT_DBG NOBUG_ASSERT_DBG
1497 #endif
1498 #ifndef ASSERT_IF
1499 #define ASSERT_IF NOBUG_ASSERT_IF
1500 #endif
1501 #ifndef ASSERT_IF_DBG
1502 #define ASSERT_IF_DBG NOBUG_ASSERT_IF_DBG
1503 #endif
1504 #ifndef CHECK
1505 #define CHECK NOBUG_CHECK
1506 #endif
1507 #ifndef CHECK
1508 #define CHECK NOBUG_CHECK
1509 #endif
1510 #ifndef INVARIANT
1511 #define INVARIANT NOBUG_INVARIANT
1512 #endif
1513 #ifndef INVARIANT_DBG
1514 #define INVARIANT_DBG NOBUG_INVARIANT_DBG
1515 #endif
1516 #ifndef INVARIANT_IF
1517 #define INVARIANT_IF NOBUG_INVARIANT_IF
1518 #endif
1519 #ifndef INVARIANT_IF_DBG
1520 #define INVARIANT_IF_DBG NOBUG_INVARIANT_IF_DBG
1521 #endif
1522 #ifndef INVARIANT_ASSERT
1523 #define INVARIANT_ASSERT NOBUG_INVARIANT_ASSERT
1524 #endif
1525 #ifndef DUMP
1526 #define DUMP NOBUG_DUMP
1527 #endif
1528 #ifndef DUMP_DBG
1529 #define DUMP_DBG NOBUG_DUMP_DBG
1530 #endif
1531 #ifndef DUMP_IF
1532 #define DUMP_IF NOBUG_DUMP_IF
1533 #endif
1534 #ifndef DUMP_IF_DBG
1535 #define DUMP_IF_DBG NOBUG_DUMP_IF_DBG
1536 #endif
1537 #ifndef DUMP_LOG
1538 #define DUMP_LOG NOBUG_DUMP_LOG
1539 #endif
1540 #ifndef DUMP_LOG_DBG
1541 #define DUMP_LOG_DBG NOBUG_DUMP_LOG_DBG
1542 #endif
1543 #ifndef DUMP_LOG_IF
1544 #define DUMP_LOG_IF NOBUG_DUMP_LOG_IF
1545 #endif
1546 #ifndef DUMP_LOG_IF_DBG
1547 #define DUMP_LOG_IF_DBG NOBUG_DUMP_LOG_IF_DBG
1548 #endif
1549 #ifndef LOG
1550 #define LOG NOBUG_LOG
1551 #endif
1552 #ifndef LOG_DBG
1553 #define LOG_DBG NOBUG_LOG_DBG
1554 #endif
1555 #ifndef LOG_IF
1556 #define LOG_IF NOBUG_LOG_IF
1557 #endif
1558 #ifndef LOG_IF_DBG
1559 #define LOG_IF_DBG NOBUG_LOG_IF_DBG
1560 #endif
1561 #ifndef ECHO
1562 #define ECHO NOBUG_ECHO
1563 #endif
1564 #ifndef ALERT
1565 #define ALERT NOBUG_ALERT
1566 #endif
1567 #ifndef ALERT_DBG
1568 #define ALERT_DBG NOBUG_ALERT_DBG
1569 #endif
1570 #ifndef ALERT_IF
1571 #define ALERT_IF NOBUG_ALERT_IF
1572 #endif
1573 #ifndef ALERT_IF_DBG
1574 #define ALERT_IF_DBG NOBUG_ALERT_IF_DBG
1575 #endif
1576 #ifndef CRITICAL
1577 #define CRITICAL NOBUG_CRITICAL
1578 #endif
1579 #ifndef CRITICAL_DBG
1580 #define CRITICAL_DBG NOBUG_CRITICAL_DBG
1581 #endif
1582 #ifndef CRITICAL_IF
1583 #define CRITICAL_IF NOBUG_CRITICAL_IF
1584 #endif
1585 #ifndef CRITICAL_IF_DBG
1586 #define CRITICAL_IF_DBG NOBUG_CRITICAL_IF_DBG
1587 #endif
1588 #ifndef ERROR
1589 #define ERROR NOBUG_ERROR
1590 #endif
1591 #ifndef ERROR_DBG
1592 #define ERROR_DBG NOBUG_ERROR_DBG
1593 #endif
1594 #ifndef ERROR_IF
1595 #define ERROR_IF NOBUG_ERROR_IF
1596 #endif
1597 #ifndef ERROR_IF_DBG
1598 #define ERROR_IF_DBG NOBUG_ERROR_IF_DBG
1599 #endif
1600 #ifndef WARN
1601 #define WARN NOBUG_WARN
1602 #endif
1603 #ifndef WARN_DBG
1604 #define WARN_DBG NOBUG_WARN_DBG
1605 #endif
1606 #ifndef WARN_IF
1607 #define WARN_IF NOBUG_WARN_IF
1608 #endif
1609 #ifndef WARN_IF_DBG
1610 #define WARN_IF_DBG NOBUG_WARN_IF_DBG
1611 #endif
1612 #ifndef INFO
1613 #define INFO NOBUG_INFO
1614 #endif
1615 #ifndef INFO_DBG
1616 #define INFO_DBG NOBUG_INFO_DBG
1617 #endif
1618 #ifndef INFO_IF
1619 #define INFO_IF NOBUG_INFO_IF
1620 #endif
1621 #ifndef INFO_IF_DBG
1622 #define INFO_IF_DBG NOBUG_INFO_IF_DBG
1623 #endif
1624 #ifndef NOTICE
1625 #define NOTICE NOBUG_NOTICE
1626 #endif
1627 #ifndef NOTICE_DBG
1628 #define NOTICE_DBG NOBUG_NOTICE_DBG
1629 #endif
1630 #ifndef NOTICE_IF
1631 #define NOTICE_IF NOBUG_NOTICE_IF
1632 #endif
1633 #ifndef NOTICE_IF_DBG
1634 #define NOTICE_IF_DBG NOBUG_NOTICE_IF_DBG
1635 #endif
1636 #ifndef TRACE
1637 #define TRACE NOBUG_TRACE
1638 #endif
1639 #ifndef TRACE_DBG
1640 #define TRACE_DBG NOBUG_TRACE_DBG
1641 #endif
1642 #ifndef TRACE_IF
1643 #define TRACE_IF NOBUG_TRACE_IF
1644 #endif
1645 #ifndef TRACE_IF_DBG
1646 #define TRACE_IF_DBG NOBUG_TRACE_IF_DBG
1647 #endif
1648 #ifndef BACKTRACE
1649 #define BACKTRACE NOBUG_BACKTRACE
1650 #endif
1651 #ifndef BACKTRACE_DBG
1652 #define BACKTRACE_DBG NOBUG_BACKTRACE_DBG
1653 #endif
1654 #ifndef DEPRECATED
1655 #define DEPRECATED NOBUG_DEPRECATED
1656 #endif
1657 #ifndef UNIMPLEMENTED
1658 #define UNIMPLEMENTED NOBUG_UNIMPLEMENTED
1659 #endif
1660 #ifndef FIXME
1661 #define FIXME NOBUG_FIXME
1662 #endif
1663 #ifndef TODO
1664 #define TODO NOBUG_TODO
1665 #endif
1666 #ifndef PLANNED
1667 #define PLANNED NOBUG_PLANNED
1668 #endif
1669 #ifndef NOTREACHED
1670 #define NOTREACHED NOBUG_NOTREACHED
1671 #endif
1672 #ifndef INJECT_GOODBAD
1673 #define INJECT_GOODBAD NOBUG_INJECT_GOODBAD
1674 #endif
1675 #ifndef INJECT_FAULT
1676 #define INJECT_FAULT NOBUG_INJECT_FAULT
1677 #endif
1678 #ifndef CLEANUP
1679 #define CLEANUP NOBUG_CLEANUP
1680 #endif
1681 #ifndef CHECKED
1682 #define CHECKED NOBUG_CHECKED
1683 #endif
1684 #ifndef UNCHECKED
1685 #define UNCHECKED NOBUG_UNCHECKED
1686 #endif
1687 #ifndef RESOURCE_ANNOUNCE
1688 #define RESOURCE_ANNOUNCE NOBUG_RESOURCE_ANNOUNCE
1689 #endif
1690 #ifndef RESOURCE_FORGET
1691 #define RESOURCE_FORGET NOBUG_RESOURCE_FORGET
1692 #endif
1693 #ifndef RESOURCE_ENTER
1694 #define RESOURCE_ENTER NOBUG_RESOURCE_ENTER
1695 #endif
1696 #ifndef RESOURCE_STATE
1697 #define RESOURCE_STATE NOBUG_RESOURCE_STATE
1698 #endif
1699 #ifndef RESOURCE_LEAVE
1700 #define RESOURCE_LEAVE NOBUG_RESOURCE_LEAVE
1701 #endif
1702 #ifndef RESOURCE_LEAVE_LOOKUP
1703 #define RESOURCE_LEAVE_LOOKUP NOBUG_RESOURCE_LEAVE_LOOKUP
1704 #endif
1705 #ifndef RESOURCE_HANDLE
1706 #define RESOURCE_HANDLE NOBUG_RESOURCE_HANDLE
1707 #endif
1708 #ifndef RESOURCE_HANDLE_INIT
1709 #define RESOURCE_HANDLE_INIT NOBUG_RESOURCE_HANDLE_INIT
1710 #endif
1711 #ifndef RESOURCE_DUMP
1712 #define RESOURCE_DUMP NOBUG_RESOURCE_DUMP
1713 #endif
1714 #ifndef RESOURCE_DUMP_IF
1715 #define RESOURCE_DUMP_IF NOBUG_RESOURCE_DUMP_IF
1716 #endif
1717 #ifndef RESOURCE_DUMPALL
1718 #define RESOURCE_DUMPALL NOBUG_RESOURCE_DUMPALL
1719 #endif
1720 #ifndef RESOURCE_DUMPALL_IF
1721 #define RESOURCE_DUMPALL_IF NOBUG_RESOURCE_DUMPALL_IF
1722 #endif
1723 #ifndef RESOURCE_LIST
1724 #define RESOURCE_LIST NOBUG_RESOURCE_LIST
1725 #endif
1726 #ifndef RESOURCE_LIST_IF
1727 #define RESOURCE_LIST_IF NOBUG_RESOURCE_LIST_IF
1728 #endif
1729 #endif /* NOBUG_DISABLE_SHORTNAMES */
1733 Tool macros
1735 #ifdef __GNUC__
1736 #define NOBUG_CLEANUP(fn) NOBUG_IF_ALPHA(__attribute__((cleanup(fn))))
1737 #else
1738 #define NOBUG_CLEANUP(fn)
1739 #endif
1742 //index.comma xref:NOBUG_ALPHA_COMMA[NOBUG_ALPHA_COMMA]:: append something after a comma in *ALPHA* builds
1743 //toolmacros [[NOBUG_ALPHA_COMMA]]
1744 //toolmacros .NOBUG_ALPHA_COMMA
1745 //toolmacros NOBUG_ALPHA_COMMA(something)
1746 //toolmacros NOBUG_ALPHA_COMMA_NULL
1747 //toolmacros
1748 //toolmacros Sometimes it is useful to have initializer code only in *ALPHA* builds, for example when you
1749 //toolmacros conditionally include resource handles only in *ALPHA* versions. An initializer can then
1750 //toolmacros use this macros to append a comman and something else only in *ALPHA* builds as in:
1751 //toolmacros struct foo = {"foo", "bar" NOBUG_ALPHA_COMMA_NULL };
1752 //toolmacros
1754 #define NOBUG_COMMA ,
1755 #define NOBUG_ALPHA_COMMA(something) NOBUG_IF_ALPHA(NOBUG_COMMA something)
1756 #define NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA(NULL)
1758 #define NOBUG_ONCE(code) \
1759 do { \
1760 static volatile int NOBUG_CAT(nobug_once_,__LINE__) = 0; \
1761 if (NOBUG_EXPECT_FALSE(!NOBUG_CAT(nobug_once_,__LINE__))) \
1763 NOBUG_CAT(nobug_once_,__LINE__) = 1; \
1764 code; \
1766 } while (0)
1768 #if __GNUC__
1769 #define NOBUG_EXPECT_FALSE(x) __builtin_expect(!!(x),0)
1770 #else
1771 #define NOBUG_EXPECT_FALSE(x) x
1772 #endif
1774 #define NOBUG_WHEN(when, ...) \
1775 do{ if (NOBUG_EXPECT_FALSE(when)){ __VA_ARGS__;}} while(0)
1778 //index.if xref:NOBUG_IF[NOBUG_IF_*]:: include code conditionally on build level
1779 //toolmacros [[NOBUG_IF]]
1780 //toolmacros .NOBUG_IF_*
1781 //toolmacros NOBUG_IF_ALPHA(...)
1782 //toolmacros NOBUG_IF_NOT_ALPHA(...)
1783 //toolmacros NOBUG_IF_BETA(...)
1784 //toolmacros NOBUG_IF_NOT_BETA(...)
1785 //toolmacros NOBUG_IF_RELEASE(...)
1786 //toolmacros NOBUG_IF_NOT_RELEASE(...)
1787 //toolmacros
1788 //toolmacros This macros allow one to conditionally include the code in '(...)' only if the
1789 //toolmacros criteria on the build level is met. If not, nothing gets substituted. Mostly used
1790 //toolmacros internally, but can also be used for custom things.
1791 //toolmacros
1793 #define NOBUG_IF_ALPHA(...) \
1794 NOBUG_IF(NOBUG_MODE_ALPHA, __VA_ARGS__) \
1796 #define NOBUG_IF_NOT_ALPHA(...) \
1797 NOBUG_IFNOT(NOBUG_MODE_ALPHA, __VA_ARGS__) \
1799 #define NOBUG_IF_BETA(...) \
1800 NOBUG_IF(NOBUG_MODE_BETA, __VA_ARGS__) \
1802 #define NOBUG_IF_NOT_BETA(...) \
1803 NOBUG_IFNOT(NOBUG_MODE_BETA, __VA_ARGS__) \
1805 #define NOBUG_IF_RELEASE(...) \
1806 NOBUG_IF(NOBUG_MODE_RELEASE, __VA_ARGS__) \
1808 #define NOBUG_IF_NOT_RELEASE(...) \
1809 NOBUG_IFNOT(NOBUG_MODE_RELEASE, __VA_ARGS__) \
1812 preprocessor hacks/metaprogramming
1815 #define NOBUG_IF(bool, ...) NOBUG_CAT(NOBUG_IF_,bool)(__VA_ARGS__)
1816 #define NOBUG_IF_1(...) __VA_ARGS__
1817 #define NOBUG_IF_0(...)
1819 #define NOBUG_IFNOT(bool, ...) NOBUG_CAT(NOBUG_IF_, NOBUG_NOT(bool))(__VA_ARGS__)
1821 #define NOBUG_NOT(bool) NOBUG_CAT(NOBUG_NOT_, bool)
1822 #define NOBUG_NOT_1 0
1823 #define NOBUG_NOT_0 1
1825 #define NOBUG_AND(a,b) NOBUG_CAT3(NOBUG_AND_, a, b)
1826 #define NOBUG_AND_00 0
1827 #define NOBUG_AND_01 0
1828 #define NOBUG_AND_10 0
1829 #define NOBUG_AND_11 1
1831 #define NOBUG_OR(a,b) NOBUG_CAT3(NOBUG_OR_, a, b)
1832 #define NOBUG_OR_00 0
1833 #define NOBUG_OR_01 1
1834 #define NOBUG_OR_10 1
1835 #define NOBUG_OR_11 1
1837 #define NOBUG_XOR(a,b) NOBUG_CAT( NOBUG_XOR_, NOBUG_CAT(a,b))
1838 #define NOBUG_XOR_00 0
1839 #define NOBUG_XOR_01 1
1840 #define NOBUG_XOR_10 1
1841 #define NOBUG_XOR_11 0
1843 #define NOBUG_CAT(a,b) NOBUG_CAT_(a,b)
1844 #define NOBUG_CAT_(a,b) a##b
1846 #define NOBUG_CAT3(a,b,c) NOBUG_CAT3_(a,b,c)
1847 #define NOBUG_CAT3_(a,b,c) a##b##c
1849 #define NOBUG_HEAD(head, ...) head
1850 #define NOBUG_TAIL(_, ...) , ## __VA_ARGS__
1852 #define NOBUG_STRINGIZE(s) NOBUG_STRINGIZE_(s)
1853 #define NOBUG_STRINGIZE_(s) #s
1857 LIBNOBUG DECLARATIONS
1859 #ifdef __cplusplus
1860 extern "C" {
1861 #endif
1864 envvar control
1866 enum nobug_log_targets
1868 NOBUG_TARGET_RINGBUFFER,
1869 NOBUG_TARGET_CONSOLE,
1870 NOBUG_TARGET_FILE,
1871 NOBUG_TARGET_SYSLOG,
1872 NOBUG_TARGET_APPLICATION
1875 struct nobug_flag
1877 const char* name;
1878 struct nobug_flag* parent;
1879 int initialized;
1880 int limits[5];
1881 struct nobug_ringbuffer* ringbuffer_target;
1882 FILE* console_target;
1883 FILE* file_target;
1887 nobug_env_parse_flag (const char* env, struct nobug_flag* flag, int default_target, int default_limit);
1890 nobug_env_init_flag (struct nobug_flag* flag, int default_target, int default_limit);
1894 ringbuffer
1896 struct nobug_ringbuffer
1898 char* pos;
1899 char* start;
1900 size_t size;
1901 size_t maxmsg;
1902 char name[256];
1905 enum nobug_ringbuffer_flags
1907 NOBUG_RINGBUFFER_DEFAULT, /* Default is to overwrite file and delete it on nobug_ringbuffer_destroy */
1908 NOBUG_RINGBUFFER_APPEND = 1, /* use existing backing file, append if possible */
1909 NOBUG_RINGBUFFER_TEMP = 2, /* unlink file instantly */
1910 NOBUG_RINGBUFFER_KEEP = 4 /* dont unlink the file at destroy */
1914 Note: some flags conflict (TEMP with KEEP) nobug_ringbuffer will not error on these but continue gracefully
1915 with sane (but undefined) semantics.
1917 struct nobug_ringbuffer*
1918 nobug_ringbuffer_init (struct nobug_ringbuffer* self, size_t size,
1919 const char * name, int flags);
1921 struct nobug_ringbuffer*
1922 nobug_ringbuffer_new (size_t size, const char * name, int flags);
1924 struct nobug_ringbuffer*
1925 nobug_ringbuffer_destroy (struct nobug_ringbuffer* self);
1927 void
1928 nobug_ringbuffer_delete (struct nobug_ringbuffer* self);
1931 nobug_ringbuffer_vprintf (struct nobug_ringbuffer* self, const char* fmt, va_list ap);
1934 nobug_ringbuffer_printf (struct nobug_ringbuffer* self, const char* fmt, ...);
1936 char*
1937 nobug_ringbuffer_prev (struct nobug_ringbuffer* self, char* pos);
1939 char*
1940 nobug_ringbuffer_next (struct nobug_ringbuffer* self, char* pos);
1943 nobug_ringbuffer_save (struct nobug_ringbuffer* self, FILE* out);
1946 nobug_ringbuffer_load (struct nobug_ringbuffer* self, FILE* in);
1948 char*
1949 nobug_ringbuffer_pos (struct nobug_ringbuffer* self);
1951 void
1952 nobug_ringbuffer_pop (struct nobug_ringbuffer* self);
1956 multithreading extras
1958 #if NOBUG_USE_PTHREAD
1960 struct nobug_tls_data
1962 const char * thread_id;
1963 struct nobug_resource_record* lock_stack;
1966 extern pthread_key_t nobug_tls_key;
1968 const char*
1969 nobug_thread_id_set (const char* name);
1971 const char*
1972 nobug_thread_id_get (void);
1974 extern pthread_mutex_t nobug_logging_mutex;
1975 extern pthread_mutex_t nobug_resource_mutex;
1976 #endif
1980 resource registry
1983 #if NOBUG_USE_PTHREAD
1984 #define NOBUG_RESOURCE_LOCK pthread_mutex_lock (&nobug_resource_mutex)
1985 #define NOBUG_RESOURCE_UNLOCK pthread_mutex_unlock (&nobug_resource_mutex)
1987 Note: errors are all fatal, we dont unlock the mutex when returning an error,
1988 this should be handled better in future but does not hurt
1990 #else
1991 #define NOBUG_RESOURCE_LOCK
1992 #define NOBUG_RESOURCE_UNLOCK
1993 #endif
1997 struct nobug_resource_record
1999 const char* type; /* type or state */
2000 const char* name; /* name */
2001 void* object_id; /* unique identifer, usually a this pointer or similar */
2002 void* data; /* tree of holders in resouces, backpointer to resource in holders */
2003 const char* extra; /* extra information, file/line usually */
2004 unsigned long counter; /* users or recursive enters */
2005 #if NOBUG_USE_PTHREAD
2006 const char* thread_id;
2007 struct nobug_resource_record* link; /* used by the deadlock detector */
2008 #endif
2011 enum nobug_resource_state
2013 NOBUG_RESOURCE_WAITING,
2014 //NOBUG_RESOURCE_TRYING,
2015 NOBUG_RESOURCE_EXCLUSIVE,
2016 NOBUG_RESOURCE_RECURSIVE
2019 extern void* nobug_resource_registry;
2021 extern const char* nobug_resource_error;
2023 extern const char* nobug_resource_states[];
2025 struct nobug_resource_record*
2026 nobug_resource_announce (const char* type, const char* name, const void* object_id, const char* extra);
2029 nobug_resource_forget (struct nobug_resource_record* node);
2031 struct nobug_resource_record*
2032 nobug_resource_find (const struct nobug_resource_record* resource, const void* object_id);
2034 struct nobug_resource_record*
2035 nobug_resource_enter (struct nobug_resource_record* resource,
2036 const char* name,
2037 const void* object_id,
2038 enum nobug_resource_state state,
2039 const char* extra);
2042 nobug_resource_state (struct nobug_resource_record* resource,
2043 enum nobug_resource_state state);
2046 nobug_resource_leave (struct nobug_resource_record* handle);
2049 struct nobug_resource_dump_context
2051 struct nobug_flag* flag;
2052 int level;
2053 const char* file;
2054 int line;
2055 const char* func;
2058 void
2059 nobug_resource_dump (struct nobug_resource_record* resource, struct nobug_resource_dump_context* context);
2061 void
2062 nobug_resource_dump_all (struct nobug_resource_dump_context* context);
2064 void
2065 nobug_resource_list (struct nobug_resource_dump_context* context);
2069 global config, data and defaults
2071 extern const char* nobug_resource_acquired;
2073 void nobug_init (void);
2075 void
2076 nobug_backtrace_glibc (void);
2078 void
2079 nobug_log (struct nobug_flag* flag, int lvl, const char* fmt, ...);
2081 typedef void (*nobug_application_cb)(struct nobug_flag* flag, int priority, const char *log, void* data);
2083 extern struct nobug_ringbuffer nobug_default_ringbuffer;
2084 extern FILE* nobug_default_file;
2085 extern struct nobug_flag nobug_flag_NOBUG_ON;
2086 extern struct nobug_flag nobug_flag_NOBUG_ANN;
2087 extern struct nobug_flag nobug_flag_nobug;
2088 extern unsigned long long nobug_counter;
2089 extern nobug_application_cb nobug_callback;
2090 extern void* nobug_callback_data;
2092 #ifdef __cplusplus
2093 } /* extern "C" */
2094 #endif
2096 #ifndef NOBUG_LIBNOBUG_C
2099 tag this translation unit as unchecked in ALPHA and BETA builds
2101 NOBUG_IF_NOT_RELEASE(NOBUG_UNCHECKED);
2103 #endif /* NOBUG_LIBNOBUG_C */
2104 #endif