2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
// SAPI raw logging. Forked from Abseil's version.
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#ifndef SANDBOXED_API_UTIL_RAW_LOGGING_H_
|
|
|
|
#define SANDBOXED_API_UTIL_RAW_LOGGING_H_
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "absl/base/attributes.h"
|
|
|
|
#include "absl/base/config.h"
|
|
|
|
#include "absl/base/log_severity.h"
|
|
|
|
#include "absl/base/macros.h"
|
|
|
|
#include "absl/base/optimization.h"
|
|
|
|
#include "absl/base/port.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "absl/strings/str_format.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/strerror.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
// `SAPI_INTERNAL_UNREACHABLE` is an unreachable statement. A program which
|
|
|
|
// reaches one has undefined behavior, and the compiler may optimize
|
|
|
|
// accordingly.
|
|
|
|
#if defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
|
|
|
|
#define SAPI_INTERNAL_UNREACHABLE __builtin_unreachable()
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#define SAPI_INTERNAL_UNREACHABLE __assume(0)
|
|
|
|
#else
|
|
|
|
#define SAPI_INTERNAL_UNREACHABLE
|
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
#ifdef ABSL_RAW_LOG
|
|
|
|
#define SAPI_RAW_LOG ABSL_RAW_LOG
|
|
|
|
#else
|
|
|
|
// 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:
|
|
|
|
// SAPI_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 SAPI_RAW_LOG(severity, ...) \
|
|
|
|
do { \
|
|
|
|
constexpr const char* absl_raw_logging_internal_basename = \
|
|
|
|
::sapi::raw_logging_internal::Basename(__FILE__, \
|
|
|
|
sizeof(__FILE__) - 1); \
|
|
|
|
::sapi::raw_logging_internal::RawLog(SAPI_RAW_LOGGING_INTERNAL_##severity, \
|
|
|
|
absl_raw_logging_internal_basename, \
|
|
|
|
__LINE__, __VA_ARGS__); \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
#ifdef ABSL_RAW_CHECK
|
|
|
|
#define SAPI_RAW_CHECK ABSL_RAW_CHECK
|
|
|
|
#else
|
|
|
|
// Similar to CHECK(condition) << message, but for low-level modules:
|
|
|
|
// we use only SAPI_RAW_LOG that does not allocate memory.
|
|
|
|
// We do not want to provide args list here to encourage this usage:
|
|
|
|
// if (!cond) SAPI_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
|
|
|
|
// so that the args are not computed when not needed.
|
|
|
|
#define SAPI_RAW_CHECK(condition, message) \
|
|
|
|
do { \
|
|
|
|
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
|
|
|
SAPI_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SAPI_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
|
|
|
|
#define SAPI_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
|
|
|
|
#define SAPI_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
|
|
|
|
#define SAPI_RAW_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Returns whether SAPI verbose logging is enabled, as determined by the
|
|
|
|
// SAPI_VLOG_LEVEL environment variable.
|
2020-12-14 19:33:29 +08:00
|
|
|
#define SAPI_VLOG_IS_ON(verbose_level) \
|
|
|
|
::sapi::raw_logging_internal::VLogIsOn(verbose_level)
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Like SAPI_RAW_LOG(), but also logs the current value of errno and its
|
|
|
|
// corresponding error message.
|
2021-05-18 20:37:10 +08:00
|
|
|
#define SAPI_RAW_PLOG(severity, format, ...) \
|
|
|
|
do { \
|
|
|
|
char sapi_raw_plog_errno_buffer[100]; \
|
|
|
|
const char* sapi_raw_plog_errno_str = \
|
|
|
|
::sapi::RawStrError(errno, sapi_raw_plog_errno_buffer, \
|
|
|
|
sizeof(sapi_raw_plog_errno_buffer)); \
|
|
|
|
char sapi_raw_plog_buffer[::sapi::raw_logging_internal::kLogBufSize]; \
|
|
|
|
absl::SNPrintF(sapi_raw_plog_buffer, sizeof(sapi_raw_plog_buffer), \
|
|
|
|
(format), ##__VA_ARGS__); \
|
|
|
|
SAPI_RAW_LOG(severity, "%s: %s [%d]", sapi_raw_plog_buffer, \
|
|
|
|
sapi_raw_plog_errno_str, errno); \
|
2020-12-14 19:33:29 +08:00
|
|
|
} while (0)
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// If verbose logging is enabled, uses SAPI_RAW_LOG() to log.
|
2020-12-14 19:33:29 +08:00
|
|
|
#define SAPI_RAW_VLOG(verbose_level, format, ...) \
|
|
|
|
if (sapi::raw_logging_internal::VLogIsOn(verbose_level)) { \
|
|
|
|
SAPI_RAW_LOG(INFO, (format), ##__VA_ARGS__); \
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Like SAPI_RAW_CHECK(), but also logs errno and a message (similar to
|
|
|
|
// SAPI_RAW_PLOG()).
|
2021-05-18 20:37:10 +08:00
|
|
|
#define SAPI_RAW_PCHECK(condition, format, ...) \
|
|
|
|
do { \
|
|
|
|
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
|
|
|
char sapi_raw_plog_errno_buffer[100]; \
|
|
|
|
const char* sapi_raw_plog_errno_str = \
|
|
|
|
::sapi::RawStrError(errno, sapi_raw_plog_errno_buffer, \
|
|
|
|
sizeof(sapi_raw_plog_errno_buffer)); \
|
|
|
|
char sapi_raw_plog_buffer[::sapi::raw_logging_internal::kLogBufSize]; \
|
|
|
|
absl::SNPrintF(sapi_raw_plog_buffer, sizeof(sapi_raw_plog_buffer), \
|
|
|
|
(format), ##__VA_ARGS__); \
|
|
|
|
SAPI_RAW_LOG(FATAL, "Check %s failed: %s: %s [%d]", #condition, \
|
|
|
|
sapi_raw_plog_buffer, sapi_raw_plog_errno_str, errno); \
|
|
|
|
} \
|
2020-12-14 19:33:29 +08:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
namespace sapi::raw_logging_internal {
|
|
|
|
|
2021-05-18 20:37:10 +08:00
|
|
|
constexpr int kLogBufSize = 3000;
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VLogIsOn(int verbose_level);
|
|
|
|
|
|
|
|
} // namespace sapi::raw_logging_internal
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_UTIL_RAW_LOGGING_H_
|