Commit 844868d5 authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[MERGE]

parents dde70f50 d63928b2
......@@ -23,24 +23,23 @@
//---------------------------------------------------------------------------
#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 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;
......@@ -69,11 +68,9 @@ std::string WLogEntry::getLogString( std::string format )
boost::ireplace_first( s, "%s", m_source );
return s;
}
LogLevel WLogEntry::getLogLevel()
{
return m_level;
......
......@@ -27,6 +27,9 @@
#include <string>
/**
* Various log levels, to distinguish output on its level.
*/
typedef enum
{
LL_DEBUG = 0,
......@@ -37,56 +40,50 @@ typedef enum
LogLevel;
/**
* TODO(schurade): Document this!
* Represents a simple log message with some attributes.
*/
class WLogEntry
{
public:
/**
*
* Construtcs a log message entry
*/
WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source );
WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source = "" );
/**
*
* Destroys a log message entry.
*/
virtual ~WLogEntry();
/**
*
* \return String of this log entry.
*/
std::string getLogString( std::string format = "[%t] *%l* %m \n" );
/**
*
* \return log level of this entry.
*/
LogLevel getLogLevel();
protected:
private:
/**
* Private standard constructor to prevent empty log entries
*/
WLogEntry();
/**
*
* The time the log message was received
*/
std::string m_time;
/**
*
* The actual message
*/
std::string m_message;
/**
*
* Log level
*/
LogLevel m_level;
/**
*
* Source (e.g. module name) where this log message comes from.
*/
std::string m_source;
};
......
......@@ -25,7 +25,7 @@
#include <iostream>
#include <string>
#include "boost/date_time/posix_time/posix_time.hpp"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem/fstream.hpp>
#include "WLogger.h"
......@@ -62,40 +62,35 @@ WLogger* WLogger::getLogger()
return logger;
}
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
*/
boost::filesystem::path p( fileName );
// TODO(schurade): check if this is a _VALID_ path (existence is not needed)
m_LogFileName = fileName;
}
void WLogger::addLogMessage( std::string message, std::string source, LogLevel level )
{
if ( m_LogLevel > level || m_FinishRequested )
......@@ -111,7 +106,6 @@ void WLogger::addLogMessage( std::string message, std::string source, LogLevel l
m_LogQueue.push( entry );
}
void WLogger::processQueue()
{
boost::mutex::scoped_lock l( m_QueueMutex );
......@@ -135,6 +129,8 @@ void WLogger::processQueue()
if ( entry.getLogLevel() >= m_LogFileLevel )
{
// TODO(schurade): first open file, then write to file, then close the file
// for atomic file usage.
boost::filesystem::path p( "walnut.log" );
boost::filesystem::ofstream ofs( p, boost::filesystem::ofstream::app );
ofs << entry.getLogString();
......@@ -142,7 +138,6 @@ void WLogger::processQueue()
}
}
void WLogger::threadMain()
{
// Since the modules run in a separate thread: such loops are possible
......
......@@ -38,7 +38,7 @@
#include "WLogEntry.h"
/**
* TODO(schurade): Document this!
* Does actual logging of WLogEntries down to stdout or something similar.
*/
class WLogger: public WThreadedRunner
{
......@@ -54,50 +54,50 @@ public:
virtual ~WLogger();
/**
* Returns pointer to the currently running logger.
* Returns pointer to the currently running logger instance.
*
* \return pointer to logger instance.
*/
static WLogger* getLogger();
/**
*
* Sets the global log level
*/
void setLogLevel( LogLevel level );
/**
*
* Sets the log level for stdout.
*/
void setSTDOUTLevel( LogLevel level );
/**
*
* Sets the log level for stderr.
*/
void setSTDERRLevel( LogLevel level );
/**
*
* Sets the log level for the given log file.
*/
void setLogFileLevel( LogLevel level );
/**
*
* Specifies the path for logging to this file and checks if the path
* exists by an assertion.
*/
void setLogFileName( std::string fileName );
/**
*
* Appends a log message to the logging queue.
*/
void addLogMessage( std::string message, std::string source = "", LogLevel level = LL_DEBUG );
/**
*
* Locks this logging instance for threadsafeness and prints the
* items of the queue.
*/
void processQueue();
/**
* \par Description
* Entry point after loading the module. Runs in separate thread.
*/
virtual void threadMain();
......@@ -105,52 +105,49 @@ public:
protected:
private:
/**
* We do not want a copy constructor, so we define it private.
*/
WLogger( const WLogger& )
{
};
WLogger( const WLogger& );
/**
*
* The actual level of logging so messages with a lower level will be
* discarded.
*/
LogLevel m_LogLevel;
/**
*
* LogLevel for stdout
*/
LogLevel m_STDOUTLevel;
/**
*
* LogLevel for stderr
*/
LogLevel m_STDERRLevel;
/**
*
* LogLevel for the given log file
*/
LogLevel m_LogFileLevel;
/**
*
* Filename of the log file
*/
std::string m_LogFileName;
/**
*
* Storage for all WLogEntries that were given to our logging instance
*/
std::vector< WLogEntry > m_SessionLog;
/**
*
* Queue for storing pending WLogEntries.
*/
std::queue< WLogEntry > m_LogQueue;
/**
*
* Mutex for doing locking due to thread-safety.
*/
boost::mutex m_QueueMutex;
};
......
//---------------------------------------------------------------------------
//
// 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_TEST_H
#define WLOGENTRY_TEST_H
#include <string>
#include <cxxtest/TestSuite.h>
#include "../WLogEntry.h"
/**
* Unit tests our log messages.
*/
class WLogEntryTest : public CxxTest::TestSuite
{
public:
/**
* If given a format string of the form: "%t :: %l :: %m" then
* the log message will replace %t with time of logging and
* %l with level of logging and %m with the message itself.
*/
void testFormatStringReplacement( void )
{
std::string dummyTime = "2009-Oct-02 14:46:50";
WLogEntry entry( dummyTime, "Dummy message", LL_INFO, "WLogEntryTest" );
// build our customized format string
std::string format = "%m :: %t %t %l";
std::string expected = "Dummy message :: 2009-Oct-02 14:46:50 %t INFO ";
TS_ASSERT_EQUALS( entry.getLogString( format ), expected );
}
/**
* If the default format string is used then the time, level and message
* should be printed in a special format.
*/
void testDefaultFormatString( void )
{
WLogEntry entry( "now", "msg", LL_WARNING );
std::string expected( "[now] *WARNING* msg \n" );
TS_ASSERT_EQUALS( expected, entry.getLogString() );
}
/**
* If an empty format string is given, then an empty string should be
* returned.
*/
void testEmptyStringAsFormatString( void )
{
WLogEntry entry( "now", "msg", LL_INFO, "WLogEntryTest" );
TS_ASSERT_EQUALS( entry.getLogString( "" ), "" );
}
/**
* If ever an unknown log level was used to construct the entry then no
* replacement should be done.
*/
void testUnkownLogLevel( void )
{
WLogEntry entry( "now", "msg", static_cast< LogLevel >( 4711 ), "WLogEntryTest" );
std::string expected( "[now] *%l* msg \n" );
TS_ASSERT_EQUALS( entry.getLogString(), expected );
}
};
#endif // WLOGENTRY_TEST_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/>.
//
//---------------------------------------------------------------------------
#ifndef WLOGGER_TEST_H
#define WLOGGER_TEST_H
#include <cxxtest/TestSuite.h>
#include "../WLogger.h"
/**
* Unit tests our WLogger facility.
*/
class WLoggerTest : public CxxTest::TestSuite
{
public:
/**
* If the logger is set to do logging only on errors and warnings then
* no debug messages or infos should be logged.
*/
void testSomething( void )
{
}
};
#endif // WLOGGER_TEST_H
......@@ -3,7 +3,7 @@ ADD_SUBDIRECTORY( test )
ADD_SUBDIRECTORY( nifti )
ADD_SUBDIRECTORY( biosig )
FILE( GLOB DATAHANDLER_LOADERS_SRC "WLoader*.cpp" )
FILE( GLOB DATAHANDLER_LOADERS_SRC "WLoader*.cpp" "*.hpp" )
# Unit tests
IF( CXXTEST_FOUND )
......
//---------------------------------------------------------------------------
//
// 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 WIOTOOLS_HPP
#define WIOTOOLS_HPP
#include <stdint.h>
#include <cassert>
/**
* Namespaces for several tools we may need while doing IO
*/
namespace wiotools
{
/**
* Checks if you are on a big endian machine or not.
*/
inline bool isBigEndian()
{
union {
uint32_t i;
char c[4];
} some = {0x01020304}; // assigning an 32 bit unsigned integer // NOLINT
return some.c[0] == 1;
}
/**
* Transforms a value of type T into the opposite byte order.
*/
template< class T > T switchByteOrder( const T value )
{
size_t numBytes = sizeof( T );
T result = value;
if( numBytes == 1 )
return result;
assert( numBytes % 2 == 0 && numBytes > 0 );
char *s = reinterpret_cast< char* >( &result );
for( size_t i = 0; i < numBytes / 2; ++i ) {
std::swap( s[i], s[ ( numBytes - 1 ) - i ] );
}
return result;
}
/**
* Transform a whole array of elements (of type T and size of sizeof(T))
* into opposite byte order.
*/
template< class T > void switchByteOrderOfArray( T *array, const size_t arraySize )
{
for( size_t i = 0; i < arraySize; ++i ) {
array[i] = switchByteOrder< T >( array[i] );
}
}
}
#endif // WIOTOOLS_HPP
......@@ -22,92 +22,138 @@
//
//---------------------------------------------------------------------------
#include <stdint.h>
#include <cassert>
#include <fstream>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include "WLoaderFibers.h"
#include "WIOTools.hpp"
#include "../WDataHandler.h"
#include "../../common/WStringUtils.hpp"
#include "../../math/WPosition.h"
WLoaderFibers::WLoaderFibers( std::string fname, boost::shared_ptr< WDataHandler > dataHandler )
WLoaderFibers::WLoaderFibers( std::string fname, boost::shared_ptr< WDataHandler > dataHandler ) throw( WDHIOFailure )
: WLoader( fname, dataHandler )
{
m_ifs = new std::ifstream( fname.c_str(), std::ifstream::in | std::ifstream::binary );
if( !( *m_ifs ) || m_ifs->bad() )
{
throw WDHIOFailure( "Load broken file '" + m_fileName + "'" );
}
}
WLoaderFibers::~WLoaderFibers() throw()
{
if( m_ifs )
{
m_ifs->close();
}
delete( m_ifs );
}
void WLoaderFibers::operator()() throw()
{
std::ifstream in( m_fileName.c_str(), std::ifstream::in | std::ifstream::binary );
try
{
if( !in || in.bad() )
{
throw WDHIOFailure( "Load broken file '" + m_fileName + "'" );
}
readHeader( &in );
if( !datasetTypeIs( "POLYDATA" ) )
{
throw WDHException( "Invalid VTK DATASET type: DATASET != POLYDATA not implemented" );
}
if( !isBinary() )
{
throw WDHException( "VTK files in ASCII format is not yet supported" );
}
readPoints( &in );
readHeader();
readPoints();
}
catch( WDHException e )
{
in.close();
m_ifs->close();
// TODO(math): we should print the file name also, since knowing that
// the file was malformated, doesn't give you the hint that there
// could be thousands of them
std::cerr << "Error :: DataHandler :: Abort loading VTK file due to: " << e.what() << std::endl;
}
in.close();
}
void WLoaderFibers::readHeader( std::ifstream* ifs ) throw( WDHIOFailure )
void WLoaderFibers::readHeader() throw( WDHIOFailure, WDHException )