trace.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_CORE_LIB_DEBUG_TRACE_H
  19. #define GRPC_CORE_LIB_DEBUG_TRACE_H
  20. #include <grpc/support/atm.h>
  21. #include <grpc/support/port_platform.h>
  22. #include <stdbool.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. void grpc_tracer_init(const char *env_var_name);
  27. void grpc_tracer_shutdown(void);
  28. #ifdef __cplusplus
  29. }
  30. #endif
  31. #if defined(__has_feature)
  32. #if __has_feature(thread_sanitizer)
  33. #define GRPC_THREADSAFE_TRACER
  34. #endif
  35. #endif
  36. #ifdef __cplusplus
  37. namespace grpc_core {
  38. class TraceFlag {
  39. public:
  40. TraceFlag(bool default_enabled, const char *name);
  41. ~TraceFlag();
  42. static bool Set(const char *tracer, bool enabled);
  43. bool enabled() {
  44. #ifdef GRPC_THREADSAFE_TRACER
  45. gpr_atm_no_barrier_load(&value_) != 0
  46. #else
  47. return value_;
  48. #endif
  49. }
  50. private:
  51. static void List();
  52. void set_enabled(bool enabled) {
  53. #ifdef GRPC_THREADSAFE_TRACER
  54. gpr_atm_no_barrier_store(&value, enabled);
  55. #else
  56. value_ = enabled;
  57. #endif
  58. }
  59. static TraceFlag *root_tracer_;
  60. TraceFlag *next_tracer_;
  61. const char *const name_;
  62. #ifdef GRPC_THREADSAFE_TRACER
  63. gpr_atm value_;
  64. #else
  65. bool value_;
  66. #endif
  67. };
  68. #ifndef NDEBUG
  69. typedef TraceFlag DebugOnlyTraceFlag;
  70. #else
  71. class DebugOnlyTraceFlag {
  72. public:
  73. DebugOnlyTraceFlag(bool default_enabled, const char *name) {}
  74. bool enabled() { return false; }
  75. };
  76. #endif
  77. } // namespace grpc_core
  78. #endif // __cplusplus
  79. #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */