OpenWalnut  1.4.0
WLogStream.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 WLOGSTREAM_H
26 #define WLOGSTREAM_H
27 
28 #include <ostream>
29 #include <string>
30 #ifndef Q_MOC_RUN
31 #include <boost/shared_ptr.hpp>
32 #endif
33 
34 #include "WLogEntry.h"
35 
36 /**
37  * Class implementing a capsule for an output stream and the needed level and format information.
38  */
39 class WLogStream // NOLINT
40 {
41 public:
42  typedef boost::shared_ptr< WLogStream > SharedPtr; //!< shared pointer type
43  typedef WLogStream* Ptr; //!< pointer type
44  typedef WLogStream& Ref; //!< reference
45  typedef const WLogStream& ConstRef; //!< const reference
46 
47  /**
48  * Constructor. Create a new stream instance. The output stream is a mandatory parameter. The others are predefined with some defaults.
49  *
50  * \param output the stream where to print log messages to
51  * \param logLevel logging level, i.e. verboseness
52  * \param format the format used for output
53  * \param colored true if coloring should be used.
54  */
55  WLogStream( std::ostream& output, LogLevel logLevel = LL_DEBUG, std::string format = "*%l [%s] %m \n", bool colored = true ); // NOLINT - we need this non-const ref here
56 
57  /**
58  * Prints the specified entry to the output stream in the right format if the log level matches.
59  *
60  * \param entry the entry to print-
61  */
62  void printEntry( const WLogEntry& entry );
63 
64  /**
65  * Sets the new log level. All new incoming logs will be filtered according to this level.
66  *
67  * \param logLevel the level
68  */
69  void setLogLevel( LogLevel logLevel );
70 
71  /**
72  * Gets the currently set log level.
73  *
74  * \return the current log level
75  */
76  LogLevel getLogLevel() const;
77 
78  /**
79  * Sets the format string.
80  *
81  * \param format the format string.
82  */
83  void setFormat( std::string format );
84 
85  /**
86  * Returns the currently set format string.
87  *
88  * \return format string.
89  */
90  std::string getFormat() const;
91 
92  /**
93  * Set whether to use colors or not. Note: this is only useful on Linux systems currently.
94  *
95  * \param colors true if colors should be used.
96  */
97  void setColored( bool colors );
98 
99  /**
100  * Getter determining whether to use colors or not.
101  *
102  * \return true if colors should be used.
103  */
104  bool isColored() const;
105 
106 private:
107  /**
108  * Disallow copy.
109  *
110  * \param rhs the stream to copy
111  */
112  WLogStream( const WLogStream& rhs );
113 
114  /**
115  * Disallow assignment.
116  *
117  * \param rhs the stream to assign to this
118  *
119  * \return this
120  */
121  WLogStream& operator=( const WLogStream& rhs );
122 
123  /**
124  * The output stream.
125  */
126  std::ostream& m_output;
127 
128  /**
129  * The logging level. All messages below this level are discarded.
130  */
131  LogLevel m_logLevel;
132 
133  /**
134  * The format of the message.
135  */
136  std::string m_format;
137 
138  /**
139  * True if colors should be used. This requires a compatible terminal.
140  */
141  bool m_color;
142 };
143 
144 #endif // WLOGSTREAM_H
145 
Class implementing a capsule for an output stream and the needed level and format information...
Definition: WLogStream.h:39
void printEntry(const WLogEntry &entry)
Prints the specified entry to the output stream in the right format if the log level matches...
Definition: WLogStream.cpp:39
WLogStream(std::ostream &output, LogLevel logLevel=LL_DEBUG, std::string format="*%l [%s] %m \n", bool colored=true)
Constructor.
Definition: WLogStream.cpp:30
std::string getFormat() const
Returns the currently set format string.
Definition: WLogStream.cpp:66
LogLevel m_logLevel
The logging level.
Definition: WLogStream.h:131
const WLogStream & ConstRef
const reference
Definition: WLogStream.h:45
void setLogLevel(LogLevel logLevel)
Sets the new log level.
Definition: WLogStream.cpp:51
WLogStream & operator=(const WLogStream &rhs)
Disallow assignment.
boost::shared_ptr< WLogStream > SharedPtr
shared pointer type
Definition: WLogStream.h:42
WLogStream * Ptr
pointer type
Definition: WLogStream.h:43
Represents a simple log message with some attributes.
Definition: WLogEntry.h:56
bool m_color
True if colors should be used.
Definition: WLogStream.h:141
LogLevel getLogLevel() const
Gets the currently set log level.
Definition: WLogStream.cpp:56
bool isColored() const
Getter determining whether to use colors or not.
Definition: WLogStream.cpp:76
void setColored(bool colors)
Set whether to use colors or not.
Definition: WLogStream.cpp:71
std::string m_format
The format of the message.
Definition: WLogStream.h:136
WLogStream & Ref
reference
Definition: WLogStream.h:44
std::ostream & m_output
The output stream.
Definition: WLogStream.h:126
void setFormat(std::string format)
Sets the format string.
Definition: WLogStream.cpp:61