From e8123471df900524cc6f59d8ef97482203e8136e Mon Sep 17 00:00:00 2001 From: Cyrus Harrison Date: Mon, 8 Mar 2021 16:28:48 -0800 Subject: [PATCH 1/2] added methods to get active info,warning, and error handlers --- CHANGELOG.md | 10 +++ src/libs/conduit/conduit_utils.cpp | 21 +++++ src/libs/conduit/conduit_utils.hpp | 61 +++++++++----- src/tests/conduit/t_conduit_utils.cpp | 109 ++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca39be69d..ba97530d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,20 @@ and this project aspires to adhere to [Semantic Versioning](https://semver.org/s ### Added +#### General +- Added `conduit::utils::info_handler()`, `conduit::utils::warning_handler()`, and `conduit::utils::error_handler()` methods, which provide access to the currently registered info, warning, and error handlers. +- Added DataType::index_t method. Creates a DataType instance that describes an `index_t`, which is an alias to either `int32`, or `int 64` controlled by the `CONDUIT_INDEX_32` compile time option. +- Added several more methods to Python DataType interface + #### Relay - Added Relay HDF5 support for reading and writing to an HDF5 dataset with offset. - Added `conduit::relay::io::hdf5::read_info` which allows you to obtain metadata from an HDF5 file. +### Fixed + +#### General +- Fixed missing implementation of DataType::is_index_t + ## [0.7.1] - Released 2021-02-11 diff --git a/src/libs/conduit/conduit_utils.cpp b/src/libs/conduit/conduit_utils.cpp index 68d1ee08f..aa330fdcd 100644 --- a/src/libs/conduit/conduit_utils.cpp +++ b/src/libs/conduit/conduit_utils.cpp @@ -104,6 +104,13 @@ set_info_handler(void(*on_info) conduit_on_info = on_info; } +//----------------------------------------------------------------------------- +conduit_info_handler +info_handler() +{ + return conduit_on_info; +} + //----------------------------------------------------------------------------- void handle_info(const std::string &msg, @@ -140,6 +147,13 @@ set_warning_handler(void(*on_warning) conduit_on_warning = on_warning; } +//----------------------------------------------------------------------------- +conduit_warning_handler +warning_handler() +{ + return conduit_on_warning; +} + //----------------------------------------------------------------------------- void handle_warning(const std::string &msg, @@ -177,6 +191,13 @@ set_error_handler(void(*on_error) conduit_on_error = on_error; } +//----------------------------------------------------------------------------- +conduit_error_handler +error_handler() +{ + return conduit_on_error; +} + //----------------------------------------------------------------------------- void handle_error(const std::string &msg, diff --git a/src/libs/conduit/conduit_utils.hpp b/src/libs/conduit/conduit_utils.hpp index c3ed26015..a31d00d4d 100644 --- a/src/libs/conduit/conduit_utils.hpp +++ b/src/libs/conduit/conduit_utils.hpp @@ -164,7 +164,6 @@ class Node; //----------------------------------------------------------------------------- namespace utils { - //----------------------------------------------------------------------------- /// Primary interface used by the conduit API when an info message is issued /// This simply dispatches the message to the currently configured info handler. @@ -175,12 +174,11 @@ namespace utils int line); //----------------------------------------------------------------------------- -/// Allows other libraries to provide an alternate info message handler. +/// Info handler callback function type //----------------------------------------------------------------------------- - void CONDUIT_API set_info_handler( void(*on_info) - (const std::string &, + typedef void(*conduit_info_handler)(const std::string &, const std::string &, - int)); + int); //----------------------------------------------------------------------------- /// Default info message handler, which prints the message to std::cout; @@ -189,6 +187,16 @@ namespace utils const std::string &file, int line); +//----------------------------------------------------------------------------- +/// Allows other libraries to provide an alternate info message handler. +//----------------------------------------------------------------------------- + void CONDUIT_API set_info_handler(conduit_info_handler on_info); + +//----------------------------------------------------------------------------- +/// Returns the active info message handler. +//----------------------------------------------------------------------------- + conduit_info_handler CONDUIT_API info_handler(); + //----------------------------------------------------------------------------- /// Primary interface used by the conduit API when a warning is issued. /// This simply dispatches the warning to the currently configured warning handler. @@ -199,12 +207,11 @@ namespace utils int line); //----------------------------------------------------------------------------- -/// Allows other libraries to provide an alternate warning handler. +/// Warning handler callback function type //----------------------------------------------------------------------------- - void CONDUIT_API set_warning_handler( void(*on_error) - (const std::string &, - const std::string &, - int)); + typedef void(*conduit_warning_handler)(const std::string &, + const std::string &, + int); //----------------------------------------------------------------------------- /// Default warning handler, which throws a conduit::Error exception. @@ -213,6 +220,15 @@ namespace utils const std::string &file, int line); +//----------------------------------------------------------------------------- +/// Allows other libraries to provide an alternate warning handler. +//----------------------------------------------------------------------------- + void CONDUIT_API set_warning_handler(conduit_warning_handler on_warning); + +//----------------------------------------------------------------------------- +/// Returns the active warning message handler. +//----------------------------------------------------------------------------- + conduit_warning_handler CONDUIT_API warning_handler(); //----------------------------------------------------------------------------- /// Primary interface used by the conduit API when an error occurs. @@ -223,20 +239,29 @@ namespace utils const std::string &file, int line); -//----------------------------------------------------------------------------- -/// Allows other libraries to provide an alternate error handler. -//----------------------------------------------------------------------------- - void CONDUIT_API set_error_handler( void(*on_error) - (const std::string &, - const std::string &, - int)); - //----------------------------------------------------------------------------- /// Default error handler, which throws a conduit::Error exception. //----------------------------------------------------------------------------- void CONDUIT_API default_error_handler(const std::string &msg, const std::string &file, int line); +//----------------------------------------------------------------------------- +/// Warning handler callback function type +//----------------------------------------------------------------------------- + typedef void(*conduit_error_handler)(const std::string &, + const std::string &, + int); + +//----------------------------------------------------------------------------- +/// Allows other libraries to provide an alternate error handler. +//----------------------------------------------------------------------------- + void CONDUIT_API set_error_handler(conduit_error_handler on_error); + +//----------------------------------------------------------------------------- +/// Returns the active warning message handler. +//----------------------------------------------------------------------------- + conduit_error_handler CONDUIT_API error_handler(); + //----------------------------------------------------------------------------- /// Helpers for common string splitting operations. diff --git a/src/tests/conduit/t_conduit_utils.cpp b/src/tests/conduit/t_conduit_utils.cpp index 976c3baae..4fc1859ca 100644 --- a/src/tests/conduit/t_conduit_utils.cpp +++ b/src/tests/conduit/t_conduit_utils.cpp @@ -24,6 +24,10 @@ bool info_occured = false; bool warning_occured = false; bool error_occured = false; +bool other_info_occured = false; +bool other_warning_occured = false; +bool other_error_occured = false; + //----------------------------------------------------------------------------- void print_msg(const std::string &msg, @@ -66,6 +70,38 @@ my_error_handler(const std::string &msg, error_occured = true; } +//----------------------------------------------------------------------------- +void +my_other_info_handler(const std::string &msg, + const std::string &file, + int line) +{ + print_msg(msg,file,line); + other_info_occured = true; +} + + +//----------------------------------------------------------------------------- +void +my_other_warning_handler(const std::string &msg, + const std::string &file, + int line) +{ + print_msg(msg,file,line); + other_warning_occured = true; +} + +//----------------------------------------------------------------------------- +void +my_other_error_handler(const std::string &msg, + const std::string &file, + int line) +{ + print_msg(msg,file,line); + other_error_occured = true; +} + + //----------------------------------------------------------------------------- TEST(conduit_utils, error_constructors) { @@ -148,6 +184,79 @@ TEST(conduit_utils, override_error) } +//----------------------------------------------------------------------------- +TEST(conduit_utils, handler_defaults_are_the_defaults) +{ + conduit::utils::conduit_info_handler on_info = conduit::utils::info_handler(); + conduit::utils::conduit_info_handler on_info_default = &conduit::utils::default_info_handler; + EXPECT_EQ(on_info,on_info_default); + + conduit::utils::conduit_warning_handler on_warning = conduit::utils::warning_handler(); + conduit::utils::conduit_warning_handler on_warning_default = &conduit::utils::default_warning_handler; + EXPECT_EQ(on_warning,on_warning_default); + + conduit::utils::conduit_error_handler on_error = conduit::utils::error_handler(); + conduit::utils::conduit_error_handler on_error_default = &conduit::utils::default_error_handler; + EXPECT_EQ(on_error,on_error_default); + + + EXPECT_NE(on_info,on_error); + EXPECT_NE(on_info,on_warning); + EXPECT_NE(on_error,on_warning); + +} + +//----------------------------------------------------------------------------- +TEST(conduit_utils, handler_defaults_push_pop) +{ + // change to other handler + conduit::utils::set_info_handler(my_other_info_handler); + conduit::utils::set_warning_handler(my_other_warning_handler); + conduit::utils::set_error_handler(my_other_error_handler); + + // ---------- + // demo push / pop like swing to default + // ---------- + conduit::utils::conduit_info_handler curr_on_info = conduit::utils::info_handler(); + conduit::utils::conduit_warning_handler curr_on_warning = conduit::utils::warning_handler(); + conduit::utils::conduit_error_handler curr_on_error = conduit::utils::error_handler(); + + // set to default + conduit::utils::set_info_handler(conduit::utils::default_info_handler); + conduit::utils::set_warning_handler(conduit::utils::default_warning_handler); + conduit::utils::set_error_handler(conduit::utils::default_error_handler); + + EXPECT_THROW(utils::handle_warning("ERROR!",__FILE__,__LINE__), + conduit::Error); + + // set to curr + conduit::utils::set_info_handler(curr_on_info); + conduit::utils::set_warning_handler(curr_on_warning); + conduit::utils::set_error_handler(curr_on_error); + + EXPECT_FALSE(other_info_occured); + EXPECT_FALSE(other_warning_occured); + EXPECT_FALSE(other_error_occured); + + utils::handle_info("INFO!",__FILE__,__LINE__); + utils::handle_warning("WARNING!",__FILE__,__LINE__); + utils::handle_error("ERROR!",__FILE__,__LINE__); + + EXPECT_TRUE(other_info_occured); + EXPECT_TRUE(other_warning_occured); + EXPECT_TRUE(other_error_occured); + + + // back set to default (for other tests !!!!) + conduit::utils::set_info_handler(conduit::utils::default_info_handler); + conduit::utils::set_warning_handler(conduit::utils::default_warning_handler); + conduit::utils::set_error_handler(conduit::utils::default_error_handler); + +} + + + + //----------------------------------------------------------------------------- TEST(conduit_utils, escape_special_chars) { From a9559cba9497f5c46bb1177cec5c82e04d2ac721 Mon Sep 17 00:00:00 2001 From: Cyrus Harrison Date: Mon, 8 Mar 2021 16:30:57 -0800 Subject: [PATCH 2/2] fix extra changelog stuff --- CHANGELOG.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba97530d5..3eeb13201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,18 +10,11 @@ and this project aspires to adhere to [Semantic Versioning](https://semver.org/s #### General - Added `conduit::utils::info_handler()`, `conduit::utils::warning_handler()`, and `conduit::utils::error_handler()` methods, which provide access to the currently registered info, warning, and error handlers. -- Added DataType::index_t method. Creates a DataType instance that describes an `index_t`, which is an alias to either `int32`, or `int 64` controlled by the `CONDUIT_INDEX_32` compile time option. -- Added several more methods to Python DataType interface #### Relay - Added Relay HDF5 support for reading and writing to an HDF5 dataset with offset. - Added `conduit::relay::io::hdf5::read_info` which allows you to obtain metadata from an HDF5 file. -### Fixed - -#### General -- Fixed missing implementation of DataType::is_index_t - ## [0.7.1] - Released 2021-02-11