Add 'status' parameter to spawn() and close stdin
[cmus.git] / cmus / debug.h
blob1aa501bcded270237f11759274eab4bae31d4e75
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
21 * DEBUG must be defined before including this file
23 * DEBUG levels:
24 * 0:
25 * 1: BUG, BUG_ON
26 * 2: BUG, BUG_ON, d_print
28 * void debug_init(FILE *stream)
29 * Call this before any other functions in this file.
30 * Sets the debug stream.
32 * void d_print(const char *format, ...)
33 * Print debugging information to the debug stream.
35 * void BUG(const char *format, ...)
36 * Print debugging information to the debug stream and to
37 * stderr if debug stream is NOT stdout or stderr.
38 * Exits with return value 127.
40 * void BUG_ON(int condition)
41 * Calls BUG if condition is true.
44 #ifndef _DEBUG_H
45 #define _DEBUG_H
47 #include <compiler.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <errno.h>
52 #include <sys/time.h>
53 #include <inttypes.h>
55 #if !defined(DEBUG)
56 #error DEBUG not defined
57 #endif
59 #if DEBUG < 0
60 #error DEBUG must be >= 0
61 #endif
63 extern void debug_init(FILE *stream);
64 extern void __debug_bug(const char *function, const char *fmt, ...) __FORMAT(2, 3) __NORETURN;
65 extern void __debug_print(const char *function, const char *fmt, ...) __FORMAT(2, 3);
67 /* ------------------------------------------------------------------------ */
69 #if DEBUG == 0
71 #define BUG(...) do { } while (0)
73 #else /* >0 */
75 #define BUG(...) __debug_bug(__FUNCTION__, __VA_ARGS__)
77 #endif
79 /* ------------------------------------------------------------------------ */
81 #if DEBUG == 0 || DEBUG == 1
83 #define d_print(...) do { } while (0)
85 static inline void timer_get(uint64_t *usec)
87 *usec = 0;
90 static inline void timer_print(const char *what, uint64_t usec)
94 #else /* >1 */
96 #define d_print(...) __debug_print(__FUNCTION__, __VA_ARGS__)
98 static inline void timer_get(uint64_t *usec)
100 struct timeval tv;
102 gettimeofday(&tv, NULL);
103 *usec = tv.tv_sec * 1e6L + tv.tv_usec;
106 static inline void timer_print(const char *what, uint64_t usec)
108 uint64_t a = usec / 1e6;
109 uint64_t b = usec - a * 1e6;
111 __debug_print("TIMER", "%s: %11Lu.%06Lu\n", what, a, b);
114 #endif
116 /* ------------------------------------------------------------------------ */
118 #define __STR(a) #a
120 #define BUG_ON(a) \
121 do { \
122 if (unlikely(a)) \
123 BUG("%s\n", __STR(a)); \
124 } while (0)
126 #endif