OpenWalnut  1.4.0
WLogEntry.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <algorithm>
00026 #include <sstream>
00027 #include <string>
00028 
00029 #include <boost/algorithm/string.hpp>
00030 
00031 #include "WLogEntry.h"
00032 #include "WTerminalColor.h"
00033 
00034 LogLevel logLevelFromString( const std::string& str )
00035 {
00036     // get lower-case version
00037     std::string strLower = str; // NOTE: this reserves the needed space
00038     std::transform( str.begin(), str.end(), strLower.begin(), tolower );
00039     if( !strLower.compare( "debug" ) )
00040     {
00041         return LL_DEBUG;
00042     }
00043     else if( !strLower.compare( "info" ) )
00044     {
00045         return LL_INFO;
00046     }
00047     else if( !strLower.compare( "warning" ) )
00048     {
00049         return LL_WARNING;
00050     }
00051     else if( !strLower.compare( "error" ) )
00052     {
00053         return LL_ERROR;
00054     }
00055     return LL_DEBUG;
00056 }
00057 
00058 WLogEntry::WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source )
00059     : m_time( logTime ),
00060       m_message( message ),
00061       m_level( level ),
00062       m_source( source ),
00063       m_errorColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed ) ),
00064       m_infoColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGGreen ) ),
00065       m_debugColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGBlue ) ),
00066       m_warningColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGYellow ) ),
00067       m_sourceColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGMagenta ) ),
00068       m_timeColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGBlack ) ),
00069       m_messageColor( WTerminalColor() )
00070 {
00071 }
00072 
00073 WLogEntry::~WLogEntry()
00074 {
00075 }
00076 
00077 std::string WLogEntry::getLogString( std::string format, bool colors ) const
00078 {
00079     std::string s = format;
00080 
00081     m_errorColor.setEnabled( colors );
00082     m_infoColor.setEnabled( colors );
00083     m_debugColor.setEnabled( colors );
00084     m_warningColor.setEnabled( colors );
00085     m_sourceColor.setEnabled( colors );
00086     m_timeColor.setEnabled( colors );
00087     m_messageColor.setEnabled( colors );
00088 
00089     boost::ireplace_first( s, "%t", m_timeColor + m_time + !m_timeColor );
00090 
00091     switch( m_level )
00092     {
00093         case LL_DEBUG:
00094             boost::ireplace_first( s, "%l", m_debugColor + "DEBUG  " + !m_debugColor );
00095             break;
00096         case LL_INFO:
00097             boost::ireplace_first( s, "%l", m_infoColor + "INFO   " + !m_infoColor );
00098             break;
00099         case LL_WARNING:
00100             boost::ireplace_first( s, "%l", m_warningColor + "WARNING" + !m_warningColor );
00101             break;
00102         case LL_ERROR:
00103             boost::ireplace_first( s, "%l", m_errorColor + "ERROR  " + !m_errorColor );
00104             break;
00105         default:
00106             break;
00107     }
00108 
00109     boost::ireplace_first( s, "%m", m_messageColor + m_message + !m_messageColor );
00110 
00111     boost::ireplace_first( s, "%s", m_sourceColor + m_source + !m_sourceColor );
00112 
00113     return s;
00114 }
00115 
00116 LogLevel WLogEntry::getLogLevel() const
00117 {
00118     return m_level;
00119 }
00120 
00121 std::string WLogEntry::getMessage() const
00122 {
00123     return m_message;
00124 }
00125 
00126 std::string WLogEntry::getSource() const
00127 {
00128     return m_source;
00129 }
00130 
00131 std::string WLogEntry::getTime() const
00132 {
00133     return m_time;
00134 }
00135