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