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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <syscall.h>
|
2020-12-14 19:33:29 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <cstdlib>
|
2020-12-14 19:33:29 +08:00
|
|
|
#include <cstring>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <limits>
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
#include "absl/base/attributes.h"
|
|
|
|
#include "absl/base/log_severity.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/numbers.h"
|
2020-12-14 19:33:29 +08:00
|
|
|
|
2022-03-25 00:04:06 +08:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include "android/log.h"
|
|
|
|
#endif
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
static const char kTruncated[] = " ... (message truncated)\n";
|
|
|
|
|
|
|
|
// sprintf the format to the buffer, adjusting *buf and *size to reflect the
|
|
|
|
// consumed bytes, and return whether the message fit without truncation. If
|
|
|
|
// truncation occurred, if possible leave room in the buffer for the message
|
|
|
|
// kTruncated[].
|
|
|
|
inline static bool VADoRawLog(char** buf, int* size, const char* format,
|
|
|
|
va_list ap) ABSL_PRINTF_ATTRIBUTE(3, 0);
|
|
|
|
inline static bool VADoRawLog(char** buf, int* size, const char* format,
|
|
|
|
va_list ap) {
|
|
|
|
int n = vsnprintf(*buf, *size, format, ap);
|
|
|
|
bool result = true;
|
|
|
|
if (n < 0 || n > *size) {
|
|
|
|
result = false;
|
|
|
|
if (static_cast<size_t>(*size) > sizeof(kTruncated)) {
|
|
|
|
n = *size - sizeof(kTruncated); // room for truncation message
|
|
|
|
} else {
|
|
|
|
n = 0; // no room for truncation message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*size -= n;
|
|
|
|
*buf += n;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
|
|
|
|
// that invoke malloc() and getenv() that might acquire some locks.
|
|
|
|
|
|
|
|
// Helper for RawLog below.
|
|
|
|
// *DoRawLog writes to *buf of *size and move them past the written portion.
|
|
|
|
// It returns true iff there was no overflow or error.
|
|
|
|
bool DoRawLog(char** buf, int* size, const char* format, ...)
|
|
|
|
ABSL_PRINTF_ATTRIBUTE(3, 4);
|
|
|
|
bool DoRawLog(char** buf, int* size, const char* format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
int n = vsnprintf(*buf, *size, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (n < 0 || n > *size) return false;
|
|
|
|
*size -= n;
|
|
|
|
*buf += n;
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-03-25 00:04:06 +08:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
android_LogPriority ConvertSeverity(absl::LogSeverity severity) {
|
|
|
|
switch (severity) {
|
|
|
|
case absl::LogSeverity::kInfo:
|
|
|
|
return ANDROID_LOG_INFO;
|
|
|
|
case absl::LogSeverity::kWarning:
|
|
|
|
return ANDROID_LOG_WARN;
|
|
|
|
case absl::LogSeverity::kError:
|
|
|
|
return ANDROID_LOG_ERROR;
|
|
|
|
case absl::LogSeverity::kFatal:
|
|
|
|
return ANDROID_LOG_FATAL;
|
|
|
|
default:
|
|
|
|
return ANDROID_LOG_INFO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
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,
|
|
|
|
const char* format, va_list ap) {
|
2021-05-18 20:37:10 +08:00
|
|
|
char buffer[sapi::raw_logging_internal::kLogBufSize];
|
2020-12-14 19:33:29 +08:00
|
|
|
char* buf = buffer;
|
|
|
|
int size = sizeof(buffer);
|
|
|
|
|
|
|
|
DoRawLog(&buf, &size, "[%s : %d] RAW: ", file, line);
|
|
|
|
|
|
|
|
bool no_chop = VADoRawLog(&buf, &size, format, ap);
|
|
|
|
if (no_chop) {
|
|
|
|
DoRawLog(&buf, &size, "\n");
|
|
|
|
} else {
|
|
|
|
DoRawLog(&buf, &size, "%s", kTruncated);
|
|
|
|
}
|
2022-03-25 00:04:06 +08:00
|
|
|
#ifndef __ANDROID__
|
2020-12-14 19:33:29 +08:00
|
|
|
sapi::raw_logging_internal::SafeWriteToStderr(buffer, strlen(buffer));
|
2022-03-25 00:04:06 +08:00
|
|
|
#else
|
|
|
|
// Logs to Android's logcat with the TAG SAPI and the log line containing
|
|
|
|
// the code location and the log output.
|
|
|
|
__android_log_print(ConvertSeverity(severity), "SAPI", "%s", buffer);
|
|
|
|
#endif
|
2020-12-14 19:33:29 +08:00
|
|
|
|
|
|
|
// Abort the process after logging a FATAL message, even if the output itself
|
|
|
|
// was suppressed.
|
|
|
|
if (severity == absl::LogSeverity::kFatal) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace sapi::raw_logging_internal {
|
|
|
|
|
|
|
|
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,
|
|
|
|
const char* format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
RawLogVA(severity, file, line, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SafeWriteToStderr(const char* s, size_t len) {
|
|
|
|
syscall(SYS_write, STDERR_FILENO, s, len);
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
bool VLogIsOn(int verbose_level) {
|
|
|
|
static int external_verbose_level = [] {
|
2022-06-08 19:52:24 +08:00
|
|
|
int external_verbose_level = std::numeric_limits<int>::min();
|
2019-03-19 00:21:48 +08:00
|
|
|
char* env_var = getenv("SAPI_VLOG_LEVEL");
|
|
|
|
if (!env_var) {
|
|
|
|
return external_verbose_level;
|
|
|
|
}
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_CHECK(absl::SimpleAtoi(env_var, &external_verbose_level) &&
|
2019-03-19 00:21:48 +08:00
|
|
|
external_verbose_level >= 0,
|
|
|
|
"SAPI_VLOG_LEVEL needs to be an integer >= 0");
|
|
|
|
return external_verbose_level;
|
|
|
|
}();
|
2022-06-08 19:52:24 +08:00
|
|
|
return verbose_level <= external_verbose_level;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
} // namespace sapi::raw_logging_internal
|