Skip to content

Commit

Permalink
Do not compile SAPI's raw_logging if using ABSL_RAW_LOG
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 622115689
Change-Id: I3c6ddb5551d57f8d11f6e929634c7692890e81a2
  • Loading branch information
happyCoder92 authored and Copybara-Service committed Apr 5, 2024
1 parent ef70284 commit 602b80d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
33 changes: 22 additions & 11 deletions sandboxed_api/util/raw_logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,28 @@

#include "sandboxed_api/util/raw_logging.h"

#include <syscall.h>
#include <unistd.h>

#include <cstdlib>
#include <limits>

#include "absl/strings/numbers.h"

#ifndef SAPI_USE_ABSL_RAW_LOG
#include <syscall.h>

#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>

#include "absl/base/attributes.h"
#include "absl/base/log_severity.h"
#include "absl/strings/numbers.h"

#ifdef __ANDROID__
#include "android/log.h"
#endif

namespace {
static const char kTruncated[] = " ... (message truncated)\n";

// sprintf the format to the buffer, adjusting *buf and *size to reflect the
Expand All @@ -56,8 +61,6 @@ inline static bool VADoRawLog(char** buf, int* size, const char* format,
return result;
}

namespace {

// CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
// that invoke malloc() and getenv() that might acquire some locks.

Expand Down Expand Up @@ -94,6 +97,15 @@ android_LogPriority ConvertSeverity(absl::LogSeverity severity) {
}
#endif

// 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) {
syscall(SYS_write, STDERR_FILENO, s, len);
}

void RawLogVA(absl::LogSeverity severity, const char* file, int line,
const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0);
void RawLogVA(absl::LogSeverity severity, const char* file, int line,
Expand All @@ -111,7 +123,7 @@ void RawLogVA(absl::LogSeverity severity, const char* file, int line,
DoRawLog(&buf, &size, "%s", kTruncated);
}
#ifndef __ANDROID__
sapi::raw_logging_internal::SafeWriteToStderr(buffer, strlen(buffer));
SafeWriteToStderr(buffer, strlen(buffer));
#else
// Logs to Android's logcat with the TAG SAPI and the log line containing
// the code location and the log output.
Expand All @@ -126,9 +138,11 @@ void RawLogVA(absl::LogSeverity severity, const char* file, int line,
}

} // namespace
#endif

namespace sapi::raw_logging_internal {

#ifndef SAPI_USE_ABSL_RAW_LOG
void RawLog(absl::LogSeverity severity, const char* file, int line,
const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
void RawLog(absl::LogSeverity severity, const char* file, int line,
Expand All @@ -138,10 +152,7 @@ void RawLog(absl::LogSeverity severity, const char* file, int line,
RawLogVA(severity, file, line, format, ap);
va_end(ap);
}

void SafeWriteToStderr(const char* s, size_t len) {
syscall(SYS_write, STDERR_FILENO, s, len);
}
#endif

bool VLogIsOn(int verbose_level) {
static int external_verbose_level = [] {
Expand Down
25 changes: 10 additions & 15 deletions sandboxed_api/util/raw_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@
// /dev/null.
#if defined(ABSL_RAW_LOG) && !(__ANDROID__)
#define SAPI_RAW_LOG ABSL_RAW_LOG
#define SAPI_USE_ABSL_RAW_LOG 1
#else
#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
// 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
Expand Down Expand Up @@ -80,17 +85,12 @@
} 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

// Returns whether SAPI verbose logging is enabled, as determined by the
// SAPI_VLOG_LEVEL environment variable.
#define SAPI_VLOG_IS_ON(verbose_level) \
::sapi::raw_logging_internal::VLogIsOn(verbose_level)
#define SAPI_VLOG_IS_ON(verbose_level) SAPI_RAW_VLOG_IS_ON(verbose_level)

#define SAPI_RAW_VLOG_IS_ON(verbose_level) SAPI_VLOG_IS_ON(verbose_level)
#define SAPI_RAW_VLOG_IS_ON(verbose_level) \
::sapi::raw_logging_internal::VLogIsOn(verbose_level)

#ifndef VLOG
// `VLOG` uses numeric levels to provide verbose logging that can configured at
Expand Down Expand Up @@ -148,20 +148,14 @@ namespace sapi::raw_logging_internal {

constexpr int kLogBufSize = 3000;

#ifndef SAPI_USE_ABSL_RAW_LOG
// 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.
Expand All @@ -170,6 +164,7 @@ constexpr const char* Basename(const char* fname, int offset) {
? fname + offset
: Basename(fname, offset - 1);
}
#endif

bool VLogIsOn(int verbose_level);

Expand Down

0 comments on commit 602b80d

Please sign in to comment.