Reworking the logging system.

This commit is contained in:
Daviw Williams 2013-05-14 16:52:16 +02:00
parent e405b46b61
commit 1010052ea6
11 changed files with 134 additions and 165 deletions

View File

@ -21,7 +21,6 @@ freely, subject to the following restrictions:
distribution. distribution.
*******************************************************************************/ *******************************************************************************/
#include "PolyVoxCore/Log.h"
#include "PolyVoxCore/MaterialDensityPair.h" #include "PolyVoxCore/MaterialDensityPair.h"
#include "PolyVoxCore/LargeVolume.h" #include "PolyVoxCore/LargeVolume.h"
#include "PolyVoxCore/LowPassFilter.h" #include "PolyVoxCore/LowPassFilter.h"
@ -47,32 +46,8 @@ using namespace std;
using namespace PolyVox; using namespace PolyVox;
using namespace std; using namespace std;
void exampleLog(string message, int severity)
{
//Identify how severe the mesage is
switch(severity)
{
case LS_DEBUG:
cout << "DEBUG: ";
break;
case LS_INFO:
cout << "INFO: ";
break;
case LS_WARN:
cout << "WARN: ";
break;
case LS_ERROR:
cout << "ERROR: ";
break;
}
//Print the message
cout << message << endl;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
logHandler = &exampleLog;
LargeVolume<MaterialDensityPair44> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(g_uVolumeSideLength-1, g_uVolumeSideLength-1, g_uVolumeSideLength-1))); LargeVolume<MaterialDensityPair44> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(g_uVolumeSideLength-1, g_uVolumeSideLength-1, g_uVolumeSideLength-1)));
//Make our volume contain a sphere in the center. //Make our volume contain a sphere in the center.

View File

@ -32,7 +32,6 @@ SET(CORE_SRC_FILES
source/ArraySizes.cpp source/ArraySizes.cpp
source/AStarPathfinder.cpp source/AStarPathfinder.cpp
source/MinizCompressor.cpp source/MinizCompressor.cpp
source/Log.cpp
source/Region.cpp source/Region.cpp
source/VertexTypes.cpp source/VertexTypes.cpp
) )
@ -66,7 +65,6 @@ SET(CORE_INC_FILES
include/PolyVoxCore/LargeVolume.h include/PolyVoxCore/LargeVolume.h
include/PolyVoxCore/LargeVolume.inl include/PolyVoxCore/LargeVolume.inl
include/PolyVoxCore/LargeVolumeSampler.inl include/PolyVoxCore/LargeVolumeSampler.inl
include/PolyVoxCore/Log.h
include/PolyVoxCore/LowPassFilter.h include/PolyVoxCore/LowPassFilter.h
include/PolyVoxCore/LowPassFilter.inl include/PolyVoxCore/LowPassFilter.inl
include/PolyVoxCore/MarchingCubesSurfaceExtractor.h include/PolyVoxCore/MarchingCubesSurfaceExtractor.h

View File

@ -24,7 +24,6 @@ freely, subject to the following restrictions:
#ifndef __PolyVox_BaseVolume_H__ #ifndef __PolyVox_BaseVolume_H__
#define __PolyVox_BaseVolume_H__ #define __PolyVox_BaseVolume_H__
#include "PolyVoxCore/Log.h"
#include "PolyVoxCore/Region.h" #include "PolyVoxCore/Region.h"
#include "PolyVoxCore/Vector.h" #include "PolyVoxCore/Vector.h"

View File

@ -25,6 +25,7 @@ freely, subject to the following restrictions:
#define __PolyVox_Config_H__ #define __PolyVox_Config_H__
#define POLYVOX_ASSERTS_ENABLED #define POLYVOX_ASSERTS_ENABLED
#define POLYVOX_LOGGING_ENABLED
#define POLYVOX_THROW_ENABLED #define POLYVOX_THROW_ENABLED
#endif #endif

View File

@ -55,6 +55,37 @@ freely, subject to the following restrictions:
#define POLYVOX_UNUSED(x) do { (void)sizeof(x); } while(0) #define POLYVOX_UNUSED(x) do { (void)sizeof(x); } while(0)
/*
* Logging
* --------
* PolyVox provides basic logging facilities which can be redirected by your application.
*/
namespace PolyVox
{
class LogLevels
{
public:
enum LogLevel
{
Debug,
Info,
Warning,
Error,
Fatal
};
};
typedef LogLevels::LogLevel LogLevel;
typedef void (*LogHandler)(const std::string& message, LogLevel logLevel);
LogHandler getLogHandler();
void setLogHandler(LogHandler newHandler);
//The actual logging function
void log(const std::string& message, LogLevel logLevel);
}
/* /*
* Assertions * Assertions
* ---------- * ----------
@ -134,7 +165,9 @@ freely, subject to the following restrictions:
* ... * ...
*/ */
#ifdef POLYVOX_THROW_ENABLED #ifdef POLYVOX_THROW_ENABLED
#define POLYVOX_THROW(type, message) throw type((message)) #define POLYVOX_THROW(type, message) \
log(message, LogLevels::Error); \
throw type((message))
#else #else
namespace PolyVox namespace PolyVox
{ {
@ -145,6 +178,7 @@ freely, subject to the following restrictions:
} }
#define POLYVOX_THROW(type, message) \ #define POLYVOX_THROW(type, message) \
log(message, LogLevels::Error); \
type except = (type)((message)); \ type except = (type)((message)); \
getThrowHandler()((except), __FILE__, __LINE__) getThrowHandler()((except), __FILE__, __LINE__)
#endif #endif

View File

@ -27,7 +27,6 @@ freely, subject to the following restrictions:
#include "PolyVoxCore/BaseVolume.h" #include "PolyVoxCore/BaseVolume.h"
#include "Impl/Block.h" #include "Impl/Block.h"
#include "PolyVoxCore/Compressor.h" #include "PolyVoxCore/Compressor.h"
#include "PolyVoxCore/Log.h"
#include "PolyVoxCore/Region.h" #include "PolyVoxCore/Region.h"
#include "PolyVoxCore/Vector.h" #include "PolyVoxCore/Vector.h"

View File

@ -1,63 +0,0 @@
/*******************************************************************************
Copyright (c) 2005-2009 David Williams
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*******************************************************************************/
#ifndef __PolyVox_Log_H__
#define __PolyVox_Log_H__
#include "Impl/TypeDef.h"
#include <string>
//Note: The functions in this file are not for the user to call - they are
//intended for internal use only. The only exception is that you may set the
//logHandler pointer to point at your own handling funtion for printing, etc.
namespace PolyVox
{
////////////////////////////////////////////////////////////////////////////////
/// Log levels for filtering logging events
////////////////////////////////////////////////////////////////////////////////
enum LogSeverity
{
LS_DEBUG, ///< Only displayed if it is a debug build
LS_INFO,
LS_WARN,
LS_ERROR
};
POLYVOX_API extern void (*logHandler)(std::string, int severity);
}
//Debug severity messages are only used if we are a debug build
#ifdef _DEBUG
#define POLYVOX_LOG_DEBUG(message) if(logHandler){logHandler(message, LS_DEBUG);}
#else
#define POLYVOX_LOG_DEBUG(message)
#endif
//Other severity levels work in both debug and release
#define POLYVOX_LOG_INFO(message) if(logHandler){logHandler(message, LS_INFO);}
#define POLYVOX_LOG_WARN(message) if(logHandler){logHandler(message, LS_WARN);}
#define POLYVOX_LOG_ERROR(message) if(logHandler){logHandler(message, LS_ERROR);}
#endif

View File

@ -25,7 +25,6 @@ freely, subject to the following restrictions:
#define __PolyVox_RawVolume_H__ #define __PolyVox_RawVolume_H__
#include "PolyVoxCore/BaseVolume.h" #include "PolyVoxCore/BaseVolume.h"
#include "PolyVoxCore/Log.h"
#include "PolyVoxCore/Region.h" #include "PolyVoxCore/Region.h"
#include "PolyVoxCore/Vector.h" #include "PolyVoxCore/Vector.h"

View File

@ -27,7 +27,6 @@ freely, subject to the following restrictions:
#include "Impl/Utility.h" #include "Impl/Utility.h"
#include "PolyVoxCore/BaseVolume.h" #include "PolyVoxCore/BaseVolume.h"
#include "PolyVoxCore/Log.h"
#include "PolyVoxCore/Region.h" #include "PolyVoxCore/Region.h"
#include "PolyVoxCore/Vector.h" #include "PolyVoxCore/Vector.h"

View File

@ -23,49 +23,106 @@ freely, subject to the following restrictions:
#include "PolyVoxCore/Impl/ErrorHandling.h" #include "PolyVoxCore/Impl/ErrorHandling.h"
#ifndef POLYVOX_THROW_ENABLED namespace PolyVox
namespace PolyVox {
void defaultLogHandler(const std::string& message, LogLevel logLevel)
{ {
void defaultThrowHandler(std::exception& e, const char* file, int line) switch(logLevel)
{ {
std::cerr << std::endl << std::endl; \ case LogLevels::Debug:
std::cerr << " PolyVox exception thrown!" << std::endl; \ {
std::cerr << " =========================" << std::endl; \ std::cout << "Debug: " << message.c_str() << std::endl;
std::cerr << " PolyVox has tried to throw an exception but it was built without support" << std::endl; \ break;
std::cerr << " for exceptions. In this scenario PolyVox will call a 'throw handler'" << std::endl; \ }
std::cerr << " and this message is being printed by the default throw handler." << std::endl << std::endl; \ case LogLevels::Info:
{
std::cerr << " If you don't want to enable exceptions then you should try to determine why" << std::endl; \ std::cout << "Info: " << message.c_str() << std::endl;
std::cerr << " this exception was thrown and make sure it doesn't happen again. If it was" << std::endl; \ break;
std::cerr << " due to something like an invalid argument to a function then you should be" << std::endl; \ }
std::cerr << " able to fix it quite easily by validating parameters as appropriate. More" << std::endl; \ case LogLevels::Warning:
std::cerr << " complex exception scenarios (out of memory, etc) might be harder to fix and" << std::endl; \ {
std::cerr << " you should replace this default handler with something which is more" << std::endl; \ std::cerr << "Warning: " << message.c_str() << std::endl;
std::cerr << " meaningful to your users." << std::endl << std::endl; \ break;
}
std::cerr << " Exception details" << std::endl; \ case LogLevels::Error:
std::cerr << " -----------------" << std::endl; \ {
std::cerr << " Line: " << line << std::endl; \ std::cerr << "Error: " << message.c_str() << std::endl;
std::cerr << " File: " << file << std::endl; \ break;
std::cerr << " Message: " << e.what() << std::endl << std::endl; \ }
case LogLevels::Fatal:
POLYVOX_HALT(); \ {
} std::cerr << "Fatal: " << message.c_str() << std::endl;
break;
ThrowHandler& getThrowHandlerInstance() }
{
static ThrowHandler s_fThrowHandler = &defaultThrowHandler;
return s_fThrowHandler;
}
ThrowHandler getThrowHandler()
{
return getThrowHandlerInstance();
}
void setThrowHandler(ThrowHandler fNewHandler)
{
getThrowHandlerInstance() = fNewHandler;
} }
} }
LogHandler& getLogHandlerInstance()
{
static LogHandler s_fLogHandler = &defaultLogHandler;
return s_fLogHandler;
}
LogHandler getLogHandler()
{
return getLogHandlerInstance();
}
void setLogHandler(LogHandler fNewHandler)
{
getLogHandlerInstance() = fNewHandler;
}
void log(const std::string& message, LogLevel logLevel)
{
LogHandler logHandler = getLogHandler();
if(logHandler)
{
logHandler(message, logLevel);
}
}
#ifndef POLYVOX_THROW_ENABLED
void defaultThrowHandler(std::exception& e, const char* file, int line)
{
std::cerr << std::endl << std::endl; \
std::cerr << " PolyVox exception thrown!" << std::endl; \
std::cerr << " =========================" << std::endl; \
std::cerr << " PolyVox has tried to throw an exception but it was built without support" << std::endl; \
std::cerr << " for exceptions. In this scenario PolyVox will call a 'throw handler'" << std::endl; \
std::cerr << " and this message is being printed by the default throw handler." << std::endl << std::endl; \
std::cerr << " If you don't want to enable exceptions then you should try to determine why" << std::endl; \
std::cerr << " this exception was thrown and make sure it doesn't happen again. If it was" << std::endl; \
std::cerr << " due to something like an invalid argument to a function then you should be" << std::endl; \
std::cerr << " able to fix it quite easily by validating parameters as appropriate. More" << std::endl; \
std::cerr << " complex exception scenarios (out of memory, etc) might be harder to fix and" << std::endl; \
std::cerr << " you should replace this default handler with something which is more" << std::endl; \
std::cerr << " meaningful to your users." << std::endl << std::endl; \
std::cerr << " Exception details" << std::endl; \
std::cerr << " -----------------" << std::endl; \
std::cerr << " Line: " << line << std::endl; \
std::cerr << " File: " << file << std::endl; \
std::cerr << " Message: " << e.what() << std::endl << std::endl; \
POLYVOX_HALT(); \
}
ThrowHandler& getThrowHandlerInstance()
{
static ThrowHandler s_fThrowHandler = &defaultThrowHandler;
return s_fThrowHandler;
}
ThrowHandler getThrowHandler()
{
return getThrowHandlerInstance();
}
void setThrowHandler(ThrowHandler fNewHandler)
{
getThrowHandlerInstance() = fNewHandler;
}
#endif #endif
}

View File

@ -1,29 +0,0 @@
/*******************************************************************************
Copyright (c) 2005-2009 David Williams
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*******************************************************************************/
#include "PolyVoxCore/Log.h"
namespace PolyVox
{
void (*logHandler)(std::string, int severity) = 0;
}