Skip to content

Commit

Permalink
Clean up message syntax and message severity.
Browse files Browse the repository at this point in the history
  • Loading branch information
nealkruis committed Feb 6, 2024
1 parent e64774a commit e14ea1c
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 144 deletions.
13 changes: 13 additions & 0 deletions include/btwxt/grid-axis.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ class GridAxis {
void calculate_cubic_spacing_ratios();
void check_grid_sorted();
void check_extrapolation_limits();
[[nodiscard]] std::string make_message(const std::string& message) const
{
return fmt::format("GridAxis '{}': {}", name, message);
}
void send_error(const std::string& message) const
{
courier->send_error(make_message(message));
}
void send_info(const std::string& message) const { courier->send_info(make_message(message)); }
void send_warning(const std::string& message) const
{
courier->send_warning(make_message(message));
}
};

// free functions
Expand Down
15 changes: 11 additions & 4 deletions include/btwxt/messaging.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Copyright (c) 2023 Big Ladder Software LLC. All rights reserved.
* See the LICENSE file for additional terms and conditions. */

#ifndef BTWXT_LOGGING_H_
#define BTWXT_LOGGING_H_
#ifndef BTWXT_MESSAGING_H_
#define BTWXT_MESSAGING_H_

#include <fmt/format.h>

Expand All @@ -12,7 +12,11 @@ namespace Btwxt {

class BtwxtDefaultCourier : public Courier::Courier {
protected:
void receive_error(const std::string& message) override { write_message("ERROR", message); }
void receive_error(const std::string& message) override
{
write_message("ERROR", message);
throw std::runtime_error(message);
}

void receive_warning(const std::string& message) override { write_message("WARNING", message); }

Expand All @@ -23,9 +27,12 @@ class BtwxtDefaultCourier : public Courier::Courier {
virtual void write_message(const std::string& message_type, const std::string& message)
{
std::cout << fmt::format(" [{}] {}", message_type, message) << std::endl;
std::cout
<< " Generated using BtwxtDefaultCourier. Consider deriving your own Courier class!"
<< std::endl;
}
};

} // namespace Btwxt

#endif // define BTWXT_LOGGING_H_
#endif // define BTWXT_MESSAGING_H_
32 changes: 13 additions & 19 deletions src/grid-axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ GridAxis::GridAxis(std::vector<double> values_in,
, courier(courier_in)
{
if (values.empty()) {
courier->send_error(
fmt::format("Cannot create grid axis (name=\"{}\") from a zero-length vector.", name));
send_error("Cannot create grid axis from a zero-length vector.");
}
check_grid_sorted();
check_extrapolation_limits();
Expand All @@ -42,14 +41,13 @@ void GridAxis::set_interpolation_method(InterpolationMethod interpolation_method

void GridAxis::set_extrapolation_method(ExtrapolationMethod extrapolation_method_in)
{
constexpr std::string_view info_format =
"A {} extrapolation method is not valid for grid axis (name=\"{}\") with only {} value. "
"Extrapolation method reset to {}.";
switch (extrapolation_method_in) {
case ExtrapolationMethod::linear: {
if (get_length() == 1) {
extrapolation_method = ExtrapolationMethod::constant;
courier->send_info(fmt::format(info_format, "linear", name, "one", "constant"));
send_warning(
"A linear extrapolation method is not valid for grid axis with only one value. "
"Extrapolation method reset to constant.");
return;
}
break;
Expand All @@ -65,10 +63,8 @@ void GridAxis::calculate_cubic_spacing_ratios()
{
if (get_length() == 1) {
interpolation_method = InterpolationMethod::linear;
courier->send_info(fmt::format(
"A cubic interpolation method is not valid for grid axis (name=\"{}\") with "
"only one value. Interpolation method reset to linear.",
name));
send_warning("A cubic interpolation method is not valid for grid axis with only one value. "
"Interpolation method reset to linear.");
}
if (interpolation_method == InterpolationMethod::linear) {
return;
Expand Down Expand Up @@ -96,24 +92,22 @@ void GridAxis::check_grid_sorted()
{
bool grid_is_sorted = vector_is_valid(values);
if (!grid_is_sorted) {
courier->send_error(fmt::format(
"Grid axis (name=\"{}\") values are not sorted, or have duplicates.", name));
send_error("Values are not sorted, or have duplicates.");
}
}

void GridAxis::check_extrapolation_limits()
{
constexpr std::string_view info_format {
"Grid axis (name=\"{}\") {} extrapolation limit ({}) is within the set of grid axis "
"values. Setting to {} axis value ({})."};
constexpr std::string_view error_format {"{} extrapolation limit ({:.3g}) is within the set of "
"grid axis values ({:.3g}-{:.3g})."};
if (extrapolation_limits.first > values[0]) {
courier->send_info(fmt::format(
info_format, name, "lower", extrapolation_limits.first, "smallest", values[0]));
send_error(fmt::format(
error_format, "Lower", extrapolation_limits.first, values[0], values.back()));
extrapolation_limits.first = values[0];
}
if (extrapolation_limits.second < values.back()) {
courier->send_info(fmt::format(
info_format, name, "upper", extrapolation_limits.second, "largest", values.back()));
send_error(fmt::format(
error_format, "Upper", extrapolation_limits.second, values[0], values.back()));
extrapolation_limits.second = values.back();
}
}
Expand Down
70 changes: 30 additions & 40 deletions src/regular-grid-interpolator-implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ std::size_t RegularGridInterpolatorImplementation::add_grid_point_data_set(
const GridPointDataSet& grid_point_data_set)
{
if (grid_point_data_set.data.size() != number_of_grid_points) {
courier->send_error(fmt::format("Input grid point data set (name=\"{}\") size ({}) does "
"not match number of grid points ({}).",
grid_point_data_set.name,
grid_point_data_set.data.size(),
number_of_grid_points));
send_error(fmt::format(
"GridPointDataSet '{}': Size ({}) does not match number of grid points ({}).",
grid_point_data_set.name,
grid_point_data_set.data.size(),
number_of_grid_points));
}
grid_point_data_sets.emplace_back(grid_point_data_set);
number_of_grid_point_data_sets++;
Expand All @@ -73,7 +73,7 @@ std::size_t RegularGridInterpolatorImplementation::add_grid_point_data_set(
void RegularGridInterpolatorImplementation::set_target(const std::vector<double>& target_in)
{
if (target_in.size() != number_of_grid_axes) {
courier->send_error(
send_error(
fmt::format("Target (size={}) and grid (size={}) do not have the same dimensions.",
target_in.size(),
number_of_grid_axes));
Expand All @@ -95,8 +95,7 @@ void RegularGridInterpolatorImplementation::set_target(const std::vector<double>
const std::vector<double>& RegularGridInterpolatorImplementation::get_target() const
{
if (!target_is_set) {
courier->send_warning(
fmt::format("The current target was requested, but no target has been set."));
send_error("The current target was requested, but no target has been set.");
}
return target;
}
Expand All @@ -111,11 +110,10 @@ void RegularGridInterpolatorImplementation::clear_target()
std::vector<double> RegularGridInterpolatorImplementation::get_results() const
{
if (number_of_grid_point_data_sets == 0u) {
courier->send_warning(
fmt::format("There are no grid point data sets. No results returned."));
send_error("There are no grid point data sets. No results returned.");
}
if (!target_is_set) {
courier->send_warning(fmt::format("Results were requested, but no target has been set."));
send_error("Results were requested, but no target has been set.");
}
return results;
}
Expand All @@ -131,8 +129,7 @@ void RegularGridInterpolatorImplementation::normalize_grid_point_data_sets_at_ta
const double scalar)
{
if (!target_is_set) {
courier->send_error(
fmt::format("Cannot normalize grid point data sets. No target has been set."));
send_error("Cannot normalize grid point data sets. No target has been set.");
}
for (std::size_t data_set_index = 0; data_set_index < number_of_grid_point_data_sets;
++data_set_index) {
Expand All @@ -146,8 +143,8 @@ double RegularGridInterpolatorImplementation::normalize_grid_point_data_set_at_t
std::size_t data_set_index, double scalar)
{
if (!target_is_set) {
courier->send_error(fmt::format(
"Cannot normalize grid point data set (name=\"{}\"). No target has been set.",
send_error(fmt::format(
"GridPointDataSet '{}': Cannot normalize grid point data set. No target has been set.",
grid_point_data_sets[data_set_index].name));
}
// create a scalar which represents the product of the inverted normalization factor and the
Expand All @@ -165,8 +162,8 @@ void RegularGridInterpolatorImplementation::normalize_grid_point_data_set(
{
auto& data_set = grid_point_data_sets[data_set_index].data;
if (scalar == 0.0) {
courier->send_error(
fmt::format("Attempt to normalize grid point data set (name=\"{}\") by zero.",
send_error(
fmt::format("GridPointDataSet '{}': Attempt to normalize grid point data set by zero.",
grid_point_data_sets[data_set_index].name));
}
scalar = 1.0 / scalar;
Expand Down Expand Up @@ -295,11 +292,8 @@ void RegularGridInterpolatorImplementation::set_axis_sizes()
{
number_of_grid_points = 1;
for (std::size_t axis_index = number_of_grid_axes; axis_index-- > 0;) {
std::size_t length = grid_axes[axis_index].get_length();
if (length == 0) {
courier->send_error(fmt::format("Grid axis (name=\"{}\") has zero length.",
grid_axes[axis_index].name));
}
std::size_t length =
grid_axes[axis_index].get_length(); // length > 0 ensured by GridAxis constructor
grid_axis_lengths[axis_index] = length;
grid_axis_step_size[axis_index] = number_of_grid_points;
number_of_grid_points *= length;
Expand Down Expand Up @@ -410,26 +404,27 @@ void RegularGridInterpolatorImplementation::consolidate_methods()
methods = get_interpolation_methods();
if (target_is_set) {
auto extrapolation_methods = get_extrapolation_methods();
constexpr std::string_view exception_format {"The target ({:.3g}) is {} the extrapolation "
"limit ({:.3g}) for grid axis (name=\"{}\")."};
constexpr std::string_view exception_format {
"GridAxis '{}': The target ({:.3g}) is {} the extrapolation "
"limit ({:.3g})."};
for (std::size_t axis_index = 0; axis_index < number_of_grid_axes; axis_index++) {
switch (target_bounds_status[axis_index]) {
case TargetBoundsStatus::extrapolate_low:
case TargetBoundsStatus::extrapolate_high:
methods[axis_index] = extrapolation_methods[axis_index];
break;
case TargetBoundsStatus::below_lower_extrapolation_limit:
courier->send_error(fmt::format(exception_format,
target[axis_index],
"below",
get_extrapolation_limits(axis_index).first,
grid_axes[axis_index].name));
send_error(fmt::format(exception_format,
grid_axes[axis_index].name,
target[axis_index],
"below",
get_extrapolation_limits(axis_index).first));
case TargetBoundsStatus::above_upper_extrapolation_limit:
courier->send_error(fmt::format(exception_format,
target[axis_index],
"above",
get_extrapolation_limits(axis_index).second,
grid_axes[axis_index].name));
send_error(fmt::format(exception_format,
grid_axes[axis_index].name,
target[axis_index],
"above",
get_extrapolation_limits(axis_index).second));
case TargetBoundsStatus::interpolate:
break;
}
Expand All @@ -444,12 +439,7 @@ void RegularGridInterpolatorImplementation::consolidate_methods()

void RegularGridInterpolatorImplementation::set_hypercube(std::vector<Method> methods_in)
{
if (methods_in.size() != number_of_grid_axes) {
courier->send_error(fmt::format("Error setting hypercube. Methods vector (size={}) and "
"grid (size={}) do not have the dimensions.",
methods_in.size(),
number_of_grid_axes));
}
assert(methods_in.size() == number_of_grid_axes);
std::size_t previous_size = hypercube.size();
std::vector<std::vector<int>> options(number_of_grid_axes, {0, 1});
reset_hypercube = false;
Expand Down
21 changes: 15 additions & 6 deletions src/regular-grid-interpolator-implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,23 @@ class RegularGridInterpolatorImplementation {

void set_axis_floor_grid_point_index(std::size_t axis_index);

void check_axis_index(std::size_t axis_index, const std::string_view& action_description) const
[[nodiscard]] std::string make_message(const std::string& message) const
{
return fmt::format("RegularGridInterpolator '{}': {}", name, message);
}
void send_error(const std::string& message) const
{
courier->send_error(make_message(message));
}

void check_axis_index(std::size_t axis_index, const std::string& action_description) const
{
if (axis_index > number_of_grid_axes - 1) {
courier->send_error(
fmt::format("Unable to {} for axis (index={}). Number of grid axes = {}.",
action_description,
axis_index,
number_of_grid_axes));
send_error(fmt::format(
"Axis index, {}, does not exist. Unable to {}. Number of grid axes = {}.",
axis_index,
action_description,
number_of_grid_axes));
}
}
};
Expand Down
Loading

0 comments on commit e14ea1c

Please sign in to comment.