log.hpp

Go to the documentation of this file.
00001 /* $Id: log.hpp 25760 2008-04-12 09:15:53Z mordante $ */
00002 /*
00003    Copyright (C) 2003 by David White <dave@whitevine.net>
00004                  2004 - 2008 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 version 2
00009    or at your option any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 //! @file log.hpp
00017 //!
00018 
00019 #ifndef LOG_HPP_INCLUDED
00020 #define LOG_HPP_INCLUDED
00021 
00022 #include <iosfwd>
00023 #include <iostream>
00024 #include <string>
00025 #include <vector>
00026 
00027 namespace lg {
00028 
00029 class logger;
00030 
00031 struct logd {
00032     char const *name_;
00033     int severity_;
00034 };
00035 
00036 class log_domain {
00037     int domain_;
00038 public:
00039     log_domain(char const *name);
00040     friend class logger;
00041 };
00042 
00043 //exposed to make inlining possible
00044 extern std::vector<logd> log_domains;
00045 
00046 bool set_log_domain_severity(std::string const &name, int severity);
00047 std::string list_logdomains();
00048 
00049 class logger {
00050     char const *name_;
00051     int severity_;
00052 public:
00053     logger(char const *name, int severity): name_(name), severity_(severity) {}
00054     std::ostream &operator()(log_domain const &domain, 
00055         bool show_names = true, bool do_indent = false) const;
00056 
00057     bool dont_log(log_domain const &domain) const
00058     {
00059         logd const &d = log_domains[domain.domain_];
00060         return severity_ > d.severity_;
00061     }   
00062 };
00063 
00064 void timestamps(bool);
00065 std::string get_timestamp(const time_t& t, const std::string& format="%Y%m%d %T ");
00066 
00067 extern logger err, warn, info, debug;
00068 extern log_domain general, ai, config, display, engine, network, mp_server,
00069     filesystem, audio, notifs, replay, help, gui, gui_parse, gui_draw, gui_event;
00070 
00071 class scope_logger
00072 {
00073     int ticks_;
00074     std::ostream *output_;
00075     const char* str_;
00076 public:
00077     scope_logger(log_domain const &domain, const char* str)
00078     : output_(0)
00079     {
00080         if (!debug.dont_log(domain)) do_log_entry(domain, str);
00081     }
00082     scope_logger(log_domain const &domain, const std::string& str)
00083     : output_(0)
00084     {
00085         if (!debug.dont_log(domain)) do_log_entry(domain, str.c_str());
00086     }
00087     ~scope_logger()
00088     {
00089         if (output_) do_log_exit();
00090     }
00091     void do_indent() const;
00092 private:
00093     void do_log_entry(log_domain const &domain, const char* str);
00094     void do_log_exit();
00095 };
00096 
00097 /**
00098  * Use this logger to send errors due to deprecated WML.
00099  * The preferred format is:
00100  * xxx is deprecated, support will be removed in version X. or
00101  * xxx is deprecated, support has been removed in version X.
00102  *
00103  * After every wml-event the errors are shown to the user,
00104  * so they can inform the campaign maintainer.
00105  */
00106 extern std::stringstream wml_error;
00107 
00108 } // namespace lg
00109 
00110 #define log_scope(a) lg::scope_logger scope_logging_object__(lg::general, a);
00111 #define log_scope2(a,b) lg::scope_logger scope_logging_object__(lg::a, b);
00112 
00113 #define LOG_STREAM(a, b) if (lg::a.dont_log(lg::b)) ; else lg::a(lg::b)
00114 
00115 // When using log_scope/log_scope2 it is nice to have all output indented.
00116 #define LOG_STREAM_INDENT(a,b) if (lg::a.dont_log(lg::b)) ; else lg::a(lg::b, true, true)
00117 
00118 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs