FEATURE: Add default logger with file- and win32 debuglogging

git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@3 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
pull/1/head
kimmi 2008-05-08 20:02:51 +00:00
parent fe773845cb
commit 96ea41502a
7 changed files with 453 additions and 2 deletions

View File

@ -0,0 +1,199 @@
#include "DefaultLogger.h"
#include "aiAssert.h"
#include "DefaultIOSystem.h"
#include "Win32DebugLogStream.h"
#include "IOStream.h"
#include "FileLogStream.h"
#include <iostream>
namespace Assimp
{
// ---------------------------------------------------------------------------
DefaultLogger *DefaultLogger::m_pLogger = NULL;
// ---------------------------------------------------------------------------
//
struct LogStreamInfo
{
unsigned int m_uiErrorSeverity;
LogStream *m_pStream;
// Constructor
LogStreamInfo( unsigned int uiErrorSev, LogStream *pStream ) :
m_uiErrorSeverity( uiErrorSev ),
m_pStream( pStream )
{
// empty
}
};
// ---------------------------------------------------------------------------
// Creates the only singleton instance
Logger *DefaultLogger::create(const std::string &name, LogSeverity severity)
{
ai_assert (NULL == m_pLogger);
m_pLogger = new DefaultLogger( name, severity );
return m_pLogger;
}
// ---------------------------------------------------------------------------
// Singleton getter
Logger *DefaultLogger::get()
{
ai_assert (NULL != m_pLogger);
return m_pLogger;
}
// ---------------------------------------------------------------------------
// Kills the only instance
void DefaultLogger::kill()
{
ai_assert (NULL != m_pLogger);
delete m_pLogger;
m_pLogger = NULL;
}
// ---------------------------------------------------------------------------
// Debug message
void DefaultLogger::debug(const std::string &message)
{
if ( m_Severity == Logger::NORMAL )
return;
const std::string msg( "Debug: " + message );
writeToStreams( msg, Logger::DEBUGGING );
}
// ---------------------------------------------------------------------------
// Logs an info
void DefaultLogger::info(const std::string &message)
{
const std::string msg( "Info: " + message );
writeToStreams( msg , Logger::INFO );
}
// ---------------------------------------------------------------------------
// Logs a warning
void DefaultLogger::warn( const std::string &message )
{
const std::string msg( "Warn: " + message );
writeToStreams( msg, Logger::WARN );
}
// ---------------------------------------------------------------------------
// Logs an error
void DefaultLogger::error( const std::string &message )
{
const std::string msg( "Error: " + message );
writeToStreams( msg, Logger::ERR );
}
// ---------------------------------------------------------------------------
// Severity setter
void DefaultLogger::setLogSeverity( LogSeverity log_severity )
{
m_Severity = log_severity;
}
// ---------------------------------------------------------------------------
// Attachs a new stream
void DefaultLogger::attachStream( LogStream *pStream, unsigned int severity )
{
ai_assert ( NULL != pStream );
LogStreamInfo *pInfo = new LogStreamInfo( severity, pStream );
m_StreamArray.push_back( pInfo );
}
// ---------------------------------------------------------------------------
// Detatch a stream
void DefaultLogger::detatchStream( LogStream *pStream, unsigned int severity )
{
ai_assert ( NULL != pStream );
for ( StreamIt it = m_StreamArray.begin();
it != m_StreamArray.end();
++it )
{
if ( (*it)->m_pStream == pStream )
{
unsigned int uiSev = (*it)->m_uiErrorSeverity;
if ( severity & Logger::INFO )
uiSev &= ( ~Logger::INFO );
if ( severity & Logger::WARN )
uiSev &= ( ~Logger::WARN );
if ( severity & Logger::ERR )
uiSev &= ( ~Logger::ERR );
(*it)->m_uiErrorSeverity = uiSev;
if ( (*it)->m_uiErrorSeverity == 0 )
{
it = m_StreamArray.erase( it );
break;
}
}
}
}
// ---------------------------------------------------------------------------
// Constructor
DefaultLogger::DefaultLogger( const std::string &name, LogSeverity severity ) :
m_Severity( severity )
{
if (name.empty())
return;
m_Streams.push_back( new FileLogStream( name ) );
m_Streams.push_back( new Win32DebugLogStream() );
}
// ---------------------------------------------------------------------------
// Destructor
DefaultLogger::~DefaultLogger()
{
for ( StreamIt it = m_StreamArray.begin();
it != m_StreamArray.end();
++it )
{
delete *it;
}
for (std::vector<LogStream*>::iterator it = m_Streams.begin();
it != m_Streams.end();
++it)
{
delete *it;
}
m_Streams.clear();
}
// ---------------------------------------------------------------------------
// Writes message to stream
void DefaultLogger::writeToStreams(const std::string &message,
ErrorSeverity ErrorSev )
{
if ( message.empty() )
return;
for ( ConstStreamIt it = this->m_StreamArray.begin();
it != m_StreamArray.end();
++it)
{
if ( ErrorSev & (*it)->m_uiErrorSeverity )
{
(*it)->m_pStream->write( message );
}
}
for (std::vector<LogStream*>::iterator it = m_Streams.begin();
it != m_Streams.end();
++it)
{
(*it)->write( message + std::string("\n"));
}
}
// ---------------------------------------------------------------------------
} // Namespace Assimp

View File

@ -0,0 +1,85 @@
#include "Logger.h"
#include <vector>
namespace Assimp
{
// ---------------------------------------------------------------------------
class IOStream;
struct LogStreamInfo;
// ---------------------------------------------------------------------------
/** @class DefaultLogger
* @brief Default logging implementation. The logger writes into a file.
* The name can be set by creating the logger. If no filename was specified
* the logger will use the standard out and error streams.
*/
class DefaultLogger :
public Logger
{
public:
/** @brief Creates the only logging instance
* @param name Name for logfile
* @param severity Log severity, VERBOSE will activate debug messages
*/
static Logger *create(const std::string &name, LogSeverity severity);
/** @brief Getter for singleton instance
* @return Only instance
*/
static Logger *get();
/** @brief Will kill the singleton instance */
static void kill();
/** @brief Logs debug infos, only been written when severity level VERBOSE is set */
void debug(const std::string &message);
/** @brief Logs an info message */
void info(const std::string &message);
/** @brief Logs a warning message */
void warn(const std::string &message);
/** @brief Logs an error message */
void error(const std::string &message);
/** @drief Severity setter */
void setLogSeverity(LogSeverity log_severity);
/** @brief Detach a still attached stream from logger */
void attachStream(LogStream *pStream, unsigned int severity);
/** @brief Detach a still attached stream from logger */
void detatchStream(LogStream *pStream, unsigned int severity);
private:
/** @brief Constructor
* @param name Name for logfile, keep this empty to use std::cout and std::cerr
* @param severity Severity of logger
*/
DefaultLogger(const std::string &name, LogSeverity severity);
/** @brief Destructor */
~DefaultLogger();
/** @brief Writes message into a file */
void writeToStreams(const std::string &message, ErrorSeverity ErrorSev );
private:
// Aliases for stream container
typedef std::vector<LogStreamInfo*> StreamArray;
typedef std::vector<LogStreamInfo*>::iterator StreamIt;
typedef std::vector<LogStreamInfo*>::const_iterator ConstStreamIt;
//! only logging instance
static DefaultLogger *m_pLogger;
//! Logger severity
LogSeverity m_Severity;
//! Attached streams
StreamArray m_StreamArray;
//! Array with default streams
std::vector<LogStream*> m_Streams;
};
// ---------------------------------------------------------------------------
} // Namespace Assimp

View File

@ -0,0 +1,66 @@
#ifndef ASSIMP_FILELOGSTREAM_H_INC
#define ASSIMP_FILELOGSTREAM_H_INC
#include "LogStream.h"
#include "IOStream.h"
namespace Assimp
{
// ---------------------------------------------------------------------------
/** @class FileLogStream
* @brief Logstream to write into a file.
*/
class FileLogStream :
public LogStream
{
public:
FileLogStream( const std::string &strFileName );
~FileLogStream();
void write( const std::string &message );
private:
IOStream *m_pStream;
};
// ---------------------------------------------------------------------------
// Constructor
inline FileLogStream::FileLogStream( const std::string &strFileName ) :
m_pStream(NULL)
{
if ( strFileName.empty() )
return;
DefaultIOSystem FileSystem;
const std::string mode = "w";
m_pStream = FileSystem.Open( strFileName, mode );
}
// ---------------------------------------------------------------------------
// Destructor
inline FileLogStream::~FileLogStream()
{
if (NULL != m_pStream)
{
DefaultIOSystem FileSystem;
FileSystem.Close( m_pStream );
}
}
// ---------------------------------------------------------------------------
// Write method
inline void FileLogStream::write( const std::string &message )
{
if (m_pStream != NULL)
{
size_t size = m_pStream->Write(message.c_str(), sizeof(char),
message.size());
int i=0;
i++;
}
}
// ---------------------------------------------------------------------------
} // Namespace Assimp
#endif

View File

@ -7,7 +7,7 @@
#include "../include/aiScene.h"
#include "aiAssert.h"
#include "MaterialSystem.h"
#include "DefaultLogger.h"
#include <boost/scoped_ptr.hpp>
#include <boost/format.hpp>
@ -26,7 +26,8 @@ ObjFileImporter::ObjFileImporter() :
m_pRootObject(NULL),
m_strAbsPath("\\")
{
// empty
DefaultLogger::create( "test.log", DefaultLogger::VERBOSE );
DefaultLogger::get()->info("Start logging");
}
// ------------------------------------------------------------------------------------------------
@ -39,6 +40,8 @@ ObjFileImporter::~ObjFileImporter()
delete m_pRootObject;
m_pRootObject = NULL;
}
DefaultLogger::kill();
}
// ------------------------------------------------------------------------------------------------
@ -48,6 +51,7 @@ bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) c
if (pFile.empty())
return false;
DefaultLogger::get()->info("ObjFileImporter::CanRead");
string::size_type pos = pFile.find_last_of(".");
if (string::npos == pos)
return false;

View File

@ -0,0 +1,59 @@
#ifndef AI_WIN32DEBUGLOGSTREAM_H_INC
#define AI_WIN32DEBUGLOGSTREAM_H_INC
#include "LogStream.h"
#ifdef _MSC_VER
#include "Windows.h"
#endif
namespace Assimp
{
#ifdef _MSC_VER
// ---------------------------------------------------------------------------
/** @class Win32DebugLogStream
* @brief Logs into the debug stream from win32.
*/
class Win32DebugLogStream :
public LogStream
{
public:
/** @brief Default constructor */
Win32DebugLogStream();
/** @brief Destructor */
~Win32DebugLogStream();
/** @brief Writer */
void write(const std::string &messgae);
};
// ---------------------------------------------------------------------------
// Default constructor
inline Win32DebugLogStream::Win32DebugLogStream()
{
// empty
}
// ---------------------------------------------------------------------------
// Default constructor
inline Win32DebugLogStream::~Win32DebugLogStream()
{
// empty
}
// ---------------------------------------------------------------------------
// Write method
inline void Win32DebugLogStream::write(const std::string &message)
{
OutputDebugString( message.c_str() );
}
// ---------------------------------------------------------------------------
#endif
} // Namespace Assimp
#endif

View File

@ -1,6 +1,8 @@
#ifndef AI_LOGSTREAM_H_INC
#define AI_LOGSTREAM_H_INC
#include <string>
namespace Assimp
{
// ---------------------------------------------------------------------------

View File

@ -235,6 +235,18 @@
</File>
</Filter>
</Filter>
<Filter
Name="Logger"
>
<File
RelativePath="..\..\include\Logger.h"
>
</File>
<File
RelativePath="..\..\include\LogStream.h"
>
</File>
</Filter>
</Filter>
<Filter
Name="headers"
@ -415,6 +427,22 @@
>
</File>
</Filter>
<Filter
Name="Logger"
>
<File
RelativePath="..\..\code\DefaultLogger.h"
>
</File>
<File
RelativePath="..\..\code\FileLogStream.h"
>
</File>
<File
RelativePath="..\..\code\Win32DebugLogStream.h"
>
</File>
</Filter>
</Filter>
<Filter
Name="sources"
@ -563,6 +591,14 @@
>
</File>
</Filter>
<Filter
Name="Logger"
>
<File
RelativePath="..\..\code\DefaultLogger.cpp"
>
</File>
</Filter>
</Filter>
</Files>
<Globals>