bf29470384
-- bdce7e57e9e886eff1114d0266781b443f7ec639 by Derek Mauro <dmauro@google.com>: Change {Get|Set}EnvironmentVariable to {Get|Set}EnvironmentVariableA for compatibility with /DUNICODE. PiperOrigin-RevId: 239229514 -- 2276ed502326a044a84060d34eb19d499e3a3be2 by Derek Mauro <dmauro@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 239228622 -- a462efb970ff43b08a362ef2343fb75ac1295a50 by Derek Mauro <dmauro@google.com>: Adding linking of CoreFoundation to CMakeLists in absl/time. Import https://github.com/abseil/abseil-cpp/pull/280. Fix #283 PiperOrigin-RevId: 239220785 -- fc23327b97f940c682aae1956cf7a1bf87f88c06 by Derek Mauro <dmauro@google.com>: Add hermetic test script that uses Docker to build with a very recent version of gcc (8.3.0 today) with libstdc++ and bazel. PiperOrigin-RevId: 239220448 -- 418c08a8f6a53e63b84e39473035774417ca3aa7 by Derek Mauro <dmauro@google.com>: Disable part of the variant exeception safety test on move assignment when using versions of libstd++ that contain a bug. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87431#c7 PiperOrigin-RevId: 239062455 -- 799722217aeda79679577843c91d5be62cbcbb42 by Matt Calabrese <calabrese@google.com>: Add internal-only IsSwappable traits corresponding to std::is_swappable and std::is_nothrow_swappable, which are used with the swap implementations of optional and variant. PiperOrigin-RevId: 239049448 -- aa46a036038a3de5c68ac5e5d3b4bf76f818d2ea by CJ Johnson <johnsoncj@google.com>: Make InlinedVectorStorage constructor explicit PiperOrigin-RevId: 239044361 -- 17949715b3aa21c794701f69f2154e91b6acabc3 by CJ Johnson <johnsoncj@google.com>: Add absl namesapce to internal/inlined_vector.h PiperOrigin-RevId: 239030789 -- 834628325953078cc08ed10d23bb8890e5bec897 by Derek Mauro <dmauro@google.com>: Add test script that uses Docker to build Abseil with gcc-4.8, libstdc++, and cmake. PiperOrigin-RevId: 239028433 -- 80fe24149ed73ed2ced995ad1e372fb060c60427 by CJ Johnson <johnsoncj@google.com>: Factors data members of InlinedVector into an impl type called InlinedVectorStorage so that (in future changes) the contents of a vector can be grouped together with a single pointer. PiperOrigin-RevId: 239021086 -- 585331436d5d4d79f845e45dcf79d918a0dc6169 by Derek Mauro <dmauro@google.com>: Add -Wno-missing-field-initializers to gcc compiler flags. gcc-4.x has spurious missing field initializer warnings. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36750 PiperOrigin-RevId: 239017217 -- 94602fe4e33ee3a552a7f2939c0f57a992f55075 by Abseil Team <absl-team@google.com>: Formatting fixes. PiperOrigin-RevId: 238983038 -- a1c1b63c08505574e0a8c491561840cecb2bb93e by Derek Mauro <dmauro@google.com>: Add hermetic test script that uses Docker to build with a very recent version of clang with libc++ and bazel. PiperOrigin-RevId: 238669118 -- e525f8d20bc2f79a0d69336b902f63858f3bff9d by Derek Mauro <dmauro@google.com>: Disable the test optionalTest.InPlaceTSFINAEBug until libc++ is updated. PiperOrigin-RevId: 238661703 -- f99a2a0b5ec424a059678f7f226600f137b4c74e by Derek Mauro <dmauro@google.com>: Correct the check for the FlatHashMap-Any test bug (list conditions instead of platforms when possible) PiperOrigin-RevId: 238653344 -- 777928035dbcbf39f361eb7d10dc3696822f692f by Jon Cohen <cohenjon@google.com>: Add install rules for Abseil CMake. These are attempted to be limited to in-project installation. This serves two purposes -- first it's morally the same as using Abseil in-source, except you don't have to rebuild us every time. Second, the presence of an install rule makes life massively simpler for package manager maintainers. Currently this doesn't install absl tests or testonly libraries. This can be added in a follow-up patch. Fixes #38, Fixes #80, Closes #182 PiperOrigin-RevId: 238645836 -- ded1c6ce697c191b7a6ff14572b3e6d183117b2c by Derek Mauro <dmauro@google.com>: Add hermetic test script that uses Docker to build with a very recent version of clang with libstdc++ and bazel. PiperOrigin-RevId: 238517815 GitOrigin-RevId: bdce7e57e9e886eff1114d0266781b443f7ec639 Change-Id: I6f745869cb8ef63851891ccac05ae9a7dd241c4f
181 lines
8.7 KiB
C++
181 lines
8.7 KiB
C++
// Copyright 2017 The Abseil Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
// Thread-safe logging routines that do not allocate any memory or
|
|
// acquire any locks, and can therefore be used by low-level memory
|
|
// allocation, synchronization, and signal-handling code.
|
|
|
|
#ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
|
#define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
|
|
|
#include <string>
|
|
|
|
#include "absl/base/attributes.h"
|
|
#include "absl/base/internal/atomic_hook.h"
|
|
#include "absl/base/log_severity.h"
|
|
#include "absl/base/macros.h"
|
|
#include "absl/base/port.h"
|
|
|
|
// This is similar to LOG(severity) << format..., but
|
|
// * it is to be used ONLY by low-level modules that can't use normal LOG()
|
|
// * it is designed to be a low-level logger that does not allocate any
|
|
// memory and does not need any locks, hence:
|
|
// * it logs straight and ONLY to STDERR w/o buffering
|
|
// * it uses an explicit printf-format and arguments list
|
|
// * it will silently chop off really long message strings
|
|
// Usage example:
|
|
// ABSL_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
|
|
// This will print an almost standard log line like this to stderr only:
|
|
// E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
|
|
|
|
#define ABSL_RAW_LOG(severity, ...) \
|
|
do { \
|
|
constexpr const char* absl_raw_logging_internal_basename = \
|
|
::absl::raw_logging_internal::Basename(__FILE__, \
|
|
sizeof(__FILE__) - 1); \
|
|
::absl::raw_logging_internal::RawLog(ABSL_RAW_LOGGING_INTERNAL_##severity, \
|
|
absl_raw_logging_internal_basename, \
|
|
__LINE__, __VA_ARGS__); \
|
|
} while (0)
|
|
|
|
// Similar to CHECK(condition) << message, but for low-level modules:
|
|
// we use only ABSL_RAW_LOG that does not allocate memory.
|
|
// We do not want to provide args list here to encourage this usage:
|
|
// if (!cond) ABSL_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
|
|
// so that the args are not computed when not needed.
|
|
#define ABSL_RAW_CHECK(condition, message) \
|
|
do { \
|
|
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
|
ABSL_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
|
|
} \
|
|
} while (0)
|
|
|
|
// ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above,
|
|
// except that if the richer log library is linked into the binary, we dispatch
|
|
// to that instead. This is potentially useful for internal logging and
|
|
// assertions, where we are using RAW_LOG neither for its async-signal-safety
|
|
// nor for its non-allocating nature, but rather because raw logging has very
|
|
// few other dependencies.
|
|
//
|
|
// The API is a subset of the above: each macro only takes two arguments. Use
|
|
// StrCat if you need to build a richer message.
|
|
#define ABSL_INTERNAL_LOG(severity, message) \
|
|
do { \
|
|
constexpr const char* absl_raw_logging_internal_basename = \
|
|
::absl::raw_logging_internal::Basename(__FILE__, \
|
|
sizeof(__FILE__) - 1); \
|
|
::absl::raw_logging_internal::internal_log_function( \
|
|
ABSL_RAW_LOGGING_INTERNAL_##severity, \
|
|
absl_raw_logging_internal_basename, __LINE__, message); \
|
|
} while (0)
|
|
|
|
#define ABSL_INTERNAL_CHECK(condition, message) \
|
|
do { \
|
|
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
|
std::string death_message = "Check " #condition " failed: "; \
|
|
death_message += std::string(message); \
|
|
ABSL_INTERNAL_LOG(FATAL, death_message); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
|
|
#define ABSL_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
|
|
#define ABSL_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
|
|
#define ABSL_RAW_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
|
|
#define ABSL_RAW_LOGGING_INTERNAL_LEVEL(severity) \
|
|
::absl::NormalizeLogSeverity(severity)
|
|
|
|
namespace absl {
|
|
namespace raw_logging_internal {
|
|
|
|
// Helper function to implement ABSL_RAW_LOG
|
|
// Logs format... at "severity" level, reporting it
|
|
// as called from file:line.
|
|
// This does not allocate memory or acquire locks.
|
|
void RawLog(absl::LogSeverity severity, const char* file, int line,
|
|
const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
|
|
|
|
// Writes the provided buffer directly to stderr, in a safe, low-level manner.
|
|
//
|
|
// In POSIX this means calling write(), which is async-signal safe and does
|
|
// not malloc. If the platform supports the SYS_write syscall, we invoke that
|
|
// directly to side-step any libc interception.
|
|
void SafeWriteToStderr(const char *s, size_t len);
|
|
|
|
// compile-time function to get the "base" filename, that is, the part of
|
|
// a filename after the last "/" or "\" path separator. The search starts at
|
|
// the end of the string; the second parameter is the length of the string.
|
|
constexpr const char* Basename(const char* fname, int offset) {
|
|
return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
|
|
? fname + offset
|
|
: Basename(fname, offset - 1);
|
|
}
|
|
|
|
// For testing only.
|
|
// Returns true if raw logging is fully supported. When it is not
|
|
// fully supported, no messages will be emitted, but a log at FATAL
|
|
// severity will cause an abort.
|
|
//
|
|
// TODO(gfalcon): Come up with a better name for this method.
|
|
bool RawLoggingFullySupported();
|
|
|
|
// Function type for a raw_logging customization hook for suppressing messages
|
|
// by severity, and for writing custom prefixes on non-suppressed messages.
|
|
//
|
|
// The installed hook is called for every raw log invocation. The message will
|
|
// be logged to stderr only if the hook returns true. FATAL errors will cause
|
|
// the process to abort, even if writing to stderr is suppressed. The hook is
|
|
// also provided with an output buffer, where it can write a custom log message
|
|
// prefix.
|
|
//
|
|
// The raw_logging system does not allocate memory or grab locks. User-provided
|
|
// hooks must avoid these operations, and must not throw exceptions.
|
|
//
|
|
// 'severity' is the severity level of the message being written.
|
|
// 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
|
|
// was located.
|
|
// 'buffer' and 'buf_size' are pointers to the buffer and buffer size. If the
|
|
// hook writes a prefix, it must increment *buffer and decrement *buf_size
|
|
// accordingly.
|
|
using LogPrefixHook = bool (*)(absl::LogSeverity severity, const char* file,
|
|
int line, char** buffer, int* buf_size);
|
|
|
|
// Function type for a raw_logging customization hook called to abort a process
|
|
// when a FATAL message is logged. If the provided AbortHook() returns, the
|
|
// logging system will call abort().
|
|
//
|
|
// 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
|
|
// was located.
|
|
// The null-terminated logged message lives in the buffer between 'buf_start'
|
|
// and 'buf_end'. 'prefix_end' points to the first non-prefix character of the
|
|
// buffer (as written by the LogPrefixHook.)
|
|
using AbortHook = void (*)(const char* file, int line, const char* buf_start,
|
|
const char* prefix_end, const char* buf_end);
|
|
|
|
// Internal logging function for ABSL_INTERNAL_LOG to dispatch to.
|
|
//
|
|
// TODO(gfalcon): When string_view no longer depends on base, change this
|
|
// interface to take its message as a string_view instead.
|
|
using InternalLogFunction = void (*)(absl::LogSeverity severity,
|
|
const char* file, int line,
|
|
const std::string& message);
|
|
|
|
extern base_internal::AtomicHook<InternalLogFunction> internal_log_function;
|
|
|
|
void RegisterInternalLogFunction(InternalLogFunction func);
|
|
|
|
} // namespace raw_logging_internal
|
|
} // namespace absl
|
|
|
|
#endif // ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|