]> git.ipfire.org Git - thirdparty/git.git/blame - trace.h
Sync with maint
[thirdparty/git.git] / trace.h
CommitLineData
5991a55c
KB
1#ifndef TRACE_H
2#define TRACE_H
3
4#include "git-compat-util.h"
5#include "strbuf.h"
6
f1ecbe0f
HW
7/**
8 * The trace API can be used to print debug messages to stderr or a file. Trace
9 * code is inactive unless explicitly enabled by setting `GIT_TRACE*` environment
10 * variables.
11 *
12 * The trace implementation automatically adds `timestamp file:line ... \n` to
13 * all trace messages. E.g.:
14 *
15 * ------------
16 * 23:59:59.123456 git.c:312 trace: built-in: git 'foo'
17 * 00:00:00.000001 builtin/foo.c:99 foo: some message
18 * ------------
19 *
20 * Bugs & Caveats
21 * --------------
22 *
23 * GIT_TRACE_* environment variables can be used to tell Git to show
24 * trace output to its standard error stream. Git can often spawn a pager
25 * internally to run its subcommand and send its standard output and
26 * standard error to it.
27 *
28 * Because GIT_TRACE_PERFORMANCE trace is generated only at the very end
29 * of the program with atexit(), which happens after the pager exits, it
30 * would not work well if you send its log to the standard error output
31 * and let Git spawn the pager at the same time.
32 *
33 * As a work around, you can for example use '--no-pager', or set
34 * GIT_TRACE_PERFORMANCE to another file descriptor which is redirected
35 * to stderr, or set GIT_TRACE_PERFORMANCE to a file specified by its
36 * absolute path.
37 *
38 * For example instead of the following command which by default may not
39 * print any performance information:
40 *
41 * ------------
42 * GIT_TRACE_PERFORMANCE=2 git log -1
43 * ------------
44 *
45 * you may want to use:
46 *
47 * ------------
48 * GIT_TRACE_PERFORMANCE=2 git --no-pager log -1
49 * ------------
50 *
51 * or:
52 *
53 * ------------
54 * GIT_TRACE_PERFORMANCE=3 3>&2 git log -1
55 * ------------
56 *
57 * or:
58 *
59 * ------------
60 * GIT_TRACE_PERFORMANCE=/path/to/log/file git log -1
61 * ------------
62 *
63 */
64
65/**
66 * Defines a trace key (or category). The default (for API functions that
67 * don't take a key) is `GIT_TRACE`.
68 *
69 * E.g. to define a trace key controlled by environment variable `GIT_TRACE_FOO`:
70 *
71 * ------------
72 * static struct trace_key trace_foo = TRACE_KEY_INIT(FOO);
73 *
74 * static void trace_print_foo(const char *message)
75 * {
76 * trace_printf_key(&trace_foo, "%s", message);
77 * }
78 * ------------
79 *
80 * Note: don't use `const` as the trace implementation stores internal state in
81 * the `trace_key` structure.
82 */
6aa30857
KB
83struct trace_key {
84 const char * const key;
85 int fd;
86 unsigned int initialized : 1;
87 unsigned int need_close : 1;
88};
89
406102a7
GK
90extern struct trace_key trace_default_key;
91
6aa30857 92#define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
8eeb25c6 93extern struct trace_key trace_perf_key;
cb507619 94extern struct trace_key trace_setup_key;
6aa30857 95
55454427 96void trace_repo_setup(const char *prefix);
f1ecbe0f
HW
97
98/**
99 * Checks whether the trace key is enabled. Used to prevent expensive
100 * string formatting before calling one of the printing APIs.
101 */
55454427 102int trace_want(struct trace_key *key);
f1ecbe0f
HW
103
104/**
105 * Disables tracing for the specified key, even if the environment variable
106 * was set.
107 */
55454427 108void trace_disable(struct trace_key *key);
f1ecbe0f
HW
109
110/**
111 * Returns nanoseconds since the epoch (01/01/1970), typically used
112 * for performance measurements.
113 * Currently there are high precision timer implementations for Linux (using
114 * `clock_gettime(CLOCK_MONOTONIC)`) and Windows (`QueryPerformanceCounter`).
115 * Other platforms use `gettimeofday` as time source.
116 */
55454427 117uint64_t getnanotime(void);
f1ecbe0f 118
55454427
DL
119void trace_command_performance(const char **argv);
120void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
c46c406a 121uint64_t trace_performance_enter(void);
66f66c59 122
e05bed96
KB
123#ifndef HAVE_VARIADIC_MACROS
124
f1ecbe0f
HW
125/**
126 * Prints a formatted message, similar to printf.
127 */
66f66c59 128__attribute__((format (printf, 1, 2)))
b199d714 129void trace_printf(const char *format, ...);
66f66c59 130
5991a55c 131__attribute__((format (printf, 2, 3)))
b199d714 132void trace_printf_key(struct trace_key *key, const char *format, ...);
66f66c59 133
f1ecbe0f
HW
134/**
135 * Prints a formatted message, followed by a quoted list of arguments.
136 */
66f66c59 137__attribute__((format (printf, 2, 3)))
b199d714 138void trace_argv_printf(const char **argv, const char *format, ...);
66f66c59 139
f1ecbe0f
HW
140/**
141 * Prints the strbuf, without additional formatting (i.e. doesn't
142 * choke on `%` or even `\0`).
143 */
55454427 144void trace_strbuf(struct trace_key *key, const struct strbuf *data);
5991a55c 145
f1ecbe0f
HW
146/**
147 * Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled.
148 *
149 * Example:
150 * ------------
151 * uint64_t t = 0;
152 * for (;;) {
153 * // ignore
154 * t -= getnanotime();
155 * // code section to measure
156 * t += getnanotime();
157 * // ignore
158 * }
159 * trace_performance(t, "frotz");
160 * ------------
161 */
09b2c1c7 162__attribute__((format (printf, 2, 3)))
b199d714 163void trace_performance(uint64_t nanos, const char *format, ...);
09b2c1c7 164
f1ecbe0f
HW
165/**
166 * Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled.
167 *
168 * Example:
169 * ------------
170 * uint64_t start = getnanotime();
171 * // code section to measure
172 * trace_performance_since(start, "foobar");
173 * ------------
174 */
09b2c1c7 175__attribute__((format (printf, 2, 3)))
b199d714 176void trace_performance_since(uint64_t start, const char *format, ...);
09b2c1c7 177
c46c406a
NTND
178__attribute__((format (printf, 1, 2)))
179void trace_performance_leave(const char *format, ...);
180
e05bed96
KB
181#else
182
183/*
184 * Macros to add file:line - see above for C-style declarations of how these
185 * should be used.
186 */
187
188/*
189 * TRACE_CONTEXT may be set to __FUNCTION__ if the compiler supports it. The
190 * default is __FILE__, as it is consistent with assert(), and static function
191 * names are not necessarily unique.
192 *
193 * __FILE__ ":" __FUNCTION__ doesn't work with GNUC, as __FILE__ is supplied
194 * by the preprocessor as a string literal, and __FUNCTION__ is filled in by
195 * the compiler as a string constant.
196 */
197#ifndef TRACE_CONTEXT
198# define TRACE_CONTEXT __FILE__
199#endif
200
201/*
202 * Note: with C99 variadic macros, __VA_ARGS__ must include the last fixed
203 * parameter ('format' in this case). Otherwise, a call without variable
204 * arguments will have a surplus ','. E.g.:
205 *
206 * #define foo(format, ...) bar(format, __VA_ARGS__)
207 * foo("test");
208 *
209 * will expand to
210 *
211 * bar("test",);
212 *
213 * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
214 * comma, but this is non-standard.
215 */
216
8eeb25c6
GK
217#define trace_printf_key(key, ...) \
218 do { \
219 if (trace_pass_fl(key)) \
220 trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, \
221 __VA_ARGS__); \
222 } while (0)
223
224#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
225
226#define trace_argv_printf(argv, ...) \
227 do { \
228 if (trace_pass_fl(&trace_default_key)) \
229 trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, \
230 argv, __VA_ARGS__); \
231 } while (0)
232
233#define trace_strbuf(key, data) \
234 do { \
235 if (trace_pass_fl(key)) \
236 trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data);\
237 } while (0)
238
239#define trace_performance(nanos, ...) \
240 do { \
241 if (trace_pass_fl(&trace_perf_key)) \
242 trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos,\
243 __VA_ARGS__); \
244 } while (0)
245
246#define trace_performance_since(start, ...) \
247 do { \
248 if (trace_pass_fl(&trace_perf_key)) \
249 trace_performance_fl(TRACE_CONTEXT, __LINE__, \
250 getnanotime() - (start), \
251 __VA_ARGS__); \
252 } while (0)
09b2c1c7 253
c46c406a
NTND
254#define trace_performance_leave(...) \
255 do { \
256 if (trace_pass_fl(&trace_perf_key)) \
257 trace_performance_leave_fl(TRACE_CONTEXT, __LINE__, \
258 getnanotime(), \
259 __VA_ARGS__); \
260 } while (0)
261
e05bed96
KB
262/* backend functions, use non-*fl macros instead */
263__attribute__((format (printf, 4, 5)))
b199d714 264void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
ad6dad09 265 const char *format, ...);
e05bed96 266__attribute__((format (printf, 4, 5)))
b199d714 267void trace_argv_printf_fl(const char *file, int line, const char **argv,
ad6dad09 268 const char *format, ...);
55454427 269void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
ad6dad09 270 const struct strbuf *data);
09b2c1c7 271__attribute__((format (printf, 4, 5)))
b199d714 272void trace_performance_fl(const char *file, int line,
ad6dad09 273 uint64_t nanos, const char *fmt, ...);
c46c406a 274__attribute__((format (printf, 4, 5)))
b199d714 275void trace_performance_leave_fl(const char *file, int line,
ad6dad09 276 uint64_t nanos, const char *fmt, ...);
8eeb25c6
GK
277static inline int trace_pass_fl(struct trace_key *key)
278{
279 return key->fd || !key->initialized;
280}
e05bed96
KB
281
282#endif /* HAVE_VARIADIC_MACROS */
283
5991a55c 284#endif /* TRACE_H */