trace.hon commit Merge branch 'ab/sha1dc' (d1b0164)
   1#ifndef TRACE_H
   2#define TRACE_H
   3
   4#include "git-compat-util.h"
   5#include "strbuf.h"
   6
   7struct trace_key {
   8        const char * const key;
   9        int fd;
  10        unsigned int initialized : 1;
  11        unsigned int  need_close : 1;
  12};
  13
  14extern struct trace_key trace_default_key;
  15
  16#define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
  17extern struct trace_key trace_perf_key;
  18extern struct trace_key trace_setup_key;
  19
  20extern void trace_repo_setup(const char *prefix);
  21extern int trace_want(struct trace_key *key);
  22extern void trace_disable(struct trace_key *key);
  23extern uint64_t getnanotime(void);
  24extern void trace_command_performance(const char **argv);
  25extern void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
  26
  27#ifndef HAVE_VARIADIC_MACROS
  28
  29__attribute__((format (printf, 1, 2)))
  30extern void trace_printf(const char *format, ...);
  31
  32__attribute__((format (printf, 2, 3)))
  33extern void trace_printf_key(struct trace_key *key, const char *format, ...);
  34
  35__attribute__((format (printf, 2, 3)))
  36extern void trace_argv_printf(const char **argv, const char *format, ...);
  37
  38extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
  39
  40/* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled. */
  41__attribute__((format (printf, 2, 3)))
  42extern void trace_performance(uint64_t nanos, const char *format, ...);
  43
  44/* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled. */
  45__attribute__((format (printf, 2, 3)))
  46extern void trace_performance_since(uint64_t start, const char *format, ...);
  47
  48#else
  49
  50/*
  51 * Macros to add file:line - see above for C-style declarations of how these
  52 * should be used.
  53 */
  54
  55/*
  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.
  59 *
  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.
  63 */
  64#ifndef TRACE_CONTEXT
  65# define TRACE_CONTEXT __FILE__
  66#endif
  67
  68/*
  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.:
  72 *
  73 *  #define foo(format, ...) bar(format, __VA_ARGS__)
  74 *  foo("test");
  75 *
  76 * will expand to
  77 *
  78 *  bar("test",);
  79 *
  80 * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
  81 * comma, but this is non-standard.
  82 */
  83
  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)
  90
  91#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
  92
  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)
  99
 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)
 105
 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)
 112
 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)
 120
 121/* backend functions, use non-*fl macros instead */
 122__attribute__((format (printf, 4, 5)))
 123extern void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
 124                                const char *format, ...);
 125__attribute__((format (printf, 4, 5)))
 126extern void trace_argv_printf_fl(const char *file, int line, const char **argv,
 127                                 const char *format, ...);
 128extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
 129                            const struct strbuf *data);
 130__attribute__((format (printf, 4, 5)))
 131extern void trace_performance_fl(const char *file, int line,
 132                                 uint64_t nanos, const char *fmt, ...);
 133static inline int trace_pass_fl(struct trace_key *key)
 134{
 135        return key->fd || !key->initialized;
 136}
 137
 138#endif /* HAVE_VARIADIC_MACROS */
 139
 140#endif /* TRACE_H */