xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/util/raw_logging.h (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 // SAPI raw logging. Forked from Abseil's version.
16*ec63e07aSXin Li 
17*ec63e07aSXin Li #ifndef SANDBOXED_API_UTIL_RAW_LOGGING_H_
18*ec63e07aSXin Li #define SANDBOXED_API_UTIL_RAW_LOGGING_H_
19*ec63e07aSXin Li 
20*ec63e07aSXin Li #include <cerrno>
21*ec63e07aSXin Li #include <cstddef>
22*ec63e07aSXin Li #include <string>
23*ec63e07aSXin Li #include <utility>
24*ec63e07aSXin Li 
25*ec63e07aSXin Li #include "absl/base/attributes.h"
26*ec63e07aSXin Li #include "absl/base/config.h"
27*ec63e07aSXin Li #include "absl/base/log_severity.h"
28*ec63e07aSXin Li #include "absl/base/macros.h"
29*ec63e07aSXin Li #include "absl/base/optimization.h"
30*ec63e07aSXin Li #include "absl/base/port.h"
31*ec63e07aSXin Li #include "absl/strings/str_cat.h"
32*ec63e07aSXin Li #include "absl/strings/str_format.h"
33*ec63e07aSXin Li #include "sandboxed_api/util/strerror.h"
34*ec63e07aSXin Li 
35*ec63e07aSXin Li // Exclude ABSL_RAW_LOG when running on Android because it will not be visible
36*ec63e07aSXin Li // in logcat since Android sends anything written to stdout and stderr to
37*ec63e07aSXin Li // /dev/null.
38*ec63e07aSXin Li #if defined(ABSL_RAW_LOG) && !(__ANDROID__)
39*ec63e07aSXin Li #define SAPI_RAW_LOG ABSL_RAW_LOG
40*ec63e07aSXin Li #else
41*ec63e07aSXin Li // This is similar to LOG(severity) << format..., but
42*ec63e07aSXin Li // * it is to be used ONLY by low-level modules that can't use normal LOG()
43*ec63e07aSXin Li // * it is designed to be a low-level logger that does not allocate any
44*ec63e07aSXin Li //   memory and does not need any locks, hence:
45*ec63e07aSXin Li // * it logs straight and ONLY to STDERR w/o buffering
46*ec63e07aSXin Li // * it uses an explicit printf-format and arguments list
47*ec63e07aSXin Li // * it will silently chop off really long message strings
48*ec63e07aSXin Li // Usage example:
49*ec63e07aSXin Li //   SAPI_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
50*ec63e07aSXin Li // This will print an almost standard log line like this to stderr only:
51*ec63e07aSXin Li //   E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
52*ec63e07aSXin Li 
53*ec63e07aSXin Li #define SAPI_RAW_LOG(severity, ...)                                            \
54*ec63e07aSXin Li   do {                                                                         \
55*ec63e07aSXin Li     constexpr const char* absl_raw_logging_internal_basename =                 \
56*ec63e07aSXin Li         ::sapi::raw_logging_internal::Basename(__FILE__,                       \
57*ec63e07aSXin Li                                                sizeof(__FILE__) - 1);          \
58*ec63e07aSXin Li     ::sapi::raw_logging_internal::RawLog(SAPI_RAW_LOGGING_INTERNAL_##severity, \
59*ec63e07aSXin Li                                          absl_raw_logging_internal_basename,   \
60*ec63e07aSXin Li                                          __LINE__, __VA_ARGS__);               \
61*ec63e07aSXin Li     if (SAPI_RAW_LOGGING_INTERNAL_##severity == ::absl::LogSeverity::kFatal) { \
62*ec63e07aSXin Li       ABSL_UNREACHABLE();                                                      \
63*ec63e07aSXin Li     }                                                                          \
64*ec63e07aSXin Li   } while (0)
65*ec63e07aSXin Li #endif
66*ec63e07aSXin Li 
67*ec63e07aSXin Li #ifdef ABSL_RAW_CHECK
68*ec63e07aSXin Li #define SAPI_RAW_CHECK ABSL_RAW_CHECK
69*ec63e07aSXin Li #else
70*ec63e07aSXin Li // Similar to CHECK(condition) << message, but for low-level modules:
71*ec63e07aSXin Li // we use only SAPI_RAW_LOG that does not allocate memory.
72*ec63e07aSXin Li // We do not want to provide args list here to encourage this usage:
73*ec63e07aSXin Li //   if (!cond)  SAPI_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
74*ec63e07aSXin Li // so that the args are not computed when not needed.
75*ec63e07aSXin Li #define SAPI_RAW_CHECK(condition, message)                             \
76*ec63e07aSXin Li   do {                                                                 \
77*ec63e07aSXin Li     if (ABSL_PREDICT_FALSE(!(condition))) {                            \
78*ec63e07aSXin Li       SAPI_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
79*ec63e07aSXin Li     }                                                                  \
80*ec63e07aSXin Li   } while (0)
81*ec63e07aSXin Li #endif
82*ec63e07aSXin Li 
83*ec63e07aSXin Li #define SAPI_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
84*ec63e07aSXin Li #define SAPI_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
85*ec63e07aSXin Li #define SAPI_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
86*ec63e07aSXin Li #define SAPI_RAW_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
87*ec63e07aSXin Li 
88*ec63e07aSXin Li // Returns whether SAPI verbose logging is enabled, as determined by the
89*ec63e07aSXin Li // SAPI_VLOG_LEVEL environment variable.
90*ec63e07aSXin Li #define SAPI_VLOG_IS_ON(verbose_level) \
91*ec63e07aSXin Li   ::sapi::raw_logging_internal::VLogIsOn(verbose_level)
92*ec63e07aSXin Li 
93*ec63e07aSXin Li #define SAPI_RAW_VLOG_IS_ON(verbose_level) SAPI_VLOG_IS_ON(verbose_level)
94*ec63e07aSXin Li 
95*ec63e07aSXin Li #ifndef VLOG
96*ec63e07aSXin Li // `VLOG` uses numeric levels to provide verbose logging that can configured at
97*ec63e07aSXin Li // runtime, globally. `VLOG` statements are logged at `INFO` severity if they
98*ec63e07aSXin Li // are logged at all; the numeric levels are on a different scale than the
99*ec63e07aSXin Li // proper severity levels. Positive levels are disabled by default. Negative
100*ec63e07aSXin Li // levels should not be used.
101*ec63e07aSXin Li #define VLOG(verbose_level)                                                \
102*ec63e07aSXin Li   for (int sapi_logging_internal_verbose_level = (verbose_level),          \
103*ec63e07aSXin Li            sapi_logging_internal_log_loop = 1;                             \
104*ec63e07aSXin Li        sapi_logging_internal_log_loop; sapi_logging_internal_log_loop = 0) \
105*ec63e07aSXin Li   LOG_IF(INFO, SAPI_VLOG_IS_ON(sapi_logging_internal_verbose_level))       \
106*ec63e07aSXin Li       .WithVerbosity(sapi_logging_internal_verbose_level)
107*ec63e07aSXin Li #endif
108*ec63e07aSXin Li 
109*ec63e07aSXin Li // Like SAPI_RAW_LOG(), but also logs the current value of errno and its
110*ec63e07aSXin Li // corresponding error message.
111*ec63e07aSXin Li #define SAPI_RAW_PLOG(severity, format, ...)                              \
112*ec63e07aSXin Li   do {                                                                    \
113*ec63e07aSXin Li     char sapi_raw_plog_errno_buffer[100];                                 \
114*ec63e07aSXin Li     const char* sapi_raw_plog_errno_str =                                 \
115*ec63e07aSXin Li         ::sapi::RawStrError(errno, sapi_raw_plog_errno_buffer,            \
116*ec63e07aSXin Li                             sizeof(sapi_raw_plog_errno_buffer));          \
117*ec63e07aSXin Li     char sapi_raw_plog_buffer[::sapi::raw_logging_internal::kLogBufSize]; \
118*ec63e07aSXin Li     absl::SNPrintF(sapi_raw_plog_buffer, sizeof(sapi_raw_plog_buffer),    \
119*ec63e07aSXin Li                    (format), ##__VA_ARGS__);                              \
120*ec63e07aSXin Li     SAPI_RAW_LOG(severity, "%s: %s [%d]", sapi_raw_plog_buffer,           \
121*ec63e07aSXin Li                  sapi_raw_plog_errno_str, errno);                         \
122*ec63e07aSXin Li   } while (0)
123*ec63e07aSXin Li 
124*ec63e07aSXin Li // If verbose logging is enabled, uses SAPI_RAW_LOG() to log.
125*ec63e07aSXin Li #define SAPI_RAW_VLOG(verbose_level, format, ...)            \
126*ec63e07aSXin Li   if (sapi::raw_logging_internal::VLogIsOn(verbose_level)) { \
127*ec63e07aSXin Li     SAPI_RAW_LOG(INFO, (format), ##__VA_ARGS__);             \
128*ec63e07aSXin Li   }
129*ec63e07aSXin Li 
130*ec63e07aSXin Li // Like SAPI_RAW_CHECK(), but also logs errno and a message (similar to
131*ec63e07aSXin Li // SAPI_RAW_PLOG()).
132*ec63e07aSXin Li #define SAPI_RAW_PCHECK(condition, format, ...)                             \
133*ec63e07aSXin Li   do {                                                                      \
134*ec63e07aSXin Li     if (ABSL_PREDICT_FALSE(!(condition))) {                                 \
135*ec63e07aSXin Li       char sapi_raw_plog_errno_buffer[100];                                 \
136*ec63e07aSXin Li       const char* sapi_raw_plog_errno_str =                                 \
137*ec63e07aSXin Li           ::sapi::RawStrError(errno, sapi_raw_plog_errno_buffer,            \
138*ec63e07aSXin Li                               sizeof(sapi_raw_plog_errno_buffer));          \
139*ec63e07aSXin Li       char sapi_raw_plog_buffer[::sapi::raw_logging_internal::kLogBufSize]; \
140*ec63e07aSXin Li       absl::SNPrintF(sapi_raw_plog_buffer, sizeof(sapi_raw_plog_buffer),    \
141*ec63e07aSXin Li                      (format), ##__VA_ARGS__);                              \
142*ec63e07aSXin Li       SAPI_RAW_LOG(FATAL, "Check %s failed: %s: %s [%d]", #condition,       \
143*ec63e07aSXin Li                    sapi_raw_plog_buffer, sapi_raw_plog_errno_str, errno);   \
144*ec63e07aSXin Li     }                                                                       \
145*ec63e07aSXin Li   } while (0)
146*ec63e07aSXin Li 
147*ec63e07aSXin Li namespace sapi::raw_logging_internal {
148*ec63e07aSXin Li 
149*ec63e07aSXin Li constexpr int kLogBufSize = 3000;
150*ec63e07aSXin Li 
151*ec63e07aSXin Li // Helper function to implement ABSL_RAW_LOG
152*ec63e07aSXin Li // Logs format... at "severity" level, reporting it
153*ec63e07aSXin Li // as called from file:line.
154*ec63e07aSXin Li // This does not allocate memory or acquire locks.
155*ec63e07aSXin Li void RawLog(absl::LogSeverity severity, const char* file, int line,
156*ec63e07aSXin Li             const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
157*ec63e07aSXin Li 
158*ec63e07aSXin Li // Writes the provided buffer directly to stderr, in a safe, low-level manner.
159*ec63e07aSXin Li //
160*ec63e07aSXin Li // In POSIX this means calling write(), which is async-signal safe and does
161*ec63e07aSXin Li // not malloc.  If the platform supports the SYS_write syscall, we invoke that
162*ec63e07aSXin Li // directly to side-step any libc interception.
163*ec63e07aSXin Li void SafeWriteToStderr(const char* s, size_t len);
164*ec63e07aSXin Li 
165*ec63e07aSXin Li // compile-time function to get the "base" filename, that is, the part of
166*ec63e07aSXin Li // a filename after the last "/" or "\" path separator.  The search starts at
167*ec63e07aSXin Li // the end of the string; the second parameter is the length of the string.
Basename(const char * fname,int offset)168*ec63e07aSXin Li constexpr const char* Basename(const char* fname, int offset) {
169*ec63e07aSXin Li   return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
170*ec63e07aSXin Li              ? fname + offset
171*ec63e07aSXin Li              : Basename(fname, offset - 1);
172*ec63e07aSXin Li }
173*ec63e07aSXin Li 
174*ec63e07aSXin Li bool VLogIsOn(int verbose_level);
175*ec63e07aSXin Li 
176*ec63e07aSXin Li }  // namespace sapi::raw_logging_internal
177*ec63e07aSXin Li 
178*ec63e07aSXin Li #endif  // SANDBOXED_API_UTIL_RAW_LOGGING_H_
179