Index: webrtc/api/rtcerror.cc |
diff --git a/webrtc/api/rtcerror.cc b/webrtc/api/rtcerror.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f9dd782be0c263c5f26b590a2ddacc7561ae3f19 |
--- /dev/null |
+++ b/webrtc/api/rtcerror.cc |
@@ -0,0 +1,113 @@ |
+/* |
+ * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/api/rtcerror.h" |
+ |
+#include "webrtc/base/arraysize.h" |
+ |
+namespace { |
+ |
+static const char* const kRTCErrorTypeNames[] = { |
+ "NONE", |
+ "UNSUPPORTED_OPERATION", |
+ "UNSUPPORTED_PARAMETER", |
+ "INVALID_PARAMETER", |
+ "INVALID_RANGE", |
+ "SYNTAX_ERROR", |
+ "INVALID_STATE", |
+ "INVALID_MODIFICATION", |
+ "NETWORK_ERROR", |
+ "RESOURCE_EXHAUSTED", |
+ "INTERNAL_ERROR", |
+}; |
+static_assert(static_cast<int>(webrtc::RTCErrorType::INTERNAL_ERROR) == |
+ (arraysize(kRTCErrorTypeNames) - 1), |
+ "kRTCErrorTypeNames must have as many strings as RTCErrorType " |
+ "has values."); |
+ |
+} // namespace |
+ |
+namespace webrtc { |
+ |
+RTCError::~RTCError() { |
+ // If we hold a message string that was built, rather than a static string, |
+ // we need to delete it. |
+ if (!static_message_) { |
+ delete message_.str_; |
+ } |
+} |
+ |
+// static |
+const RTCError& RTCError::OK() { |
+ static const RTCError ok; |
+ return ok; |
+} |
+ |
+const char* RTCError::message() const { |
+ if (static_message_) { |
+ return message_.static_; |
+ } else { |
+ return message_.str_->c_str(); |
+ } |
+} |
+ |
+void RTCError::set_message(const char* message) { |
+ if (!static_message_) { |
+ delete message_.str_; |
+ static_message_ = true; |
+ } |
+ message_.static_ = message; |
+} |
+ |
+void RTCError::set_message(const std::string& message) { |
+ if (static_message_) { |
+ message_.str_ = new std::string(message); |
+ static_message_ = false; |
+ } else { |
+ *message_.str_ = message; |
+ } |
+} |
+ |
+std::ostream& operator<<(std::ostream& stream, RTCErrorType error) { |
+ int index = static_cast<int>(error); |
+ return stream << kRTCErrorTypeNames[index]; |
+} |
+ |
+webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, |
+ const std::string& message, |
+ rtc::LoggingSeverity severity) { |
+ // "No error" shouldn't be logged as an error. |
+ RTC_DCHECK(type != RTCErrorType::NONE); |
+ rtc::LogMessage(__FILE__, __LINE__, severity).stream() << message << " (" |
+ << type << ")"; |
+ return webrtc::RTCError(type, message); |
+} |
+ |
+webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, |
+ const char* message, |
+ rtc::LoggingSeverity severity) { |
+ // "No error" shouldn't be logged as an error. |
+ RTC_DCHECK(type != RTCErrorType::NONE); |
+ rtc::LogMessage(__FILE__, __LINE__, severity).stream() << message << " (" |
+ << type << ")"; |
+ return webrtc::RTCError(type, message); |
+} |
+ |
+webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, |
+ const char* message) { |
+ return CreateAndLogError(type, message, rtc::LS_ERROR); |
+} |
+ |
+webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, |
+ const std::string& message) { |
+ return CreateAndLogError(type, message, rtc::LS_ERROR); |
+} |
+ |
+} // namespace webrtc |