1/* dzl-debug.h.in
2 *
3 * Copyright (C) 2013-2017 Christian Hergert <christian@hergert.me>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef DZL_DEBUG_H
20#define DZL_DEBUG_H
21
22#ifndef DZL_ENABLE_TRACE
23# define DZL_ENABLE_TRACE @ENABLE_TRACING@
24#endif
25#if DZL_ENABLE_TRACE != 1
26# undef DZL_ENABLE_TRACE
27#endif
28
29#include <glib.h>
30
31#ifdef DZL_ENABLE_TRACE
32# include <execinfo.h>
33#endif
34
35G_BEGIN_DECLS
36
37/**
38 * DZL_LOG_LEVEL_TRACE: (skip)
39 */
40#ifndef DZL_LOG_LEVEL_TRACE
41# define DZL_LOG_LEVEL_TRACE ((GLogLevelFlags)(1 << G_LOG_LEVEL_USER_SHIFT))
42#endif
43
44#ifdef DZL_ENABLE_TRACE
45# define DZL_TRACE_MSG(fmt, ...)                                         \
46   g_log(G_LOG_DOMAIN, DZL_LOG_LEVEL_TRACE, "  MSG: %s():%d: " fmt,       \
47         G_STRFUNC, __LINE__, ##__VA_ARGS__)
48# define DZL_PROBE                                                       \
49   g_log(G_LOG_DOMAIN, DZL_LOG_LEVEL_TRACE, "PROBE: %s():%d",            \
50         G_STRFUNC, __LINE__)
51# define DZL_TODO(_msg)                                                  \
52   g_log(G_LOG_DOMAIN, DZL_LOG_LEVEL_TRACE, " TODO: %s():%d: %s",        \
53         G_STRFUNC, __LINE__, _msg)
54# define DZL_ENTRY                                                       \
55   g_log(G_LOG_DOMAIN, DZL_LOG_LEVEL_TRACE, "ENTRY: %s():%d",            \
56         G_STRFUNC, __LINE__)
57# define DZL_EXIT                                                        \
58   G_STMT_START {                                                        \
59      g_log(G_LOG_DOMAIN, DZL_LOG_LEVEL_TRACE, " EXIT: %s():%d",         \
60            G_STRFUNC, __LINE__);                                        \
61      return;                                                            \
62   } G_STMT_END
63# define DZL_GOTO(_l)                                                    \
64   G_STMT_START {                                                        \
65      g_log(G_LOG_DOMAIN, DZL_LOG_LEVEL_TRACE, " GOTO: %s():%d ("#_l")", \
66            G_STRFUNC, __LINE__);                                        \
67      goto _l;                                                           \
68   } G_STMT_END
69# define DZL_RETURN(_r)                                                  \
70   G_STMT_START {                                                        \
71      g_log(G_LOG_DOMAIN, DZL_LOG_LEVEL_TRACE, " EXIT: %s():%d ",        \
72            G_STRFUNC, __LINE__);                                        \
73      return _r;                                                         \
74   } G_STMT_END
75# define DZL_BACKTRACE                                                   \
76  G_STMT_START {                                                         \
77    gpointer btbuf[64];                                                  \
78    int btbuflen = backtrace (btbuf, G_N_ELEMENTS (btbuf));              \
79    char **symnames = backtrace_symbols (btbuf, btbuflen);               \
80    for (guint _i = 0; _i < btbuflen; _i++) {                            \
81      g_log(G_LOG_DOMAIN, DZL_LOG_LEVEL_TRACE, "TRACE: [%-2d]: %s",      \
82            _i, symnames[_i]);                                           \
83    }                                                                    \
84    free (symnames);                                                     \
85  } G_STMT_END
86#else
87# define DZL_TODO(_msg)
88# define DZL_PROBE
89# define DZL_TRACE_MSG(fmt, ...)
90# define DZL_ENTRY
91# define DZL_GOTO(_l)   goto _l
92# define DZL_EXIT       return
93# define DZL_RETURN(_r) return _r
94# define DZL_BACKTRACE  G_STMT_START { } G_STMT_END
95#endif
96
97G_END_DECLS
98
99#endif /* DZL_DEBUG_H */
100