Commit db666eea by Sebastian Eichelbaum

[CHANGE] unified log stream output to be used by android for example.

parent 434e931b
//---------------------------------------------------------------------------
//
// 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 "WLogOutputProvider.h"
WLogOutputProvider::WLogOutputProvider( LogLevel logLevel, std::string format, bool colored ):
m_logLevel( logLevel ),
m_format( format ),
m_color( colored )
{
// initialize members
}
WLogOutputProvider::~WLogOutputProvider()
{
// cleanup
}
void WLogOutputProvider::setLogLevel( LogLevel logLevel )
{
m_logLevel = logLevel;
}
LogLevel WLogOutputProvider::getLogLevel() const
{
return m_logLevel;
}
void WLogOutputProvider::setFormat( std::string format )
{
m_format = format;
}
std::string WLogOutputProvider::getFormat() const
{
return m_format;
}
void WLogOutputProvider::setColored( bool colors )
{
m_color = colors;
}
bool WLogOutputProvider::isColored() const
{
return m_color;
}
//---------------------------------------------------------------------------
//
// 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 WLOGOUTPUTPROVIDER_H
#define WLOGOUTPUTPROVIDER_H
#include <string>
#include <boost/shared_ptr.hpp>
#include "WLogEntry.h"
/**
* Basic output class for the WLogger logging system. This is an abstract class providing the needed interface for WLogger.
*/
class WLogOutputProvider
{
public:
/**
* Convenience typedef for a boost::shared_ptr< WLogOutputProvider >.
*/
typedef boost::shared_ptr< WLogOutputProvider > SPtr;
/**
* Convenience typedef for a boost::shared_ptr< const WLogOutputProvider >.
*/
typedef boost::shared_ptr< const WLogOutputProvider > ConstSPtr;
/**
* Constructor. Create a new output instance.
*
* \param logLevel logging level, i.e. verboseness
* \param format the format used for output
* \param colored true if coloring should be used.
*/
WLogOutputProvider( LogLevel logLevel = LL_DEBUG, std::string format = "*%l [%s] %m \n", bool colored = true );
/**
* Destructor.
*/
virtual ~WLogOutputProvider();
/**
* Prints the specified entry to the output. Implement this method for each specific output system
*
* \param entry the entry to print.
*/
virtual void printEntry( const WLogEntry& entry ) = 0;
/**
* Sets the new log level. All new incoming logs will be filtered according to this level.
*
* \param logLevel the level
*/
void setLogLevel( LogLevel logLevel );
/**
* Gets the currently set log level.
*
* \return the current log level
*/
LogLevel getLogLevel() const;
/**
* Sets the format string.
*
* \param format the format string.
*/
void setFormat( std::string format );
/**
* Returns the currently set format string.
*
* \return format string.
*/
std::string getFormat() const;
/**
* Set whether to use colors or not. Note: this is only useful on Linux systems currently.
*
* \param colors true if colors should be used.
*/
void setColored( bool colors );
/**
* Getter determining whether to use colors or not.
*
* \return true if colors should be used.
*/
bool isColored() const;
protected:
/**
* The logging level. All messages below this level are discarded.
*/
LogLevel m_logLevel;
/**
* The format of the message.
*/
std::string m_format;
/**
* True if colors should be used. This requires a compatible terminal.
*/
bool m_color;
private:
/**
* Disallow copy.
*
* \param rhs the stream to copy
*/
WLogOutputProvider( const WLogOutputProvider& rhs );
/**
* Disallow assignment.
*
* \param rhs the stream to assign to this
*
* \return this
*/
WLogOutputProvider& operator=( const WLogOutputProvider& rhs );
};
#endif // WLOGOUTPUTPROVIDER_H
......@@ -27,11 +27,9 @@
#include "WLogStream.h"
WLogStream::WLogStream( std::ostream& output, LogLevel logLevel, std::string format, bool colored ): // NOLINT - we need this non-const ref here
m_output( output ),
m_logLevel( logLevel ),
m_format( format ),
m_color( colored )
WLogStream::WLogStream( std::ostream& output, LogLevel logLevel, std::string format, bool colored ): // NOLINT - we need this non-const ref here
WLogOutputProvider( logLevel, format, colored ),
m_output( output )
{
// do nothing
}
......@@ -48,33 +46,3 @@ void WLogStream::printEntry( const WLogEntry& entry )
m_output.flush();
}
void WLogStream::setLogLevel( LogLevel logLevel )
{
m_logLevel = logLevel;
}
LogLevel WLogStream::getLogLevel() const
{
return m_logLevel;
}
void WLogStream::setFormat( std::string format )
{
m_format = format;
}
std::string WLogStream::getFormat() const
{
return m_format;
}
void WLogStream::setColored( bool colors )
{
m_color = colors;
}
bool WLogStream::isColored() const
{
return m_color;
}
......@@ -29,12 +29,14 @@
#include <string>
#include <boost/shared_ptr.hpp>
#include "WLogOutputProvider.h"
#include "WLogEntry.h"
/**
* Class implementing a capsule for an output stream and the needed level and format information.
* Class implementing a capsule for an output stream and the needed level and format information. This implements WLogOutputProvider for
* std::ostream.
*/
class WLogStream // NOLINT
class WLogStream: public WLogOutputProvider
{
public:
typedef boost::shared_ptr< WLogStream > SharedPtr; //!< shared pointer type
......@@ -55,88 +57,15 @@ public:
/**
* Prints the specified entry to the output stream in the right format if the log level matches.
*
* \param entry the entry to print-
* \param entry the entry to print.
*/
void printEntry( const WLogEntry& entry );
/**
* Sets the new log level. All new incoming logs will be filtered according to this level.
*
* \param logLevel the level
*/
void setLogLevel( LogLevel logLevel );
/**
* Gets the currently set log level.
*
* \return the current log level
*/
LogLevel getLogLevel() const;
/**
* Sets the format string.
*
* \param format the format string.
*/
void setFormat( std::string format );
/**
* Returns the currently set format string.
*
* \return format string.
*/
std::string getFormat() const;
/**
* Set whether to use colors or not. Note: this is only useful on Linux systems currently.
*
* \param colors true if colors should be used.
*/
void setColored( bool colors );
/**
* Getter determining whether to use colors or not.
*
* \return true if colors should be used.
*/
bool isColored() const;
virtual void printEntry( const WLogEntry& entry );
private:
/**
* Disallow copy.
*
* \param rhs the stream to copy
*/
WLogStream( const WLogStream& rhs );
/**
* Disallow assignment.
*
* \param rhs the stream to assign to this
*
* \return this
*/
WLogStream& operator=( const WLogStream& rhs );
/**
* The output stream.
*/
std::ostream& m_output;
/**
* The logging level. All messages below this level are discarded.
*/
LogLevel m_logLevel;
/**
* The format of the message.
*/
std::string m_format;
/**
* True if colors should be used. This requires a compatible terminal.
*/
bool m_color;
};
#endif // WLOGSTREAM_H
......
......@@ -32,6 +32,7 @@
#include "exceptions/WPreconditionNotMet.h"
#include "WLogger.h"
#include "WLogStream.h"
/**
* Used for program wide access to the logger.
......@@ -49,7 +50,7 @@ void WLogger::startup( std::ostream& output, LogLevel level ) // NOLINT - we ne
WLogger::WLogger( std::ostream& output, LogLevel level ): // NOLINT - we need this non-const ref here
m_outputs()
{
m_outputs.push_back( WLogStream::SharedPtr( new WLogStream( output, level ) ) );
m_outputs.push_back( WLogOutputProvider::SPtr( new WLogStream( output, level ) ) );
addLogMessage( "Initalizing Logger", "Logger", LL_INFO );
addLogMessage( "===============================================================================", "Logger", LL_INFO );
......@@ -113,12 +114,12 @@ std::string WLogger::getDefaultFormat()
return m_outputs[0]->getFormat();
}
void WLogger::addStream( WLogStream::SharedPtr s )
void WLogger::addOutputProvider( WLogOutputProvider::SPtr s )
{
m_outputs.push_back( s );
}
void WLogger::removeStream( WLogStream::SharedPtr s )
void WLogger::removeOutputProvider( WLogOutputProvider::SPtr s )
{
m_outputs.remove( s );
}
......
......@@ -34,14 +34,14 @@
#include <boost/signals2/signal.hpp>
#include "WLogEntry.h"
#include "WLogStream.h"
#include "WLogOutputProvider.h"
#include "WStringUtils.h"
#include "WSharedSequenceContainer.h"
/**
* This class defines the interface for adding logs and managing several output streams for them. The actual log entry is in \ref WLogEntry and
* the output is done in \ref WLogStream.
* the output is done in \ref WLogOutputProvider.
*/
class WLogger // NOLINT
{
......@@ -72,14 +72,14 @@ public:
*
* \param s the stream to add.
*/
void addStream( WLogStream::SharedPtr s );
void addOutputProvider( WLogOutputProvider::SPtr s );
/**
* Remove the given stream.
*
* \param s the stream to remove
*/
void removeStream( WLogStream::SharedPtr s );
void removeOutputProvider( WLogOutputProvider::SPtr s );
/**
* Set the default format used for log entries.
......@@ -127,7 +127,7 @@ public:
/**
* Subscribe to the specified signal.
*
* \note If you want to listen to incoming log entries, you can also utilize the WLogStream class.
* \note If you want to listen to incoming log entries, you can also utilize the WLogOutputProvider class.
*
* \param event the kind of signal the callback should be used for.
* \param callback the callback.
......@@ -154,7 +154,7 @@ private:
/**
* The output stream list type.
*/
typedef WSharedSequenceContainer< std::vector< WLogStream::SharedPtr > > Outputs;
typedef WSharedSequenceContainer< std::vector< WLogOutputProvider::SPtr > > Outputs;
/**
* The list of outputs to print the messages to.
......
......@@ -164,8 +164,7 @@ int main( int argc, char** argv )
if( crashLogFile.is_open() )
{
// create the WLogStream. Set special format and DISABLE colors.
WLogStream::SharedPtr crashLog = WLogStream::SharedPtr( new WLogStream( crashLogFile, LL_DEBUG, "%t %l %s: %m\n", false ) );
WLogger::getLogger()->addStream( crashLog );
WLogger::getLogger()->addOutputProvider( WLogOutputProvider::SPtr( new WLogStream( crashLogFile, LL_DEBUG, "%t %l %s: %m\n", false ) ) );
// NOTE: the stream flushes after each entry. This is needed if a crash occurs.
if( !fallbackLog )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment