-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.h
155 lines (124 loc) · 4.58 KB
/
debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* @file debug.h
* @author Jens Munk Hansen <[email protected]>
* @date Sun Jul 12 16:16:54 2015
*
* @brief Debug macros
*
* Include this and define SPS_DEBUG to use the debug macros.
*
*/
/*
* This file is part of SOFUS.
*
* SOFUS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SOFUS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SOFUS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <sps/stdio.h>
#include <sps/cenv.h>
#ifndef SPS_DEBUG
# define SPS_DEBUG 0
#endif
#ifndef __SPS_FUNCTION__
# ifdef _MSC_VER
# define __SPS_FUNCTION__ __FUNCTION__
# else
# define __SPS_FUNCTION__ __FUNCTION__
# endif
#endif
#ifndef __SPS_PRETTY_FUNCTION__
# ifdef _MSC_VER
# define __SPS_PRETTY_FUNCTION__ __FUNCSIG__
# else
# define __SPS_PRETTY_FUNCTION__ __PRETTY_FUNCTION__
# endif
#endif
#ifndef __SPS_DEBUG_FILE__
# define __SPS_DEBUG_FILE__ 0
#endif
#ifndef __SPS_DEBUG_LINE__
# define __SPS_DEBUG_LINE__ 1
#endif
#ifndef __SPS_DEBUG_FUNCTION__
# define __SPS_DEBUG_FUNCTION__ 1
#endif
/*!
* #MULTI_LINE_MACRO_BEGIN() can be used for creating other macros, which
* work inside conditional statements, e.g.
*
* if (cond) SOME_MACRO(i++; j--;)
*/
#define MULTI_LINE_MACRO_BEGIN do {
/*!
* #MULTI_LINE_MACRO_END() is the closing part for #MULTI_LINE_MACRO_BEGIN()
*/
#define MULTI_LINE_MACRO_END \
__pragma(warning(push)) \
__pragma(warning(disable: 4127)) \
} while(0) \
__pragma(warning(pop))
// Compiler bug in MSVC. Disabling C4127 is not working for a do { } while (0)
#if defined(_WIN32)
# if (__SPS_DEBUG_FUNCTION__) && (__SPS_DEBUG_LINE__) && (__SPS_DEBUG_FILE__)
# define debug_print(fmt, ...) \
__pragma(warning(push)) \
__pragma(warning(disable: 4127)) \
for(;;) { \
if (SPS_DEBUG) fprintf(stderr, "%s:%s():%d: " fmt, __FILE__, __SPS_FUNCTION__, __LINE__, __VA_ARGS__); \
} break; } \
__pragma(warning(pop))
# else
# define debug_print(fmt,...) \
__pragma(warning(push)) \
__pragma(warning(disable: 4127)) \
__pragma(warning(disable: 6271)) \
for(;;) { if (SPS_DEBUG) { fprintf(stderr, "%s(): " fmt, __SPS_FUNCTION__, __VA_ARGS__);} break; } \
__pragma(warning(pop))
# endif
# define debug_cond_print(cond, fmt,...) \
__pragma(warning(push)) \
__pragma(warning(disable: 4127)) \
for(;;) { if (cond) { fprintf(stdout, "%s(): " fmt, __SPS_FUNCTION__, __VA_ARGS__);} break; } \
__pragma(warning(pop))
#elif defined(C99)
// C99 is more stricht and do not allow ## __VA_ARGS__
# if (__SPS_DEBUG_FUNCTION__) && (__SPS_DEBUG_LINE__) && (__SPS_DEBUG_FILE__)
# define debug_print(...) \
do { if (SPS_DEBUG) fprintf(stderr, "%s:%d:%s(): " FIRST(__VA_ARGS__), __FILE__, \
__SPS_FUNCTION__, __LINE__ REST(__VA_ARGS__)); } while (0)
# else
# define debug_print(...) \
do { if (SPS_DEBUG) fprintf(stderr, "%s(): " FIRST(__VA_ARGS__), __SPS_FUNCTION__ REST(__VA_ARGS__)); } while (0)
# endif
# define debug_cond_print(cond, ...) \
do { if (cond) fprintf(stdout, "%s(): " FIRST(__VA_ARGS__), __SPS_FUNCTION__ REST(__VA_ARGS__)); } while (0)
#else
# if (__SPS_DEBUG_FUNCTION__) && (__SPS_DEBUG_LINE__) && (__SPS_DEBUG_FILE__)
# define debug_print(fmt, ...) \
do { if (SPS_DEBUG) fprintf(stderr, "%s:%s():%d: " fmt, __FILE__, \
__SPS_FUNCTION__, __LINE__, ## __VA_ARGS__); } while (0)
# else
# define debug_print(fmt, ...) \
do { if (SPS_DEBUG) fprintf(stderr, "%s(): " fmt, __SPS_FUNCTION__, ## __VA_ARGS__); } while (0)
# endif
# define debug_cond_print(cond, fmt, ...) \
do { if (cond) fprintf(stdout, "%s:%d " fmt, __FILE__, __LINE__, ## __VA_ARGS__); } while (0)
#endif
// Alternative way
// MULTI_LINE_MACRO_BEGIN if (DEBUG) fprintf(stderr, "%s(): " fmt, __SPS_FUNCTION__, __VA_ARGS__); MULTI_LINE_MACRO_END
/* Local variables: */
/* indent-tabs-mode: nil */
/* tab-width: 2 */
/* c-basic-offset: 2 */
/* End: */