add :refresh command
[cmus.git] / debug.h
blobf4cea4c3370202974c6314dfb35c3577cdf82336
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 */
5 #ifndef DEBUG_H
6 #define DEBUG_H
8 #include <compiler.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <sys/time.h>
14 #include <inttypes.h>
16 #if !defined(DEBUG)
17 #error DEBUG not defined
18 #endif
20 #if DEBUG < 0
21 #error DEBUG must be >= 0
22 #endif
24 void debug_init(void);
25 void __debug_bug(const char *function, const char *fmt, ...) __FORMAT(2, 3) __NORETURN;
26 void __debug_print(const char *function, const char *fmt, ...) __FORMAT(2, 3);
28 /* ------------------------------------------------------------------------ */
30 #if DEBUG == 0
32 #define BUG(...) do { } while (0)
34 #else /* >0 */
36 #define BUG(...) __debug_bug(__FUNCTION__, __VA_ARGS__)
38 #endif
40 /* ------------------------------------------------------------------------ */
42 #if DEBUG == 0 || DEBUG == 1
44 #define d_print(...) do { } while (0)
46 static inline void timer_get(uint64_t *usec)
48 *usec = 0;
51 static inline void timer_print(const char *what, uint64_t usec)
55 #else /* >1 */
57 #define d_print(...) __debug_print(__FUNCTION__, __VA_ARGS__)
59 static inline void timer_get(uint64_t *usec)
61 struct timeval tv;
63 gettimeofday(&tv, NULL);
64 *usec = tv.tv_sec * 1e6L + tv.tv_usec;
67 static inline void timer_print(const char *what, uint64_t usec)
69 uint64_t a = usec / 1e6;
70 uint64_t b = usec - a * 1e6;
72 __debug_print("TIMER", "%s: %11Lu.%06Lu\n", what, a, b);
75 #endif
77 /* ------------------------------------------------------------------------ */
79 #define __STR(a) #a
81 #define BUG_ON(a) \
82 do { \
83 if (unlikely(a)) \
84 BUG("%s\n", __STR(a)); \
85 } while (0)
87 #endif