From de0afb7df60d8206704f6f3c6ddbf982823a389f Mon Sep 17 00:00:00 2001 From: philipel Date: Wed, 8 Mar 2023 12:32:14 +0100 Subject: [PATCH] Move RtcEventLogParseStatus and related macros to a separate file and buildtarget. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:14801 Change-Id: I204f46d47933a1ad95682042bc7009e421109ba1 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/296660 Reviewed-by: Björn Terelius Commit-Queue: Philip Eliasson Cr-Commit-Position: refs/heads/main@{#39505} --- logging/BUILD.gn | 7 + .../events/rtc_event_field_encoding_parser.cc | 1 + .../events/rtc_event_field_encoding_parser.h | 79 +--------- .../events/rtc_event_log_parse_status.h | 148 ++++++++++++++++++ logging/rtc_event_log/rtc_event_log_parser.cc | 55 ------- 5 files changed, 157 insertions(+), 133 deletions(-) create mode 100644 logging/rtc_event_log/events/rtc_event_log_parse_status.h diff --git a/logging/BUILD.gn b/logging/BUILD.gn index 05c1f53f5d..3dffbef039 100644 --- a/logging/BUILD.gn +++ b/logging/BUILD.gn @@ -33,6 +33,12 @@ rtc_source_set("rtc_event_log_api") { deps = [ "../api/rtc_event_log" ] } +rtc_source_set("rtc_event_log_parse_status") { + sources = [ "rtc_event_log/events/rtc_event_log_parse_status.h" ] + deps = [ "../rtc_base:checks" ] + absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] +} + rtc_library("rtc_event_field") { sources = [ "rtc_event_log/events/fixed_length_encoding_parameters_v3.cc", @@ -47,6 +53,7 @@ rtc_library("rtc_event_field") { ] deps = [ + ":rtc_event_log_parse_status", ":rtc_event_number_encodings", "../api:array_view", "../api/rtc_event_log", diff --git a/logging/rtc_event_log/events/rtc_event_field_encoding_parser.cc b/logging/rtc_event_log/events/rtc_event_field_encoding_parser.cc index a9b0c08307..f0cdf8a0f7 100644 --- a/logging/rtc_event_log/events/rtc_event_field_encoding_parser.cc +++ b/logging/rtc_event_log/events/rtc_event_field_encoding_parser.cc @@ -15,6 +15,7 @@ #include "absl/types/optional.h" #include "logging/rtc_event_log/encoder/var_int.h" #include "logging/rtc_event_log/events/rtc_event_field_encoding.h" +#include "logging/rtc_event_log/events/rtc_event_log_parse_status.h" #include "rtc_base/bitstream_reader.h" #include "rtc_base/checks.h" diff --git a/logging/rtc_event_log/events/rtc_event_field_encoding_parser.h b/logging/rtc_event_log/events/rtc_event_field_encoding_parser.h index fc87faf611..89dbb19298 100644 --- a/logging/rtc_event_log/events/rtc_event_field_encoding_parser.h +++ b/logging/rtc_event_log/events/rtc_event_field_encoding_parser.h @@ -16,84 +16,7 @@ #include "absl/strings/string_view.h" #include "logging/rtc_event_log/events/rtc_event_field_encoding.h" - -// TODO(terelius): Compared to a generic 'Status' class, this -// class allows us additional information about the context -// in which the error occurred. This is currently limited to -// the source location (file and line), but we plan on adding -// information about the event and field name being parsed. -// If/when we start using absl::Status in WebRTC, consider -// whether payloads would be an appropriate alternative. -class RtcEventLogParseStatus { - template - friend class RtcEventLogParseStatusOr; - - public: - static RtcEventLogParseStatus Success() { return RtcEventLogParseStatus(); } - static RtcEventLogParseStatus Error(absl::string_view error, - absl::string_view file, - int line) { - return RtcEventLogParseStatus(error, file, line); - } - - bool ok() const { return error_.empty(); } - ABSL_DEPRECATED("Use ok() instead") explicit operator bool() const { - return ok(); - } - - std::string message() const { return error_; } - - private: - RtcEventLogParseStatus() : error_() {} - RtcEventLogParseStatus(absl::string_view error, - absl::string_view file, - int line) - : error_(std::string(error) + " (" + std::string(file) + ": " + - std::to_string(line) + ")") {} - - std::string error_; -}; - -template -class RtcEventLogParseStatusOr { - public: - RtcEventLogParseStatusOr(RtcEventLogParseStatus status) // NOLINT - : status_(status), value_() {} - RtcEventLogParseStatusOr(const T& value) // NOLINT - : status_(), value_(value) {} - - bool ok() const { return status_.ok(); } - - std::string message() const { return status_.message(); } - - RtcEventLogParseStatus status() const { return status_; } - - const T& value() const { - RTC_DCHECK(ok()); - return value_; - } - - T& value() { - RTC_DCHECK(ok()); - return value_; - } - - static RtcEventLogParseStatusOr Error(absl::string_view error, - absl::string_view file, - int line) { - return RtcEventLogParseStatusOr(error, file, line); - } - - private: - RtcEventLogParseStatusOr() : status_() {} - RtcEventLogParseStatusOr(absl::string_view error, - absl::string_view file, - int line) - : status_(error, file, line), value_() {} - - RtcEventLogParseStatus status_; - T value_; -}; +#include "logging/rtc_event_log/events/rtc_event_log_parse_status.h" namespace webrtc { diff --git a/logging/rtc_event_log/events/rtc_event_log_parse_status.h b/logging/rtc_event_log/events/rtc_event_log_parse_status.h new file mode 100644 index 0000000000..d675aba7ab --- /dev/null +++ b/logging/rtc_event_log/events/rtc_event_log_parse_status.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef LOGGING_RTC_EVENT_LOG_EVENTS_RTC_EVENT_LOG_PARSE_STATUS_H_ +#define LOGGING_RTC_EVENT_LOG_EVENTS_RTC_EVENT_LOG_PARSE_STATUS_H_ + +#include +#include + +#include "absl/strings/string_view.h" + +#define RTC_PARSE_CHECK_OR_RETURN(X) \ + do { \ + if (!(X)) \ + return ParsedRtcEventLog::ParseStatus::Error(#X, __FILE__, __LINE__); \ + } while (0) + +#define RTC_PARSE_CHECK_OR_RETURN_MESSAGE(X, M) \ + do { \ + if (!(X)) \ + return ParsedRtcEventLog::ParseStatus::Error((M), __FILE__, __LINE__); \ + } while (0) + +#define RTC_PARSE_CHECK_OR_RETURN_OP(OP, X, Y) \ + do { \ + if (!((X)OP(Y))) \ + return ParsedRtcEventLog::ParseStatus::Error(#X #OP #Y, __FILE__, \ + __LINE__); \ + } while (0) + +#define RTC_PARSE_CHECK_OR_RETURN_EQ(X, Y) \ + RTC_PARSE_CHECK_OR_RETURN_OP(==, X, Y) + +#define RTC_PARSE_CHECK_OR_RETURN_NE(X, Y) \ + RTC_PARSE_CHECK_OR_RETURN_OP(!=, X, Y) + +#define RTC_PARSE_CHECK_OR_RETURN_LT(X, Y) RTC_PARSE_CHECK_OR_RETURN_OP(<, X, Y) + +#define RTC_PARSE_CHECK_OR_RETURN_LE(X, Y) \ + RTC_PARSE_CHECK_OR_RETURN_OP(<=, X, Y) + +#define RTC_PARSE_CHECK_OR_RETURN_GT(X, Y) RTC_PARSE_CHECK_OR_RETURN_OP(>, X, Y) + +#define RTC_PARSE_CHECK_OR_RETURN_GE(X, Y) \ + RTC_PARSE_CHECK_OR_RETURN_OP(>=, X, Y) + +#define RTC_PARSE_WARN_AND_RETURN_SUCCESS_IF(X, M) \ + do { \ + if (X) { \ + RTC_LOG(LS_WARNING) << (M); \ + return ParsedRtcEventLog::ParseStatus::Success(); \ + } \ + } while (0) + +#define RTC_RETURN_IF_ERROR(X) \ + do { \ + const ParsedRtcEventLog::ParseStatus _rtc_parse_status(X); \ + if (!_rtc_parse_status.ok()) { \ + return _rtc_parse_status; \ + } \ + } while (0) + +// TODO(terelius): Compared to a generic 'Status' class, this +// class allows us additional information about the context +// in which the error occurred. This is currently limited to +// the source location (file and line), but we plan on adding +// information about the event and field name being parsed. +// If/when we start using absl::Status in WebRTC, consider +// whether payloads would be an appropriate alternative. +class RtcEventLogParseStatus { + template + friend class RtcEventLogParseStatusOr; + + public: + static RtcEventLogParseStatus Success() { return RtcEventLogParseStatus(); } + static RtcEventLogParseStatus Error(absl::string_view error, + absl::string_view file, + int line) { + return RtcEventLogParseStatus(error, file, line); + } + + bool ok() const { return error_.empty(); } + ABSL_DEPRECATED("Use ok() instead") explicit operator bool() const { + return ok(); + } + + std::string message() const { return error_; } + + private: + RtcEventLogParseStatus() : error_() {} + RtcEventLogParseStatus(absl::string_view error, + absl::string_view file, + int line) + : error_(std::string(error) + " (" + std::string(file) + ": " + + std::to_string(line) + ")") {} + + std::string error_; +}; + +template +class RtcEventLogParseStatusOr { + public: + RtcEventLogParseStatusOr(RtcEventLogParseStatus status) // NOLINT + : status_(status), value_() {} + RtcEventLogParseStatusOr(const T& value) // NOLINT + : status_(), value_(value) {} + + bool ok() const { return status_.ok(); } + + std::string message() const { return status_.message(); } + + RtcEventLogParseStatus status() const { return status_; } + + const T& value() const { + RTC_DCHECK(ok()); + return value_; + } + + T& value() { + RTC_DCHECK(ok()); + return value_; + } + + static RtcEventLogParseStatusOr Error(absl::string_view error, + absl::string_view file, + int line) { + return RtcEventLogParseStatusOr(error, file, line); + } + + private: + RtcEventLogParseStatusOr() : status_() {} + RtcEventLogParseStatusOr(absl::string_view error, + absl::string_view file, + int line) + : status_(error, file, line), value_() {} + + RtcEventLogParseStatus status_; + T value_; +}; + +#endif // LOGGING_RTC_EVENT_LOG_EVENTS_RTC_EVENT_LOG_PARSE_STATUS_H_ diff --git a/logging/rtc_event_log/rtc_event_log_parser.cc b/logging/rtc_event_log/rtc_event_log_parser.cc index a793efb5a0..a5e901d8ac 100644 --- a/logging/rtc_event_log/rtc_event_log_parser.cc +++ b/logging/rtc_event_log/rtc_event_log_parser.cc @@ -45,61 +45,6 @@ #include "rtc_base/protobuf_utils.h" #include "rtc_base/system/file_wrapper.h" -// These macros were added to convert existing code using RTC_CHECKs -// to returning a Status object instead. Macros are necessary (over -// e.g. helper functions) since we want to return from the current -// function. -#define RTC_PARSE_CHECK_OR_RETURN(X) \ - do { \ - if (!(X)) \ - return ParsedRtcEventLog::ParseStatus::Error(#X, __FILE__, __LINE__); \ - } while (0) - -#define RTC_PARSE_CHECK_OR_RETURN_MESSAGE(X, M) \ - do { \ - if (!(X)) \ - return ParsedRtcEventLog::ParseStatus::Error((M), __FILE__, __LINE__); \ - } while (0) - -#define RTC_PARSE_CHECK_OR_RETURN_OP(OP, X, Y) \ - do { \ - if (!((X)OP(Y))) \ - return ParsedRtcEventLog::ParseStatus::Error(#X #OP #Y, __FILE__, \ - __LINE__); \ - } while (0) - -#define RTC_PARSE_CHECK_OR_RETURN_EQ(X, Y) \ - RTC_PARSE_CHECK_OR_RETURN_OP(==, X, Y) - -#define RTC_PARSE_CHECK_OR_RETURN_NE(X, Y) \ - RTC_PARSE_CHECK_OR_RETURN_OP(!=, X, Y) - -#define RTC_PARSE_CHECK_OR_RETURN_LT(X, Y) RTC_PARSE_CHECK_OR_RETURN_OP(<, X, Y) - -#define RTC_PARSE_CHECK_OR_RETURN_LE(X, Y) \ - RTC_PARSE_CHECK_OR_RETURN_OP(<=, X, Y) - -#define RTC_PARSE_CHECK_OR_RETURN_GT(X, Y) RTC_PARSE_CHECK_OR_RETURN_OP(>, X, Y) - -#define RTC_PARSE_CHECK_OR_RETURN_GE(X, Y) \ - RTC_PARSE_CHECK_OR_RETURN_OP(>=, X, Y) - -#define RTC_PARSE_WARN_AND_RETURN_SUCCESS_IF(X, M) \ - do { \ - if (X) { \ - RTC_LOG(LS_WARNING) << (M); \ - return ParsedRtcEventLog::ParseStatus::Success(); \ - } \ - } while (0) - -#define RTC_RETURN_IF_ERROR(X) \ - do { \ - const ParsedRtcEventLog::ParseStatus _rtc_parse_status(X); \ - if (!_rtc_parse_status.ok()) { \ - return _rtc_parse_status; \ - } \ - } while (0) - using webrtc_event_logging::ToSigned; using webrtc_event_logging::ToUnsigned;