diff --git a/rerun_cpp/src/rerun.hpp b/rerun_cpp/src/rerun.hpp index 9496c00153cb..290c763a252a 100644 --- a/rerun_cpp/src/rerun.hpp +++ b/rerun_cpp/src/rerun.hpp @@ -7,6 +7,6 @@ #include "rerun/datatypes.hpp" // Rerun API. +#include "rerun/error.hpp" #include "rerun/recording_stream.hpp" #include "rerun/sdk_info.hpp" -#include "rerun/status.hpp" diff --git a/rerun_cpp/src/rerun/status.cpp b/rerun_cpp/src/rerun/error.cpp similarity index 63% rename from rerun_cpp/src/rerun/status.cpp rename to rerun_cpp/src/rerun/error.cpp index 95ded7fe24b0..3d52affcc900 100644 --- a/rerun_cpp/src/rerun/status.cpp +++ b/rerun_cpp/src/rerun/error.cpp @@ -1,4 +1,4 @@ -#include "status.hpp" +#include "error.hpp" #include @@ -6,15 +6,15 @@ namespace rerun { static StatusLogHandler global_log_handler = nullptr; static void* global_log_handler_user_data = nullptr; - Status::Status(const rr_error& status) - : code(static_cast(status.code)), description(status.description) {} + Error::Error(const rr_error& status) + : code(static_cast(status.code)), description(status.description) {} - void Status::set_log_handler(StatusLogHandler handler, void* userdata) { + void Error::set_log_handler(StatusLogHandler handler, void* userdata) { global_log_handler = handler; global_log_handler_user_data = userdata; } - void Status::log() const { + void Error::log() const { if (global_log_handler) { global_log_handler(*this, global_log_handler_user_data); } else { diff --git a/rerun_cpp/src/rerun/status.hpp b/rerun_cpp/src/rerun/error.hpp similarity index 82% rename from rerun_cpp/src/rerun/status.hpp rename to rerun_cpp/src/rerun/error.hpp index 912f49aef7ab..894e75f2ff8a 100644 --- a/rerun_cpp/src/rerun/status.hpp +++ b/rerun_cpp/src/rerun/error.hpp @@ -13,7 +13,7 @@ namespace rerun { /// Status codes returned by the SDK as part of `Status`. /// /// Category codes are used to group errors together, but are never returned directly. - enum class StatusCode : uint32_t { + enum class ErrorCode : uint32_t { Ok = 0, // Invalid argument errors. @@ -38,46 +38,46 @@ namespace rerun { }; /// Callback function type for log handlers. - using StatusLogHandler = void (*)(const class Status& status, void* userdata); + using StatusLogHandler = void (*)(const class Error& status, void* userdata); /// Status outcome object (success or error) returned for fallible operations. /// /// Converts to `true` for success, `false` for failure. - class [[nodiscard]] Status { + class [[nodiscard]] Error { public: /// Result code for the given operation. - StatusCode code = StatusCode::Ok; + ErrorCode code = ErrorCode::Ok; /// Human readable description of the error. std::string description; public: - Status() = default; + Error() = default; - Status(StatusCode _code, std::string _description) + Error(ErrorCode _code, std::string _description) : code(_code), description(std::move(_description)) {} /// Construct from a C status object. - Status(const rr_error& status); + Error(const rr_error& status); /// Returns true if the code is `Ok`. bool is_ok() const { - return code == StatusCode::Ok; + return code == ErrorCode::Ok; } /// Returns true if the code is not `Ok`. bool is_err() const { - return code != StatusCode::Ok; + return code != ErrorCode::Ok; } - /// Sets global log handler called for `log` and `log_error`. + /// Sets global log handler called for `log` and `log_on_failure`. /// /// The default will log to stderr. /// /// @param handler The handler to call, or `nullptr` to reset to the default. /// @param userdata Userdata pointer that will be passed to each invocation of the handler. /// - /// @see log, log_error + /// @see log, log_on_failure static void set_log_handler(StatusLogHandler handler, void* userdata = nullptr); /// Logs this status via the global log handler. @@ -88,7 +88,7 @@ namespace rerun { /// Logs this status if failed via the global log handler. /// /// @see set_log_handler - void log_error() const { + void log_on_failure() const { if (is_err()) { log(); } diff --git a/rerun_cpp/src/rerun/recording_stream.cpp b/rerun_cpp/src/rerun/recording_stream.cpp index 86cf4028f29e..6ebd2b61c53e 100644 --- a/rerun_cpp/src/rerun/recording_stream.cpp +++ b/rerun_cpp/src/rerun/recording_stream.cpp @@ -29,7 +29,7 @@ namespace rerun { rr_error status = {}; this->_id = rr_recording_stream_new(&store_info, &status); - Status(status).log_error(); + Error(status).log_on_failure(); } RecordingStream::RecordingStream(RecordingStream&& other) @@ -75,13 +75,13 @@ namespace rerun { } } - Status RecordingStream::connect(const char* tcp_addr, float flush_timeout_sec) { + Error RecordingStream::connect(const char* tcp_addr, float flush_timeout_sec) { rr_error status = {}; rr_recording_stream_connect(_id, tcp_addr, flush_timeout_sec, &status); return status; } - Status RecordingStream::save(const char* path) { + Error RecordingStream::save(const char* path) { rr_error status = {}; rr_recording_stream_save(_id, path, &status); return status; @@ -91,7 +91,7 @@ namespace rerun { rr_recording_stream_flush_blocking(_id); } - Status RecordingStream::try_log_data_row( + Error RecordingStream::try_log_data_row( const char* entity_path, size_t num_instances, size_t num_data_cells, const DataCell* data_cells ) { @@ -99,8 +99,8 @@ namespace rerun { std::vector c_data_cells(num_data_cells); for (size_t i = 0; i < num_data_cells; i++) { if (data_cells[i].buffer == nullptr) { - return Status( - StatusCode::UnexpectedNullArgument, + return Error( + ErrorCode::UnexpectedNullArgument, "DataCell buffer is null for cell " + std::to_string(i) ); } diff --git a/rerun_cpp/src/rerun/recording_stream.hpp b/rerun_cpp/src/rerun/recording_stream.hpp index 3fa2b23ad23b..88f85fe3096e 100644 --- a/rerun_cpp/src/rerun/recording_stream.hpp +++ b/rerun_cpp/src/rerun/recording_stream.hpp @@ -5,7 +5,7 @@ #include #include "data_cell.hpp" -#include "status.hpp" +#include "error.hpp" // TODO(#2873): Should avoid leaking arrow headers. #include @@ -102,12 +102,12 @@ namespace rerun { /// timeout, and can cause a call to `flush` to block indefinitely. /// /// This function returns immediately. - Status connect(const char* tcp_addr = "127.0.0.1:9876", float flush_timeout_sec = 2.0); + Error connect(const char* tcp_addr = "127.0.0.1:9876", float flush_timeout_sec = 2.0); /// Stream all log-data to a given file. /// /// This function returns immediately. - Status save(const char* path); + Error save(const char* path); /// Initiates a flush the batching pipeline and waits for it to propagate. /// @@ -125,7 +125,7 @@ namespace rerun { /// TODO(andreas): Would be nice if this were able to combine both log_archetype and /// log_components! /// - /// Logs any failure via `Status::log_error` + /// Logs any failure via `Error::log_on_failure` template void log(const char* entity_path, const T& archetype) { log_archetype(entity_path, archetype); @@ -135,17 +135,17 @@ namespace rerun { /// /// Prefer this interface for ease of use over the more general `log_components` interface. /// - /// Logs any failure via `Status::log_error` + /// Logs any failure via `Error::log_on_failure` template void log_archetype(const char* entity_path, const T& archetype) { - try_log_archetype(entity_path, archetype).log_error(); + try_log_archetype(entity_path, archetype).log_on_failure(); } /// Logs a an archetype, returning an error on failure. /// /// @see log_archetype template - Status try_log_archetype(const char* entity_path, const T& archetype) { + Error try_log_archetype(const char* entity_path, const T& archetype) { const auto data_cells = archetype.to_data_cells().ValueOrDie(); // TODO: Error handling. return try_log_data_row( entity_path, @@ -164,17 +164,17 @@ namespace rerun { /// /// TODO(andreas): More documentation, examples etc. /// - /// Logs any failure via `Status::log_error` + /// Logs any failure via `Error::log_on_failure` template void log_components(const char* entity_path, const Ts&... component_array) { - try_log_components(entity_path, component_array...).log_error(); + try_log_components(entity_path, component_array...).log_on_failure(); } /// Logs a list of component arrays, returning an error on failure. /// /// @see log_components template - Status try_log_components(const char* entity_path, const Ts&... component_array) { + Error try_log_components(const char* entity_path, const Ts&... component_array) { // TODO(andreas): Handle splats. const size_t num_instances = size_of_first_collection(component_array...); @@ -194,7 +194,7 @@ namespace rerun { /// /// I.e. logs a number of components arrays (each with a same number of instances) to a /// single entity path. - Status try_log_data_row( + Error try_log_data_row( const char* entity_path, size_t num_instances, size_t num_data_cells, const DataCell* data_cells ); diff --git a/rerun_cpp/tests/status_check.hpp b/rerun_cpp/tests/error_check.hpp similarity index 59% rename from rerun_cpp/tests/status_check.hpp rename to rerun_cpp/tests/error_check.hpp index 977e1f1d3bed..85bf6149f04b 100644 --- a/rerun_cpp/tests/status_check.hpp +++ b/rerun_cpp/tests/error_check.hpp @@ -1,35 +1,35 @@ #include -#include +#include /// Checks if the given operation logs the expected status code. template auto check_logged_status( - Op operation, rerun::StatusCode expected_status_code = rerun::StatusCode::Ok + Op operation, rerun::ErrorCode expected_status_code = rerun::ErrorCode::Ok ) { - static rerun::Status last_logged_status; + static rerun::Error last_logged_status; // Set to Ok since nothing logged indicates success for most methods. - last_logged_status.code = rerun::StatusCode::Ok; + last_logged_status.code = rerun::ErrorCode::Ok; - rerun::Status::set_log_handler( - [](const rerun::Status& status, void* userdata) { - *static_cast(userdata) = status; + rerun::Error::set_log_handler( + [](const rerun::Error& status, void* userdata) { + *static_cast(userdata) = status; }, &last_logged_status ); struct CheckOnDestruct { - rerun::StatusCode expected_status_code; + rerun::ErrorCode expected_status_code; ~CheckOnDestruct() { CHECK(last_logged_status.code == expected_status_code); - if (expected_status_code != rerun::StatusCode::Ok) { + if (expected_status_code != rerun::ErrorCode::Ok) { CHECK(last_logged_status.description.length() > 0); } else { CHECK(last_logged_status.description.length() == 0); } - rerun::Status::set_log_handler(nullptr); + rerun::Error::set_log_handler(nullptr); } } check = {expected_status_code}; diff --git a/rerun_cpp/tests/recording_stream.cpp b/rerun_cpp/tests/recording_stream.cpp index 7d89ae56b070..72bf40a1e37c 100644 --- a/rerun_cpp/tests/recording_stream.cpp +++ b/rerun_cpp/tests/recording_stream.cpp @@ -6,7 +6,7 @@ #include #pragma GCC diagnostic pop -#include "status_check.hpp" +#include "error_check.hpp" #include #include @@ -61,7 +61,7 @@ SCENARIO("RecordingStream can be created, destroyed and lists correct properties THEN("creating a new stream logs a null argument error") { check_logged_status( [&] { rr::RecordingStream stream(nullptr, kind); }, - rr::StatusCode::UnexpectedNullArgument + rr::ErrorCode::UnexpectedNullArgument ); } } @@ -69,7 +69,7 @@ SCENARIO("RecordingStream can be created, destroyed and lists correct properties THEN("creating a new stream logs an invalid string argument error") { check_logged_status( [&] { rr::RecordingStream stream("\xc3\x28", kind); }, - rr::StatusCode::InvalidStringArgument + rr::ErrorCode::InvalidStringArgument ); } } @@ -193,7 +193,7 @@ SCENARIO("RecordingStream can log to file", TEST_TAG) { AND_GIVEN("a nullptr for the save path") { THEN("then the save call returns a null argument error") { - CHECK(stream0->save(nullptr).code == rr::StatusCode::UnexpectedNullArgument); + CHECK(stream0->save(nullptr).code == rr::ErrorCode::UnexpectedNullArgument); } } AND_GIVEN("valid save path " << test_rrd0) { @@ -202,7 +202,7 @@ SCENARIO("RecordingStream can log to file", TEST_TAG) { THEN("then the save call fails") { CHECK( stream0->save(test_rrd0.c_str()).code == - rr::StatusCode::RecordingStreamSaveFailure + rr::ErrorCode::RecordingStreamSaveFailure ); } } @@ -263,14 +263,14 @@ SCENARIO("RecordingStream can log to file", TEST_TAG) { void test_logging_to_connection(const char* address, rr::RecordingStream& stream) { AND_GIVEN("a nullptr for the socket address") { THEN("then the connect call returns a null argument error") { - CHECK(stream.connect(nullptr, 0.0f).code == rr::StatusCode::UnexpectedNullArgument); + CHECK(stream.connect(nullptr, 0.0f).code == rr::ErrorCode::UnexpectedNullArgument); } } AND_GIVEN("an invalid address for the socket address") { THEN("then the save call fails") { CHECK( stream.connect("definitely not valid!", 0.0f).code == - rr::StatusCode::InvalidSocketAddress + rr::ErrorCode::InvalidSocketAddress ); } } @@ -337,11 +337,11 @@ SCENARIO("Recording stream handles invalid logging gracefully", TEST_TAG) { rr::RecordingStream stream("test"); AND_GIVEN("an invalid path") { - auto variant = GENERATE(table({ - std::tuple("////", rr::StatusCode::InvalidEntityPath), - std::tuple( + auto variant = GENERATE(table({ + std::tuple("////", rr::ErrorCode::InvalidEntityPath), + std::tuple( nullptr, - rr::StatusCode::UnexpectedNullArgument + rr::ErrorCode::UnexpectedNullArgument ), })); const auto [path, error] = variant; @@ -386,7 +386,7 @@ SCENARIO("Recording stream handles invalid logging gracefully", TEST_TAG) { THEN("try_log_data_row fails with UnexpectedNullArgument") { CHECK( stream.try_log_data_row(path, 1, 1, &cell).code == - rr::StatusCode::UnexpectedNullArgument + rr::ErrorCode::UnexpectedNullArgument ); } } @@ -399,7 +399,7 @@ SCENARIO("Recording stream handles invalid logging gracefully", TEST_TAG) { THEN("try_log_data_row fails with UnexpectedNullArgument") { CHECK( stream.try_log_data_row(path, 1, 1, &cell).code == - rr::StatusCode::UnexpectedNullArgument + rr::ErrorCode::UnexpectedNullArgument ); } } @@ -413,7 +413,7 @@ SCENARIO("Recording stream handles invalid logging gracefully", TEST_TAG) { THEN("try_log_data_row fails with ArrowIpcMessageParsingFailure") { CHECK( stream.try_log_data_row(path, 1, 1, &cell).code == - rr::StatusCode::ArrowIpcMessageParsingFailure + rr::ErrorCode::ArrowIpcMessageParsingFailure ); } }