summaryrefslogtreecommitdiffstats
path: root/shell/cc-debug.h.in
blob: 226f82e034cefad16a0ccc5c7c69c91a64fa51c0 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* cc-debug.h.in
 *
 * Copyright © 2018 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <glib.h>

/**
 * SECTION:cc-debug
 * @short_description: Debugging macros
 * @title:Debugging
 * @stability:stable
 *
 * Macros used for tracing and debugging code. These
 * are only valid when Settings is compiled with tracing
 * suppoer (pass `--enable-tracing` to the configure
 * script to do that).
 */

G_BEGIN_DECLS

#ifndef CC_ENABLE_TRACE
# define CC_ENABLE_TRACE @ENABLE_TRACING@
#endif
#if CC_ENABLE_TRACE != 1
# undef CC_ENABLE_TRACE
#endif

/**
 * CC_LOG_LEVEL_TRACE: (skip)
 */
#ifndef CC_LOG_LEVEL_TRACE
# define CC_LOG_LEVEL_TRACE ((GLogLevelFlags)(1 << G_LOG_LEVEL_USER_SHIFT))
#endif

#ifdef CC_ENABLE_TRACE

/**
 * CC_TRACE_MSG:
 * @fmt: printf-like format of the message
 * @...: arguments for @fmt
 *
 * Prints a trace message.
 */
# define CC_TRACE_MSG(fmt, ...)                                        \
   g_log(G_LOG_DOMAIN, CC_LOG_LEVEL_TRACE, "  MSG: %s():%d: " fmt,     \
         G_STRFUNC, __LINE__, ##__VA_ARGS__)

/**
 * CC_PROBE:
 *
 * Prints a probing message. Put this macro in the code when
 * you want to check the program reaches a certain section
 * of code.
 */
# define CC_PROBE                                                      \
   g_log(G_LOG_DOMAIN, CC_LOG_LEVEL_TRACE, "PROBE: %s():%d",           \
         G_STRFUNC, __LINE__)

/**
 * CC_TODO:
 * @_msg: the message to print
 *
 * Prints a TODO message.
 */
# define CC_TODO(_msg)                                                 \
   g_log(G_LOG_DOMAIN, CC_LOG_LEVEL_TRACE, " TODO: %s():%d: %s",       \
         G_STRFUNC, __LINE__, _msg)

/**
 * CC_ENTRY:
 *
 * Prints an entry message. This shouldn't be used in
 * critical functions. Place this at the beggining of
 * the function, before any assertion.
 */
# define CC_ENTRY                                                      \
   g_log(G_LOG_DOMAIN, CC_LOG_LEVEL_TRACE, "ENTRY: %s():%d",           \
         G_STRFUNC, __LINE__)

/**
 * CC_EXIT:
 *
 * Prints an exit message. This shouldn't be used in
 * critical functions. Place this at the end of
 * the function, after any relevant code. If the
 * function returns something, use CC_RETURN()
 * instead.
 */
# define CC_EXIT                                                       \
   G_STMT_START {                                                        \
      g_log(G_LOG_DOMAIN, CC_LOG_LEVEL_TRACE, " EXIT: %s():%d",        \
            G_STRFUNC, __LINE__);                                        \
      return;                                                            \
   } G_STMT_END

/**
 * CC_GOTO:
 * @_l: goto tag
 *
 * Logs a goto jump.
 */
# define CC_GOTO(_l)                                                   \
   G_STMT_START {                                                        \
      g_log(G_LOG_DOMAIN, CC_LOG_LEVEL_TRACE, " GOTO: %s():%d ("#_l")",\
            G_STRFUNC, __LINE__);                                        \
      goto _l;                                                           \
   } G_STMT_END

/**
 * CC_RETURN:
 * @_r: the return value.
 *
 * Prints an exit message, and returns @_r. See #CC_EXIT.
 */
# define CC_RETURN(_r)                                                 \
   G_STMT_START {                                                        \
      g_log(G_LOG_DOMAIN, CC_LOG_LEVEL_TRACE, " EXIT: %s():%d ",       \
            G_STRFUNC, __LINE__);                                        \
      return _r;                                                         \
   } G_STMT_END

#else

/**
 * CC_TODO:
 * @_msg: the message to print
 *
 * Prints a TODO message.
 */
# define CC_TODO(_msg)

/**
 * CC_PROBE:
 *
 * Prints a probing message.
 */
# define CC_PROBE

/**
 * CC_TRACE_MSG:
 * @fmt: printf-like format of the message
 * @...: arguments for @fmt
 *
 * Prints a trace message.
 */
# define CC_TRACE_MSG(fmt, ...)

/**
 * CC_ENTRY:
 *
 * Prints a probing message. This shouldn't be used in
 * critical functions. Place this at the beggining of
 * the function, before any assertion.
 */
# define CC_ENTRY

/**
 * CC_GOTO:
 * @_l: goto tag
 *
 * Logs a goto jump.
 */
# define CC_GOTO(_l)   goto _l

/**
 * CC_EXIT:
 *
 * Prints an exit message. This shouldn't be used in
 * critical functions. Place this at the end of
 * the function, after any relevant code. If the
 * function returns somethin, use CC_RETURN()
 * instead.
 */
# define CC_EXIT       return

/**
 * CC_RETURN:
 * @_r: the return value.
 *
 * Prints an exit message, and returns @_r. See #CC_EXIT.
 */
# define CC_RETURN(_r) return _r
#endif

/**
 * _CC_BUG: (skip)
 */
#define _CC_BUG(Component, Description, File, Line, Func, ...)                        \
  G_STMT_START {                                                                        \
    g_printerr ("-----------------------------------------------------------------\n"); \
    g_printerr ("You've found a bug in Settings or one of its dependent libraries.\n"); \
    g_printerr ("Please help us help you by filing a bug report at:\n");                \
    g_printerr ("\n");                                                                  \
    g_printerr ("@BUGREPORT_URL@&component=%s\n", Component);                           \
    g_printerr ("\n");                                                                  \
    g_printerr ("%s:%d in function %s()\n", File, Line, Func);                          \
    g_printerr ("\n");                                                                  \
    g_printerr (Description"\n", ##__VA_ARGS__);                                        \
    g_printerr ("-----------------------------------------------------------------\n"); \
  } G_STMT_END

/**
 * CC_BUG:
 * @Component: the component
 * @Description: the description
 * @...: extra arguments
 *
 * Logs a bug-friendly message.
 */
#define CC_BUG(Component, Description, ...) \
  _CC_BUG(Component, Description, __FILE__, __LINE__, G_STRFUNC, ##__VA_ARGS__)

G_END_DECLS