ide-debug.h.in 7 KB
Newer Older
1
/* ide-debug.h.in
2 3 4
 *
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 *
5 6 7 8
 * 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 3 of the License, or
 * (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 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17 18 19 20 21
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef IDE_DEBUG_H
#define IDE_DEBUG_H

22
#include <execinfo.h>
23 24 25 26
#include <glib.h>

G_BEGIN_DECLS

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
/**
 * SECTION:ide-debug
 * @title: Debug logging and tracing
 * @short_description: tracing and debug facilities for Builder and plugins
 *
 * The debug macros such as %IDE_ENTRY, %IDE_EXIT, and %IDE_RETURN provide
 * helpers for tracing Builder and plugins at runtime.
 *
 * These tracing macros will compile out when Builder is configured for a
 * release build. Otherwise, running Builder with the "-vvvv" command line
 * argument will show tracing output.
 *
 * Since: 3.18
 */

/**
 * IDE_ENTRY: (skip)
 *
 * Traces the entry into a function. Place this at the beginning of your
 * function above pre-condition checks.
 *
 * Since: 3.18
 */

/**
 * IDE_EXIT: (skip)
 *
 * Traces the exit from a function. Use this instead of "return" to return
 * and log the function exiting.
 *
 * Since: 3.18
 */

/**
 * IDE_RETURN: (skip)
 *
 * Similar to %IDE_EXIT but allows providing a return value.
 *
 * Since: 3.18
 */

/**
 * IDE_GOTO: (skip)
 * @_l: the label to jump to
 *
 * Appends to the jump to label to the tracing log and then jumps
 * to the label @_l.
 *
 * Since: 3.18
 */

/**
 * IDE_TODO: (skip)
 * @_msg: the message to append to the trace log
 *
 * Appends to the tracing log that unsupported code has been
 * reached.
 *
 * Since: 3.18
 */

/**
 * IDE_PROBE: (skip)
 *
 * Appends to the tracing log that a line of code was reached.
 *
 * Since: 3.18
 */

/**
 * IDE_TRACE_MSG: (skip)
 *
 * Similar to %IDE_PROBE but allows specifying a log message.
 *
 * Since: 3.18
 */

104 105
#ifndef IDE_ENABLE_TRACE
# define IDE_ENABLE_TRACE @ENABLE_TRACING@
106
#endif
107 108
#if IDE_ENABLE_TRACE != 1
# undef IDE_ENABLE_TRACE
109 110
#endif

111
/**
112
 * IDE_LOG_LEVEL_TRACE: (skip)
113
 */
114
#ifndef IDE_LOG_LEVEL_TRACE
115
# define IDE_LOG_LEVEL_TRACE ((GLogLevelFlags)(1 << G_LOG_LEVEL_USER_SHIFT))
116 117
#endif

118
#ifdef IDE_ENABLE_TRACE
119
# define IDE_TRACE_MSG(fmt, ...)                                         \
120
   g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, "  MSG: %s():%d: " fmt,      \
121
         G_STRFUNC, __LINE__, ##__VA_ARGS__)
122 123
# define IDE_PROBE                                                       \
   g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, "PROBE: %s():%d",            \
124
         G_STRFUNC, __LINE__)
125 126
# define IDE_TODO(_msg)                                                  \
   g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, " TODO: %s():%d: %s",        \
127
         G_STRFUNC, __LINE__, _msg)
128 129
# define IDE_ENTRY                                                       \
   g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, "ENTRY: %s():%d",            \
130
         G_STRFUNC, __LINE__)
131 132 133 134 135
# define IDE_EXIT                                                        \
   G_STMT_START {                                                        \
      g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, " EXIT: %s():%d",         \
            G_STRFUNC, __LINE__);                                        \
      return;                                                            \
136
   } G_STMT_END
137 138 139 140 141
# define IDE_GOTO(_l)                                                    \
   G_STMT_START {                                                        \
      g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, " GOTO: %s():%d ("#_l")", \
            G_STRFUNC, __LINE__);                                        \
      goto _l;                                                           \
142
   } G_STMT_END
143 144 145 146 147
# define IDE_RETURN(_r)                                                  \
   G_STMT_START {                                                        \
      g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, " EXIT: %s():%d ",        \
            G_STRFUNC, __LINE__);                                        \
      return _r;                                                         \
148
   } G_STMT_END
149 150 151 152 153 154 155 156 157 158 159
# define IDE_BACKTRACE                                                   \
  G_STMT_START {                                                         \
    gpointer btbuf[64];                                                  \
    int btbuflen = backtrace (btbuf, G_N_ELEMENTS (btbuf));              \
    char **symnames = backtrace_symbols (btbuf, btbuflen);               \
    for (guint _i = 0; _i < btbuflen; _i++) {                            \
      g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, "TRACE: [%-2d]: %s",      \
            _i, symnames[_i]);                                           \
    }                                                                    \
    free (symnames);                                                     \
  } G_STMT_END
160
#else
161 162 163 164 165 166 167 168
# define IDE_TODO(_msg)          G_STMT_START {            } G_STMT_END
# define IDE_PROBE               G_STMT_START {            } G_STMT_END
# define IDE_TRACE_MSG(fmt, ...) G_STMT_START {            } G_STMT_END
# define IDE_ENTRY               G_STMT_START {            } G_STMT_END
# define IDE_GOTO(_l)            G_STMT_START { goto _l;   } G_STMT_END
# define IDE_EXIT                G_STMT_START { return;    } G_STMT_END
# define IDE_RETURN(_r)          G_STMT_START { return _r; } G_STMT_END
# define IDE_BACKTRACE           G_STMT_START {            } G_STMT_END
169 170
#endif

171 172 173
#define _IDE_BUG(Component, Description, File, Line, Func, ...)                         \
  G_STMT_START {                                                                        \
    g_printerr ("-----------------------------------------------------------------\n"); \
Michael Catanzaro's avatar
Michael Catanzaro committed
174
    g_printerr ("You've found a bug in Builder or one of its dependent libraries.\n");  \
175 176 177 178 179 180 181 182 183 184 185 186
    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
#define IDE_BUG(Component, Description, ...) \
  _IDE_BUG(Component, Description, __FILE__, __LINE__, G_STRFUNC, ##__VA_ARGS__)

187 188 189
G_END_DECLS

#endif /* IDE_DEBUG_H */