Merge branch 'develop' into feature/bounds-checks

This commit is contained in:
Daviw Williams
2013-05-27 13:00:02 +02:00
7 changed files with 219 additions and 129 deletions

View File

@ -376,7 +376,6 @@ namespace PolyVox
{
//Should never happen
POLYVOX_THROW(std::invalid_argument, "Wrap mode parameter has an unrecognised value.");
return VoxelType();
}
}
}

View File

@ -62,36 +62,117 @@ freely, subject to the following restrictions:
* PolyVox provides basic logging facilities which can be redirected by your application.
*/
#define LOG_DECLARATION(name) \
std::ostream& log(name)(void); \
void set(name)Stream(std::ostream& nameStream);
namespace PolyVox
{
class LogLevels
namespace Impl
{
public:
enum LogLevel
std::ostream*& getTraceStreamInstance();
std::ostream*& getDebugStreamInstance();
std::ostream*& getInfoStreamInstance();
std::ostream*& getWarningStreamInstance();
std::ostream*& getErrorStreamInstance();
std::ostream*& getFatalStreamInstance();
}
/// Get a stream which will consume all input without outputting anything.
std::ostream* getNullStream(void);
// These take pointers rather than references to emphasise that the
// user needs to keep the target alive as long as PolyVox is writing data.
void setTraceStream(std::ostream* pStream);
void setDebugStream(std::ostream* pStream);
void setInfoStream(std::ostream* pStream);
void setWarningStream(std::ostream* pStream);
void setErrorStream(std::ostream* pStream);
void setFatalStream(std::ostream* pStream);
// Automatically appending 'std::endl' as described here: http://stackoverflow.com/a/2179782
struct logTrace
{
logTrace(){}
~logTrace(){*(Impl::getTraceStreamInstance()) << std::endl;}
template<class T>
logTrace &operator<<(const T &x)
{
Debug,
Info,
Warning,
Error,
Fatal
};
*(Impl::getTraceStreamInstance()) << x;
return *this;
}
};
typedef LogLevels::LogLevel LogLevel;
typedef void (*LogHandler)(const std::string& message, LogLevel logLevel);
// Automatically appending 'std::endl' as described here: http://stackoverflow.com/a/2179782
struct logDebug
{
logDebug(){}
~logDebug(){*(Impl::getDebugStreamInstance()) << std::endl;}
LogHandler getLogHandler();
void setLogHandler(LogHandler newHandler);
template<class T>
logDebug &operator<<(const T &x)
{
*(Impl::getDebugStreamInstance()) << x;
return *this;
}
};
// The actual logging function
void log(const std::string& message, LogLevel logLevel);
// Automatically appending 'std::endl' as described here: http://stackoverflow.com/a/2179782
struct logInfo
{
logInfo(){}
~logInfo(){*(Impl::getInfoStreamInstance()) << std::endl;}
// Some handy wrappers
void logDebug (const std::string& message);
void logInfo (const std::string& message);
void logWarning(const std::string& message);
void logError (const std::string& message);
void logFatal (const std::string& message);
template<class T>
logInfo &operator<<(const T &x)
{
*(Impl::getInfoStreamInstance()) << x;
return *this;
}
};
// Automatically appending 'std::endl' as described here: http://stackoverflow.com/a/2179782
struct logWarning
{
logWarning(){}
~logWarning(){*(Impl::getWarningStreamInstance()) << std::endl;}
template<class T>
logWarning &operator<<(const T &x)
{
*(Impl::getWarningStreamInstance()) << x;
return *this;
}
};
// Automatically appending 'std::endl' as described here: http://stackoverflow.com/a/2179782
struct logError
{
logError(){}
~logError(){*(Impl::getErrorStreamInstance()) << std::endl;}
template<class T>
logError &operator<<(const T &x)
{
*(Impl::getErrorStreamInstance()) << x;
return *this;
}
};
// Automatically appending 'std::endl' as described here: http://stackoverflow.com/a/2179782
struct logFatal
{
logFatal(){}
~logFatal(){*(Impl::getFatalStreamInstance()) << std::endl;}
template<class T>
logFatal &operator<<(const T &x)
{
*(Impl::getFatalStreamInstance()) << x;
return *this;
}
};
}
/*
@ -114,14 +195,13 @@ namespace PolyVox
{ \
if (!(condition)) \
{ \
std::stringstream ss; \
ss << std::endl << std::endl; \
ss << " PolyVox Assertion Failed!" << std::endl; \
ss << " =========================" << std::endl; \
ss << " Condition: " << #condition << std::endl; \
ss << " Message: " << (message) << std::endl; \
ss << " Location: " << "Line " << __LINE__ << " of " << __FILE__ << std::endl << std::endl; \
PolyVox::logFatal(ss.str()); \
PolyVox::logFatal() << "\n"; \
PolyVox::logFatal() << " PolyVox Assertion Failed!"; \
PolyVox::logFatal() << " ========================="; \
PolyVox::logFatal() << " Condition: " << #condition; \
PolyVox::logFatal() << " Message: " << (message); \
PolyVox::logFatal() << " Location: " << "Line " << __LINE__ << " of " << __FILE__; \
PolyVox::logFatal() << "\n"; \
POLYVOX_HALT(); \
} \
} while(0) \
@ -176,7 +256,7 @@ namespace PolyVox
*/
#ifdef POLYVOX_THROW_ENABLED
#define POLYVOX_THROW(type, message) \
PolyVox::logError(message); \
PolyVox::logError() << (message); \
throw type((message))
#else
namespace PolyVox
@ -188,7 +268,7 @@ namespace PolyVox
}
#define POLYVOX_THROW(type, message) \
PolyVox::logError(message); \
PolyVox::logError() << (message); \
type except = (type)((message)); \
getThrowHandler()((except), __FILE__, __LINE__)
#endif

View File

@ -243,7 +243,6 @@ namespace PolyVox
{
//Should never happen
POLYVOX_THROW(std::invalid_argument, "Wrap mode parameter has an unrecognised value.");
return VoxelType();
}
}
}

View File

@ -201,7 +201,6 @@ namespace PolyVox
{
//Should never happen
POLYVOX_THROW(std::invalid_argument, "Wrap mode parameter has an unrecognised value.");
return VoxelType();
}
}
}

View File

@ -201,7 +201,6 @@ namespace PolyVox
{
//Should never happen
POLYVOX_THROW(std::invalid_argument, "Wrap mode parameter has an unrecognised value.");
return VoxelType();
}
}
}

View File

@ -25,116 +25,118 @@ freely, subject to the following restrictions:
namespace PolyVox
{
void defaultLogHandler(const std::string& message, LogLevel logLevel)
/**
* \return A pointer to the null stream.
*/
std::ostream* getNullStream(void)
{
switch(logLevel)
// Passing zero to the stream constructor guarentees it will discard all input. See
// here http://stackoverflow.com/a/8244052 and here http://stackoverflow.com/a/6240980
static std::ostream s_NullStream(0);
return &s_NullStream;
}
// These create the global stream instances, created on demand.
namespace Impl
{
std::ostream*& getTraceStreamInstance()
{
case LogLevels::Debug:
{
// Debug messages are not output by this default log handler.
// Provide a custom handler if you want to process them.
break;
}
case LogLevels::Info:
{
std::cout << "Info: " << message.c_str() << std::endl;
break;
}
case LogLevels::Warning:
{
std::cerr << "Warning: " << message.c_str() << std::endl;
break;
}
case LogLevels::Error:
{
std::cerr << "Error: " << message.c_str() << std::endl;
break;
}
case LogLevels::Fatal:
{
std::cerr << "Fatal: " << message.c_str() << std::endl;
break;
}
static std::ostream* s_pTraceStream = getNullStream();
return s_pTraceStream;
}
std::ostream*& getDebugStreamInstance()
{
static std::ostream* s_pDebugStream = getNullStream();
return s_pDebugStream;
}
std::ostream*& getInfoStreamInstance()
{
static std::ostream* s_pInfoStream = &(std::cout);
return s_pInfoStream;
}
std::ostream*& getWarningStreamInstance()
{
static std::ostream* s_pWarningStream = &(std::cerr);
return s_pWarningStream;
}
std::ostream*& getErrorStreamInstance()
{
static std::ostream* s_pErrorStream = &(std::cerr);
return s_pErrorStream;
}
std::ostream*& getFatalStreamInstance()
{
static std::ostream* s_pFatalStream = &(std::cerr);
return s_pFatalStream;
}
}
LogHandler& getLogHandlerInstance()
void setTraceStream(std::ostream* pStream)
{
static LogHandler s_fLogHandler = &defaultLogHandler;
return s_fLogHandler;
Impl::getTraceStreamInstance() = pStream;
}
LogHandler getLogHandler()
void setDebugStream(std::ostream* pStream)
{
return getLogHandlerInstance();
Impl::getDebugStreamInstance() = pStream;
}
void setLogHandler(LogHandler fNewHandler)
void setInfoStream(std::ostream* pStream)
{
getLogHandlerInstance() = fNewHandler;
Impl::getInfoStreamInstance() = pStream;
}
void log(const std::string& message, LogLevel logLevel)
void setWarningStream(std::ostream* pStream)
{
LogHandler logHandler = getLogHandler();
if(logHandler)
{
logHandler(message, logLevel);
}
Impl::getWarningStreamInstance() = pStream;
}
// Some handy wrappers
void logDebug(const std::string& message)
{
log(message, LogLevels::Debug );
void setErrorStream(std::ostream* pStream)
{
Impl::getErrorStreamInstance() = pStream;
}
void logInfo(const std::string& message)
// Fatal stream for logging
std::ostream*& getFatalStreamInstance()
{
log(message, LogLevels::Info);
static std::ostream* s_pFatalStream = &(std::cerr);
return s_pFatalStream;
}
void logWarning(const std::string& message)
void setFatalStream(std::ostream* pStream)
{
log(message, LogLevels::Warning);
}
void logError(const std::string& message)
{
log(message, LogLevels::Error);
}
void logFatal(const std::string& message)
{
log(message, LogLevels::Fatal);
getFatalStreamInstance() = pStream;
}
#ifndef POLYVOX_THROW_ENABLED
void defaultThrowHandler(std::exception& e, const char* file, int line)
{
std::stringstream ss; \
ss << std::endl << std::endl; \
ss << " PolyVox exception thrown!" << std::endl; \
ss << " =========================" << std::endl; \
ss << " PolyVox has tried to throw an exception but it was built without support" << std::endl; \
ss << " for exceptions. In this scenario PolyVox will call a 'throw handler'" << std::endl; \
ss << " and this message is being printed by the default throw handler." << std::endl << std::endl; \
ss << " If you don't want to enable exceptions then you should try to determine why" << std::endl; \
ss << " this exception was thrown and make sure it doesn't happen again. If it was" << std::endl; \
ss << " due to something like an invalid argument to a function then you should be" << std::endl; \
ss << " able to fix it quite easily by validating parameters as appropriate. More" << std::endl; \
ss << " complex exception scenarios (out of memory, etc) might be harder to fix and" << std::endl; \
ss << " you should replace this default handler with something which is more" << std::endl; \
ss << " meaningful to your users." << std::endl << std::endl; \
ss << " Exception details" << std::endl; \
ss << " -----------------" << std::endl; \
ss << " Line: " << line << std::endl; \
ss << " File: " << file << std::endl; \
ss << " Message: " << e.what() << std::endl << std::endl; \
PolyVox::logFatal(ss.str()); \
logFatal() << "\n"; \
logFatal() << " PolyVox exception thrown!"; \
logFatal() << " ========================="; \
logFatal() << " PolyVox has tried to throw an exception but it was built without support"; \
logFatal() << " for exceptions. In this scenario PolyVox will call a 'throw handler'"; \
logFatal() << " and this message is being printed by the default throw handler."; \
logFatal() << "\n"; \
logFatal() << " If you don't want to enable exceptions then you should try to determine why"; \
logFatal() << " this exception was thrown and make sure it doesn't happen again. If it was"; \
logFatal() << " due to something like an invalid argument to a function then you should be"; \
logFatal() << " able to fix it quite easily by validating parameters as appropriate. More"; \
logFatal() << " complex exception scenarios (out of memory, etc) might be harder to fix and"; \
logFatal() << " you should replace this default handler with something which is more"; \
logFatal() << " meaningful to your users."; \
logFatal() << "\n"; \
logFatal() << " Exception details"; \
logFatal() << " -----------------"; \
logFatal() << " Line: " << line; \
logFatal() << " File: " << file; \
logFatal() << " Message: " << e.what(); \
logFatal() << "\n"; \
POLYVOX_HALT(); \
}