Merge branch 'bc/doc-use-docbook-5'
[git.git] / trace.h
blob9fa3e7a59407541d288da6ce2720279e2e23ab4e
1 #ifndef TRACE_H
2 #define TRACE_H
4 #include "git-compat-util.h"
5 #include "strbuf.h"
7 struct trace_key {
8 const char * const key;
9 int fd;
10 unsigned int initialized : 1;
11 unsigned int need_close : 1;
14 extern struct trace_key trace_default_key;
16 #define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
17 extern struct trace_key trace_perf_key;
18 extern struct trace_key trace_setup_key;
20 void trace_repo_setup(const char *prefix);
21 int trace_want(struct trace_key *key);
22 void trace_disable(struct trace_key *key);
23 uint64_t getnanotime(void);
24 void trace_command_performance(const char **argv);
25 void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
26 uint64_t trace_performance_enter(void);
28 #ifndef HAVE_VARIADIC_MACROS
30 __attribute__((format (printf, 1, 2)))
31 void trace_printf(const char *format, ...);
33 __attribute__((format (printf, 2, 3)))
34 void trace_printf_key(struct trace_key *key, const char *format, ...);
36 __attribute__((format (printf, 2, 3)))
37 void trace_argv_printf(const char **argv, const char *format, ...);
39 void trace_strbuf(struct trace_key *key, const struct strbuf *data);
41 /* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled. */
42 __attribute__((format (printf, 2, 3)))
43 void trace_performance(uint64_t nanos, const char *format, ...);
45 /* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled. */
46 __attribute__((format (printf, 2, 3)))
47 void trace_performance_since(uint64_t start, const char *format, ...);
49 __attribute__((format (printf, 1, 2)))
50 void trace_performance_leave(const char *format, ...);
52 #else
55 * Macros to add file:line - see above for C-style declarations of how these
56 * should be used.
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.
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.
68 #ifndef TRACE_CONTEXT
69 # define TRACE_CONTEXT __FILE__
70 #endif
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.:
77 * #define foo(format, ...) bar(format, __VA_ARGS__)
78 * foo("test");
80 * will expand to
82 * bar("test",);
84 * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
85 * comma, but this is non-standard.
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)
95 #define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
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)
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)
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)
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)
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)
133 /* backend functions, use non-*fl macros instead */
134 __attribute__((format (printf, 4, 5)))
135 void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
136 const char *format, ...);
137 __attribute__((format (printf, 4, 5)))
138 void trace_argv_printf_fl(const char *file, int line, const char **argv,
139 const char *format, ...);
140 void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
141 const struct strbuf *data);
142 __attribute__((format (printf, 4, 5)))
143 void trace_performance_fl(const char *file, int line,
144 uint64_t nanos, const char *fmt, ...);
145 __attribute__((format (printf, 4, 5)))
146 void trace_performance_leave_fl(const char *file, int line,
147 uint64_t nanos, const char *fmt, ...);
148 static inline int trace_pass_fl(struct trace_key *key)
150 return key->fd || !key->initialized;
153 #endif /* HAVE_VARIADIC_MACROS */
155 #endif /* TRACE_H */