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