]> git.ipfire.org Git - thirdparty/git.git/blame - trace.h
t5318: avoid unnecessary command substitutions
[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
6aa30857
KB
7struct trace_key {
8 const char * const key;
9 int fd;
10 unsigned int initialized : 1;
11 unsigned int need_close : 1;
12};
13
406102a7
GK
14extern struct trace_key trace_default_key;
15
6aa30857 16#define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
8eeb25c6 17extern struct trace_key trace_perf_key;
cb507619 18extern struct trace_key trace_setup_key;
6aa30857 19
5991a55c 20extern void trace_repo_setup(const char *prefix);
6aa30857
KB
21extern int trace_want(struct trace_key *key);
22extern void trace_disable(struct trace_key *key);
148d6771 23extern uint64_t getnanotime(void);
578da039 24extern void trace_command_performance(const char **argv);
32359838 25extern void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
66f66c59 26
e05bed96
KB
27#ifndef HAVE_VARIADIC_MACROS
28
66f66c59
KB
29__attribute__((format (printf, 1, 2)))
30extern void trace_printf(const char *format, ...);
31
5991a55c 32__attribute__((format (printf, 2, 3)))
6aa30857 33extern void trace_printf_key(struct trace_key *key, const char *format, ...);
66f66c59
KB
34
35__attribute__((format (printf, 2, 3)))
36extern void trace_argv_printf(const char **argv, const char *format, ...);
37
c69dfd24 38extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
5991a55c 39
09b2c1c7
KB
40/* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled. */
41__attribute__((format (printf, 2, 3)))
42extern void trace_performance(uint64_t nanos, const char *format, ...);
43
44/* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled. */
45__attribute__((format (printf, 2, 3)))
46extern void trace_performance_since(uint64_t start, const char *format, ...);
47
e05bed96
KB
48#else
49
50/*
51 * Macros to add file:line - see above for C-style declarations of how these
52 * should be used.
53 */
54
55/*
56 * TRACE_CONTEXT may be set to __FUNCTION__ if the compiler supports it. The
57 * default is __FILE__, as it is consistent with assert(), and static function
58 * names are not necessarily unique.
59 *
60 * __FILE__ ":" __FUNCTION__ doesn't work with GNUC, as __FILE__ is supplied
61 * by the preprocessor as a string literal, and __FUNCTION__ is filled in by
62 * the compiler as a string constant.
63 */
64#ifndef TRACE_CONTEXT
65# define TRACE_CONTEXT __FILE__
66#endif
67
68/*
69 * Note: with C99 variadic macros, __VA_ARGS__ must include the last fixed
70 * parameter ('format' in this case). Otherwise, a call without variable
71 * arguments will have a surplus ','. E.g.:
72 *
73 * #define foo(format, ...) bar(format, __VA_ARGS__)
74 * foo("test");
75 *
76 * will expand to
77 *
78 * bar("test",);
79 *
80 * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
81 * comma, but this is non-standard.
82 */
83
8eeb25c6
GK
84#define trace_printf_key(key, ...) \
85 do { \
86 if (trace_pass_fl(key)) \
87 trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, \
88 __VA_ARGS__); \
89 } while (0)
90
91#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
92
93#define trace_argv_printf(argv, ...) \
94 do { \
95 if (trace_pass_fl(&trace_default_key)) \
96 trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, \
97 argv, __VA_ARGS__); \
98 } while (0)
99
100#define trace_strbuf(key, data) \
101 do { \
102 if (trace_pass_fl(key)) \
103 trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data);\
104 } while (0)
105
106#define trace_performance(nanos, ...) \
107 do { \
108 if (trace_pass_fl(&trace_perf_key)) \
109 trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos,\
110 __VA_ARGS__); \
111 } while (0)
112
113#define trace_performance_since(start, ...) \
114 do { \
115 if (trace_pass_fl(&trace_perf_key)) \
116 trace_performance_fl(TRACE_CONTEXT, __LINE__, \
117 getnanotime() - (start), \
118 __VA_ARGS__); \
119 } while (0)
09b2c1c7 120
e05bed96
KB
121/* backend functions, use non-*fl macros instead */
122__attribute__((format (printf, 4, 5)))
123extern void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
124 const char *format, ...);
125__attribute__((format (printf, 4, 5)))
126extern void trace_argv_printf_fl(const char *file, int line, const char **argv,
127 const char *format, ...);
128extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
129 const struct strbuf *data);
09b2c1c7
KB
130__attribute__((format (printf, 4, 5)))
131extern void trace_performance_fl(const char *file, int line,
132 uint64_t nanos, const char *fmt, ...);
8eeb25c6
GK
133static inline int trace_pass_fl(struct trace_key *key)
134{
135 return key->fd || !key->initialized;
136}
e05bed96
KB
137
138#endif /* HAVE_VARIADIC_MACROS */
139
5991a55c 140#endif /* TRACE_H */