trace.hon commit Merge branch 'dt/remote-helper-doc-re-lock-option' (cab037c)
   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
  20void trace_repo_setup(const char *prefix);
  21int trace_want(struct trace_key *key);
  22void trace_disable(struct trace_key *key);
  23uint64_t getnanotime(void);
  24void trace_command_performance(const char **argv);
  25void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
  26uint64_t trace_performance_enter(void);
  27
  28#ifndef HAVE_VARIADIC_MACROS
  29
  30__attribute__((format (printf, 1, 2)))
  31void trace_printf(const char *format, ...);
  32
  33__attribute__((format (printf, 2, 3)))
  34void trace_printf_key(struct trace_key *key, const char *format, ...);
  35
  36__attribute__((format (printf, 2, 3)))
  37void trace_argv_printf(const char **argv, const char *format, ...);
  38
  39void trace_strbuf(struct trace_key *key, const struct strbuf *data);
  40
  41/* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled. */
  42__attribute__((format (printf, 2, 3)))
  43void trace_performance(uint64_t nanos, const char *format, ...);
  44
  45/* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled. */
  46__attribute__((format (printf, 2, 3)))
  47void trace_performance_since(uint64_t start, const char *format, ...);
  48
  49__attribute__((format (printf, 1, 2)))
  50void trace_performance_leave(const char *format, ...);
  51
  52#else
  53
  54/*
  55 * Macros to add file:line - see above for C-style declarations of how these
  56 * should be used.
  57 */
  58
  59/*
  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.
  63 *
  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.
  67 */
  68#ifndef TRACE_CONTEXT
  69# define TRACE_CONTEXT __FILE__
  70#endif
  71
  72/*
  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.:
  76 *
  77 *  #define foo(format, ...) bar(format, __VA_ARGS__)
  78 *  foo("test");
  79 *
  80 * will expand to
  81 *
  82 *  bar("test",);
  83 *
  84 * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
  85 * comma, but this is non-standard.
  86 */
  87
  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)
  94
  95#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
  96
  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)
 103
 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)
 109
 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)
 116
 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)
 124
 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)
 132
 133/* backend functions, use non-*fl macros instead */
 134__attribute__((format (printf, 4, 5)))
 135void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
 136                         const char *format, ...);
 137__attribute__((format (printf, 4, 5)))
 138void trace_argv_printf_fl(const char *file, int line, const char **argv,
 139                          const char *format, ...);
 140void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
 141                     const struct strbuf *data);
 142__attribute__((format (printf, 4, 5)))
 143void trace_performance_fl(const char *file, int line,
 144                          uint64_t nanos, const char *fmt, ...);
 145__attribute__((format (printf, 4, 5)))
 146void trace_performance_leave_fl(const char *file, int line,
 147                                uint64_t nanos, const char *fmt, ...);
 148static inline int trace_pass_fl(struct trace_key *key)
 149{
 150        return key->fd || !key->initialized;
 151}
 152
 153#endif /* HAVE_VARIADIC_MACROS */
 154
 155#endif /* TRACE_H */