abseil-cpp/absl/base/internal/raw_logging.h
Go to the documentation of this file.
1 // 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 // Thread-safe logging routines that do not allocate any memory or
16 // acquire any locks, and can therefore be used by low-level memory
17 // allocation, synchronization, and signal-handling code.
18 
19 #ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_
20 #define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
21 
22 #include <string>
23 
24 #include "absl/base/attributes.h"
25 #include "absl/base/config.h"
26 #include "absl/base/internal/atomic_hook.h"
27 #include "absl/base/log_severity.h"
28 #include "absl/base/macros.h"
29 #include "absl/base/optimization.h"
30 #include "absl/base/port.h"
31 
32 // This is similar to LOG(severity) << format..., but
33 // * it is to be used ONLY by low-level modules that can't use normal LOG()
34 // * it is designed to be a low-level logger that does not allocate any
35 // memory and does not need any locks, hence:
36 // * it logs straight and ONLY to STDERR w/o buffering
37 // * it uses an explicit printf-format and arguments list
38 // * it will silently chop off really long message strings
39 // Usage example:
40 // ABSL_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
41 // This will print an almost standard log line like this to stderr only:
42 // E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
43 
44 #define ABSL_RAW_LOG(severity, ...) \
45  do { \
46  constexpr const char* absl_raw_logging_internal_basename = \
47  ::absl::raw_logging_internal::Basename(__FILE__, \
48  sizeof(__FILE__) - 1); \
49  ::absl::raw_logging_internal::RawLog(ABSL_RAW_LOGGING_INTERNAL_##severity, \
50  absl_raw_logging_internal_basename, \
51  __LINE__, __VA_ARGS__); \
52  } while (0)
53 
54 // Similar to CHECK(condition) << message, but for low-level modules:
55 // we use only ABSL_RAW_LOG that does not allocate memory.
56 // We do not want to provide args list here to encourage this usage:
57 // if (!cond) ABSL_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
58 // so that the args are not computed when not needed.
59 #define ABSL_RAW_CHECK(condition, message) \
60  do { \
61  if (ABSL_PREDICT_FALSE(!(condition))) { \
62  ABSL_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
63  } \
64  } while (0)
65 
66 // ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above,
67 // except that if the richer log library is linked into the binary, we dispatch
68 // to that instead. This is potentially useful for internal logging and
69 // assertions, where we are using RAW_LOG neither for its async-signal-safety
70 // nor for its non-allocating nature, but rather because raw logging has very
71 // few other dependencies.
72 //
73 // The API is a subset of the above: each macro only takes two arguments. Use
74 // StrCat if you need to build a richer message.
75 #define ABSL_INTERNAL_LOG(severity, message) \
76  do { \
77  constexpr const char* absl_raw_logging_internal_filename = __FILE__; \
78  ::absl::raw_logging_internal::internal_log_function( \
79  ABSL_RAW_LOGGING_INTERNAL_##severity, \
80  absl_raw_logging_internal_filename, __LINE__, message); \
81  if (ABSL_RAW_LOGGING_INTERNAL_##severity == ::absl::LogSeverity::kFatal) \
82  ABSL_INTERNAL_UNREACHABLE; \
83  } while (0)
84 
85 #define ABSL_INTERNAL_CHECK(condition, message) \
86  do { \
87  if (ABSL_PREDICT_FALSE(!(condition))) { \
88  std::string death_message = "Check " #condition " failed: "; \
89  death_message += std::string(message); \
90  ABSL_INTERNAL_LOG(FATAL, death_message); \
91  } \
92  } while (0)
93 
94 #define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
95 #define ABSL_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
96 #define ABSL_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
97 #define ABSL_RAW_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
98 #define ABSL_RAW_LOGGING_INTERNAL_LEVEL(severity) \
99  ::absl::NormalizeLogSeverity(severity)
100 
101 namespace absl {
103 namespace raw_logging_internal {
104 
105 // Helper function to implement ABSL_RAW_LOG
106 // Logs format... at "severity" level, reporting it
107 // as called from file:line.
108 // This does not allocate memory or acquire locks.
109 void RawLog(absl::LogSeverity severity, const char* file, int line,
110  const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
111 
112 // Writes the provided buffer directly to stderr, in a signal-safe, low-level
113 // manner.
114 void AsyncSignalSafeWriteToStderr(const char* s, size_t len);
115 
116 // compile-time function to get the "base" filename, that is, the part of
117 // a filename after the last "/" or "\" path separator. The search starts at
118 // the end of the string; the second parameter is the length of the string.
119 constexpr const char* Basename(const char* fname, int offset) {
120  return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
121  ? fname + offset
122  : Basename(fname, offset - 1);
123 }
124 
125 // For testing only.
126 // Returns true if raw logging is fully supported. When it is not
127 // fully supported, no messages will be emitted, but a log at FATAL
128 // severity will cause an abort.
129 //
130 // TODO(gfalcon): Come up with a better name for this method.
132 
133 // Function type for a raw_logging customization hook for suppressing messages
134 // by severity, and for writing custom prefixes on non-suppressed messages.
135 //
136 // The installed hook is called for every raw log invocation. The message will
137 // be logged to stderr only if the hook returns true. FATAL errors will cause
138 // the process to abort, even if writing to stderr is suppressed. The hook is
139 // also provided with an output buffer, where it can write a custom log message
140 // prefix.
141 //
142 // The raw_logging system does not allocate memory or grab locks. User-provided
143 // hooks must avoid these operations, and must not throw exceptions.
144 //
145 // 'severity' is the severity level of the message being written.
146 // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
147 // was located.
148 // 'buf' and 'buf_size' are pointers to the buffer and buffer size. If the
149 // hook writes a prefix, it must increment *buf and decrement *buf_size
150 // accordingly.
152  const char* file, int line, char** buf,
153  int* buf_size);
154 
155 // Function type for a raw_logging customization hook called to abort a process
156 // when a FATAL message is logged. If the provided AbortHook() returns, the
157 // logging system will call abort().
158 //
159 // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
160 // was located.
161 // The NUL-terminated logged message lives in the buffer between 'buf_start'
162 // and 'buf_end'. 'prefix_end' points to the first non-prefix character of the
163 // buffer (as written by the LogFilterAndPrefixHook.)
164 //
165 // The lifetime of the filename and message buffers will not end while the
166 // process remains alive.
167 using AbortHook = void (*)(const char* file, int line, const char* buf_start,
168  const char* prefix_end, const char* buf_end);
169 
170 // Internal logging function for ABSL_INTERNAL_LOG to dispatch to.
171 //
172 // TODO(gfalcon): When string_view no longer depends on base, change this
173 // interface to take its message as a string_view instead.
175  const char* file, int line,
177 
181 
182 // Registers hooks of the above types. Only a single hook of each type may be
183 // registered. It is an error to call these functions multiple times with
184 // different input arguments.
185 //
186 // These functions are safe to call at any point during initialization; they do
187 // not block or malloc, and are async-signal safe.
191 
192 } // namespace raw_logging_internal
194 } // namespace absl
195 
196 #endif // ABSL_BASE_INTERNAL_RAW_LOGGING_H_
http2_test_server.format
format
Definition: http2_test_server.py:118
bloat_diff.severity
def severity
Definition: bloat_diff.py:143
absl::raw_logging_internal::RegisterAbortHook
void RegisterAbortHook(AbortHook func)
Definition: abseil-cpp/absl/base/internal/raw_logging.cc:241
bool
bool
Definition: setup_once.h:312
absl::raw_logging_internal::AsyncSignalSafeWriteToStderr
void AsyncSignalSafeWriteToStderr(const char *s, size_t len)
Definition: abseil-cpp/absl/base/internal/raw_logging.cc:200
file
const grpc_generator::File * file
Definition: python_private_generator.h:38
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
#define ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
Definition: abseil-cpp/absl/base/internal/atomic_hook.h:49
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
absl::raw_logging_internal::InternalLogFunction
void(*)(absl::LogSeverity severity, const char *file, int line, const std::string &message) InternalLogFunction
Definition: abseil-cpp/absl/base/internal/raw_logging.h:176
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
absl::base_internal::AtomicHook
Definition: abseil-cpp/absl/base/internal/atomic_hook.h:43
absl::raw_logging_internal::LogFilterAndPrefixHook
bool(*)(absl::LogSeverity severity, const char *file, int line, char **buf, int *buf_size) LogFilterAndPrefixHook
Definition: abseil-cpp/absl/base/internal/raw_logging.h:153
absl::raw_logging_internal::AbortHook
void(*)(const char *file, int line, const char *buf_start, const char *prefix_end, const char *buf_end) AbortHook
Definition: abseil-cpp/absl/base/internal/raw_logging.h:168
absl::LogSeverity
LogSeverity
Definition: abseil-cpp/absl/base/log_severity.h:69
absl::raw_logging_internal::RegisterLogFilterAndPrefixHook
void RegisterLogFilterAndPrefixHook(LogFilterAndPrefixHook func)
Definition: abseil-cpp/absl/base/internal/raw_logging.cc:237
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::raw_logging_internal::RawLog
void RawLog(absl::LogSeverity severity, const char *file, int line, const char *format,...)
Definition: abseil-cpp/absl/base/internal/raw_logging.cc:217
ABSL_PRINTF_ATTRIBUTE
#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
Definition: abseil-cpp/absl/base/attributes.h:92
ABSL_DLL
#define ABSL_DLL
Definition: third_party/abseil-cpp/absl/base/config.h:746
absl::raw_logging_internal::RegisterInternalLogFunction
void RegisterInternalLogFunction(InternalLogFunction func)
Definition: abseil-cpp/absl/base/internal/raw_logging.cc:243
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
regen-readme.line
line
Definition: regen-readme.py:30
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
absl::raw_logging_internal::internal_log_function
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_DLL base_internal::AtomicHook< InternalLogFunction > internal_log_function
Definition: bloaty/third_party/abseil-cpp/absl/base/internal/raw_logging.h:179
absl::raw_logging_internal::Basename
constexpr const char * Basename(const char *fname, int offset)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:119
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142
absl::raw_logging_internal::RawLoggingFullySupported
bool RawLoggingFullySupported()
Definition: abseil-cpp/absl/base/internal/raw_logging.cc:225


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:59