| 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 "rtc_base/checks.h" |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | RTC_NORETURN void WriteFatalLogAndAbort(const std::string& output) { |
| 41 | const char* output_c = output.c_str(); |
| 42 | #if defined(WEBRTC_ANDROID) |
| 43 | __android_log_print(ANDROID_LOG_ERROR, RTC_LOG_TAG_ANDROID, "%s\n", output_c); |
| 44 | #endif |
| 45 | fflush(stdout); |
| 46 | fprintf(stderr, "%s", output_c); |
| 47 | fflush(stderr); |
| 48 | #if defined(WEBRTC_WIN) |
| 49 | DebugBreak(); |
| 50 | #endif |
| 51 | abort(); |
| 52 | } |
| 53 | |
| 54 | #if defined(__GNUC__) |
| 55 | __attribute__((__format__(__printf__, 2, 3))) |
| 56 | #endif |
| 57 | void AppendFormat(std::string* s, const char* fmt, ...) { |
| 58 | va_list args, copy; |
| 59 | va_start(args, fmt); |
| 60 | va_copy(copy, args); |
| 61 | const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy); |
| 62 | va_end(copy); |
| 63 | |
| 64 | if (predicted_length > 0) { |
| 65 | const size_t size = s->size(); |
| 66 | s->resize(size + predicted_length); |
| 67 | // Pass "+ 1" to vsnprintf to include space for the '\0'. |
| 68 | std::vsnprintf(&((*s)[size]), predicted_length + 1, fmt, args); |
| 69 | } |
| 70 | va_end(args); |
| 71 | } |
| 72 | } // namespace |
| 73 | |
| 74 | namespace rtc { |
| 75 | namespace webrtc_checks_impl { |
| 76 | |
| 77 | #if RTC_CHECK_MSG_ENABLED |
| 78 | // Reads one argument from args, appends it to s and advances fmt. |
| 79 | // Returns true iff an argument was sucessfully parsed. |
| 80 | bool ParseArg(va_list* args, const CheckArgType** fmt, std::string* s) { |
| 81 | if (**fmt == CheckArgType::kEnd) |
| 82 | return false; |
| 83 | |
| 84 | switch (**fmt) { |
| 85 | case CheckArgType::kInt: |
| 86 | AppendFormat(s, "%d", va_arg(*args, int)); |
| 87 | break; |
| 88 | case CheckArgType::kLong: |
| 89 | AppendFormat(s, "%ld", va_arg(*args, long)); |
| 90 | break; |
| 91 | case CheckArgType::kLongLong: |
| 92 | AppendFormat(s, "%lld", va_arg(*args, long long)); |
| 93 | break; |
| 94 | case CheckArgType::kUInt: |
| 95 | AppendFormat(s, "%u", va_arg(*args, unsigned)); |
| 96 | break; |
| 97 | case CheckArgType::kULong: |
| 98 | AppendFormat(s, "%lu", va_arg(*args, unsigned long)); |
| 99 | break; |
| 100 | case CheckArgType::kULongLong: |
| 101 | AppendFormat(s, "%llu", va_arg(*args, unsigned long long)); |
| 102 | break; |
| 103 | case CheckArgType::kDouble: |
| 104 | AppendFormat(s, "%g", va_arg(*args, double)); |
| 105 | break; |
| 106 | case CheckArgType::kLongDouble: |
| 107 | AppendFormat(s, "%Lg", va_arg(*args, long double)); |
| 108 | break; |
| 109 | case CheckArgType::kCharP: |
| 110 | s->append(va_arg(*args, const char*)); |
| 111 | break; |
| 112 | case CheckArgType::kStdString: |
| 113 | s->append(*va_arg(*args, const std::string*)); |
| 114 | break; |
| 115 | case CheckArgType::kStringView: { |
| 116 | const absl::string_view sv = *va_arg(*args, const absl::string_view*); |
| 117 | s->append(sv.data(), sv.size()); |
| 118 | break; |
| 119 | } |
| 120 | case CheckArgType::kVoidP: |
| 121 | AppendFormat(s, "%p", va_arg(*args, const void*)); |
| 122 | break; |
| 123 | default: |
| 124 | s->append("[Invalid CheckArgType]"); |
| 125 | return false; |
| 126 | } |
| 127 | (*fmt)++; |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | RTC_NORETURN void FatalLog(const char* file, |
| 132 | int line, |
| 133 | const char* message, |
| 134 | const CheckArgType* fmt, |
| 135 | ...) { |
| 136 | va_list args; |
| 137 | va_start(args, fmt); |
| 138 | |
| 139 | std::string s; |
| 140 | AppendFormat(&s, |
| 141 | "\n\n" |
| 142 | "#\n" |
| 143 | "# Fatal error in: %s, line %d\n" |
| 144 | "# last system error: %u\n" |
| 145 | "# Check failed: %s", |
| 146 | file, line, LAST_SYSTEM_ERROR, message); |
| 147 | |
| 148 | if (*fmt == CheckArgType::kCheckOp) { |
| 149 | // This log message was generated by RTC_CHECK_OP, so we have to complete |
| 150 | // the error message using the operands that have been passed as the first |
| 151 | // two arguments. |
| 152 | fmt++; |
| 153 | |
| 154 | std::string s1, s2; |
| 155 | if (ParseArg(&args, &fmt, &s1) && ParseArg(&args, &fmt, &s2)) |
| 156 | AppendFormat(&s, " (%s vs. %s)\n# ", s1.c_str(), s2.c_str()); |
| 157 | } else { |
| 158 | s.append("\n# "); |
| 159 | } |
| 160 | |
| 161 | // Append all the user-supplied arguments to the message. |
| 162 | while (ParseArg(&args, &fmt, &s)) |
| 163 | ; |
| 164 | |
| 165 | va_end(args); |
| 166 | |
| 167 | WriteFatalLogAndAbort(s); |
| 168 | } |
| 169 | #else // RTC_CHECK_MSG_ENABLED |
| 170 | RTC_NORETURN void FatalLog(const char* file, int line) { |
| 171 | std::string s; |
| 172 | AppendFormat(&s, |
| 173 | "\n\n" |
| 174 | "#\n" |
| 175 | "# Fatal error in: %s, line %d\n" |
| 176 | "# last system error: %u\n" |
| 177 | "# Check failed.\n" |
| 178 | "# ", |
| 179 | file, line, LAST_SYSTEM_ERROR); |
| 180 | WriteFatalLogAndAbort(s); |
| 181 | } |
| 182 | #endif // RTC_CHECK_MSG_ENABLED |
| 183 | |
| 184 | #if RTC_DCHECK_IS_ON |
| 185 | |
| 186 | RTC_NORETURN void UnreachableCodeReached(const char* file, int line) { |
| 187 | std::string s; |
| 188 | AppendFormat(&s, |
| 189 | "\n\n" |
| 190 | "#\n" |
| 191 | "# Unreachable code reached: %s, line %d\n" |
| 192 | "# last system error: %u\n" |
| 193 | "# ", |
| 194 | file, line, LAST_SYSTEM_ERROR); |
| 195 | WriteFatalLogAndAbort(s); |
| 196 | } |
| 197 | |
| 198 | #else // !RTC_DCHECK_IS_ON |
| 199 | |
| 200 | RTC_NORETURN void UnreachableCodeReached() { |
| 201 | std::string s; |
| 202 | AppendFormat(&s, |
| 203 | "\n\n" |
| 204 | "#\n" |
| 205 | "# Unreachable code reached (file and line unknown)\n" |
| 206 | "# last system error: %u\n" |
| 207 | "# ", |
| 208 | LAST_SYSTEM_ERROR); |
| 209 | WriteFatalLogAndAbort(s); |
| 210 | } |
| 211 | |
| 212 | #endif // !RTC_DCHECK_IS_ON |
| 213 | |
| 214 | } // namespace webrtc_checks_impl |
| 215 | } // namespace rtc |
| 216 | |
| 217 | // Function to call from the C version of the RTC_CHECK and RTC_DCHECK macros. |
| 218 | RTC_NORETURN void rtc_FatalMessage(const char* file, |
| 219 | int line, |
| 220 | const char* msg) { |
| 221 | #if RTC_CHECK_MSG_ENABLED |
| 222 | static constexpr rtc::webrtc_checks_impl::CheckArgType t[] = { |
| 223 | rtc::webrtc_checks_impl::CheckArgType::kEnd}; |
| 224 | rtc::webrtc_checks_impl::FatalLog(file, line, msg, t); |
| 225 | #else |
| 226 | rtc::webrtc_checks_impl::FatalLog(file, line); |
| 227 | #endif |
| 228 | } |