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

[CHANGE] - colors can be disabled now

parent 06eeabaa
......@@ -27,15 +27,25 @@
#include <boost/algorithm/string.hpp>
#include "WTerminalColor.h"
#include "WLogEntry.h"
WLogEntry:: WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source, bool colored )
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_colored( colored )
m_colored( colored ),
m_errorColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed ) ),
m_infoColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGGreen ) ),
m_debugColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGBlue ) ),
m_warningColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGYellow ) ),
m_sourceColor( WTerminalColor( WTerminalColor::Off, WTerminalColor::FGMagenta ) ),
m_timeColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGBlack ) ),
m_messageColor( WTerminalColor() )
{
setColored( colored );
}
WLogEntry::~WLogEntry()
......@@ -46,36 +56,29 @@ std::string WLogEntry::getLogString( std::string format )
{
std::string s = format;
std::string red = color( Bold, FGRed );
std::string green = color( Bold, FGGreen );
std::string blue = color( Bold, FGBlue );
std::string yellow = color( Bold, FGYellow );
std::string magenta = color( Off, FGMagenta );
std::string gray = color( Bold, FGBlack );
boost::ireplace_first( s, "%t", gray + m_time + reset() );
boost::ireplace_first( s, "%t", m_timeColor + m_time + !m_timeColor );
switch ( m_level )
{
case LL_DEBUG:
boost::ireplace_first( s, "%l", blue + "DEBUG " + reset() );
boost::ireplace_first( s, "%l", m_debugColor + "DEBUG " + !m_debugColor );
break;
case LL_INFO:
boost::ireplace_first( s, "%l", green + "INFO " + reset() );
boost::ireplace_first( s, "%l", m_infoColor + "INFO " + !m_infoColor );
break;
case LL_WARNING:
boost::ireplace_first( s, "%l", yellow + "WARNING" + reset() );
boost::ireplace_first( s, "%l", m_warningColor + "WARNING" + !m_warningColor );
break;
case LL_ERROR:
boost::ireplace_first( s, "%l", red + "ERROR " + reset() );
boost::ireplace_first( s, "%l", m_errorColor + "ERROR " + !m_errorColor );
break;
default:
break;
}
boost::ireplace_first( s, "%m", m_message );
boost::ireplace_first( s, "%m", m_messageColor + m_message + !m_messageColor );
boost::ireplace_first( s, "%s", magenta + m_source + reset() );
boost::ireplace_first( s, "%s", m_sourceColor + m_source + !m_sourceColor );
return s;
}
......@@ -85,56 +88,16 @@ LogLevel WLogEntry::getLogLevel()
return m_level;
}
std::string WLogEntry::color( CColorAttrib attrib, CColorForeground foreground, CColorBackground background )
{
#ifdef __linux__
if ( m_colored )
{
std::ostringstream ss;
char cStart = 0x1B;
ss << cStart << "[" << attrib << ";" << foreground << ";" << background << "m";
return ss.str();
}
#endif
return "";
}
std::string WLogEntry::color( CColorAttrib attrib, CColorForeground foreground )
{
#ifdef __linux__
if ( m_colored )
{
std::ostringstream ss;
char cStart = 0x1B;
ss << cStart << "[" << attrib << ";" << foreground << "m";
return ss.str();
}
#endif
return "";
}
std::string WLogEntry::reset()
{
#ifdef __linux__
if ( m_colored )
{
std::ostringstream ss;
char cStart = 0x1B;
ss << cStart << "[0m";
return ss.str();
}
#endif
return "";
}
void WLogEntry::setColored( bool colors )
{
m_colored = colors;
m_errorColor.setEnabled( colors );
m_infoColor.setEnabled( colors );
m_debugColor.setEnabled( colors );
m_warningColor.setEnabled( colors );
m_sourceColor.setEnabled( colors );
m_timeColor.setEnabled( colors );
m_messageColor.setEnabled( colors );
}
bool WLogEntry::isColored()
......
......@@ -27,6 +27,8 @@
#include <string>
#include "WTerminalColor.h"
/**
* Various log levels, to distinguish output on its level.
*/
......@@ -113,77 +115,40 @@ private:
*/
bool m_colored;
/**
* Define possible attributes.
*/
enum CColorAttrib
{
Off = 0,
Bold = 1,
Underscore = 4,
Blink = 5,
Reverse = 7,
Concealed = 8
};
/**
* Foreground colors.
*/
enum CColorForeground
{
FGBlack = 30,
FGRed = 31,
FGGreen = 32,
FGYellow = 33,
FGBlue = 34,
FGMagenta = 35,
FGCyan = 36,
FGWhite = 37
};
/**
* Background colors.
*/
enum CColorBackground
{
BGBlack = 40,
BGRed = 41,
BGGreen = 42,
BGYellow = 43,
BGBlue = 44,
BGMagenta = 45,
BGCyan = 46,
BGWhite = 47
};
/**
* Create a control string to activate colors. It will be active until reset was called. When the background should be not set, leave it.
*
* \param attrib the attribute to set
* \param foreground the foreground color
* \param background the background color.
*
* \return control string
/**
* Color used for error logs.
*/
std::string color( CColorAttrib attrib, CColorForeground foreground, CColorBackground background );
WTerminalColor m_errorColor;
/**
* Create a control string to activate colors. It will be active until reset was called. When the background should be not set, leave it.
*
* \param attrib the attribute to set
* \param foreground the foreground color
* \param background the background color.
*
* \return control string
/**
* Color used for info logs
*/
std::string color( CColorAttrib attrib, CColorForeground foreground );
WTerminalColor m_infoColor;
/**
* Resets all attributes and colors to default.
*
* \return the proper control string.
/**
* Color used for debug logs.
*/
WTerminalColor m_debugColor;
/**
* Color used for warning logs.
*/
WTerminalColor m_warningColor;
/**
* Color used for source field.
*/
WTerminalColor m_sourceColor;
/**
* Color used for time.
*/
WTerminalColor m_timeColor;
/**
* Color used for the message.
*/
std::string reset();
WTerminalColor m_messageColor;
};
#endif // WLOGENTRY_H
......@@ -127,7 +127,7 @@ public:
* \return format string. See WLogEntry for details.
*/
std::string getDefaultFileFormat();
/**
* Appends a log message to the logging queue.
*/
......
//---------------------------------------------------------------------------
//
// 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 <iostream>
#include <sstream>
#include "WTerminalColor.h"
WTerminalColor::WTerminalColor():
m_attrib( Default ),
m_foreground( FGBlack ),
m_background( BGBlack ),
m_enabled( false )
{
m_colorString = "";
m_colorResetString = "";
generateControlStrings();
}
WTerminalColor::WTerminalColor( TerminalColorAttribute attrib, TerminalColorForeground foreground, TerminalColorBackground background ):
m_attrib( attrib ),
m_foreground( foreground ),
m_background( background ),
m_enabled( true )
{
m_colorString = "";
m_colorResetString = "";
generateControlStrings();
}
WTerminalColor::~WTerminalColor()
{
// cleanup
}
void WTerminalColor::generateControlStrings()
{
m_colorString = "";
m_colorResetString = "";
#ifdef __linux__
if ( m_enabled && ( m_attrib != Default ) )
{
std::ostringstream ss;
char cStart = 0x1B;
ss << cStart << "[" << m_attrib << ";" << m_foreground;
// handle an unset background specially
if ( m_background == BGNone )
{
ss << "m";
}
else
{
ss << ";" << m_background << "m";
}
m_colorString = ss.str();
ss.clear();
ss << cStart << "[0m";
m_colorResetString = ss.str();
}
#endif
}
std::ostream& WTerminalColor::operator<<( std::ostream& ostr )
{
return ostr << m_colorString;
}
std::string& WTerminalColor::operator!()
{
return m_colorResetString;
}
std::string& WTerminalColor::operator()()
{
return m_colorString;
}
std::string WTerminalColor::operator+( const std::string& in )
{
return m_colorString + in;
}
void WTerminalColor::setEnabled( bool enabled )
{
m_enabled = enabled;
generateControlStrings();
}
bool WTerminalColor::isEnabled()
{
return m_enabled;
}
//---------------------------------------------------------------------------
//
// 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 WTERMINALCOLOR_H
#define WTERMINALCOLOR_H
#include <string>
/**
* Helper class to provide a convenient way to colorize output on the console.
*/
class WTerminalColor
{
public:
/**
* Define possible attributes.
*/
enum TerminalColorAttribute
{
Off = 0,
Bold = 1,
Underscore = 4,
Blink = 5,
Reverse = 7,
Concealed = 8,
Default = 9 // this actually disables coloring
};
/**
* Foreground colors.
*/
enum TerminalColorForeground
{
FGBlack = 30,
FGRed = 31,
FGGreen = 32,
FGYellow = 33,
FGBlue = 34,
FGMagenta = 35,
FGCyan = 36,
FGWhite = 37
};
/**
* Background colors.
*/
enum TerminalColorBackground
{
BGNone = 50,
BGBlack = 40,
BGRed = 41,
BGGreen = 42,
BGYellow = 43,
BGBlue = 44,
BGMagenta = 45,
BGCyan = 46,
BGWhite = 47
};
/**
* Constructor to create a color code which actually does not do any coloring.
*/
WTerminalColor();
/**
* Creates a new terminal color.
*
* \param attrib attribute, like bold or blink
* \param foreground foreground color
* \param background background color
*/
WTerminalColor( TerminalColorAttribute attrib, TerminalColorForeground foreground, TerminalColorBackground background = BGNone );
/**
* Destructor.
*/
virtual ~WTerminalColor();
/**
* Gives the control string which actually enables the color.
*
* \return the color control string
*/
std::ostream& operator<<( std::ostream& ostr );
/**
* Gives the control string which actually enables the color.
*
* \return the color control string
*/
std::string& operator()();
/**
* Combines strings.
*
* \param in the string to combine
*
* \return the concatenated string.
*/
std::string operator+( const std::string& in );
/**
* Resets the color and returns control string.
*
* \return the control string which resets color settings.
*/
std::string& operator!();
/**
* With this you can easily trigger whether the color control string is used or if "" is returned.
*
* \param enabled true to have colors.
*/
void setEnabled( bool enabled );
/**
* Is coloring enabled?
*/
bool isEnabled();
protected:
/**
* The string actually containing the control sequence to enable colors on the console.
*/
std::string m_colorString;
/**
* Control sequence to reset color.
*/
std::string m_colorResetString;
/**
* Color attributes.
*/
TerminalColorAttribute m_attrib;
/**
* The foreground color.
*/
TerminalColorForeground m_foreground;
/**
* The background color.
*/
TerminalColorBackground m_background;
private:
/**
* Actually generates the control sequences.
*/
void generateControlStrings();
/**
* True when colors should are used.
*/
bool m_enabled;
};
#endif // WTERMINALCOLOR_H
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