/*] Copyright (c) 2009-2010, Charles McGarvey [************************** **] All rights reserved. * * vi:ts=4 sw=4 tw=75 * * Distributable under the terms and conditions of the 2-clause BSD license; * see the file COPYING for a complete text of the license. * **************************************************************************/ #ifndef _MOOF_LOG_HH_ #define _MOOF_LOG_HH_ /** * \file log.hh * Functions related to logging the process. * The logging functions are log_error(), log_warning(), and log_info(), * listed from most critical to least critical. */ #include // exit #include namespace moof { class script; /** * A class for handling a log priority. There are two ways to log * messages: by treating a log object as a function whose parameters are * printed with default spacing, or by treating a log object as an output * stream. Either way, any object can be printed to the log as long as * there is an override for the ostream insertion operator. */ class log { public: /** * A type for the level or priority of a log message. */ enum level { none = 0, ///< Disable all logging. error = 1, ///< Log only errors. warning = 2, ///< Log warnings and errors. info = 3, ///< Log everything. }; /** * Set the lowest-priority log message that will be outputted to the * log. Any logging with a lower priority will be ignored. * \param level The log level. */ static void level(level level); /** * Get the current lowest-priority log level. If unchanged, the * default level is info. * \return The log level. */ static enum level level(); /** * Import log functions to a script. * \param The script. */ static void import(script& script); /** * Construct a log with a certain priority and prefix string. * \param level The log level. * \param prefix The string printed before each log message. */ log(enum level level, const char* prefix) : level_(level), prefix_(prefix) /* only pass literal strings */ {} template void operator () (const A& a) { *this << a << std::endl; } template void operator () (const A& a, const B& b) { *this << a << " " << b << std::endl; } template void operator () (const A& a, const B& b, const C& c) { *this << a << " " << b << " " << c << std::endl; } template void operator () (const A& a, const B& b, const C& c, const D& d) { *this << a << " " << b << " " << c << " " << d << std::endl; } template void operator () (const A& a, const B& b, const C& c, const D& d, const E& e) { *this << a << " " << b << " " << c << " " << d << " " << e << std::endl; } private: template friend std::ostream& operator << (log&, const T&); static enum level global_level_; enum level level_; const char* prefix_; }; extern std::ostream& log; extern std::ostream& null_log; extern class log log_error; extern class log log_warning; extern class log log_info; template inline std::ostream& operator << (class log& log, const T& item) { if (log::global_level_ < log.level_) return null_log; return moof::log << log.prefix_ << item; } } // namespace moof #endif // _MOOF_LOG_HH_