You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.0 KiB

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