Skip to content

Commit

Permalink
Merge a9559cb into 0096176
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrush committed Mar 9, 2021
2 parents 0096176 + a9559cb commit 18b7753
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ 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.

#### 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.
Expand Down
21 changes: 21 additions & 0 deletions src/libs/conduit/conduit_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
61 changes: 43 additions & 18 deletions src/libs/conduit/conduit_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
109 changes: 109 additions & 0 deletions src/tests/conduit/t_conduit_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 18b7753

Please sign in to comment.