Commit 47bbe1d5 authored by David Moxey's avatar David Moxey

Convert ErrorUtil to a struct

parent 2c7ef689
///////////////////////////////////////////////////////////////////////////////
//
// File ErrorUtil.cpp
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: error related utilities
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <LibUtilities/BasicUtils/ErrorUtil.hpp>
namespace Nektar {
// Defines default outstream in library.
std::ostream *ErrorUtil::m_outStream = &std::cerr;
}
......@@ -37,8 +37,8 @@
#include <iostream>
#include <stdexcept>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <sstream>
#include <LibUtilities/LibUtilitiesDeclspec.h>
#if defined(NEKTAR_USE_MPI)
......@@ -49,46 +49,56 @@
#include <execinfo.h>
#endif
namespace ErrorUtil
namespace Nektar
{
static boost::optional<std::ostream&> outStream;
inline static void SetErrorStream(std::ostream& o)
{
outStream = o;
}
inline static bool HasCustomErrorStream()
struct ErrorUtil
{
class NekError : public std::runtime_error
{
return outStream ? true : false;
}
public:
NekError(const std::string& message) : std::runtime_error(message)
{
}
};
enum ErrType
{
efatal,
ewarning
};
};
class NekError : public std::runtime_error
inline static void SetErrorStream(std::ostream& o)
{
public:
NekError(const std::string& message) : std::runtime_error(message) {}
};
inline static void Error(ErrType type, const char *routine, int lineNumber, const char *msg, unsigned int level, bool DoComm = false)
m_outStream = &o;
}
inline static bool HasCustomErrorStream()
{
// The user of outStream is primarily for the unit tests.
// The unit tests often generate errors on purpose to make sure
// invalid usage is flagged appropriately. Printing the error
// messages to cerr made the unit test output hard to parse.
std::string baseMsg = std::string("Level ") +
boost::lexical_cast<std::string>(level) +
std::string(" assertion violation\n") +
return m_outStream == &std::cerr;
}
inline static void Error(ErrType type,
const char *routine,
int lineNumber,
const char *msg,
unsigned int level,
bool DoComm = false)
{
// The user of outStream is primarily for the unit tests. The unit
// tests often generate errors on purpose to make sure invalid usage is
// flagged appropriately. Printing the error messages to cerr made the
// unit test output hard to parse.
std::stringstream ss;
ss << "Level " << level << " assertion violation" << std::endl;
#if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
std::string("Where : ") + boost::lexical_cast<std::string>(routine) + std::string("[") + boost::lexical_cast<std::string>(lineNumber) + std::string("]\n") + std::string("Message : ") +
ss << "Where : " routine << "[" << lineNumber << "]" << std::endl;
#endif
msg;
ss << msg;
std::string baseMsg = ss.str();
// Default rank is zero. If MPI used and initialised, populate with
// the correct rank. Messages are only printed on rank zero.
......@@ -128,17 +138,8 @@ namespace ErrorUtil
case efatal:
if (!rank)
{
if (outStream)
{
(*outStream) << btMessage;
(*outStream) << "Fatal : " << baseMsg << std::endl;
}
else
{
std::cerr << btMessage;
std::cerr << std::endl << "Fatal : " << baseMsg
<< std::endl;
}
(*m_outStream) << btMessage;
(*m_outStream) << "Fatal : " << baseMsg << std::endl;
}
#if defined(NEKTAR_USE_MPI) && !defined(NEKTAR_USE_CWIPI)
......@@ -155,20 +156,12 @@ namespace ErrorUtil
case ewarning:
if (!rank)
{
if (outStream)
{
(*outStream) << btMessage;
(*outStream) << "Warning: " << baseMsg << std::endl;
}
else
{
std::cerr << btMessage;
std::cerr << "Warning: " << baseMsg << std::endl;
}
(*m_outStream) << btMessage;
(*m_outStream) << "Warning: " << baseMsg << std::endl;
}
break;
default:
std::cerr << "Unknown warning type: " << baseMsg << std::endl;
(*m_outStream) << "Unknown warning type: " << baseMsg << std::endl;
}
}
......@@ -181,7 +174,10 @@ namespace ErrorUtil
{
Error(type, routine, lineNumber, msg, 0);
}
} // end of namespace
private:
static std::ostream *m_outStream;
};
/// Assert Level 0 -- Fundamental assert which
/// is used whether in FULLDEBUG, DEBUG or OPT
......@@ -252,5 +248,7 @@ namespace ErrorUtil
#define WARNINGL2(condition,msg)
#endif //NEKTAR_FULLDEBUG
}
#endif //ERRORUTIL_HPP
......@@ -256,9 +256,7 @@ public:
return it.first;
}
}
std::string errstr = "Module '"
+ boost::lexical_cast<std::string>(pDesc)
+ "' is not known.";
std::string errstr = "Module '" + pDesc + "' is not known.";
ASSERTL0(false, errstr);
}
......
......@@ -42,6 +42,7 @@
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread/locks.hpp>
#include <boost/lexical_cast.hpp>
#include <LibUtilities/BasicUtils/ErrorUtil.hpp>
......
......@@ -41,6 +41,7 @@ SET(BasicUtilsSources
./BasicUtils/CompressData.cpp
./BasicUtils/CsvIO.cpp
./BasicUtils/Equation.cpp
./BasicUtils/ErrorUtil.cpp
./BasicUtils/FieldIO.cpp
./BasicUtils/FieldIOXml.cpp
./BasicUtils/FileSystem.cpp
......
......@@ -40,6 +40,7 @@
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/cuthill_mckee_ordering.hpp>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment