blob: 074e026adbbb5ebe1e6fb00cb8565a3b7df5f1c7 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2017 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/base/internal/raw_logging.h"
16
17#include <stddef.h>
18#include <cstdarg>
19#include <cstdio>
20#include <cstdlib>
21#include <cstring>
22
23#include "absl/base/attributes.h"
24#include "absl/base/config.h"
25#include "absl/base/internal/atomic_hook.h"
26#include "absl/base/log_severity.h"
27
28// We know how to perform low-level writes to stderr in POSIX and Windows. For
29// these platforms, we define the token ABSL_LOW_LEVEL_WRITE_SUPPORTED.
30// Much of raw_logging.cc becomes a no-op when we can't output messages,
31// although a FATAL ABSL_RAW_LOG message will still abort the process.
32
33// ABSL_HAVE_POSIX_WRITE is defined when the platform provides posix write()
34// (as from unistd.h)
35//
36// This preprocessor token is also defined in raw_io.cc. If you need to copy
37// this, consider moving both to config.h instead.
38#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
39 defined(__Fuchsia__) || defined(__native_client__) || \
Austin Schuhb4691e92020-12-31 12:37:18 -080040 defined(__EMSCRIPTEN__) || defined(__ASYLO__)
Austin Schuh36244a12019-09-21 17:52:38 -070041
Austin Schuhb4691e92020-12-31 12:37:18 -080042#include <unistd.h>
Austin Schuh36244a12019-09-21 17:52:38 -070043
44#define ABSL_HAVE_POSIX_WRITE 1
45#define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
46#else
47#undef ABSL_HAVE_POSIX_WRITE
48#endif
49
50// ABSL_HAVE_SYSCALL_WRITE is defined when the platform provides the syscall
51// syscall(SYS_write, /*int*/ fd, /*char* */ buf, /*size_t*/ len);
52// for low level operations that want to avoid libc.
53#if (defined(__linux__) || defined(__FreeBSD__)) && !defined(__ANDROID__)
54#include <sys/syscall.h>
55#define ABSL_HAVE_SYSCALL_WRITE 1
56#define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
57#else
58#undef ABSL_HAVE_SYSCALL_WRITE
59#endif
60
61#ifdef _WIN32
62#include <io.h>
63
64#define ABSL_HAVE_RAW_IO 1
65#define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
66#else
67#undef ABSL_HAVE_RAW_IO
68#endif
69
Austin Schuhb4691e92020-12-31 12:37:18 -080070namespace absl {
71ABSL_NAMESPACE_BEGIN
72namespace raw_logging_internal {
73namespace {
Austin Schuh36244a12019-09-21 17:52:38 -070074
Austin Schuhb4691e92020-12-31 12:37:18 -080075// TODO(gfalcon): We want raw-logging to work on as many platforms as possible.
76// Explicitly `#error` out when not `ABSL_LOW_LEVEL_WRITE_SUPPORTED`, except for
77// a selected set of platforms for which we expect not to be able to raw log.
78
79ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
80 absl::base_internal::AtomicHook<LogPrefixHook>
81 log_prefix_hook;
82ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
83 absl::base_internal::AtomicHook<AbortHook>
84 abort_hook;
Austin Schuh36244a12019-09-21 17:52:38 -070085
86#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
Austin Schuhb4691e92020-12-31 12:37:18 -080087constexpr char kTruncated[] = " ... (message truncated)\n";
Austin Schuh36244a12019-09-21 17:52:38 -070088
89// sprintf the format to the buffer, adjusting *buf and *size to reflect the
90// consumed bytes, and return whether the message fit without truncation. If
91// truncation occurred, if possible leave room in the buffer for the message
92// kTruncated[].
Austin Schuhb4691e92020-12-31 12:37:18 -080093bool VADoRawLog(char** buf, int* size, const char* format, va_list ap)
94 ABSL_PRINTF_ATTRIBUTE(3, 0);
95bool VADoRawLog(char** buf, int* size, const char* format, va_list ap) {
Austin Schuh36244a12019-09-21 17:52:38 -070096 int n = vsnprintf(*buf, *size, format, ap);
97 bool result = true;
98 if (n < 0 || n > *size) {
99 result = false;
100 if (static_cast<size_t>(*size) > sizeof(kTruncated)) {
101 n = *size - sizeof(kTruncated); // room for truncation message
102 } else {
Austin Schuhb4691e92020-12-31 12:37:18 -0800103 n = 0; // no room for truncation message
Austin Schuh36244a12019-09-21 17:52:38 -0700104 }
105 }
106 *size -= n;
107 *buf += n;
108 return result;
109}
110#endif // ABSL_LOW_LEVEL_WRITE_SUPPORTED
111
Austin Schuhb4691e92020-12-31 12:37:18 -0800112constexpr int kLogBufSize = 3000;
Austin Schuh36244a12019-09-21 17:52:38 -0700113
114// CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
115// that invoke malloc() and getenv() that might acquire some locks.
116
117// Helper for RawLog below.
118// *DoRawLog writes to *buf of *size and move them past the written portion.
119// It returns true iff there was no overflow or error.
120bool DoRawLog(char** buf, int* size, const char* format, ...)
121 ABSL_PRINTF_ATTRIBUTE(3, 4);
122bool DoRawLog(char** buf, int* size, const char* format, ...) {
123 va_list ap;
124 va_start(ap, format);
125 int n = vsnprintf(*buf, *size, format, ap);
126 va_end(ap);
127 if (n < 0 || n > *size) return false;
128 *size -= n;
129 *buf += n;
130 return true;
131}
132
133void RawLogVA(absl::LogSeverity severity, const char* file, int line,
134 const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0);
135void RawLogVA(absl::LogSeverity severity, const char* file, int line,
136 const char* format, va_list ap) {
137 char buffer[kLogBufSize];
138 char* buf = buffer;
139 int size = sizeof(buffer);
140#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
141 bool enabled = true;
142#else
143 bool enabled = false;
144#endif
145
146#ifdef ABSL_MIN_LOG_LEVEL
147 if (severity < static_cast<absl::LogSeverity>(ABSL_MIN_LOG_LEVEL) &&
148 severity < absl::LogSeverity::kFatal) {
149 enabled = false;
150 }
151#endif
152
153 auto log_prefix_hook_ptr = log_prefix_hook.Load();
154 if (log_prefix_hook_ptr) {
155 enabled = log_prefix_hook_ptr(severity, file, line, &buf, &size);
156 } else {
157 if (enabled) {
158 DoRawLog(&buf, &size, "[%s : %d] RAW: ", file, line);
159 }
160 }
161 const char* const prefix_end = buf;
162
163#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
164 if (enabled) {
165 bool no_chop = VADoRawLog(&buf, &size, format, ap);
166 if (no_chop) {
167 DoRawLog(&buf, &size, "\n");
168 } else {
169 DoRawLog(&buf, &size, "%s", kTruncated);
170 }
Austin Schuhb4691e92020-12-31 12:37:18 -0800171 SafeWriteToStderr(buffer, strlen(buffer));
Austin Schuh36244a12019-09-21 17:52:38 -0700172 }
173#else
174 static_cast<void>(format);
175 static_cast<void>(ap);
176#endif
177
178 // Abort the process after logging a FATAL message, even if the output itself
179 // was suppressed.
180 if (severity == absl::LogSeverity::kFatal) {
181 abort_hook(file, line, buffer, prefix_end, buffer + kLogBufSize);
182 abort();
183 }
184}
185
Austin Schuhb4691e92020-12-31 12:37:18 -0800186// Non-formatting version of RawLog().
187//
188// TODO(gfalcon): When string_view no longer depends on base, change this
189// interface to take its message as a string_view instead.
190void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line,
191 const std::string& message) {
192 RawLog(severity, file, line, "%.*s", static_cast<int>(message.size()),
193 message.data());
194}
195
Austin Schuh36244a12019-09-21 17:52:38 -0700196} // namespace
197
Austin Schuh36244a12019-09-21 17:52:38 -0700198void SafeWriteToStderr(const char *s, size_t len) {
199#if defined(ABSL_HAVE_SYSCALL_WRITE)
200 syscall(SYS_write, STDERR_FILENO, s, len);
201#elif defined(ABSL_HAVE_POSIX_WRITE)
202 write(STDERR_FILENO, s, len);
203#elif defined(ABSL_HAVE_RAW_IO)
204 _write(/* stderr */ 2, s, len);
205#else
206 // stderr logging unsupported on this platform
207 (void) s;
208 (void) len;
209#endif
210}
211
212void RawLog(absl::LogSeverity severity, const char* file, int line,
Austin Schuh36244a12019-09-21 17:52:38 -0700213 const char* format, ...) {
214 va_list ap;
215 va_start(ap, format);
216 RawLogVA(severity, file, line, format, ap);
217 va_end(ap);
218}
219
Austin Schuh36244a12019-09-21 17:52:38 -0700220bool RawLoggingFullySupported() {
221#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
222 return true;
223#else // !ABSL_LOW_LEVEL_WRITE_SUPPORTED
224 return false;
225#endif // !ABSL_LOW_LEVEL_WRITE_SUPPORTED
226}
227
Austin Schuhb4691e92020-12-31 12:37:18 -0800228ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_DLL
229 absl::base_internal::AtomicHook<InternalLogFunction>
230 internal_log_function(DefaultInternalLog);
231
232void RegisterLogPrefixHook(LogPrefixHook func) { log_prefix_hook.Store(func); }
233
234void RegisterAbortHook(AbortHook func) { abort_hook.Store(func); }
Austin Schuh36244a12019-09-21 17:52:38 -0700235
236void RegisterInternalLogFunction(InternalLogFunction func) {
237 internal_log_function.Store(func);
238}
239
240} // namespace raw_logging_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800241ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700242} // namespace absl