log.hpp

Go to the documentation of this file.
00001 /* $Id: log.hpp 53742 2012-04-01 08:23:04Z mordante $ */
00002 /*
00003    Copyright (C) 2003 by David White <dave@whitevine.net>
00004                  2004 - 2012 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
00015 */
00016 
00017 /**
00018  * @file
00019  * Standard logging facilities (interface).
00020  */
00021 
00022 #ifndef LOG_HPP_INCLUDED
00023 #define LOG_HPP_INCLUDED
00024 
00025 #ifndef __func__
00026  #ifdef __FUNCTION__
00027   #define __func__ __FUNCTION__
00028  #endif
00029 #endif
00030 
00031 #include <iostream> // needed else all files including log.hpp need to do it.
00032 #include <sstream> // as above. iostream (actually, iosfwd) declares stringstream as an incomplete type, but does not define it
00033 #include <string>
00034 #include <utility>
00035 
00036 namespace lg {
00037 
00038 /**
00039  * Helper class to redirect the output of the logger in a certain scope.
00040  *
00041  * The main usage of the redirection is for the unit tests to validate the
00042  * ourput on the logger with the expected output.
00043  */
00044 class tredirect_output_setter
00045 {
00046 public:
00047 
00048     /**
00049      * Constructor.
00050      *
00051      * @param stream              The stream to direct the output to.
00052      */
00053     explicit tredirect_output_setter(std::ostream& stream);
00054 
00055     ~tredirect_output_setter();
00056 
00057 private:
00058 
00059     /**
00060      * The previously set redirection.
00061      *
00062      * This value is stored here to be restored in this destructor.
00063      */
00064     std::ostream* old_stream_;
00065 };
00066 
00067 class logger;
00068 
00069 typedef std::pair<const std::string, int> logd;
00070 
00071 class log_domain {
00072     logd *domain_;
00073 public:
00074     log_domain(char const *name);
00075     friend class logger;
00076 };
00077 
00078 bool set_log_domain_severity(std::string const &name, int severity);
00079 std::string list_logdomains(const std::string& filter);
00080 
00081 class logger {
00082     char const *name_;
00083     int severity_;
00084 public:
00085     logger(char const *name, int severity): name_(name), severity_(severity) {}
00086     std::ostream &operator()(log_domain const &domain,
00087         bool show_names = true, bool do_indent = false) const;
00088 
00089     bool dont_log(log_domain const &domain) const
00090     {
00091         return severity_ > domain.domain_->second;
00092     }
00093 };
00094 
00095 void timestamps(bool);
00096 std::string get_timestamp(const time_t& t, const std::string& format="%Y%m%d %H:%M:%S ");
00097 
00098 extern logger err, warn, info, debug;
00099 extern log_domain general;
00100 
00101 class scope_logger
00102 {
00103     int ticks_;
00104     std::ostream *output_;
00105     std::string str_;
00106 public:
00107     scope_logger(log_domain const &domain, const char* str) :
00108         ticks_(0),
00109         output_(NULL),
00110         str_()
00111     {
00112         if (!debug.dont_log(domain)) do_log_entry(domain, str);
00113     }
00114     scope_logger(log_domain const &domain, const std::string& str) :
00115         ticks_(0),
00116         output_(NULL),
00117         str_()
00118     {
00119         if (!debug.dont_log(domain)) do_log_entry(domain, str);
00120     }
00121     ~scope_logger()
00122     {
00123         if (output_) do_log_exit();
00124     }
00125     void do_indent() const;
00126 private:
00127     void do_log_entry(log_domain const &domain, const std::string& str);
00128     void do_log_exit();
00129 };
00130 
00131 /**
00132  * Use this logger to send errors due to deprecated WML.
00133  * The preferred format is:
00134  * xxx is deprecated, support will be removed in version X. or
00135  * xxx is deprecated, support has been removed in version X.
00136  *
00137  * After every wml-event the errors are shown to the user,
00138  * so they can inform the campaign maintainer.
00139  */
00140 extern std::stringstream wml_error;
00141 
00142 } // namespace lg
00143 
00144 #define log_scope(a) lg::scope_logger scope_logging_object__(lg::general, a);
00145 #define log_scope2(a,b) lg::scope_logger scope_logging_object__(a, b);
00146 
00147 #define LOG_STREAM(a, b) if (lg::a.dont_log(b)) ; else lg::a(b)
00148 
00149 // When using log_scope/log_scope2 it is nice to have all output indented.
00150 #define LOG_STREAM_INDENT(a,b) if (lg::a.dont_log(b)) ; else lg::a(b, true, true)
00151 
00152 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:54 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs