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