1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker //
15*9356374aSAndroid Build Coastguard Worker // Thread-safe logging routines that do not allocate any memory or
16*9356374aSAndroid Build Coastguard Worker // acquire any locks, and can therefore be used by low-level memory
17*9356374aSAndroid Build Coastguard Worker // allocation, synchronization, and signal-handling code.
18*9356374aSAndroid Build Coastguard Worker
19*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_
20*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
21*9356374aSAndroid Build Coastguard Worker
22*9356374aSAndroid Build Coastguard Worker #include <string>
23*9356374aSAndroid Build Coastguard Worker
24*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
25*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
26*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/atomic_hook.h"
27*9356374aSAndroid Build Coastguard Worker #include "absl/base/log_severity.h"
28*9356374aSAndroid Build Coastguard Worker #include "absl/base/macros.h"
29*9356374aSAndroid Build Coastguard Worker #include "absl/base/optimization.h"
30*9356374aSAndroid Build Coastguard Worker #include "absl/base/port.h"
31*9356374aSAndroid Build Coastguard Worker
32*9356374aSAndroid Build Coastguard Worker // This is similar to LOG(severity) << format..., but
33*9356374aSAndroid Build Coastguard Worker // * it is to be used ONLY by low-level modules that can't use normal LOG()
34*9356374aSAndroid Build Coastguard Worker // * it is designed to be a low-level logger that does not allocate any
35*9356374aSAndroid Build Coastguard Worker // memory and does not need any locks, hence:
36*9356374aSAndroid Build Coastguard Worker // * it logs straight and ONLY to STDERR w/o buffering
37*9356374aSAndroid Build Coastguard Worker // * it uses an explicit printf-format and arguments list
38*9356374aSAndroid Build Coastguard Worker // * it will silently chop off really long message strings
39*9356374aSAndroid Build Coastguard Worker // Usage example:
40*9356374aSAndroid Build Coastguard Worker // ABSL_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
41*9356374aSAndroid Build Coastguard Worker // This will print an almost standard log line like this to stderr only:
42*9356374aSAndroid Build Coastguard Worker // E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
43*9356374aSAndroid Build Coastguard Worker
44*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG(severity, ...) \
45*9356374aSAndroid Build Coastguard Worker do { \
46*9356374aSAndroid Build Coastguard Worker constexpr const char* absl_raw_log_internal_basename = \
47*9356374aSAndroid Build Coastguard Worker ::absl::raw_log_internal::Basename(__FILE__, sizeof(__FILE__) - 1); \
48*9356374aSAndroid Build Coastguard Worker ::absl::raw_log_internal::RawLog(ABSL_RAW_LOG_INTERNAL_##severity, \
49*9356374aSAndroid Build Coastguard Worker absl_raw_log_internal_basename, __LINE__, \
50*9356374aSAndroid Build Coastguard Worker __VA_ARGS__); \
51*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_##severity; \
52*9356374aSAndroid Build Coastguard Worker } while (0)
53*9356374aSAndroid Build Coastguard Worker
54*9356374aSAndroid Build Coastguard Worker // Similar to CHECK(condition) << message, but for low-level modules:
55*9356374aSAndroid Build Coastguard Worker // we use only ABSL_RAW_LOG that does not allocate memory.
56*9356374aSAndroid Build Coastguard Worker // We do not want to provide args list here to encourage this usage:
57*9356374aSAndroid Build Coastguard Worker // if (!cond) ABSL_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
58*9356374aSAndroid Build Coastguard Worker // so that the args are not computed when not needed.
59*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_CHECK(condition, message) \
60*9356374aSAndroid Build Coastguard Worker do { \
61*9356374aSAndroid Build Coastguard Worker if (ABSL_PREDICT_FALSE(!(condition))) { \
62*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
63*9356374aSAndroid Build Coastguard Worker } \
64*9356374aSAndroid Build Coastguard Worker } while (0)
65*9356374aSAndroid Build Coastguard Worker
66*9356374aSAndroid Build Coastguard Worker // ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above,
67*9356374aSAndroid Build Coastguard Worker // except that if the richer log library is linked into the binary, we dispatch
68*9356374aSAndroid Build Coastguard Worker // to that instead. This is potentially useful for internal logging and
69*9356374aSAndroid Build Coastguard Worker // assertions, where we are using RAW_LOG neither for its async-signal-safety
70*9356374aSAndroid Build Coastguard Worker // nor for its non-allocating nature, but rather because raw logging has very
71*9356374aSAndroid Build Coastguard Worker // few other dependencies.
72*9356374aSAndroid Build Coastguard Worker //
73*9356374aSAndroid Build Coastguard Worker // The API is a subset of the above: each macro only takes two arguments. Use
74*9356374aSAndroid Build Coastguard Worker // StrCat if you need to build a richer message.
75*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_LOG(severity, message) \
76*9356374aSAndroid Build Coastguard Worker do { \
77*9356374aSAndroid Build Coastguard Worker constexpr const char* absl_raw_log_internal_filename = __FILE__; \
78*9356374aSAndroid Build Coastguard Worker ::absl::raw_log_internal::internal_log_function( \
79*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG_INTERNAL_##severity, absl_raw_log_internal_filename, \
80*9356374aSAndroid Build Coastguard Worker __LINE__, message); \
81*9356374aSAndroid Build Coastguard Worker ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_##severity; \
82*9356374aSAndroid Build Coastguard Worker } while (0)
83*9356374aSAndroid Build Coastguard Worker
84*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_CHECK(condition, message) \
85*9356374aSAndroid Build Coastguard Worker do { \
86*9356374aSAndroid Build Coastguard Worker if (ABSL_PREDICT_FALSE(!(condition))) { \
87*9356374aSAndroid Build Coastguard Worker std::string death_message = "Check " #condition " failed: "; \
88*9356374aSAndroid Build Coastguard Worker death_message += std::string(message); \
89*9356374aSAndroid Build Coastguard Worker ABSL_INTERNAL_LOG(FATAL, death_message); \
90*9356374aSAndroid Build Coastguard Worker } \
91*9356374aSAndroid Build Coastguard Worker } while (0)
92*9356374aSAndroid Build Coastguard Worker
93*9356374aSAndroid Build Coastguard Worker #ifndef NDEBUG
94*9356374aSAndroid Build Coastguard Worker
95*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_DLOG(severity, ...) ABSL_RAW_LOG(severity, __VA_ARGS__)
96*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_DCHECK(condition, message) ABSL_RAW_CHECK(condition, message)
97*9356374aSAndroid Build Coastguard Worker
98*9356374aSAndroid Build Coastguard Worker #else // NDEBUG
99*9356374aSAndroid Build Coastguard Worker
100*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_DLOG(severity, ...) \
101*9356374aSAndroid Build Coastguard Worker while (false) ABSL_RAW_LOG(severity, __VA_ARGS__)
102*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_DCHECK(condition, message) \
103*9356374aSAndroid Build Coastguard Worker while (false) ABSL_RAW_CHECK(condition, message)
104*9356374aSAndroid Build Coastguard Worker
105*9356374aSAndroid Build Coastguard Worker #endif // NDEBUG
106*9356374aSAndroid Build Coastguard Worker
107*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_INFO ::absl::LogSeverity::kInfo
108*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_WARNING ::absl::LogSeverity::kWarning
109*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_ERROR ::absl::LogSeverity::kError
110*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_FATAL ::absl::LogSeverity::kFatal
111*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_DFATAL ::absl::kLogDebugFatal
112*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_LEVEL(severity) \
113*9356374aSAndroid Build Coastguard Worker ::absl::NormalizeLogSeverity(severity)
114*9356374aSAndroid Build Coastguard Worker
115*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_INFO
116*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_WARNING
117*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_ERROR
118*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_FATAL ABSL_UNREACHABLE()
119*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_DFATAL
120*9356374aSAndroid Build Coastguard Worker #define ABSL_RAW_LOG_INTERNAL_MAYBE_UNREACHABLE_LEVEL(severity)
121*9356374aSAndroid Build Coastguard Worker
122*9356374aSAndroid Build Coastguard Worker namespace absl {
123*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
124*9356374aSAndroid Build Coastguard Worker namespace raw_log_internal {
125*9356374aSAndroid Build Coastguard Worker
126*9356374aSAndroid Build Coastguard Worker // Helper function to implement ABSL_RAW_LOG
127*9356374aSAndroid Build Coastguard Worker // Logs format... at "severity" level, reporting it
128*9356374aSAndroid Build Coastguard Worker // as called from file:line.
129*9356374aSAndroid Build Coastguard Worker // This does not allocate memory or acquire locks.
130*9356374aSAndroid Build Coastguard Worker void RawLog(absl::LogSeverity severity, const char* file, int line,
131*9356374aSAndroid Build Coastguard Worker const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
132*9356374aSAndroid Build Coastguard Worker
133*9356374aSAndroid Build Coastguard Worker // Writes the provided buffer directly to stderr, in a signal-safe, low-level
134*9356374aSAndroid Build Coastguard Worker // manner. Preserves errno.
135*9356374aSAndroid Build Coastguard Worker void AsyncSignalSafeWriteError(const char* s, size_t len);
136*9356374aSAndroid Build Coastguard Worker
137*9356374aSAndroid Build Coastguard Worker // compile-time function to get the "base" filename, that is, the part of
138*9356374aSAndroid Build Coastguard Worker // a filename after the last "/" or "\" path separator. The search starts at
139*9356374aSAndroid Build Coastguard Worker // the end of the string; the second parameter is the length of the string.
Basename(const char * fname,int offset)140*9356374aSAndroid Build Coastguard Worker constexpr const char* Basename(const char* fname, int offset) {
141*9356374aSAndroid Build Coastguard Worker return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
142*9356374aSAndroid Build Coastguard Worker ? fname + offset
143*9356374aSAndroid Build Coastguard Worker : Basename(fname, offset - 1);
144*9356374aSAndroid Build Coastguard Worker }
145*9356374aSAndroid Build Coastguard Worker
146*9356374aSAndroid Build Coastguard Worker // For testing only.
147*9356374aSAndroid Build Coastguard Worker // Returns true if raw logging is fully supported. When it is not
148*9356374aSAndroid Build Coastguard Worker // fully supported, no messages will be emitted, but a log at FATAL
149*9356374aSAndroid Build Coastguard Worker // severity will cause an abort.
150*9356374aSAndroid Build Coastguard Worker //
151*9356374aSAndroid Build Coastguard Worker // TODO(gfalcon): Come up with a better name for this method.
152*9356374aSAndroid Build Coastguard Worker bool RawLoggingFullySupported();
153*9356374aSAndroid Build Coastguard Worker
154*9356374aSAndroid Build Coastguard Worker // Function type for a raw_log customization hook for suppressing messages
155*9356374aSAndroid Build Coastguard Worker // by severity, and for writing custom prefixes on non-suppressed messages.
156*9356374aSAndroid Build Coastguard Worker //
157*9356374aSAndroid Build Coastguard Worker // The installed hook is called for every raw log invocation. The message will
158*9356374aSAndroid Build Coastguard Worker // be logged to stderr only if the hook returns true. FATAL errors will cause
159*9356374aSAndroid Build Coastguard Worker // the process to abort, even if writing to stderr is suppressed. The hook is
160*9356374aSAndroid Build Coastguard Worker // also provided with an output buffer, where it can write a custom log message
161*9356374aSAndroid Build Coastguard Worker // prefix.
162*9356374aSAndroid Build Coastguard Worker //
163*9356374aSAndroid Build Coastguard Worker // The raw_log system does not allocate memory or grab locks. User-provided
164*9356374aSAndroid Build Coastguard Worker // hooks must avoid these operations, and must not throw exceptions.
165*9356374aSAndroid Build Coastguard Worker //
166*9356374aSAndroid Build Coastguard Worker // 'severity' is the severity level of the message being written.
167*9356374aSAndroid Build Coastguard Worker // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
168*9356374aSAndroid Build Coastguard Worker // was located.
169*9356374aSAndroid Build Coastguard Worker // 'buf' and 'buf_size' are pointers to the buffer and buffer size. If the
170*9356374aSAndroid Build Coastguard Worker // hook writes a prefix, it must increment *buf and decrement *buf_size
171*9356374aSAndroid Build Coastguard Worker // accordingly.
172*9356374aSAndroid Build Coastguard Worker using LogFilterAndPrefixHook = bool (*)(absl::LogSeverity severity,
173*9356374aSAndroid Build Coastguard Worker const char* file, int line, char** buf,
174*9356374aSAndroid Build Coastguard Worker int* buf_size);
175*9356374aSAndroid Build Coastguard Worker
176*9356374aSAndroid Build Coastguard Worker // Function type for a raw_log customization hook called to abort a process
177*9356374aSAndroid Build Coastguard Worker // when a FATAL message is logged. If the provided AbortHook() returns, the
178*9356374aSAndroid Build Coastguard Worker // logging system will call abort().
179*9356374aSAndroid Build Coastguard Worker //
180*9356374aSAndroid Build Coastguard Worker // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
181*9356374aSAndroid Build Coastguard Worker // was located.
182*9356374aSAndroid Build Coastguard Worker // The NUL-terminated logged message lives in the buffer between 'buf_start'
183*9356374aSAndroid Build Coastguard Worker // and 'buf_end'. 'prefix_end' points to the first non-prefix character of the
184*9356374aSAndroid Build Coastguard Worker // buffer (as written by the LogFilterAndPrefixHook.)
185*9356374aSAndroid Build Coastguard Worker //
186*9356374aSAndroid Build Coastguard Worker // The lifetime of the filename and message buffers will not end while the
187*9356374aSAndroid Build Coastguard Worker // process remains alive.
188*9356374aSAndroid Build Coastguard Worker using AbortHook = void (*)(const char* file, int line, const char* buf_start,
189*9356374aSAndroid Build Coastguard Worker const char* prefix_end, const char* buf_end);
190*9356374aSAndroid Build Coastguard Worker
191*9356374aSAndroid Build Coastguard Worker // Internal logging function for ABSL_INTERNAL_LOG to dispatch to.
192*9356374aSAndroid Build Coastguard Worker //
193*9356374aSAndroid Build Coastguard Worker // TODO(gfalcon): When string_view no longer depends on base, change this
194*9356374aSAndroid Build Coastguard Worker // interface to take its message as a string_view instead.
195*9356374aSAndroid Build Coastguard Worker using InternalLogFunction = void (*)(absl::LogSeverity severity,
196*9356374aSAndroid Build Coastguard Worker const char* file, int line,
197*9356374aSAndroid Build Coastguard Worker const std::string& message);
198*9356374aSAndroid Build Coastguard Worker
199*9356374aSAndroid Build Coastguard Worker ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_DLL extern base_internal::AtomicHook<
200*9356374aSAndroid Build Coastguard Worker InternalLogFunction>
201*9356374aSAndroid Build Coastguard Worker internal_log_function;
202*9356374aSAndroid Build Coastguard Worker
203*9356374aSAndroid Build Coastguard Worker // Registers hooks of the above types. Only a single hook of each type may be
204*9356374aSAndroid Build Coastguard Worker // registered. It is an error to call these functions multiple times with
205*9356374aSAndroid Build Coastguard Worker // different input arguments.
206*9356374aSAndroid Build Coastguard Worker //
207*9356374aSAndroid Build Coastguard Worker // These functions are safe to call at any point during initialization; they do
208*9356374aSAndroid Build Coastguard Worker // not block or malloc, and are async-signal safe.
209*9356374aSAndroid Build Coastguard Worker void RegisterLogFilterAndPrefixHook(LogFilterAndPrefixHook func);
210*9356374aSAndroid Build Coastguard Worker void RegisterAbortHook(AbortHook func);
211*9356374aSAndroid Build Coastguard Worker void RegisterInternalLogFunction(InternalLogFunction func);
212*9356374aSAndroid Build Coastguard Worker
213*9356374aSAndroid Build Coastguard Worker } // namespace raw_log_internal
214*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
215*9356374aSAndroid Build Coastguard Worker } // namespace absl
216*9356374aSAndroid Build Coastguard Worker
217*9356374aSAndroid Build Coastguard Worker #endif // ABSL_BASE_INTERNAL_RAW_LOGGING_H_
218