globals.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/log/globals.h"
  15. #include <atomic>
  16. #include <cstddef>
  17. #include <cstdint>
  18. #include <cstdlib>
  19. #include <cstring>
  20. #include <string>
  21. #include "absl/base/attributes.h"
  22. #include "absl/base/config.h"
  23. #include "absl/base/internal/atomic_hook.h"
  24. #include "absl/base/internal/raw_logging.h"
  25. #include "absl/base/log_severity.h"
  26. #include "absl/hash/hash.h"
  27. #include "absl/strings/string_view.h"
  28. namespace absl {
  29. ABSL_NAMESPACE_BEGIN
  30. namespace {
  31. // These atomics represent logging library configuration.
  32. // Integer types are used instead of absl::LogSeverity to ensure that a
  33. // lock-free std::atomic is used when possible.
  34. ABSL_CONST_INIT std::atomic<int> min_log_level{
  35. static_cast<int>(absl::LogSeverityAtLeast::kInfo)};
  36. ABSL_CONST_INIT std::atomic<int> stderrthreshold{
  37. static_cast<int>(absl::LogSeverityAtLeast::kError)};
  38. // We evaluate this value as a hash comparison to avoid having to
  39. // hold a mutex or make a copy (to access the value of a string-typed flag) in
  40. // very hot codepath.
  41. ABSL_CONST_INIT std::atomic<size_t> log_backtrace_at_hash{0};
  42. ABSL_CONST_INIT std::atomic<bool> prepend_log_prefix{true};
  43. constexpr char kDefaultAndroidTag[] = "native";
  44. ABSL_CONST_INIT std::atomic<const char*> android_log_tag{kDefaultAndroidTag};
  45. ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
  46. absl::base_internal::AtomicHook<log_internal::LoggingGlobalsListener>
  47. logging_globals_listener;
  48. size_t HashSiteForLogBacktraceAt(absl::string_view file, int line) {
  49. return absl::HashOf(file, line);
  50. }
  51. void TriggerLoggingGlobalsListener() {
  52. auto* listener = logging_globals_listener.Load();
  53. if (listener != nullptr) listener();
  54. }
  55. } // namespace
  56. namespace log_internal {
  57. void RawSetMinLogLevel(absl::LogSeverityAtLeast severity) {
  58. min_log_level.store(static_cast<int>(severity), std::memory_order_release);
  59. }
  60. void RawSetStderrThreshold(absl::LogSeverityAtLeast severity) {
  61. stderrthreshold.store(static_cast<int>(severity), std::memory_order_release);
  62. }
  63. void RawEnableLogPrefix(bool on_off) {
  64. prepend_log_prefix.store(on_off, std::memory_order_release);
  65. }
  66. void SetLoggingGlobalsListener(LoggingGlobalsListener l) {
  67. logging_globals_listener.Store(l);
  68. }
  69. } // namespace log_internal
  70. absl::LogSeverityAtLeast MinLogLevel() {
  71. return static_cast<absl::LogSeverityAtLeast>(
  72. min_log_level.load(std::memory_order_acquire));
  73. }
  74. void SetMinLogLevel(absl::LogSeverityAtLeast severity) {
  75. log_internal::RawSetMinLogLevel(severity);
  76. TriggerLoggingGlobalsListener();
  77. }
  78. namespace log_internal {
  79. ScopedMinLogLevel::ScopedMinLogLevel(absl::LogSeverityAtLeast severity)
  80. : saved_severity_(absl::MinLogLevel()) {
  81. absl::SetMinLogLevel(severity);
  82. }
  83. ScopedMinLogLevel::~ScopedMinLogLevel() {
  84. absl::SetMinLogLevel(saved_severity_);
  85. }
  86. } // namespace log_internal
  87. absl::LogSeverityAtLeast StderrThreshold() {
  88. return static_cast<absl::LogSeverityAtLeast>(
  89. stderrthreshold.load(std::memory_order_acquire));
  90. }
  91. void SetStderrThreshold(absl::LogSeverityAtLeast severity) {
  92. log_internal::RawSetStderrThreshold(severity);
  93. TriggerLoggingGlobalsListener();
  94. }
  95. ScopedStderrThreshold::ScopedStderrThreshold(absl::LogSeverityAtLeast severity)
  96. : saved_severity_(absl::StderrThreshold()) {
  97. absl::SetStderrThreshold(severity);
  98. }
  99. ScopedStderrThreshold::~ScopedStderrThreshold() {
  100. absl::SetStderrThreshold(saved_severity_);
  101. }
  102. namespace log_internal {
  103. const char* GetAndroidNativeTag() {
  104. return android_log_tag.load(std::memory_order_acquire);
  105. }
  106. } // namespace log_internal
  107. void SetAndroidNativeTag(const char* tag) {
  108. ABSL_CONST_INIT static std::atomic<const std::string*> user_log_tag(nullptr);
  109. ABSL_INTERNAL_CHECK(tag, "tag must be non-null.");
  110. const std::string* tag_str = new std::string(tag);
  111. ABSL_INTERNAL_CHECK(
  112. android_log_tag.exchange(tag_str->c_str(), std::memory_order_acq_rel) ==
  113. kDefaultAndroidTag,
  114. "SetAndroidNativeTag() must only be called once per process!");
  115. user_log_tag.store(tag_str, std::memory_order_relaxed);
  116. }
  117. namespace log_internal {
  118. bool ShouldLogBacktraceAt(absl::string_view file, int line) {
  119. const size_t flag_hash =
  120. log_backtrace_at_hash.load(std::memory_order_relaxed);
  121. return flag_hash != 0 && flag_hash == HashSiteForLogBacktraceAt(file, line);
  122. }
  123. } // namespace log_internal
  124. void SetLogBacktraceLocation(absl::string_view file, int line) {
  125. log_backtrace_at_hash.store(HashSiteForLogBacktraceAt(file, line),
  126. std::memory_order_relaxed);
  127. }
  128. void ClearLogBacktraceLocation() {
  129. log_backtrace_at_hash.store(0, std::memory_order_relaxed);
  130. }
  131. bool ShouldPrependLogPrefix() {
  132. return prepend_log_prefix.load(std::memory_order_acquire);
  133. }
  134. void EnableLogPrefix(bool on_off) {
  135. log_internal::RawEnableLogPrefix(on_off);
  136. TriggerLoggingGlobalsListener();
  137. }
  138. ABSL_NAMESPACE_END
  139. } // namespace absl