Commit ed08b3bc authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[CHANGE] - colors can be turned of now

parent a53f15d0
......@@ -29,11 +29,12 @@
#include "WLogEntry.h"
WLogEntry:: WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source )
WLogEntry:: WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source, bool colored )
: m_time( logTime ),
m_message( message ),
m_level( level ),
m_source( source )
m_source( source ),
m_colored( colored )
{
}
......@@ -87,35 +88,57 @@ LogLevel WLogEntry::getLogLevel()
std::string WLogEntry::color( CColorAttrib attrib, CColorForeground foreground, CColorBackground background )
{
#ifdef __linux__
std::ostringstream ss;
if ( m_colored )
{
std::ostringstream ss;
char cStart = 0x1B;
ss << cStart << "[" << attrib << ";" << foreground << ";" << background << "m";
char cStart = 0x1B;
ss << cStart << "[" << attrib << ";" << foreground << ";" << background << "m";
return ss.str();
return ss.str();
}
#endif
return "";
}
std::string WLogEntry::color( CColorAttrib attrib, CColorForeground foreground )
{
#ifdef __linux__
std::ostringstream ss;
if ( m_colored )
{
std::ostringstream ss;
char cStart = 0x1B;
ss << cStart << "[" << attrib << ";" << foreground << "m";
char cStart = 0x1B;
ss << cStart << "[" << attrib << ";" << foreground << "m";
return ss.str();
return ss.str();
}
#endif
return "";
}
std::string WLogEntry::reset()
{
#ifdef __linux__
std::ostringstream ss;
if ( m_colored )
{
std::ostringstream ss;
char cStart = 0x1B;
ss << cStart << "[0m";
return ss.str();
char cStart = 0x1B;
ss << cStart << "[0m";
return ss.str();
}
#endif
return "";
}
void WLogEntry::setColored( bool colors )
{
m_colored = colors;
}
bool WLogEntry::isColored()
{
return m_colored;
}
......@@ -45,10 +45,17 @@ LogLevel;
class WLogEntry
{
public:
/**
* Construtcs a log message entry
* Creates a new log message.
*
* \param logTime the time
* \param message the message
* \param level the log level
* \param source the source, sending the log
* \param colored true if colors should be used.
*/
WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source = "" );
WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source = "", bool colored = true );
/**
* Destroys a log message entry.
......@@ -65,6 +72,20 @@ public:
*/
LogLevel getLogLevel();
/**
* 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();
protected:
private:
/**
......@@ -87,6 +108,11 @@ private:
*/
std::string m_source;
/**
* Flag determining whether colors should be used or not.
*/
bool m_colored;
/**
* Define possible attributes.
*/
......
......@@ -45,7 +45,7 @@ public:
void testFormatStringReplacement( void )
{
std::string dummyTime = "2009-Oct-02 14:46:50";
WLogEntry entry( dummyTime, "Dummy message", LL_INFO, "WLogEntryTest" );
WLogEntry entry( dummyTime, "Dummy message", LL_INFO, "WLogEntryTest", false );
// 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 ";
......@@ -58,7 +58,7 @@ public:
*/
void testDefaultFormatString( void )
{
WLogEntry entry( "now", "msg", LL_WARNING );
WLogEntry entry( "now", "msg", LL_WARNING, "", false );
std::string expected( "[now] *WARNING* msg \n" );
TS_ASSERT_EQUALS( expected, entry.getLogString() );
}
......@@ -69,7 +69,7 @@ public:
*/
void testEmptyStringAsFormatString( void )
{
WLogEntry entry( "now", "msg", LL_INFO, "WLogEntryTest" );
WLogEntry entry( "now", "msg", LL_INFO, "WLogEntryTest", false );
TS_ASSERT_EQUALS( entry.getLogString( "" ), "" );
}
......@@ -79,7 +79,7 @@ public:
*/
void testUnkownLogLevel( void )
{
WLogEntry entry( "now", "msg", static_cast< LogLevel >( 4711 ), "WLogEntryTest" );
WLogEntry entry( "now", "msg", static_cast< LogLevel >( 4711 ), "WLogEntryTest", false );
std::string expected( "[now] *%l* msg \n" );
TS_ASSERT_EQUALS( entry.getLogString(), expected );
}
......
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