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