raw_logging.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2017 The Abseil Authors.
  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. #include "absl/base/internal/raw_logging.h"
  15. #include <stddef.h>
  16. #include <cstdarg>
  17. #include <cstdio>
  18. #include <cstdlib>
  19. #include <cstring>
  20. #include "absl/base/attributes.h"
  21. #include "absl/base/config.h"
  22. #include "absl/base/internal/atomic_hook.h"
  23. #include "absl/base/log_severity.h"
  24. // We know how to perform low-level writes to stderr in POSIX and Windows. For
  25. // these platforms, we define the token ABSL_LOW_LEVEL_WRITE_SUPPORTED.
  26. // Much of raw_logging.cc becomes a no-op when we can't output messages,
  27. // although a FATAL ABSL_RAW_LOG message will still abort the process.
  28. // ABSL_HAVE_POSIX_WRITE is defined when the platform provides posix write()
  29. // (as from unistd.h)
  30. //
  31. // This preprocessor token is also defined in raw_io.cc. If you need to copy
  32. // this, consider moving both to config.h instead.
  33. #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
  34. defined(__Fuchsia__) || defined(__native_client__) || \
  35. defined(__EMSCRIPTEN__) || defined(__ASYLO__)
  36. #include <unistd.h>
  37. #define ABSL_HAVE_POSIX_WRITE 1
  38. #define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
  39. #else
  40. #undef ABSL_HAVE_POSIX_WRITE
  41. #endif
  42. // ABSL_HAVE_SYSCALL_WRITE is defined when the platform provides the syscall
  43. // syscall(SYS_write, /*int*/ fd, /*char* */ buf, /*size_t*/ len);
  44. // for low level operations that want to avoid libc.
  45. #if (defined(__linux__) || defined(__FreeBSD__)) && !defined(__ANDROID__)
  46. #include <sys/syscall.h>
  47. #define ABSL_HAVE_SYSCALL_WRITE 1
  48. #define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
  49. #else
  50. #undef ABSL_HAVE_SYSCALL_WRITE
  51. #endif
  52. #ifdef _WIN32
  53. #include <io.h>
  54. #define ABSL_HAVE_RAW_IO 1
  55. #define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
  56. #else
  57. #undef ABSL_HAVE_RAW_IO
  58. #endif
  59. namespace absl {
  60. ABSL_NAMESPACE_BEGIN
  61. namespace raw_logging_internal {
  62. namespace {
  63. // TODO(gfalcon): We want raw-logging to work on as many platforms as possible.
  64. // Explicitly `#error` out when not `ABSL_LOW_LEVEL_WRITE_SUPPORTED`, except for
  65. // a selected set of platforms for which we expect not to be able to raw log.
  66. ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
  67. absl::base_internal::AtomicHook<LogPrefixHook>
  68. log_prefix_hook;
  69. ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
  70. absl::base_internal::AtomicHook<AbortHook>
  71. abort_hook;
  72. #ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
  73. constexpr char kTruncated[] = " ... (message truncated)\n";
  74. // sprintf the format to the buffer, adjusting *buf and *size to reflect the
  75. // consumed bytes, and return whether the message fit without truncation. If
  76. // truncation occurred, if possible leave room in the buffer for the message
  77. // kTruncated[].
  78. bool VADoRawLog(char** buf, int* size, const char* format, va_list ap)
  79. ABSL_PRINTF_ATTRIBUTE(3, 0);
  80. bool VADoRawLog(char** buf, int* size, const char* format, va_list ap) {
  81. int n = vsnprintf(*buf, *size, format, ap);
  82. bool result = true;
  83. if (n < 0 || n > *size) {
  84. result = false;
  85. if (static_cast<size_t>(*size) > sizeof(kTruncated)) {
  86. n = *size - sizeof(kTruncated); // room for truncation message
  87. } else {
  88. n = 0; // no room for truncation message
  89. }
  90. }
  91. *size -= n;
  92. *buf += n;
  93. return result;
  94. }
  95. #endif // ABSL_LOW_LEVEL_WRITE_SUPPORTED
  96. constexpr int kLogBufSize = 3000;
  97. // CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
  98. // that invoke malloc() and getenv() that might acquire some locks.
  99. // Helper for RawLog below.
  100. // *DoRawLog writes to *buf of *size and move them past the written portion.
  101. // It returns true iff there was no overflow or error.
  102. bool DoRawLog(char** buf, int* size, const char* format, ...)
  103. ABSL_PRINTF_ATTRIBUTE(3, 4);
  104. bool DoRawLog(char** buf, int* size, const char* format, ...) {
  105. va_list ap;
  106. va_start(ap, format);
  107. int n = vsnprintf(*buf, *size, format, ap);
  108. va_end(ap);
  109. if (n < 0 || n > *size) return false;
  110. *size -= n;
  111. *buf += n;
  112. return true;
  113. }
  114. void RawLogVA(absl::LogSeverity severity, const char* file, int line,
  115. const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0);
  116. void RawLogVA(absl::LogSeverity severity, const char* file, int line,
  117. const char* format, va_list ap) {
  118. char buffer[kLogBufSize];
  119. char* buf = buffer;
  120. int size = sizeof(buffer);
  121. #ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
  122. bool enabled = true;
  123. #else
  124. bool enabled = false;
  125. #endif
  126. #ifdef ABSL_MIN_LOG_LEVEL
  127. if (severity < static_cast<absl::LogSeverity>(ABSL_MIN_LOG_LEVEL) &&
  128. severity < absl::LogSeverity::kFatal) {
  129. enabled = false;
  130. }
  131. #endif
  132. auto log_prefix_hook_ptr = log_prefix_hook.Load();
  133. if (log_prefix_hook_ptr) {
  134. enabled = log_prefix_hook_ptr(severity, file, line, &buf, &size);
  135. } else {
  136. if (enabled) {
  137. DoRawLog(&buf, &size, "[%s : %d] RAW: ", file, line);
  138. }
  139. }
  140. const char* const prefix_end = buf;
  141. #ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
  142. if (enabled) {
  143. bool no_chop = VADoRawLog(&buf, &size, format, ap);
  144. if (no_chop) {
  145. DoRawLog(&buf, &size, "\n");
  146. } else {
  147. DoRawLog(&buf, &size, "%s", kTruncated);
  148. }
  149. SafeWriteToStderr(buffer, strlen(buffer));
  150. }
  151. #else
  152. static_cast<void>(format);
  153. static_cast<void>(ap);
  154. #endif
  155. // Abort the process after logging a FATAL message, even if the output itself
  156. // was suppressed.
  157. if (severity == absl::LogSeverity::kFatal) {
  158. abort_hook(file, line, buffer, prefix_end, buffer + kLogBufSize);
  159. abort();
  160. }
  161. }
  162. // Non-formatting version of RawLog().
  163. //
  164. // TODO(gfalcon): When string_view no longer depends on base, change this
  165. // interface to take its message as a string_view instead.
  166. void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line,
  167. const std::string& message) {
  168. RawLog(severity, file, line, "%.*s", static_cast<int>(message.size()),
  169. message.data());
  170. }
  171. } // namespace
  172. void SafeWriteToStderr(const char *s, size_t len) {
  173. #if defined(ABSL_HAVE_SYSCALL_WRITE)
  174. syscall(SYS_write, STDERR_FILENO, s, len);
  175. #elif defined(ABSL_HAVE_POSIX_WRITE)
  176. write(STDERR_FILENO, s, len);
  177. #elif defined(ABSL_HAVE_RAW_IO)
  178. _write(/* stderr */ 2, s, len);
  179. #else
  180. // stderr logging unsupported on this platform
  181. (void) s;
  182. (void) len;
  183. #endif
  184. }
  185. void RawLog(absl::LogSeverity severity, const char* file, int line,
  186. const char* format, ...) {
  187. va_list ap;
  188. va_start(ap, format);
  189. RawLogVA(severity, file, line, format, ap);
  190. va_end(ap);
  191. }
  192. bool RawLoggingFullySupported() {
  193. #ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
  194. return true;
  195. #else // !ABSL_LOW_LEVEL_WRITE_SUPPORTED
  196. return false;
  197. #endif // !ABSL_LOW_LEVEL_WRITE_SUPPORTED
  198. }
  199. ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_DLL
  200. absl::base_internal::AtomicHook<InternalLogFunction>
  201. internal_log_function(DefaultInternalLog);
  202. void RegisterLogPrefixHook(LogPrefixHook func) { log_prefix_hook.Store(func); }
  203. void RegisterAbortHook(AbortHook func) { abort_hook.Store(func); }
  204. void RegisterInternalLogFunction(InternalLogFunction func) {
  205. internal_log_function.Store(func);
  206. }
  207. } // namespace raw_logging_internal
  208. ABSL_NAMESPACE_END
  209. } // namespace absl