Commit 91f64e84 authored by schurade's avatar schurade
Browse files

[ADD] a logging class for console and file output

parent bc90009a
......@@ -30,7 +30,7 @@ ENDIF( CMAKE_BUILD_TYPE STREQUAL "Static" )
# To see which boost libs we currently use, you may run the following command
# in the trunk/src directory on a linux box to make some investigations:
# grep -i include `find . -type f` | grep boost | awk '{print $2}' | sort | uniq
FIND_PACKAGE( Boost REQUIRED program_options thread filesystem )
FIND_PACKAGE( Boost REQUIRED program_options thread filesystem date_time )
ASSERT_GE_VERSION( "Boost" "${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION}.${Boost_SUBMINOR_VERSION}" 1.36.0 )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include <boost/algorithm/string.hpp>
#include "WLogEntry.h"
WLogEntry:: WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source ) :
m_time( logTime ),
m_message( message ),
m_level( level ),
m_source( source )
{
}
WLogEntry::~WLogEntry()
{
}
std::string WLogEntry::getLogString( std::string format )
{
std::string s = format;
boost::ireplace_first( s, "%t", m_time );
switch ( m_level )
{
case LL_DEBUG:
boost::ireplace_first( s, "%l", "DEBUG " );
break;
case LL_INFO:
boost::ireplace_first( s, "%l", "INFO " );
break;
case LL_WARNING:
boost::ireplace_first( s, "%l", "WARNING" );
break;
case LL_ERROR:
boost::ireplace_first( s, "%l", "ERROR " );
break;
default:
break;
}
boost::ireplace_first( s, "%m", m_message );
boost::ireplace_first( s, "%s", m_source );
return s;
}
LogLevel WLogEntry::getLogLevel()
{
return m_level;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WLOGENTRY_H
#define WLOGENTRY_H
#include <string>
typedef enum
{
LL_DEBUG = 0,
LL_INFO,
LL_WARNING,
LL_ERROR
}
LogLevel;
/**
* TODO(schurade): Document this!
*/
class WLogEntry
{
public:
WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source );
virtual ~WLogEntry();
std::string getLogString( std::string format = "[%t] *%l* %m \n" );
LogLevel getLogLevel();
protected:
/**
*
*/
std::string m_time;
/**
*
*/
std::string m_message;
/**
*
*/
LogLevel m_level;
/**
*
*/
std::string m_source;
private:
WLogEntry();
};
#endif // WLOGENTRY_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include "boost/date_time/posix_time/posix_time.hpp"
#include <boost/filesystem/fstream.hpp>
#include "WLogger.h"
WLogger::WLogger():
WThreadedRunner()
{
init();
}
WLogger::WLogger( std::string fileName, LogLevel level ):
WThreadedRunner(),
m_LogLevel( level ),
m_STDOUTLevel( level ),
m_STDERRLevel( LL_ERROR ),
m_LogFileLevel( level ),
m_LogFileName( fileName )
{
}
WLogger::~WLogger()
{
}
void WLogger::setLogLevel( LogLevel level )
{
m_LogLevel = level;
}
void WLogger::setSTDOUTLevel( LogLevel level )
{
m_STDOUTLevel = level;
}
void WLogger::setSTDERRLevel( LogLevel level )
{
m_STDERRLevel = level;
}
void WLogger::setLogFileLevel( LogLevel level )
{
m_LogFileLevel = level;
}
void WLogger::setLogFileName( std::string fileName )
{
/**
* TODO (schurade) check if it's a valid filename
*/
m_LogFileName = fileName;
}
bool WLogger::init()
{
m_LogLevel = LL_DEBUG;
m_STDOUTLevel = LL_DEBUG;
m_STDERRLevel = LL_ERROR;
m_LogFileLevel = LL_DEBUG;
m_LogFileName = "walnut.log";
addLogMessage( "Initalizing Logger", "Logger", LL_DEBUG );
addLogMessage( "===============================================================================", "Logger", LL_INFO );
addLogMessage( "= Starting Log Session =", "Logger", LL_INFO );
addLogMessage( "===============================================================================", "Logger", LL_INFO );
return true;
}
void WLogger::addLogMessage( std::string message, std::string source, LogLevel level )
{
if ( m_LogLevel > level )
{
return;
}
boost::posix_time::ptime t( boost::posix_time::second_clock::local_time() );
std::string timeString( to_simple_string( t ) );
WLogEntry entry( timeString, message, level, source );
m_LogQueue.push( entry );
// TODO(schurade): this must be called from the kernel and made thread safe
processQueue();
}
void WLogger::processQueue()
{
while ( !m_LogQueue.empty() )
{
WLogEntry entry = m_LogQueue.front();
m_LogQueue.pop();
m_SessionLog.push_back( entry );
if ( entry.getLogLevel() >= m_STDOUTLevel )
{
std::cout << entry.getLogString();
}
if ( entry.getLogLevel() >= m_STDERRLevel )
{
std::cerr << entry.getLogString();
}
if ( entry.getLogLevel() >= m_LogFileLevel )
{
boost::filesystem::path p( "walnut.log" );
boost::filesystem::ofstream ofs( p, boost::filesystem::ofstream::app );
ofs << entry.getLogString();
}
}
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WLOGGER_H
#define WLOGGER_H
#include <queue>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include "../common/WThreadedRunner.h"
#include "WLogEntry.h"
/**
* TODO(schurade): Document this!
*/
class WLogger: public WThreadedRunner
{
public:
/**
* Default constructor.
*/
WLogger();
/**
* Constructor
*/
WLogger( std::string fileName, LogLevel level = LL_DEBUG );
/**
* Destructor.
*/
virtual ~WLogger();
/**
*
*/
void setLogLevel( LogLevel level );
/**
*
*/
void setSTDOUTLevel( LogLevel level );
/**
*
*/
void setSTDERRLevel( LogLevel level );
/**
*
*/
void setLogFileLevel( LogLevel level );
/**
*
*/
void setLogFileName( std::string fileName );
/**
*
*/
void addLogMessage( std::string message, std::string source = "", LogLevel level = LL_DEBUG );
/**
*
*/
void processQueue();
protected:
/**
*
*/
LogLevel m_LogLevel;
/**
*
*/
LogLevel m_STDOUTLevel;
/**
*
*/
LogLevel m_STDERRLevel;
/**
*
*/
LogLevel m_LogFileLevel;
/**
*
*/
std::string m_LogFileName;
/**
*
*/
std::vector< WLogEntry > m_SessionLog;
/**
*
*/
std::queue< WLogEntry > m_LogQueue;
private:
/**
*
*/
bool init();
};
#endif // WLOGGER_H
......@@ -28,19 +28,21 @@
#include <osg/Group>
#include <osg/Geode>
#include "../kernel/WKernel.h"
#include "WGEScene.h"
WGEScene::WGEScene():
osg::Group()
{
std::cout << "Initializing OpenSceneGraph Root Node" << std::endl;
WKernel::getRunningKernel()->getLogger()->addLogMessage( "Initializing OpenSceneGraph Root Node", "GE", LL_DEBUG );
// initialize members
}
WGEScene::~WGEScene()
{
std::cout << "Shutting down OpenSceneGraph Root Node" << std::endl;
WKernel::getRunningKernel()->getLogger()->addLogMessage( "Shutting down OpenSceneGraph Root Node", "GE", LL_DEBUG );
// cleanup
}
......
......@@ -37,7 +37,7 @@
WGraphicsEngine::WGraphicsEngine():
WThreadedRunner()
{
std::cout << "Initializing Graphics Engine" << std::endl;
WKernel::getRunningKernel()->getLogger()->addLogMessage( "Initializing Graphics Engine", "GE", LL_DEBUG );
// initialize members
m_RootNode = new WGEScene();
......@@ -46,7 +46,7 @@ WGraphicsEngine::WGraphicsEngine():
WGraphicsEngine::~WGraphicsEngine()
{
// cleanup
std::cout << "Shutting down Graphics Engine" << std::endl;
WKernel::getRunningKernel()->getLogger()->addLogMessage( "Shutting down Graphics Engine", "GE", LL_DEBUG );
}
osg::ref_ptr<WGEScene> WGraphicsEngine::getScene()
......
......@@ -24,8 +24,13 @@
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <boost/algorithm/string.hpp>
#include "boost/lexical_cast.hpp"
#include <boost/tokenizer.hpp>
#include "../kernel/WKernel.h"
#include "WShader.h"
......@@ -34,6 +39,7 @@ WShader::WShader()
{
}
WShader::WShader( std::string fileName )
{
m_VertexObject = readShaderFromFile( fileName + ".vs", osg::Shader::VERTEX );
......@@ -52,21 +58,24 @@ WShader::WShader( std::string fileName )
}
}
WShader::~WShader()
{
}
osg::Shader* WShader::readShaderFromFile( std::string fileName, osg::Shader::Type type )
{
std::string fileText = readTextFile( fileName );
std::cout << fileText << std::endl;
// std::cout << fileText << std::endl;
osg::Shader* shader = new osg::Shader( type, fileText );
return shader;
}
std::string WShader::readTextFile( std::string fileName )
{
std::string fileText;
......@@ -74,6 +83,17 @@ std::string WShader::readTextFile( std::string fileName )
std::ifstream ifs( ( WKernel::getRunningKernel()->getShaderPath() + fileName ).c_str() );
std::string line;
std::map< std::string, float >::const_iterator mi = m_defines.begin();
while ( mi != m_defines.end() )
{
fileText += "#define ";
fileText += mi->first;
fileText += " ";
fileText += boost::lexical_cast< std::string, float >( mi->second );
fileText += '\n';
}
while ( getline( ifs, line ) )
{
if ( isIncludeLine( line ) )
......@@ -90,34 +110,61 @@ std::string WShader::readTextFile( std::string fileName )
return fileText;
}
bool WShader::isIncludeLine( std::string line )
{
if ( line.substr( 0, 8 ) == "#include" )
if ( boost::find_first( line, "#include" ) )
{
return true;
}
return false;
}
std::string WShader::getIncludeFileName( std::string line )
{
size_t pos = 0;
std::string fileName;
while ( line[pos++] != '\"' )
int count = 0;
for ( size_t i = 0 ; i < line.length() ; ++i )
{
if ( line[i] == '\"' )
{
++count;
}
}
while ( ( pos < line.length() ) && ( line[pos] != '\"' ) )
if ( count < 2 )
{
fileName += line[pos];
++pos;
// TODO(schurade): here we could throw an exception
return 0;
}
return fileName;
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep( "\"" );
tokenizer tok( line, sep );
tokenizer::iterator it = tok.begin();
++it;
return *it;
}
osg::Program* WShader::getProgramObject()
{
return m_ProgramObject;
}
void WShader::setDefine( std::string key, float value )
{
if ( key.length() > 0 )
{
m_defines[key] = value;
}
}
void WShader::eraseDefine( std::string key</