OpenWalnut  1.5.0dev
WLogEntry.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <algorithm>
26 #include <sstream>
27 #include <string>
28 
29 #include <boost/algorithm/string.hpp>
30 
31 #include "WLogEntry.h"
32 #include "WTerminalColor.h"
33 
34 LogLevel logLevelFromString( const std::string& str )
35 {
36  // get lower-case version
37  std::string strLower = str; // NOTE: this reserves the needed space
38  std::transform( str.begin(), str.end(), strLower.begin(), tolower );
39  if( !strLower.compare( "debug" ) )
40  {
41  return LL_DEBUG;
42  }
43  else if( !strLower.compare( "info" ) )
44  {
45  return LL_INFO;
46  }
47  else if( !strLower.compare( "warning" ) )
48  {
49  return LL_WARNING;
50  }
51  else if( !strLower.compare( "error" ) )
52  {
53  return LL_ERROR;
54  }
55  return LL_DEBUG;
56 }
57 
58 WLogEntry::WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source )
59  : m_time( logTime ),
60  m_message( message ),
61  m_level( level ),
62  m_source( source ),
63  m_errorColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed ) ),
64  m_infoColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGGreen ) ),
65  m_debugColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGBlue ) ),
66  m_warningColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGYellow ) ),
67  m_sourceColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGMagenta ) ),
68  m_timeColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGBlack ) ),
69  m_messageColor( WTerminalColor() )
70 {
71 }
72 
74 {
75 }
76 
77 std::string WLogEntry::getLogString( std::string format, bool colors ) const
78 {
79  std::string s = format;
80 
81  m_errorColor.setEnabled( colors );
82  m_infoColor.setEnabled( colors );
83  m_debugColor.setEnabled( colors );
84  m_warningColor.setEnabled( colors );
85  m_sourceColor.setEnabled( colors );
86  m_timeColor.setEnabled( colors );
87  m_messageColor.setEnabled( colors );
88 
89  boost::ireplace_first( s, "%t", m_timeColor + m_time + !m_timeColor );
90 
91  switch( m_level )
92  {
93  case LL_DEBUG:
94  boost::ireplace_first( s, "%l", m_debugColor + "DEBUG " + !m_debugColor );
95  break;
96  case LL_INFO:
97  boost::ireplace_first( s, "%l", m_infoColor + "INFO " + !m_infoColor );
98  break;
99  case LL_WARNING:
100  boost::ireplace_first( s, "%l", m_warningColor + "WARNING" + !m_warningColor );
101  break;
102  case LL_ERROR:
103  boost::ireplace_first( s, "%l", m_errorColor + "ERROR " + !m_errorColor );
104  break;
105  default:
106  break;
107  }
108 
109  boost::ireplace_first( s, "%m", m_messageColor + m_message + !m_messageColor );
110 
111  boost::ireplace_first( s, "%s", m_sourceColor + m_source + !m_sourceColor );
112 
113  return s;
114 }
115 
116 LogLevel WLogEntry::getLogLevel() const
117 {
118  return m_level;
119 }
120 
121 std::string WLogEntry::getMessage() const
122 {
123  return m_message;
124 }
125 
126 std::string WLogEntry::getSource() const
127 {
128  return m_source;
129 }
130 
131 std::string WLogEntry::getTime() const
132 {
133  return m_time;
134 }
135 
virtual ~WLogEntry()
Destroys a log message entry.
Definition: WLogEntry.cpp:73
std::string m_source
Source (e.g.
Definition: WLogEntry.h:128
WLogEntry(std::string logTime, std::string message, LogLevel level, std::string source="")
Creates a new log message.
Definition: WLogEntry.cpp:58
std::string m_message
The actual message.
Definition: WLogEntry.h:118
std::string getLogString(std::string format="[%t] *%l* %m \n", bool colors=true) const
Definition: WLogEntry.cpp:77
LogLevel m_level
Log level.
Definition: WLogEntry.h:123
LogLevel getLogLevel() const
Definition: WLogEntry.cpp:116
WTerminalColor m_timeColor
Color used for time.
Definition: WLogEntry.h:170
std::string getMessage() const
Returns the plain message of the entry.
Definition: WLogEntry.cpp:121
WTerminalColor m_errorColor
Color used for error logs.
Definition: WLogEntry.h:135
WTerminalColor m_sourceColor
Color used for source field.
Definition: WLogEntry.h:163
WTerminalColor m_messageColor
Color used for the message.
Definition: WLogEntry.h:177
std::string m_time
The time the log message was received.
Definition: WLogEntry.h:113
std::string getSource() const
Returns the sender of the log.
Definition: WLogEntry.cpp:126
std::string getTime() const
Returns the formatted time string.
Definition: WLogEntry.cpp:131
WTerminalColor m_warningColor
Color used for warning logs.
Definition: WLogEntry.h:156
WTerminalColor m_infoColor
Color used for info logs.
Definition: WLogEntry.h:142
WTerminalColor m_debugColor
Color used for debug logs.
Definition: WLogEntry.h:149
Helper class to provide a convenient way to colorize output on the console.
void setEnabled(bool enabled)
With this you can easily trigger whether the color control string is used or if "" is returned.