24b32f8f4225b509f2bc5f824455bc17f1132b61
   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 }
  17
  18extern void trace_repo_setup(const char *prefix);
  19extern int trace_want(struct trace_key *key);
  20extern void trace_disable(struct trace_key *key);
  21extern uint64_t getnanotime(void);
  22extern void trace_command_performance(const char **argv);
  23extern void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
  24
  25#ifndef HAVE_VARIADIC_MACROS
  26
  27__attribute__((format (printf, 1, 2)))
  28extern void trace_printf(const char *format, ...);
  29
  30__attribute__((format (printf, 2, 3)))
  31extern void trace_printf_key(struct trace_key *key, const char *format, ...);
  32
  33__attribute__((format (printf, 2, 3)))
  34extern void trace_argv_printf(const char **argv, const char *format, ...);
  35
  36extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
  37
  38/* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled. */
  39__attribute__((format (printf, 2, 3)))
  40extern void trace_performance(uint64_t nanos, const char *format, ...);
  41
  42/* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled. */
  43__attribute__((format (printf, 2, 3)))
  44extern void trace_performance_since(uint64_t start, const char *format, ...);
  45
  46#else
  47
  48/*
  49 * Macros to add file:line - see above for C-style declarations of how these
  50 * should be used.
  51 */
  52
  53/*
  54 * TRACE_CONTEXT may be set to __FUNCTION__ if the compiler supports it. The
  55 * default is __FILE__, as it is consistent with assert(), and static function
  56 * names are not necessarily unique.
  57 *
  58 * __FILE__ ":" __FUNCTION__ doesn't work with GNUC, as __FILE__ is supplied
  59 * by the preprocessor as a string literal, and __FUNCTION__ is filled in by
  60 * the compiler as a string constant.
  61 */
  62#ifndef TRACE_CONTEXT
  63# define TRACE_CONTEXT __FILE__
  64#endif
  65
  66/*
  67 * Note: with C99 variadic macros, __VA_ARGS__ must include the last fixed
  68 * parameter ('format' in this case). Otherwise, a call without variable
  69 * arguments will have a surplus ','. E.g.:
  70 *
  71 *  #define foo(format, ...) bar(format, __VA_ARGS__)
  72 *  foo("test");
  73 *
  74 * will expand to
  75 *
  76 *  bar("test",);
  77 *
  78 * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
  79 * comma, but this is non-standard.
  80 */
  81
  82#define trace_printf(...) \
  83        trace_printf_key_fl(TRACE_CONTEXT, __LINE__, &trace_default_key, __VA_ARGS__)
  84
  85#define trace_printf_key(key, ...) \
  86        trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, __VA_ARGS__)
  87
  88#define trace_argv_printf(argv, ...) \
  89        trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, argv, __VA_ARGS__)
  90
  91#define trace_strbuf(key, data) \
  92        trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data)
  93
  94#define trace_performance(nanos, ...) \
  95        trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos, __VA_ARGS__)
  96
  97#define trace_performance_since(start, ...) \
  98        trace_performance_fl(TRACE_CONTEXT, __LINE__, getnanotime() - (start), \
  99                             __VA_ARGS__)
 100
 101/* backend functions, use non-*fl macros instead */
 102__attribute__((format (printf, 4, 5)))
 103extern void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
 104                                const char *format, ...);
 105__attribute__((format (printf, 4, 5)))
 106extern void trace_argv_printf_fl(const char *file, int line, const char **argv,
 107                                 const char *format, ...);
 108extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
 109                            const struct strbuf *data);
 110__attribute__((format (printf, 4, 5)))
 111extern void trace_performance_fl(const char *file, int line,
 112                                 uint64_t nanos, const char *fmt, ...);
 113
 114#endif /* HAVE_VARIADIC_MACROS */
 115
 116#endif /* TRACE_H */