Skip to content

Commit

Permalink
Improve logs when product is not found in catalog. (#1305)
Browse files Browse the repository at this point in the history
User is informed of the wrong key and is provided a list of valid inputs.
  • Loading branch information
TotoGaz authored Feb 11, 2021
1 parent f4e7faf commit f033e76
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/coreComponents/codingUtilities/StringUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ namespace geosx
namespace stringutilities
{

string toLower( string const & input )
{
string output;
output.resize( input.size() );
auto const toLowerCase = []( unsigned char c ) { return std::tolower( c ); };
std::transform( input.cbegin(), input.cend(), output.begin(), toLowerCase );
return output;
}

/**
* String tokenizing function
**/
Expand Down
15 changes: 11 additions & 4 deletions src/coreComponents/codingUtilities/StringUtilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
#ifndef GEOSX_CODINGUTILITIES_STRINGUTILITIES_HPP_
#define GEOSX_CODINGUTILITIES_STRINGUTILITIES_HPP_

#include <cxxabi.h>
#include <algorithm>
#include <cstring>
#include <cxxabi.h>
#include <iomanip>
#include <map>
#include <memory>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <map>


#include "common/DataTypes.hpp"
Expand All @@ -50,6 +50,13 @@ inline bool streq( char const * const strA, string const & strB )
inline bool streq( char const * const strA, char const * const strB )
{ return !strcmp( strA, strB ); }

/**
* @brief Return a copy of the string in lower case.
* @param input The input string which is not modified.
* @return A new string instance.
*/
string toLower( string const & input );

/**
* @brief Join strings or other printable objects with a delimiter.
* @tparam S type of delimiter, usually char, char const * or string
Expand Down
56 changes: 50 additions & 6 deletions src/coreComponents/dataRepository/ObjectCatalog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
*/

#include "common/Logger.hpp"
#include "codingUtilities/StringUtilities.hpp"
#include "LvArray/src/system.hpp"

#include <unordered_map>

#include <iostream>
#include <list>
#include <memory>
#include <unordered_map>

#ifndef OBJECTCATALOGVERBOSE
/**
Expand Down Expand Up @@ -144,17 +145,60 @@ class CatalogInterface
return getCatalog().count( objectTypeName );
}

/**
* @brief Returns the product keys of the catalog. Keys are sorted in alphabetical order, case insensitive.
* @return An STL container.
*/
static std::list< typename CatalogType::key_type > getKeys()
{
std::list< typename CatalogType::key_type > keys;
for( typename CatalogType::value_type const & pair: getCatalog() )
{
keys.push_back( pair.first );
}
auto const cmp = []( string const & a,
string const & b ) -> bool
{
return stringutilities::toLower( a ) < stringutilities::toLower( b );
};
keys.sort( cmp );

return keys;
}

/**
* @brief Static method to create a new object that derives from BASETYPE
* @param[in] objectTypeName the key to the catalog entry that is able to create
* the correct type.
* @param[in] objectTypeName the key to the catalog entry that is able to create the correct type.
* @param args these are the arguments to the constructor of the target type
* @return passes a unique_ptr<BASETYPE> to the newly allocated class.
*
* @note The simulation is killed if the builder is not found.
*/
//START_SPHINX_2
static std::unique_ptr< BASETYPE > factory( std::string const & objectTypeName, ARGS... args )
static std::unique_ptr< BASETYPE > factory( std::string const & objectTypeName,
ARGS... args )
{
return getCatalog().at( objectTypeName ).get()->allocate( args ... );
// We stop the simulation if the product is not found
if( !hasKeyName( objectTypeName ) )
{
std::list< typename CatalogType::key_type > keys = getKeys();
string const tmp = stringutilities::strjoin( keys.cbegin(), keys.cend(), ",\n" );

string errorMsg = "Could not find keyword \"" + objectTypeName + "\" in this context. ";
errorMsg += "Please be sure that all your keywords are properly spelled or that input file parameters have not changed.\n";
errorMsg += "All available keys are: [\n" + tmp + "\n]";
GEOSX_ERROR( errorMsg );
}

// We also stop the simulation if the builder is not here.
CatalogInterface< BASETYPE, ARGS... > const * builder = getCatalog().at( objectTypeName ).get();
if( builder == nullptr )
{
const string errorMsg = "\"" + objectTypeName + "\" could be found. But the builder is invalid.\n";
GEOSX_ERROR( errorMsg );
}

return builder->allocate( args ... );
}
//STOP_SPHINX

Expand Down

0 comments on commit f033e76

Please sign in to comment.