Merge branch 'tq/t1510'
[git/raj.git] / trace.h
blob2b6a1bc17c2cc1a8642d8c7bd460808638f28d77
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 extern void trace_repo_setup(const char *prefix);
21 extern int trace_want(struct trace_key *key);
22 extern void trace_disable(struct trace_key *key);
23 extern uint64_t getnanotime(void);
24 extern void trace_command_performance(const char **argv);
25 extern void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
27 #ifndef HAVE_VARIADIC_MACROS
29 __attribute__((format (printf, 1, 2)))
30 extern void trace_printf(const char *format, ...);
32 __attribute__((format (printf, 2, 3)))
33 extern void trace_printf_key(struct trace_key *key, const char *format, ...);
35 __attribute__((format (printf, 2, 3)))
36 extern void trace_argv_printf(const char **argv, const char *format, ...);
38 extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
40 /* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled. */
41 __attribute__((format (printf, 2, 3)))
42 extern void trace_performance(uint64_t nanos, const char *format, ...);
44 /* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled. */
45 __attribute__((format (printf, 2, 3)))
46 extern void trace_performance_since(uint64_t start, const char *format, ...);
48 #else
51 * Macros to add file:line - see above for C-style declarations of how these
52 * should be used.
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.
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.
64 #ifndef TRACE_CONTEXT
65 # define TRACE_CONTEXT __FILE__
66 #endif
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.:
73 * #define foo(format, ...) bar(format, __VA_ARGS__)
74 * foo("test");
76 * will expand to
78 * bar("test",);
80 * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
81 * comma, but this is non-standard.
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)
91 #define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
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)
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)
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)
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)
121 /* backend functions, use non-*fl macros instead */
122 __attribute__((format (printf, 4, 5)))
123 extern void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
124 const char *format, ...);
125 __attribute__((format (printf, 4, 5)))
126 extern void trace_argv_printf_fl(const char *file, int line, const char **argv,
127 const char *format, ...);
128 extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
129 const struct strbuf *data);
130 __attribute__((format (printf, 4, 5)))
131 extern void trace_performance_fl(const char *file, int line,
132 uint64_t nanos, const char *fmt, ...);
133 static inline int trace_pass_fl(struct trace_key *key)
135 return key->fd || !key->initialized;
138 #endif /* HAVE_VARIADIC_MACROS */
140 #endif /* TRACE_H */