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}
This commit is contained in:
philipel 2023-03-08 12:32:14 +01:00 committed by WebRTC LUCI CQ
parent a0be70a87c
commit de0afb7df6
5 changed files with 157 additions and 133 deletions

View file

@ -33,6 +33,12 @@ rtc_source_set("rtc_event_log_api") {
deps = [ "../api/rtc_event_log" ] 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") { rtc_library("rtc_event_field") {
sources = [ sources = [
"rtc_event_log/events/fixed_length_encoding_parameters_v3.cc", "rtc_event_log/events/fixed_length_encoding_parameters_v3.cc",
@ -47,6 +53,7 @@ rtc_library("rtc_event_field") {
] ]
deps = [ deps = [
":rtc_event_log_parse_status",
":rtc_event_number_encodings", ":rtc_event_number_encodings",
"../api:array_view", "../api:array_view",
"../api/rtc_event_log", "../api/rtc_event_log",

View file

@ -15,6 +15,7 @@
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "logging/rtc_event_log/encoder/var_int.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_field_encoding.h"
#include "logging/rtc_event_log/events/rtc_event_log_parse_status.h"
#include "rtc_base/bitstream_reader.h" #include "rtc_base/bitstream_reader.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"

View file

@ -16,84 +16,7 @@
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "logging/rtc_event_log/events/rtc_event_field_encoding.h" #include "logging/rtc_event_log/events/rtc_event_field_encoding.h"
#include "logging/rtc_event_log/events/rtc_event_log_parse_status.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_;
};
namespace webrtc { namespace webrtc {

View file

@ -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_

View file

@ -45,61 +45,6 @@
#include "rtc_base/protobuf_utils.h" #include "rtc_base/protobuf_utils.h"
#include "rtc_base/system/file_wrapper.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::ToSigned;
using webrtc_event_logging::ToUnsigned; using webrtc_event_logging::ToUnsigned;