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