globals.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "y_absl/log/internal/globals.h"
  15. #include <atomic>
  16. #include <cstdio>
  17. #if defined(__EMSCRIPTEN__)
  18. #include <emscripten/console.h>
  19. #endif
  20. #include "y_absl/base/attributes.h"
  21. #include "y_absl/base/config.h"
  22. #include "y_absl/base/internal/raw_logging.h"
  23. #include "y_absl/base/log_severity.h"
  24. #include "y_absl/strings/string_view.h"
  25. #include "y_absl/strings/strip.h"
  26. #include "y_absl/time/time.h"
  27. namespace y_absl {
  28. Y_ABSL_NAMESPACE_BEGIN
  29. namespace log_internal {
  30. namespace {
  31. // Keeps track of whether Logging initialization is finalized.
  32. // Log messages generated before that will go to stderr.
  33. Y_ABSL_CONST_INIT std::atomic<bool> logging_initialized(false);
  34. // The TimeZone used for logging. This may only be set once.
  35. Y_ABSL_CONST_INIT std::atomic<y_absl::TimeZone*> timezone_ptr{nullptr};
  36. // If true, the logging library will symbolize stack in fatal messages
  37. Y_ABSL_CONST_INIT std::atomic<bool> symbolize_stack_trace(true);
  38. // Specifies maximum number of stack frames to report in fatal messages.
  39. Y_ABSL_CONST_INIT std::atomic<int> max_frames_in_stack_trace(64);
  40. Y_ABSL_CONST_INIT std::atomic<bool> exit_on_dfatal(true);
  41. Y_ABSL_CONST_INIT std::atomic<bool> suppress_sigabort_trace(false);
  42. } // namespace
  43. bool IsInitialized() {
  44. return logging_initialized.load(std::memory_order_acquire);
  45. }
  46. void SetInitialized() {
  47. logging_initialized.store(true, std::memory_order_release);
  48. }
  49. void WriteToStderr(y_absl::string_view message, y_absl::LogSeverity severity) {
  50. if (message.empty()) return;
  51. #if defined(__EMSCRIPTEN__)
  52. // In WebAssembly, bypass filesystem emulation via fwrite.
  53. // Skip a trailing newline character as emscripten_errn adds one itself.
  54. const auto message_minus_newline = y_absl::StripSuffix(message, "\n");
  55. // emscripten_errn was introduced in 3.1.41 but broken in standalone mode
  56. // until 3.1.43.
  57. #if Y_ABSL_INTERNAL_EMSCRIPTEN_VERSION >= 3001043
  58. emscripten_errn(message_minus_newline.data(), message_minus_newline.size());
  59. #else
  60. TString null_terminated_message(message_minus_newline);
  61. _emscripten_err(null_terminated_message.c_str());
  62. #endif
  63. #else
  64. // Avoid using std::cerr from this module since we may get called during
  65. // exit code, and cerr may be partially or fully destroyed by then.
  66. std::fwrite(message.data(), message.size(), 1, stderr);
  67. #endif
  68. #if defined(_WIN64) || defined(_WIN32) || defined(_WIN16)
  69. // C99 requires stderr to not be fully-buffered by default (7.19.3.7), but
  70. // MS CRT buffers it anyway, so we must `fflush` to ensure the string hits
  71. // the console/file before the program dies (and takes the libc buffers
  72. // with it).
  73. // https://docs.microsoft.com/en-us/cpp/c-runtime-library/stream-i-o
  74. if (severity >= y_absl::LogSeverity::kWarning) {
  75. std::fflush(stderr);
  76. }
  77. #else
  78. // Avoid unused parameter warning in this branch.
  79. (void)severity;
  80. #endif
  81. }
  82. void SetTimeZone(y_absl::TimeZone tz) {
  83. y_absl::TimeZone* expected = nullptr;
  84. y_absl::TimeZone* new_tz = new y_absl::TimeZone(tz);
  85. // timezone_ptr can only be set once, otherwise new_tz is leaked.
  86. if (!timezone_ptr.compare_exchange_strong(expected, new_tz,
  87. std::memory_order_release,
  88. std::memory_order_relaxed)) {
  89. Y_ABSL_RAW_LOG(FATAL,
  90. "y_absl::log_internal::SetTimeZone() has already been called");
  91. }
  92. }
  93. const y_absl::TimeZone* TimeZone() {
  94. return timezone_ptr.load(std::memory_order_acquire);
  95. }
  96. bool ShouldSymbolizeLogStackTrace() {
  97. return symbolize_stack_trace.load(std::memory_order_acquire);
  98. }
  99. void EnableSymbolizeLogStackTrace(bool on_off) {
  100. symbolize_stack_trace.store(on_off, std::memory_order_release);
  101. }
  102. int MaxFramesInLogStackTrace() {
  103. return max_frames_in_stack_trace.load(std::memory_order_acquire);
  104. }
  105. void SetMaxFramesInLogStackTrace(int max_num_frames) {
  106. max_frames_in_stack_trace.store(max_num_frames, std::memory_order_release);
  107. }
  108. bool ExitOnDFatal() { return exit_on_dfatal.load(std::memory_order_acquire); }
  109. void SetExitOnDFatal(bool on_off) {
  110. exit_on_dfatal.store(on_off, std::memory_order_release);
  111. }
  112. bool SuppressSigabortTrace() {
  113. return suppress_sigabort_trace.load(std::memory_order_acquire);
  114. }
  115. bool SetSuppressSigabortTrace(bool on_off) {
  116. return suppress_sigabort_trace.exchange(on_off);
  117. }
  118. } // namespace log_internal
  119. Y_ABSL_NAMESPACE_END
  120. } // namespace y_absl