trace: add 'file:line' to all trace output
[git] / trace.h
1 #ifndef TRACE_H
2 #define TRACE_H
3
4 #include "git-compat-util.h"
5 #include "strbuf.h"
6
7 struct trace_key {
8         const char * const key;
9         int fd;
10         unsigned int initialized : 1;
11         unsigned int  need_close : 1;
12 };
13
14 #define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
15
16 extern void trace_repo_setup(const char *prefix);
17 extern int trace_want(struct trace_key *key);
18 extern void trace_disable(struct trace_key *key);
19
20 #ifndef HAVE_VARIADIC_MACROS
21
22 __attribute__((format (printf, 1, 2)))
23 extern void trace_printf(const char *format, ...);
24
25 __attribute__((format (printf, 2, 3)))
26 extern void trace_printf_key(struct trace_key *key, const char *format, ...);
27
28 __attribute__((format (printf, 2, 3)))
29 extern void trace_argv_printf(const char **argv, const char *format, ...);
30
31 extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
32
33 #else
34
35 /*
36  * Macros to add file:line - see above for C-style declarations of how these
37  * should be used.
38  */
39
40 /*
41  * TRACE_CONTEXT may be set to __FUNCTION__ if the compiler supports it. The
42  * default is __FILE__, as it is consistent with assert(), and static function
43  * names are not necessarily unique.
44  *
45  * __FILE__ ":" __FUNCTION__ doesn't work with GNUC, as __FILE__ is supplied
46  * by the preprocessor as a string literal, and __FUNCTION__ is filled in by
47  * the compiler as a string constant.
48  */
49 #ifndef TRACE_CONTEXT
50 # define TRACE_CONTEXT __FILE__
51 #endif
52
53 /*
54  * Note: with C99 variadic macros, __VA_ARGS__ must include the last fixed
55  * parameter ('format' in this case). Otherwise, a call without variable
56  * arguments will have a surplus ','. E.g.:
57  *
58  *  #define foo(format, ...) bar(format, __VA_ARGS__)
59  *  foo("test");
60  *
61  * will expand to
62  *
63  *  bar("test",);
64  *
65  * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
66  * comma, but this is non-standard.
67  */
68
69 #define trace_printf(...) \
70         trace_printf_key_fl(TRACE_CONTEXT, __LINE__, NULL, __VA_ARGS__)
71
72 #define trace_printf_key(key, ...) \
73         trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, __VA_ARGS__)
74
75 #define trace_argv_printf(argv, ...) \
76         trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, argv, __VA_ARGS__)
77
78 #define trace_strbuf(key, data) \
79         trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data)
80
81 /* backend functions, use non-*fl macros instead */
82 __attribute__((format (printf, 4, 5)))
83 extern void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
84                                 const char *format, ...);
85 __attribute__((format (printf, 4, 5)))
86 extern void trace_argv_printf_fl(const char *file, int line, const char **argv,
87                                  const char *format, ...);
88 extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
89                             const struct strbuf *data);
90
91 #endif /* HAVE_VARIADIC_MACROS */
92
93 #endif /* TRACE_H */