THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.7KB

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