src/Logger.hh
author nireco
Sat, 31 Jan 2009 12:33:08 +0200
changeset 443 5d1119729f58
parent 418 194bc810a570
permissions -rw-r--r--
worm02 two pics to comment
#ifndef LOGGER_HH
#define LOGGER_HH

#include "Types.hh"

#include <iostream>

/**
 * Utility class used for logging, use Engine::log() to construct these. Implements the same streaming behaviour as 
 * std::ostream, but the construtor prints out a header, and the destructor a newline. 
 *
 * Useage example:
 * 
 * \code
 *      Engine::log(INFO, "foo.bar") << "value of quux=" << quux;
 *
 * \endcode
 *
 * This will result in output like the following to stdout:
 *
 * \verbatim
 * INFO [foo.bar] value of quux=5
 * \endverbatim
 *
 * @see Engine::log
 */
class Logger {
    private:
        /**
         * The stream we output to
         */
        std::ostream &stream;

        /**
         * Do we even care?
         */
        bool show;

    public:
        /**
         * If the given level is smaller than the given maximum level, output the header. Otherwise, act as a
         * no-op.
         */
        Logger (std::ostream &stream, LogLevel level, const char *module, LogLevel max_level);
        
        /**
         * Stream the given value to our output stream, just like with std::ostream, unless we're acting as a no-op.
         */
        template <typename T> Logger& operator<< (const T val);
        
        /**
         * Output the final newline
         */
        ~Logger (void);
};

/*
 * Logger template method definition
 */
template <typename T> 
inline Logger& Logger::operator<< (const T val) {
    if (show)
        stream << val;
    
    // chaining
    return *this;
}

#endif /* LOGGER_HH */