OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2017 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 #include "webrtc/api/rtcerror.h" |
| 12 |
| 13 #include "webrtc/base/arraysize.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 static const char* const kRTCErrorTypeNames[] = { |
| 18 "NONE", |
| 19 "UNSUPPORTED_OPERATION", |
| 20 "UNSUPPORTED_PARAMETER", |
| 21 "INVALID_PARAMETER", |
| 22 "INVALID_RANGE", |
| 23 "SYNTAX_ERROR", |
| 24 "INVALID_STATE", |
| 25 "INVALID_MODIFICATION", |
| 26 "NETWORK_ERROR", |
| 27 "RESOURCE_EXHAUSTED", |
| 28 "INTERNAL_ERROR", |
| 29 }; |
| 30 static_assert(static_cast<int>(webrtc::RTCErrorType::INTERNAL_ERROR) == |
| 31 (arraysize(kRTCErrorTypeNames) - 1), |
| 32 "kRTCErrorTypeNames must have as many strings as RTCErrorType " |
| 33 "has values."); |
| 34 |
| 35 } // namespace |
| 36 |
| 37 namespace webrtc { |
| 38 |
| 39 RTCError::~RTCError() { |
| 40 // If we hold a message string that was built, rather than a static string, |
| 41 // we need to delete it. |
| 42 if (!static_message_) { |
| 43 delete message_.str_; |
| 44 } |
| 45 } |
| 46 |
| 47 // static |
| 48 const RTCError& RTCError::OK() { |
| 49 static const RTCError ok; |
| 50 return ok; |
| 51 } |
| 52 |
| 53 const char* RTCError::message() const { |
| 54 if (static_message_) { |
| 55 return message_.static_; |
| 56 } else { |
| 57 return message_.str_->c_str(); |
| 58 } |
| 59 } |
| 60 |
| 61 void RTCError::set_message(const char* message) { |
| 62 if (!static_message_) { |
| 63 delete message_.str_; |
| 64 static_message_ = true; |
| 65 } |
| 66 message_.static_ = message; |
| 67 } |
| 68 |
| 69 void RTCError::set_message(const std::string& message) { |
| 70 if (static_message_) { |
| 71 message_.str_ = new std::string(message); |
| 72 static_message_ = false; |
| 73 } else { |
| 74 *message_.str_ = message; |
| 75 } |
| 76 } |
| 77 |
| 78 std::ostream& operator<<(std::ostream& stream, RTCErrorType error) { |
| 79 int index = static_cast<int>(error); |
| 80 return stream << kRTCErrorTypeNames[index]; |
| 81 } |
| 82 |
| 83 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, |
| 84 const std::string& message, |
| 85 rtc::LoggingSeverity severity) { |
| 86 // "No error" shouldn't be logged as an error. |
| 87 RTC_DCHECK(type != RTCErrorType::NONE); |
| 88 rtc::LogMessage(__FILE__, __LINE__, severity).stream() << message << " (" |
| 89 << type << ")"; |
| 90 return webrtc::RTCError(type, message); |
| 91 } |
| 92 |
| 93 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, |
| 94 const char* message, |
| 95 rtc::LoggingSeverity severity) { |
| 96 // "No error" shouldn't be logged as an error. |
| 97 RTC_DCHECK(type != RTCErrorType::NONE); |
| 98 rtc::LogMessage(__FILE__, __LINE__, severity).stream() << message << " (" |
| 99 << type << ")"; |
| 100 return webrtc::RTCError(type, message); |
| 101 } |
| 102 |
| 103 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, |
| 104 const char* message) { |
| 105 return CreateAndLogError(type, message, rtc::LS_ERROR); |
| 106 } |
| 107 |
| 108 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, |
| 109 const std::string& message) { |
| 110 return CreateAndLogError(type, message, rtc::LS_ERROR); |
| 111 } |
| 112 |
| 113 } // namespace webrtc |
OLD | NEW |