raw_logging.h
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 // Thread-safe logging routines that do not allocate any memory or
00016 // acquire any locks, and can therefore be used by low-level memory
00017 // allocation, synchronization, and signal-handling code.
00018 
00019 #ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_
00020 #define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
00021 
00022 #include <string>
00023 
00024 #include "absl/base/attributes.h"
00025 #include "absl/base/internal/atomic_hook.h"
00026 #include "absl/base/log_severity.h"
00027 #include "absl/base/macros.h"
00028 #include "absl/base/port.h"
00029 
00030 // This is similar to LOG(severity) << format..., but
00031 // * it is to be used ONLY by low-level modules that can't use normal LOG()
00032 // * it is designed to be a low-level logger that does not allocate any
00033 //   memory and does not need any locks, hence:
00034 // * it logs straight and ONLY to STDERR w/o buffering
00035 // * it uses an explicit printf-format and arguments list
00036 // * it will silently chop off really long message strings
00037 // Usage example:
00038 //   ABSL_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
00039 // This will print an almost standard log line like this to stderr only:
00040 //   E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
00041 
00042 #define ABSL_RAW_LOG(severity, ...)                                            \
00043   do {                                                                         \
00044     constexpr const char* absl_raw_logging_internal_basename =                 \
00045         ::absl::raw_logging_internal::Basename(__FILE__,                       \
00046                                                sizeof(__FILE__) - 1);          \
00047     ::absl::raw_logging_internal::RawLog(ABSL_RAW_LOGGING_INTERNAL_##severity, \
00048                                          absl_raw_logging_internal_basename,   \
00049                                          __LINE__, __VA_ARGS__);               \
00050   } while (0)
00051 
00052 // Similar to CHECK(condition) << message, but for low-level modules:
00053 // we use only ABSL_RAW_LOG that does not allocate memory.
00054 // We do not want to provide args list here to encourage this usage:
00055 //   if (!cond)  ABSL_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
00056 // so that the args are not computed when not needed.
00057 #define ABSL_RAW_CHECK(condition, message)                             \
00058   do {                                                                 \
00059     if (ABSL_PREDICT_FALSE(!(condition))) {                            \
00060       ABSL_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
00061     }                                                                  \
00062   } while (0)
00063 
00064 // ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above,
00065 // except that if the richer log library is linked into the binary, we dispatch
00066 // to that instead.  This is potentially useful for internal logging and
00067 // assertions, where we are using RAW_LOG neither for its async-signal-safety
00068 // nor for its non-allocating nature, but rather because raw logging has very
00069 // few other dependencies.
00070 //
00071 // The API is a subset of the above: each macro only takes two arguments.  Use
00072 // StrCat if you need to build a richer message.
00073 #define ABSL_INTERNAL_LOG(severity, message)                          \
00074   do {                                                                \
00075     constexpr const char* absl_raw_logging_internal_basename =        \
00076         ::absl::raw_logging_internal::Basename(__FILE__,              \
00077                                                sizeof(__FILE__) - 1); \
00078     ::absl::raw_logging_internal::internal_log_function(              \
00079         ABSL_RAW_LOGGING_INTERNAL_##severity,                         \
00080         absl_raw_logging_internal_basename, __LINE__, message);       \
00081   } while (0)
00082 
00083 #define ABSL_INTERNAL_CHECK(condition, message)                    \
00084   do {                                                             \
00085     if (ABSL_PREDICT_FALSE(!(condition))) {                        \
00086       std::string death_message = "Check " #condition " failed: "; \
00087       death_message += std::string(message);                       \
00088       ABSL_INTERNAL_LOG(FATAL, death_message);                     \
00089     }                                                              \
00090   } while (0)
00091 
00092 #define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
00093 #define ABSL_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
00094 #define ABSL_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
00095 #define ABSL_RAW_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
00096 #define ABSL_RAW_LOGGING_INTERNAL_LEVEL(severity) \
00097   ::absl::NormalizeLogSeverity(severity)
00098 
00099 namespace absl {
00100 namespace raw_logging_internal {
00101 
00102 // Helper function to implement ABSL_RAW_LOG
00103 // Logs format... at "severity" level, reporting it
00104 // as called from file:line.
00105 // This does not allocate memory or acquire locks.
00106 void RawLog(absl::LogSeverity severity, const char* file, int line,
00107             const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
00108 
00109 // Writes the provided buffer directly to stderr, in a safe, low-level manner.
00110 //
00111 // In POSIX this means calling write(), which is async-signal safe and does
00112 // not malloc.  If the platform supports the SYS_write syscall, we invoke that
00113 // directly to side-step any libc interception.
00114 void SafeWriteToStderr(const char *s, size_t len);
00115 
00116 // compile-time function to get the "base" filename, that is, the part of
00117 // a filename after the last "/" or "\" path separator.  The search starts at
00118 // the end of the string; the second parameter is the length of the string.
00119 constexpr const char* Basename(const char* fname, int offset) {
00120   return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
00121              ? fname + offset
00122              : Basename(fname, offset - 1);
00123 }
00124 
00125 // For testing only.
00126 // Returns true if raw logging is fully supported. When it is not
00127 // fully supported, no messages will be emitted, but a log at FATAL
00128 // severity will cause an abort.
00129 //
00130 // TODO(gfalcon): Come up with a better name for this method.
00131 bool RawLoggingFullySupported();
00132 
00133 // Function type for a raw_logging customization hook for suppressing messages
00134 // by severity, and for writing custom prefixes on non-suppressed messages.
00135 //
00136 // The installed hook is called for every raw log invocation.  The message will
00137 // be logged to stderr only if the hook returns true.  FATAL errors will cause
00138 // the process to abort, even if writing to stderr is suppressed.  The hook is
00139 // also provided with an output buffer, where it can write a custom log message
00140 // prefix.
00141 //
00142 // The raw_logging system does not allocate memory or grab locks.  User-provided
00143 // hooks must avoid these operations, and must not throw exceptions.
00144 //
00145 // 'severity' is the severity level of the message being written.
00146 // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
00147 // was located.
00148 // 'buffer' and 'buf_size' are pointers to the buffer and buffer size.  If the
00149 // hook writes a prefix, it must increment *buffer and decrement *buf_size
00150 // accordingly.
00151 using LogPrefixHook = bool (*)(absl::LogSeverity severity, const char* file,
00152                                int line, char** buffer, int* buf_size);
00153 
00154 // Function type for a raw_logging customization hook called to abort a process
00155 // when a FATAL message is logged.  If the provided AbortHook() returns, the
00156 // logging system will call abort().
00157 //
00158 // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
00159 // was located.
00160 // The null-terminated logged message lives in the buffer between 'buf_start'
00161 // and 'buf_end'.  'prefix_end' points to the first non-prefix character of the
00162 // buffer (as written by the LogPrefixHook.)
00163 using AbortHook = void (*)(const char* file, int line, const char* buf_start,
00164                            const char* prefix_end, const char* buf_end);
00165 
00166 // Internal logging function for ABSL_INTERNAL_LOG to dispatch to.
00167 //
00168 // TODO(gfalcon): When string_view no longer depends on base, change this
00169 // interface to take its message as a string_view instead.
00170 using InternalLogFunction = void (*)(absl::LogSeverity severity,
00171                                      const char* file, int line,
00172                                      const std::string& message);
00173 
00174 extern base_internal::AtomicHook<InternalLogFunction> internal_log_function;
00175 
00176 void RegisterInternalLogFunction(InternalLogFunction func);
00177 
00178 }  // namespace raw_logging_internal
00179 }  // namespace absl
00180 
00181 #endif  // ABSL_BASE_INTERNAL_RAW_LOGGING_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15