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