src/Logger.hh
author Tero Marttila <terom@fixme.fi>
Thu, 22 Jan 2009 02:38:33 +0200
branchnew_graphics
changeset 418 194bc810a570
parent 365 65295dfbbf64
permissions -rw-r--r--
add --log-level option, improve Config/Logger documentation, fix NETWORK_EANBLED typos in Engine
#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 */