trace.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. void grpc_tracer_init(const char* env_var_name);
  24. void grpc_tracer_shutdown(void);
  25. #if defined(__has_feature)
  26. #if __has_feature(thread_sanitizer)
  27. #define GRPC_THREADSAFE_TRACER
  28. #endif
  29. #endif
  30. namespace grpc_core {
  31. class TraceFlag;
  32. class TraceFlagList {
  33. public:
  34. static bool Set(const char* name, bool enabled);
  35. static void Add(TraceFlag* flag);
  36. private:
  37. static void LogAllTracers();
  38. static TraceFlag* root_tracer_;
  39. };
  40. namespace testing {
  41. void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag);
  42. }
  43. class TraceFlag {
  44. public:
  45. TraceFlag(bool default_enabled, const char* name);
  46. ~TraceFlag() {}
  47. const char* name() const { return name_; }
  48. bool enabled() {
  49. #ifdef GRPC_THREADSAFE_TRACER
  50. return gpr_atm_no_barrier_load(&value_) != 0;
  51. #else
  52. return value_;
  53. #endif
  54. }
  55. private:
  56. friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag);
  57. friend class TraceFlagList;
  58. void set_enabled(bool enabled) {
  59. #ifdef GRPC_THREADSAFE_TRACER
  60. gpr_atm_no_barrier_store(&value_, enabled);
  61. #else
  62. value_ = enabled;
  63. #endif
  64. }
  65. TraceFlag* next_tracer_;
  66. const char* const name_;
  67. #ifdef GRPC_THREADSAFE_TRACER
  68. gpr_atm value_;
  69. #else
  70. bool value_;
  71. #endif
  72. };
  73. #ifndef NDEBUG
  74. typedef TraceFlag DebugOnlyTraceFlag;
  75. #else
  76. class DebugOnlyTraceFlag {
  77. public:
  78. DebugOnlyTraceFlag(bool default_enabled, const char* name) {}
  79. bool enabled() { return false; }
  80. private:
  81. void set_enabled(bool enabled) {}
  82. };
  83. #endif
  84. } // namespace grpc_core
  85. #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */