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.
*******************************************************************************/
#include "PolyVoxCore/Log.h"
#include "PolyVoxCore/MaterialDensityPair.h"
#include "PolyVoxCore/LargeVolume.h"
#include "PolyVoxCore/LowPassFilter.h"
@ -47,32 +46,8 @@ using namespace std;
using namespace PolyVox;
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[])
{
logHandler = &exampleLog;
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.

View File

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

View File

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

View File

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

View File

@ -55,6 +55,37 @@ freely, subject to the following restrictions:
#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
* ----------
@ -134,7 +165,9 @@ freely, subject to the following restrictions:
* ...
*/
#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
namespace PolyVox
{
@ -145,6 +178,7 @@ freely, subject to the following restrictions:
}
#define POLYVOX_THROW(type, message) \
log(message, LogLevels::Error); \
type except = (type)((message)); \
getThrowHandler()((except), __FILE__, __LINE__)
#endif

View File

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

View File

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

View File

@ -23,9 +23,66 @@ freely, subject to the following restrictions:
#include "PolyVoxCore/Impl/ErrorHandling.h"
#ifndef POLYVOX_THROW_ENABLED
namespace PolyVox
{
void defaultLogHandler(const std::string& message, LogLevel logLevel)
{
switch(logLevel)
{
case LogLevels::Debug:
{
std::cout << "Debug: " << message.c_str() << std::endl;
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;
}
}
}
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; \
@ -67,5 +124,5 @@ freely, subject to the following restrictions:
{
getThrowHandlerInstance() = fNewHandler;
}
}
#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;
}