OpenWalnut  1.4.0
WLogStream.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WLOGSTREAM_H
00026 #define WLOGSTREAM_H
00027 
00028 #include <ostream>
00029 #include <string>
00030 #include <boost/shared_ptr.hpp>
00031 
00032 #include "WLogEntry.h"
00033 
00034 /**
00035  * Class implementing a capsule for an output stream and the needed level and format information.
00036  */
00037 class WLogStream // NOLINT
00038 {
00039 public:
00040     typedef boost::shared_ptr< WLogStream > SharedPtr;  //!< shared pointer type
00041     typedef WLogStream* Ptr; //!< pointer type
00042     typedef WLogStream& Ref; //!< reference
00043     typedef const WLogStream& ConstRef; //!< const reference
00044 
00045     /**
00046      * Constructor. Create a new stream instance. The output stream is a mandatory parameter. The others are predefined with some defaults.
00047      *
00048      * \param output the stream where to print log messages to
00049      * \param logLevel logging level, i.e. verboseness
00050      * \param format the format used for output
00051      * \param colored true if coloring should be used.
00052      */
00053     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
00054 
00055     /**
00056      * Prints the specified entry to the output stream in the right format if the log level matches.
00057      *
00058      * \param entry the entry to print-
00059      */
00060     void printEntry( const WLogEntry& entry );
00061 
00062     /**
00063      * Sets the new log level. All new incoming logs will be filtered according to this level.
00064      *
00065      * \param logLevel the level
00066      */
00067     void setLogLevel( LogLevel logLevel );
00068 
00069     /**
00070      * Gets the currently set log level.
00071      *
00072      * \return the current log level
00073      */
00074     LogLevel getLogLevel() const;
00075 
00076     /**
00077      * Sets the format string.
00078      *
00079      * \param format the format string.
00080      */
00081     void setFormat( std::string format );
00082 
00083     /**
00084      * Returns the currently set format string.
00085      *
00086      * \return format string.
00087      */
00088     std::string getFormat() const;
00089 
00090     /**
00091      * Set whether to use colors or not. Note: this is only useful on Linux systems currently.
00092      *
00093      * \param colors true if colors should be used.
00094      */
00095     void setColored( bool colors );
00096 
00097     /**
00098      * Getter determining whether to use colors or not.
00099      *
00100      * \return true if colors should be used.
00101      */
00102     bool isColored() const;
00103 
00104 private:
00105     /**
00106      * Disallow copy.
00107      *
00108      * \param rhs the stream to copy
00109      */
00110     WLogStream( const WLogStream& rhs );
00111 
00112     /**
00113      * Disallow assignment.
00114      *
00115      * \param rhs the stream to assign to this
00116      *
00117      * \return this
00118      */
00119     WLogStream& operator=( const WLogStream& rhs );
00120 
00121     /**
00122      * The output stream.
00123      */
00124     std::ostream& m_output;
00125 
00126     /**
00127      * The logging level. All messages below this level are discarded.
00128      */
00129     LogLevel m_logLevel;
00130 
00131     /**
00132      * The format of the message.
00133      */
00134     std::string m_format;
00135 
00136     /**
00137      * True if colors should be used. This requires a compatible terminal.
00138      */
00139     bool m_color;
00140 };
00141 
00142 #endif  // WLOGSTREAM_H
00143