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"
26 #include "absl/base/log_severity.h"
27 #include "absl/base/macros.h"
28 #include "absl/base/port.h"
29 
30 // This is similar to LOG(severity) << format..., but
31 // * it is to be used ONLY by low-level modules that can't use normal LOG()
32 // * it is designed to be a low-level logger that does not allocate any
33 // memory and does not need any locks, hence:
34 // * it logs straight and ONLY to STDERR w/o buffering
35 // * it uses an explicit printf-format and arguments list
36 // * it will silently chop off really long message strings
37 // Usage example:
38 // ABSL_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
39 // This will print an almost standard log line like this to stderr only:
40 // E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
41 
42 #define ABSL_RAW_LOG(severity, ...) \
43  do { \
44  constexpr const char* absl_raw_logging_internal_basename = \
45  ::absl::raw_logging_internal::Basename(__FILE__, \
46  sizeof(__FILE__) - 1); \
47  ::absl::raw_logging_internal::RawLog(ABSL_RAW_LOGGING_INTERNAL_##severity, \
48  absl_raw_logging_internal_basename, \
49  __LINE__, __VA_ARGS__); \
50  } while (0)
51 
52 // Similar to CHECK(condition) << message, but for low-level modules:
53 // we use only ABSL_RAW_LOG that does not allocate memory.
54 // We do not want to provide args list here to encourage this usage:
55 // if (!cond) ABSL_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
56 // so that the args are not computed when not needed.
57 #define ABSL_RAW_CHECK(condition, message) \
58  do { \
59  if (ABSL_PREDICT_FALSE(!(condition))) { \
60  ABSL_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
61  } \
62  } while (0)
63 
64 // ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above,
65 // except that if the richer log library is linked into the binary, we dispatch
66 // to that instead. This is potentially useful for internal logging and
67 // assertions, where we are using RAW_LOG neither for its async-signal-safety
68 // nor for its non-allocating nature, but rather because raw logging has very
69 // few other dependencies.
70 //
71 // The API is a subset of the above: each macro only takes two arguments. Use
72 // StrCat if you need to build a richer message.
73 #define ABSL_INTERNAL_LOG(severity, message) \
74  do { \
75  constexpr const char* absl_raw_logging_internal_basename = \
76  ::absl::raw_logging_internal::Basename(__FILE__, \
77  sizeof(__FILE__) - 1); \
78  ::absl::raw_logging_internal::internal_log_function( \
79  ABSL_RAW_LOGGING_INTERNAL_##severity, \
80  absl_raw_logging_internal_basename, __LINE__, message); \
81  } while (0)
82 
83 #define ABSL_INTERNAL_CHECK(condition, message) \
84  do { \
85  if (ABSL_PREDICT_FALSE(!(condition))) { \
86  std::string death_message = "Check " #condition " failed: "; \
87  death_message += std::string(message); \
88  ABSL_INTERNAL_LOG(FATAL, death_message); \
89  } \
90  } while (0)
91 
92 #define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
93 #define ABSL_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
94 #define ABSL_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
95 #define ABSL_RAW_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
96 #define ABSL_RAW_LOGGING_INTERNAL_LEVEL(severity) \
97  ::absl::NormalizeLogSeverity(severity)
98 
99 namespace absl {
100 namespace raw_logging_internal {
101 
102 // Helper function to implement ABSL_RAW_LOG
103 // Logs format... at "severity" level, reporting it
104 // as called from file:line.
105 // This does not allocate memory or acquire locks.
106 void RawLog(absl::LogSeverity severity, const char* file, int line,
107  const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
108 
109 // Writes the provided buffer directly to stderr, in a safe, low-level manner.
110 //
111 // In POSIX this means calling write(), which is async-signal safe and does
112 // not malloc. If the platform supports the SYS_write syscall, we invoke that
113 // directly to side-step any libc interception.
114 void SafeWriteToStderr(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 // 'buffer' and 'buf_size' are pointers to the buffer and buffer size. If the
149 // hook writes a prefix, it must increment *buffer and decrement *buf_size
150 // accordingly.
151 using LogPrefixHook = bool (*)(absl::LogSeverity severity, const char* file,
152  int line, char** buffer, int* buf_size);
153 
154 // Function type for a raw_logging customization hook called to abort a process
155 // when a FATAL message is logged. If the provided AbortHook() returns, the
156 // logging system will call abort().
157 //
158 // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
159 // was located.
160 // The null-terminated logged message lives in the buffer between 'buf_start'
161 // and 'buf_end'. 'prefix_end' points to the first non-prefix character of the
162 // buffer (as written by the LogPrefixHook.)
163 using AbortHook = void (*)(const char* file, int line, const char* buf_start,
164  const char* prefix_end, const char* buf_end);
165 
166 // Internal logging function for ABSL_INTERNAL_LOG to dispatch to.
167 //
168 // TODO(gfalcon): When string_view no longer depends on base, change this
169 // interface to take its message as a string_view instead.
170 using InternalLogFunction = void (*)(absl::LogSeverity severity,
171  const char* file, int line,
172  const std::string& message);
173 
175 
177 
178 } // namespace raw_logging_internal
179 } // namespace absl
180 
181 #endif // ABSL_BASE_INTERNAL_RAW_LOGGING_H_
void SafeWriteToStderr(const char *s, size_t len)
Definition: raw_logging.cc:186
constexpr const char * Basename(const char *fname, int offset)
Definition: raw_logging.h:119
LogSeverity
Definition: log_severity.h:28
void RegisterInternalLogFunction(InternalLogFunction func)
Definition: raw_logging.cc:230
Definition: algorithm.h:29
bool(*)(absl::LogSeverity severity, const char *file, int line, char **buffer, int *buf_size) LogPrefixHook
Definition: raw_logging.h:152
base_internal::AtomicHook< InternalLogFunction > internal_log_function
std::string format(const std::string &, const time_point< seconds > &, const femtoseconds &, const time_zone &)
void RawLog(absl::LogSeverity severity, const char *file, int line, const char *format,...) ABSL_PRINTF_ATTRIBUTE(4
Definition: raw_logging.cc:202
void(*)(absl::LogSeverity severity, const char *file, int line, const std::string &message) InternalLogFunction
Definition: raw_logging.h:172
#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
Definition: attributes.h:116
void(*)(const char *file, int line, const char *buf_start, const char *prefix_end, const char *buf_end) AbortHook
Definition: raw_logging.h:164


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:20