|
| 1 | +/* |
| 2 | + * Copyright 2006 The WebRTC Project Authors. All rights reserved. |
| 3 | + * |
| 4 | + * Use of this source code is governed by a BSD-style license |
| 5 | + * that can be found in the LICENSE file in the root of the source |
| 6 | + * tree. An additional intellectual property rights grant can be found |
| 7 | + * in the file PATENTS. All contributing project authors may |
| 8 | + * be found in the AUTHORS file in the root of the source tree. |
| 9 | + */ |
| 10 | + |
| 11 | +// Most of this was borrowed (with minor modifications) from V8's and Chromium's |
| 12 | +// src/base/logging.cc. |
| 13 | + |
| 14 | +#include <cstdarg> |
| 15 | +#include <cstdio> |
| 16 | +#include <cstdlib> |
| 17 | + |
| 18 | +#if defined(WEBRTC_ANDROID) |
| 19 | +#define RTC_LOG_TAG_ANDROID "rtc" |
| 20 | +#include <android/log.h> // NOLINT |
| 21 | +#endif |
| 22 | + |
| 23 | +#if defined(WEBRTC_WIN) |
| 24 | +#include <windows.h> |
| 25 | +#endif |
| 26 | + |
| 27 | +#if defined(WEBRTC_WIN) |
| 28 | +#define LAST_SYSTEM_ERROR (::GetLastError()) |
| 29 | +#elif defined(__native_client__) && __native_client__ |
| 30 | +#define LAST_SYSTEM_ERROR (0) |
| 31 | +#elif defined(WEBRTC_POSIX) |
| 32 | +#include <errno.h> |
| 33 | +#define LAST_SYSTEM_ERROR (errno) |
| 34 | +#endif // WEBRTC_WIN |
| 35 | + |
| 36 | +#include "./checks.h" |
| 37 | + |
| 38 | +namespace { |
| 39 | +#if defined(__GNUC__) |
| 40 | +__attribute__((__format__(__printf__, 2, 3))) |
| 41 | +#endif |
| 42 | +void AppendFormat(std::string* s, const char* fmt, ...) { |
| 43 | + va_list args, copy; |
| 44 | + va_start(args, fmt); |
| 45 | + va_copy(copy, args); |
| 46 | + const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy); |
| 47 | + va_end(copy); |
| 48 | + |
| 49 | + if (predicted_length > 0) { |
| 50 | + const size_t size = s->size(); |
| 51 | + s->resize(size + predicted_length); |
| 52 | + // Pass "+ 1" to vsnprintf to include space for the '0円'. |
| 53 | + std::vsnprintf(&((*s)[size]), predicted_length + 1, fmt, args); |
| 54 | + } |
| 55 | + va_end(args); |
| 56 | +} |
| 57 | +} // namespace |
| 58 | + |
| 59 | +namespace rtc { |
| 60 | +namespace webrtc_checks_impl { |
| 61 | + |
| 62 | +// Reads one argument from args, appends it to s and advances fmt. |
| 63 | +// Returns true iff an argument was sucessfully parsed. |
| 64 | +bool ParseArg(va_list* args, const CheckArgType** fmt, std::string* s) { |
| 65 | + if (**fmt == CheckArgType::kEnd) |
| 66 | + return false; |
| 67 | + |
| 68 | + switch (**fmt) { |
| 69 | + case CheckArgType::kInt: |
| 70 | + AppendFormat(s, "%d", va_arg(*args, int)); |
| 71 | + break; |
| 72 | + case CheckArgType::kLong: |
| 73 | + AppendFormat(s, "%ld", va_arg(*args, long)); |
| 74 | + break; |
| 75 | + case CheckArgType::kLongLong: |
| 76 | + AppendFormat(s, "%lld", va_arg(*args, long long)); |
| 77 | + break; |
| 78 | + case CheckArgType::kUInt: |
| 79 | + AppendFormat(s, "%u", va_arg(*args, unsigned)); |
| 80 | + break; |
| 81 | + case CheckArgType::kULong: |
| 82 | + AppendFormat(s, "%lu", va_arg(*args, unsigned long)); |
| 83 | + break; |
| 84 | + case CheckArgType::kULongLong: |
| 85 | + AppendFormat(s, "%llu", va_arg(*args, unsigned long long)); |
| 86 | + break; |
| 87 | + case CheckArgType::kDouble: |
| 88 | + AppendFormat(s, "%g", va_arg(*args, double)); |
| 89 | + break; |
| 90 | + case CheckArgType::kLongDouble: |
| 91 | + AppendFormat(s, "%Lg", va_arg(*args, long double)); |
| 92 | + break; |
| 93 | + case CheckArgType::kCharP: |
| 94 | + s->append(va_arg(*args, const char*)); |
| 95 | + break; |
| 96 | + case CheckArgType::kStdString: |
| 97 | + s->append(*va_arg(*args, const std::string*)); |
| 98 | + break; |
| 99 | + case CheckArgType::kStringView: { |
| 100 | + const absl::string_view sv = *va_arg(*args, const absl::string_view*); |
| 101 | + s->append(sv.data(), sv.size()); |
| 102 | + break; |
| 103 | + } |
| 104 | + case CheckArgType::kVoidP: |
| 105 | + AppendFormat(s, "%p", va_arg(*args, const void*)); |
| 106 | + break; |
| 107 | + default: |
| 108 | + s->append("[Invalid CheckArgType]"); |
| 109 | + return false; |
| 110 | + } |
| 111 | + (*fmt)++; |
| 112 | + return true; |
| 113 | +} |
| 114 | + |
| 115 | +RTC_NORETURN void FatalLog(const char* file, |
| 116 | + int line, |
| 117 | + const char* message, |
| 118 | + const CheckArgType* fmt, |
| 119 | + ...) { |
| 120 | + va_list args; |
| 121 | + va_start(args, fmt); |
| 122 | + |
| 123 | + std::string s; |
| 124 | + AppendFormat(&s, |
| 125 | + "\n\n" |
| 126 | + "#\n" |
| 127 | + "# Fatal error in: %s, line %d\n" |
| 128 | + "# last system error: %u\n" |
| 129 | + "# Check failed: %s", |
| 130 | + file, line, LAST_SYSTEM_ERROR, message); |
| 131 | + |
| 132 | + if (*fmt == CheckArgType::kCheckOp) { |
| 133 | + // This log message was generated by RTC_CHECK_OP, so we have to complete |
| 134 | + // the error message using the operands that have been passed as the first |
| 135 | + // two arguments. |
| 136 | + fmt++; |
| 137 | + |
| 138 | + std::string s1, s2; |
| 139 | + if (ParseArg(&args, &fmt, &s1) && ParseArg(&args, &fmt, &s2)) |
| 140 | + AppendFormat(&s, " (%s vs. %s)\n# ", s1.c_str(), s2.c_str()); |
| 141 | + } else { |
| 142 | + s.append("\n# "); |
| 143 | + } |
| 144 | + |
| 145 | + // Append all the user-supplied arguments to the message. |
| 146 | + while (ParseArg(&args, &fmt, &s)) |
| 147 | + ; |
| 148 | + |
| 149 | + va_end(args); |
| 150 | + |
| 151 | + const char* output = s.c_str(); |
| 152 | + |
| 153 | +#if defined(WEBRTC_ANDROID) |
| 154 | + __android_log_print(ANDROID_LOG_ERROR, RTC_LOG_TAG_ANDROID, "%s\n", output); |
| 155 | +#endif |
| 156 | + |
| 157 | + fflush(stdout); |
| 158 | + fprintf(stderr, "%s", output); |
| 159 | + fflush(stderr); |
| 160 | + abort(); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace webrtc_checks_impl |
| 164 | +} // namespace rtc |
| 165 | + |
| 166 | +// Function to call from the C version of the RTC_CHECK and RTC_DCHECK macros. |
| 167 | +RTC_NORETURN void rtc_FatalMessage(const char* file, int line, |
| 168 | + const char* msg) { |
| 169 | + static constexpr rtc::webrtc_checks_impl::CheckArgType t[] = { |
| 170 | + rtc::webrtc_checks_impl::CheckArgType::kEnd}; |
| 171 | + FatalLog(file, line, msg, t); |
| 172 | +} |
0 commit comments