Merge branch 'maint-0.4.8'
[tor.git] / src / ext / timeouts / timeout-debug.h
blobfc727a6b4247ffef73c00d2007378c64da551983
1 /*
2 * D E B U G R O U T I N E S
4 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
6 #if TIMEOUT_DEBUG - 0
7 #include <stdlib.h>
8 #include <stdio.h>
10 #undef TIMEOUT_DEBUG
11 #define TIMEOUT_DEBUG 1
12 #define DEBUG_LEVEL timeout_debug
14 static int timeout_debug;
16 #define SAYit_(lvl, fmt, ...) do { \
17 if (DEBUG_LEVEL >= (lvl)) \
18 fprintf(stderr, fmt "%s", __FILE__, __LINE__, __func__, __VA_ARGS__); \
19 } while (0)
21 #define SAYit(lvl, ...) SAYit_((lvl), "%s:%d:%s: " __VA_ARGS__, "\n")
23 #define PANIC(...) do { \
24 SAYit(0, __VA_ARGS__); \
25 _Exit(EXIT_FAILURE); \
26 } while (0)
27 #else
28 #undef TIMEOUT_DEBUG
29 #define TIMEOUT_DEBUG 0
30 #define DEBUG_LEVEL 0
32 #define SAYit(...) (void)0
33 #endif
35 #define SAY(...) SAYit(1, __VA_ARGS__)
36 #define HAI SAY("HAI")
39 static inline char *fmt_(char *buf, uint64_t ts, int wheel_bit, int wheel_num) {
40 char *p = buf;
41 int wheel, n, i;
43 for (wheel = wheel_num - 2; wheel >= 0; wheel--) {
44 n = ((1 << wheel_bit) - 1) & (ts >> (wheel * WHEEL_BIT));
46 for (i = wheel_bit - 1; i >= 0; i--) {
47 *p++ = '0' + !!(n & (1 << i));
50 if (wheel != 0)
51 *p++ = ':';
54 *p = 0;
56 return buf;
57 } /* fmt_() */
59 #define fmt(ts) fmt_(((char[((1 << WHEEL_BIT) * WHEEL_NUM) + WHEEL_NUM + 1]){ 0 }), (ts), WHEEL_BIT, WHEEL_NUM)
62 static inline char *bin64_(char *buf, uint64_t n, int wheel_bit) {
63 char *p = buf;
64 int i;
66 for (i = 0; i < (1 << wheel_bit); i++) {
67 *p++ = "01"[0x1 & (n >> (((1 << wheel_bit) - 1) - i))];
70 *p = 0;
72 return buf;
73 } /* bin64_() */
75 #define bin64(ts) bin64_(((char[((1 << WHEEL_BIT) * WHEEL_NUM) + 1]){ 0 }), (ts), WHEEL_BIT)