]> pd.if.org Git - nbds/blob - include/lwt.h
use a vtable approach for generic map interface
[nbds] / include / lwt.h
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  *
5  * lightweight tracing
6  */ 
7 #ifndef LWT_H
8 #define LWT_H
9
10 #ifndef ENABLE_TRACE
11 #define TRACE(...) do { } while (0)
12 #else
13 #define TRACE(flag, format, v1, v2) lwt_trace(flag, format, (size_t)(v1), (size_t)(v2))
14 #endif
15
16 #ifdef NDEBUG
17 #define ASSERT(x) 
18 #else
19 #define ASSERT(x) if (!(x)) { lwt_halt(); assert(!#x); }
20 #endif
21
22 // Dump trace records to <file_name>. The file should be post-processed with "sort" before viewing.
23 void lwt_dump (const char *file_name) __attribute__ ((externally_visible));
24
25 // <flags> indicates what kind of trace messages should be included in the dump. <flags> is a sequence of letters
26 // followed by numbers (e.g. "x1c9n2g3"). The letters indicate trace categories and the numbers are trace levels 
27 // for each category. If a category appears in <flags>, then messages from that category will be included in the
28 // dump if they have a trace level less than or equal to the one specified in <flags>. Categories are case
29 // sensitive.
30 void lwt_set_trace_level (const char *flags);
31
32 // <flag> is a two character string containing a letter followed by a number (e.g. "f3"). The letter indicates a
33 // trace category, and the number a trace level. <flag> controls whether or not the trace message gets included in
34 // the dump. It is only included when its specified category is enabled at a trace level greater than or equal to
35 // the one in <flag>. Categories are case sensitive. 
36 static inline void lwt_trace (const char *flag, const char *format, size_t value1, size_t value2) {
37     extern char flag_state_[256];
38     if (EXPECT_FALSE(flag_state_[(unsigned)flag[0]] >= flag[1])) {
39         // embed <flags> in <format> so we don't have to make the lwt_record_t any bigger than it already is
40         format = (const char *)((size_t)format | ((uint64_t)flag[0] << 56) | ((uint64_t)flag[1] << 48));
41         extern void lwt_trace_i (const char *format, size_t value1, size_t value2);
42         lwt_trace_i(format, value1, value2);
43     }
44 }
45
46 void lwt_halt (void);
47
48 #endif//LWT_H