OpenWalnut  1.5.0dev
WLogEntry.h
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 #ifndef WLOGENTRY_H
26 #define WLOGENTRY_H
27 
28 #include <string>
29 
30 #include "WTerminalColor.h"
31 
32 
33 /**
34  * Various log levels, to distinguish output on its level.
35  */
36 typedef enum
37 {
38  LL_DEBUG = 0,
39  LL_INFO,
40  LL_WARNING,
41  LL_ERROR
42 }
43 LogLevel;
44 
45 /**
46  * Simple function to convert a given string to an log level. If the string is invalid, LL_DEBUG is returned.
47  *
48  * \param str the string containing the log level string-representation
49  * \return the loglevel
50  */
51 LogLevel logLevelFromString( const std::string& str );
52 
53 /**
54  * Represents a simple log message with some attributes.
55  */
56 class WLogEntry // NOLINT
57 {
58 public:
59  /**
60  * Creates a new log message.
61  *
62  * \param logTime the time
63  * \param message the message
64  * \param level the log level
65  * \param source the source, sending the log
66  */
67  WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source = "" );
68 
69  /**
70  * Destroys a log message entry.
71  */
72  virtual ~WLogEntry();
73 
74  /**
75  * \param format A string describing the output format in c printf style
76  * \param colors True if colors should be used. True is the default.
77  *
78  * \return String of this log entry.
79  */
80  std::string getLogString( std::string format = "[%t] *%l* %m \n", bool colors = true ) const;
81 
82  /**
83  * \return log level of this entry.
84  */
85  LogLevel getLogLevel() const;
86 
87  /**
88  * Returns the plain message of the entry.
89  *
90  * \return the message
91  */
92  std::string getMessage() const;
93 
94  /**
95  * Returns the sender of the log.
96  *
97  * \return sender
98  */
99  std::string getSource() const;
100 
101  /**
102  * Returns the formatted time string.
103  *
104  * \return time string
105  */
106  std::string getTime() const;
107 
108 protected:
109 private:
110  /**
111  * The time the log message was received
112  */
113  std::string m_time;
114 
115  /**
116  * The actual message
117  */
118  std::string m_message;
119 
120  /**
121  * Log level
122  */
123  LogLevel m_level;
124 
125  /**
126  * Source (e.g. module name) where this log message comes from.
127  */
128  std::string m_source;
129 
130  /**
131  * Color used for error logs.
132  *
133  * \note it is mutable to allow en-/disabling the colors during getLogString.
134  */
136 
137  /**
138  * Color used for info logs
139  *
140  * \note it is mutable to allow en-/disabling the colors during getLogString.
141  */
143 
144  /**
145  * Color used for debug logs.
146  *
147  * \note it is mutable to allow en-/disabling the colors during getLogString.
148  */
150 
151  /**
152  * Color used for warning logs.
153  *
154  * \note it is mutable to allow en-/disabling the colors during getLogString.
155  */
157 
158  /**
159  * Color used for source field.
160  *
161  * \note it is mutable to allow en-/disabling the colors during getLogString.
162  */
164 
165  /**
166  * Color used for time.
167  *
168  * \note it is mutable to allow en-/disabling the colors during getLogString.
169  */
171 
172  /**
173  * Color used for the message.
174  *
175  * \note it is mutable to allow en-/disabling the colors during getLogString.
176  */
178 };
179 
180 #endif // WLOGENTRY_H
181 
Represents a simple log message with some attributes.
Definition: WLogEntry.h:57
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.