Move RtcEventLogParseStatus and related macros to a separate file and buildtarget.
Bug: webrtc:14801
Change-Id: I204f46d47933a1ad95682042bc7009e421109ba1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/296660
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Commit-Queue: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39505}
diff --git a/logging/BUILD.gn b/logging/BUILD.gn
index 05c1f53..3dffbef 100644
--- a/logging/BUILD.gn
+++ b/logging/BUILD.gn
@@ -33,6 +33,12 @@
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 @@
]
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 a9b0c08..f0cdf8a 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 fc87faf..89dbb19 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 <typename T>
- 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 <typename T>
-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 0000000..d675aba
--- /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 <string>
+#include <vector>
+
+#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 <typename T>
+ 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 <typename T>
+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 a793efb..a5e901d 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;