• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(ABSL_RAW_LOG)
36 #define SAPI_RAW_LOG ABSL_RAW_LOG
37 #define SAPI_USE_ABSL_RAW_LOG 1
38 #else
39 #define SAPI_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
40 #define SAPI_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
41 #define SAPI_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
42 #define SAPI_RAW_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
43 // This is similar to LOG(severity) << format..., but
44 // * it is to be used ONLY by low-level modules that can't use normal LOG()
45 // * it is designed to be a low-level logger that does not allocate any
46 //   memory and does not need any locks, hence:
47 // * it logs straight and ONLY to STDERR w/o buffering
48 // * it uses an explicit printf-format and arguments list
49 // * it will silently chop off really long message strings
50 // Usage example:
51 //   SAPI_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
52 // This will print an almost standard log line like this to stderr only:
53 //   E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
54 
55 #define SAPI_RAW_LOG(severity, ...)                                            \
56   do {                                                                         \
57     constexpr const char* absl_raw_logging_internal_basename =                 \
58         ::sapi::raw_logging_internal::Basename(__FILE__,                       \
59                                                sizeof(__FILE__) - 1);          \
60     ::sapi::raw_logging_internal::RawLog(SAPI_RAW_LOGGING_INTERNAL_##severity, \
61                                          absl_raw_logging_internal_basename,   \
62                                          __LINE__, __VA_ARGS__);               \
63     if (SAPI_RAW_LOGGING_INTERNAL_##severity == ::absl::LogSeverity::kFatal) { \
64       ABSL_UNREACHABLE();                                                      \
65     }                                                                          \
66   } while (0)
67 #endif
68 
69 #ifdef ABSL_RAW_CHECK
70 #define SAPI_RAW_CHECK ABSL_RAW_CHECK
71 #else
72 // Similar to CHECK(condition) << message, but for low-level modules:
73 // we use only SAPI_RAW_LOG that does not allocate memory.
74 // We do not want to provide args list here to encourage this usage:
75 //   if (!cond)  SAPI_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
76 // so that the args are not computed when not needed.
77 #define SAPI_RAW_CHECK(condition, message)                             \
78   do {                                                                 \
79     if (ABSL_PREDICT_FALSE(!(condition))) {                            \
80       SAPI_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
81     }                                                                  \
82   } while (0)
83 #endif
84 
85 // Returns whether SAPI raw verbose logging is enabled, as determined by the
86 // SAPI_VLOG_LEVEL environment variable.
87 #define SAPI_RAW_VLOG_IS_ON(verbose_level) \
88   ::sapi::raw_logging_internal::VLogIsOn(verbose_level)
89 
90 // Like SAPI_RAW_LOG(), but also logs the current value of errno and its
91 // corresponding error message.
92 #define SAPI_RAW_PLOG(severity, format, ...)                              \
93   do {                                                                    \
94     char sapi_raw_plog_errno_buffer[100];                                 \
95     const char* sapi_raw_plog_errno_str =                                 \
96         ::sapi::RawStrError(errno, sapi_raw_plog_errno_buffer,            \
97                             sizeof(sapi_raw_plog_errno_buffer));          \
98     char sapi_raw_plog_buffer[::sapi::raw_logging_internal::kLogBufSize]; \
99     absl::SNPrintF(sapi_raw_plog_buffer, sizeof(sapi_raw_plog_buffer),    \
100                    (format), ##__VA_ARGS__);                              \
101     SAPI_RAW_LOG(severity, "%s: %s [%d]", sapi_raw_plog_buffer,           \
102                  sapi_raw_plog_errno_str, errno);                         \
103   } while (0)
104 
105 // If verbose logging is enabled, uses SAPI_RAW_LOG() to log.
106 #define SAPI_RAW_VLOG(verbose_level, format, ...)            \
107   if (sapi::raw_logging_internal::VLogIsOn(verbose_level)) { \
108     SAPI_RAW_LOG(INFO, (format), ##__VA_ARGS__);             \
109   }
110 
111 // Like SAPI_RAW_CHECK(), but also logs errno and a message (similar to
112 // SAPI_RAW_PLOG()).
113 #define SAPI_RAW_PCHECK(condition, format, ...)                             \
114   do {                                                                      \
115     if (ABSL_PREDICT_FALSE(!(condition))) {                                 \
116       char sapi_raw_plog_errno_buffer[100];                                 \
117       const char* sapi_raw_plog_errno_str =                                 \
118           ::sapi::RawStrError(errno, sapi_raw_plog_errno_buffer,            \
119                               sizeof(sapi_raw_plog_errno_buffer));          \
120       char sapi_raw_plog_buffer[::sapi::raw_logging_internal::kLogBufSize]; \
121       absl::SNPrintF(sapi_raw_plog_buffer, sizeof(sapi_raw_plog_buffer),    \
122                      (format), ##__VA_ARGS__);                              \
123       SAPI_RAW_LOG(FATAL, "Check %s failed: %s: %s [%d]", #condition,       \
124                    sapi_raw_plog_buffer, sapi_raw_plog_errno_str, errno);   \
125     }                                                                       \
126   } while (0)
127 
128 namespace sapi::raw_logging_internal {
129 
130 constexpr int kLogBufSize = 3000;
131 
132 #ifndef SAPI_USE_ABSL_RAW_LOG
133 // Helper function to implement ABSL_RAW_LOG
134 // Logs format... at "severity" level, reporting it
135 // as called from file:line.
136 // This does not allocate memory or acquire locks.
137 void RawLog(absl::LogSeverity severity, const char* file, int line,
138             const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
139 
140 // compile-time function to get the "base" filename, that is, the part of
141 // a filename after the last "/" or "\" path separator.  The search starts at
142 // the end of the string; the second parameter is the length of the string.
Basename(const char * fname,int offset)143 constexpr const char* Basename(const char* fname, int offset) {
144   return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
145              ? fname + offset
146              : Basename(fname, offset - 1);
147 }
148 #endif
149 
150 bool VLogIsOn(int verbose_level);
151 
152 }  // namespace sapi::raw_logging_internal
153 
154 #endif  // SANDBOXED_API_UTIL_RAW_LOGGING_H_
155