xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/util/raw_logging.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #include "sandboxed_api/util/raw_logging.h"
16*ec63e07aSXin Li 
17*ec63e07aSXin Li #include <syscall.h>
18*ec63e07aSXin Li #include <unistd.h>
19*ec63e07aSXin Li 
20*ec63e07aSXin Li #include <cstdarg>
21*ec63e07aSXin Li #include <cstdio>
22*ec63e07aSXin Li #include <cstdlib>
23*ec63e07aSXin Li #include <cstring>
24*ec63e07aSXin Li #include <limits>
25*ec63e07aSXin Li 
26*ec63e07aSXin Li #include "absl/base/attributes.h"
27*ec63e07aSXin Li #include "absl/base/log_severity.h"
28*ec63e07aSXin Li #include "absl/strings/numbers.h"
29*ec63e07aSXin Li 
30*ec63e07aSXin Li #ifdef __ANDROID__
31*ec63e07aSXin Li #include "android/log.h"
32*ec63e07aSXin Li #endif
33*ec63e07aSXin Li 
34*ec63e07aSXin Li static const char kTruncated[] = " ... (message truncated)\n";
35*ec63e07aSXin Li 
36*ec63e07aSXin Li // sprintf the format to the buffer, adjusting *buf and *size to reflect the
37*ec63e07aSXin Li // consumed bytes, and return whether the message fit without truncation.  If
38*ec63e07aSXin Li // truncation occurred, if possible leave room in the buffer for the message
39*ec63e07aSXin Li // kTruncated[].
40*ec63e07aSXin Li inline static bool VADoRawLog(char** buf, int* size, const char* format,
41*ec63e07aSXin Li                               va_list ap) ABSL_PRINTF_ATTRIBUTE(3, 0);
VADoRawLog(char ** buf,int * size,const char * format,va_list ap)42*ec63e07aSXin Li inline static bool VADoRawLog(char** buf, int* size, const char* format,
43*ec63e07aSXin Li                               va_list ap) {
44*ec63e07aSXin Li   int n = vsnprintf(*buf, *size, format, ap);
45*ec63e07aSXin Li   bool result = true;
46*ec63e07aSXin Li   if (n < 0 || n > *size) {
47*ec63e07aSXin Li     result = false;
48*ec63e07aSXin Li     if (static_cast<size_t>(*size) > sizeof(kTruncated)) {
49*ec63e07aSXin Li       n = *size - sizeof(kTruncated);  // room for truncation message
50*ec63e07aSXin Li     } else {
51*ec63e07aSXin Li       n = 0;  // no room for truncation message
52*ec63e07aSXin Li     }
53*ec63e07aSXin Li   }
54*ec63e07aSXin Li   *size -= n;
55*ec63e07aSXin Li   *buf += n;
56*ec63e07aSXin Li   return result;
57*ec63e07aSXin Li }
58*ec63e07aSXin Li 
59*ec63e07aSXin Li namespace {
60*ec63e07aSXin Li 
61*ec63e07aSXin Li // CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
62*ec63e07aSXin Li // that invoke malloc() and getenv() that might acquire some locks.
63*ec63e07aSXin Li 
64*ec63e07aSXin Li // Helper for RawLog below.
65*ec63e07aSXin Li // *DoRawLog writes to *buf of *size and move them past the written portion.
66*ec63e07aSXin Li // It returns true iff there was no overflow or error.
67*ec63e07aSXin Li bool DoRawLog(char** buf, int* size, const char* format, ...)
68*ec63e07aSXin Li     ABSL_PRINTF_ATTRIBUTE(3, 4);
DoRawLog(char ** buf,int * size,const char * format,...)69*ec63e07aSXin Li bool DoRawLog(char** buf, int* size, const char* format, ...) {
70*ec63e07aSXin Li   va_list ap;
71*ec63e07aSXin Li   va_start(ap, format);
72*ec63e07aSXin Li   int n = vsnprintf(*buf, *size, format, ap);
73*ec63e07aSXin Li   va_end(ap);
74*ec63e07aSXin Li   if (n < 0 || n > *size) return false;
75*ec63e07aSXin Li   *size -= n;
76*ec63e07aSXin Li   *buf += n;
77*ec63e07aSXin Li   return true;
78*ec63e07aSXin Li }
79*ec63e07aSXin Li 
80*ec63e07aSXin Li #ifdef __ANDROID__
ConvertSeverity(absl::LogSeverity severity)81*ec63e07aSXin Li android_LogPriority ConvertSeverity(absl::LogSeverity severity) {
82*ec63e07aSXin Li   switch (severity) {
83*ec63e07aSXin Li     case absl::LogSeverity::kInfo:
84*ec63e07aSXin Li       return ANDROID_LOG_INFO;
85*ec63e07aSXin Li     case absl::LogSeverity::kWarning:
86*ec63e07aSXin Li       return ANDROID_LOG_WARN;
87*ec63e07aSXin Li     case absl::LogSeverity::kError:
88*ec63e07aSXin Li       return ANDROID_LOG_ERROR;
89*ec63e07aSXin Li     case absl::LogSeverity::kFatal:
90*ec63e07aSXin Li       return ANDROID_LOG_FATAL;
91*ec63e07aSXin Li     default:
92*ec63e07aSXin Li       return ANDROID_LOG_INFO;
93*ec63e07aSXin Li   }
94*ec63e07aSXin Li }
95*ec63e07aSXin Li #endif
96*ec63e07aSXin Li 
97*ec63e07aSXin Li void RawLogVA(absl::LogSeverity severity, const char* file, int line,
98*ec63e07aSXin Li               const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0);
RawLogVA(absl::LogSeverity severity,const char * file,int line,const char * format,va_list ap)99*ec63e07aSXin Li void RawLogVA(absl::LogSeverity severity, const char* file, int line,
100*ec63e07aSXin Li               const char* format, va_list ap) {
101*ec63e07aSXin Li   char buffer[sapi::raw_logging_internal::kLogBufSize];
102*ec63e07aSXin Li   char* buf = buffer;
103*ec63e07aSXin Li   int size = sizeof(buffer);
104*ec63e07aSXin Li 
105*ec63e07aSXin Li   DoRawLog(&buf, &size, "[%s : %d] RAW: ", file, line);
106*ec63e07aSXin Li 
107*ec63e07aSXin Li   bool no_chop = VADoRawLog(&buf, &size, format, ap);
108*ec63e07aSXin Li   if (no_chop) {
109*ec63e07aSXin Li     DoRawLog(&buf, &size, "\n");
110*ec63e07aSXin Li   } else {
111*ec63e07aSXin Li     DoRawLog(&buf, &size, "%s", kTruncated);
112*ec63e07aSXin Li   }
113*ec63e07aSXin Li #ifndef __ANDROID__
114*ec63e07aSXin Li   sapi::raw_logging_internal::SafeWriteToStderr(buffer, strlen(buffer));
115*ec63e07aSXin Li #else
116*ec63e07aSXin Li   // Logs to Android's logcat with the TAG SAPI and the log line containing
117*ec63e07aSXin Li   // the code location and the log output.
118*ec63e07aSXin Li   __android_log_print(ConvertSeverity(severity), "SAPI", "%s", buffer);
119*ec63e07aSXin Li #endif
120*ec63e07aSXin Li 
121*ec63e07aSXin Li   // Abort the process after logging a FATAL message, even if the output itself
122*ec63e07aSXin Li   // was suppressed.
123*ec63e07aSXin Li   if (severity == absl::LogSeverity::kFatal) {
124*ec63e07aSXin Li     abort();
125*ec63e07aSXin Li   }
126*ec63e07aSXin Li }
127*ec63e07aSXin Li 
128*ec63e07aSXin Li }  // namespace
129*ec63e07aSXin Li 
130*ec63e07aSXin Li namespace sapi::raw_logging_internal {
131*ec63e07aSXin Li 
132*ec63e07aSXin Li void RawLog(absl::LogSeverity severity, const char* file, int line,
133*ec63e07aSXin Li             const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
RawLog(absl::LogSeverity severity,const char * file,int line,const char * format,...)134*ec63e07aSXin Li void RawLog(absl::LogSeverity severity, const char* file, int line,
135*ec63e07aSXin Li             const char* format, ...) {
136*ec63e07aSXin Li   va_list ap;
137*ec63e07aSXin Li   va_start(ap, format);
138*ec63e07aSXin Li   RawLogVA(severity, file, line, format, ap);
139*ec63e07aSXin Li   va_end(ap);
140*ec63e07aSXin Li }
141*ec63e07aSXin Li 
SafeWriteToStderr(const char * s,size_t len)142*ec63e07aSXin Li void SafeWriteToStderr(const char* s, size_t len) {
143*ec63e07aSXin Li   syscall(SYS_write, STDERR_FILENO, s, len);
144*ec63e07aSXin Li }
145*ec63e07aSXin Li 
VLogIsOn(int verbose_level)146*ec63e07aSXin Li bool VLogIsOn(int verbose_level) {
147*ec63e07aSXin Li   static int external_verbose_level = [] {
148*ec63e07aSXin Li     int external_verbose_level = std::numeric_limits<int>::min();
149*ec63e07aSXin Li     char* env_var = getenv("SAPI_VLOG_LEVEL");
150*ec63e07aSXin Li     if (!env_var) {
151*ec63e07aSXin Li       return external_verbose_level;
152*ec63e07aSXin Li     }
153*ec63e07aSXin Li     SAPI_RAW_CHECK(absl::SimpleAtoi(env_var, &external_verbose_level) &&
154*ec63e07aSXin Li                        external_verbose_level >= 0,
155*ec63e07aSXin Li                    "SAPI_VLOG_LEVEL needs to be an integer >= 0");
156*ec63e07aSXin Li     return external_verbose_level;
157*ec63e07aSXin Li   }();
158*ec63e07aSXin Li   return verbose_level <= external_verbose_level;
159*ec63e07aSXin Li }
160*ec63e07aSXin Li 
161*ec63e07aSXin Li }  // namespace sapi::raw_logging_internal
162