| 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 | #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 = 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 | vsnprintf(&((*s)[size]), predicted_length + 1, fmt, args); |
| 54 | } |
| 55 | va_end(args); |
| 56 | } |
| 57 | } |
| 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::kVoidP: |
| 100 | AppendFormat(s, "%p", va_arg(*args, const void*)); |
| 101 | break; |
| 102 | default: |
| 103 | s->append("[Invalid CheckArgType]"); |
| 104 | return false; |
| 105 | } |
| 106 | (*fmt)++; |
| 107 | return true; |
| 108 | } |
| 109 |
| 110 | RTC_NORETURN void FatalLog(const char* file, |
| 111 | int line, |
| 112 | const char* message, |
| 113 | const CheckArgType* fmt, |
| 114 | ...) { |
| 115 | va_list args; |
| 116 | va_start(args, fmt); |
| 117 |
| 118 | std::string s; |
| 119 | AppendFormat(&s, |
| 120 | "\n\n" |
| 121 | "#\n" |
| 122 | "# Fatal error in: %s, line %d\n" |
| 123 | "# last system error: %u\n" |
| 124 | "# Check failed: %s", |
| 125 | file, line, LAST_SYSTEM_ERROR, message); |
| 126 |
| 127 | if (*fmt == CheckArgType::kCheckOp) { |
| 128 | // This log message was generated by RTC_CHECK_OP, so we have to complete |
| 129 | // the error message using the operands that have been passed as the first |
| 130 | // two arguments. |
| 131 | fmt++; |
| 132 |
| 133 | std::string s1, s2; |
| 134 | if (ParseArg(&args, &fmt, &s1) && ParseArg(&args, &fmt, &s2)) |
| 135 | AppendFormat(&s, " (%s vs. %s)\n# ", s1.c_str(), s2.c_str()); |
| 136 | } else { |
| 137 | s.append("\n# "); |
| 138 | } |
| 139 |
| 140 | // Append all the user-supplied arguments to the message. |
| 141 | while (ParseArg(&args, &fmt, &s)) |
| 142 | ; |
| 143 |
| 144 | va_end(args); |
| 145 |
| 146 | const char* output = s.c_str(); |
| 147 |
| 148 | #if defined(WEBRTC_ANDROID) |
| 149 | __android_log_print(ANDROID_LOG_ERROR, RTC_LOG_TAG_ANDROID, "%s\n", output); |
| 150 | #endif |
| 151 |
| 152 | fflush(stdout); |
| 153 | fprintf(stderr, "%s", output); |
| 154 | fflush(stderr); |
| 155 | abort(); |
| 156 | } |
| 157 |
| 158 | } // namespace webrtc_checks_impl |
| 159 | } // namespace rtc |
| 160 |
| 161 | // Function to call from the C version of the RTC_CHECK and RTC_DCHECK macros. |
| 162 | RTC_NORETURN void rtc_FatalMessage(const char* file, int line, |
| 163 | const char* msg) { |
| 164 | static constexpr rtc::webrtc_checks_impl::CheckArgType t[] = { |
| 165 | rtc::webrtc_checks_impl::CheckArgType::kEnd}; |
| 166 | FatalLog(file, line, msg, t); |
| 167 | } |
| 168 |