The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
log.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <[email protected]>
3  2004 - 2015 by Guillaume Melquiond <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Standard logging facilities (interface).
19  *
20  * To use one of the standard log channels, put something like the following at the start
21  * of your .cpp file:
22  *
23  * static lg::log_domain log_display("display");
24  * #define ERR_DP LOG_STREAM(err, log_display)
25  * #define LOG_DP LOG_STREAM(info, log_display)
26  *
27  * Then stream logging info to ERR_DP, or LOG_DP, as if it were an ostream like std::cerr.
28  * (In general it will actually be std::cerr at runtime when logging is enabled.)
29  *
30  * LOG_DP << "Found a window resize event: ...\n";
31  *
32  * Please do not use iomanip features like std::hex directly on the logger. Because of the
33  * design of the logger, this will result in all of the loggers (in fact std::cerr) being
34  * imbued with std::hex. Please use a formatter instead.
35  *
36  * #include "formatter.hpp"
37  *
38  * LOG_DP << (formatter() << "The random seed is: '" << std::hex << seed << "'\n").str();
39  *
40  * It might be nice if somehow the logger class / macros could support using iomanip
41  * things directly, but right now it doesn't, and it seems that it would complicate the
42  * design greatly enough that it doesn't seem worth it.
43  */
44 
45 #ifndef LOG_HPP_INCLUDED
46 #define LOG_HPP_INCLUDED
47 
48 #ifndef __func__
49  #ifdef __FUNCTION__
50  #define __func__ __FUNCTION__
51  #endif
52 #endif
53 
54 #include <iostream> // needed else all files including log.hpp need to do it.
55 #include <sstream> // as above. iostream (actually, iosfwd) declares stringstream as an incomplete type, but does not define it
56 #include <string>
57 #include <utility>
58 
59 #include <boost/date_time/posix_time/posix_time_types.hpp>
60 
61 using boost::posix_time::ptime;
62 
63 namespace lg {
64 
65 /**
66  * Helper class to redirect the output of the logger in a certain scope.
67  *
68  * The main usage of the redirection is for the unit tests to validate the
69  * output on the logger with the expected output.
70  */
72 {
73 public:
74 
75  /**
76  * Constructor.
77  *
78  * @param stream The stream to direct the output to.
79  */
80  explicit tredirect_output_setter(std::ostream& stream);
81 
83 
84 private:
85 
86  /**
87  * The previously set redirection.
88  *
89  * This value is stored here to be restored in this destructor.
90  */
91  std::ostream* old_stream_;
92 };
93 
94 class logger;
95 
96 typedef std::pair<const std::string, int> logd;
97 
98 class log_domain {
99  logd *domain_;
100 public:
101  log_domain(char const *name);
102  friend class logger;
103 };
104 
106 bool set_log_domain_severity(std::string const &name, const logger &lg);
109 
110 void set_strict_severity(int severity);
111 void set_strict_severity(const logger &lg);
112 bool broke_strict();
113 
114 class logger {
115  char const *name_;
117 public:
118  logger(char const *name, int severity): name_(name), severity_(severity) {}
119  std::ostream &operator()(log_domain const &domain,
120  bool show_names = true, bool do_indent = false) const;
121 
122  bool dont_log(log_domain const &domain) const
123  {
124  return severity_ > domain.domain_->second;
125  }
126 
127  int get_severity() const
128  {
129  return severity_;
130  }
131 
133  {
134  return name_;
135  }
136 };
137 
138 void timestamps(bool);
139 void precise_timestamps(bool);
140 std::string get_timestamp(const time_t& t, const std::string& format="%Y%m%d %H:%M:%S ");
141 std::string get_timespan(const time_t& t);
142 
143 logger &err(), &warn(), &info(), &debug();
144 log_domain& general();
145 
147 {
148  ptime ticks_;
149  std::ostream *output_;
151 public:
152  scope_logger(log_domain const &domain, const char* str) :
153  output_(nullptr)
154  {
155  if (!debug().dont_log(domain)) do_log_entry(domain, str);
156  }
157  scope_logger(log_domain const &domain, const std::string& str) :
158  output_(nullptr)
159  {
160  if (!debug().dont_log(domain)) do_log_entry(domain, str);
161  }
163  {
164  if (output_) do_log_exit();
165  }
166  void do_indent() const;
167 private:
168  void do_log_entry(log_domain const &domain, const std::string& str);
169  void do_log_exit();
170 };
171 
172 /**
173  * Use this logger to send errors due to deprecated WML.
174  * The preferred format is:
175  * xxx is deprecated; support will be removed in version X. or
176  * xxx is deprecated; support has been removed in version X.
177  *
178  * After every wml-event the errors are shown to the user,
179  * so they can inform the campaign maintainer.
180  */
181 std::stringstream& wml_error();
182 
183 } // namespace lg
184 
185 #define log_scope(description) lg::scope_logger scope_logging_object__(lg::general(), description);
186 #define log_scope2(domain,description) lg::scope_logger scope_logging_object__(domain, description);
187 
188 #define LOG_STREAM(level, domain) if (lg::level().dont_log(domain)) ; else lg::level()(domain)
189 
190 // When using log_scope/log_scope2 it is nice to have all output indented.
191 #define LOG_STREAM_INDENT(level,domain) if (lg::level().dont_log(domain)) ; else lg::level()(domain, true, true)
192 
193 #endif
char const * name_
Definition: log.hpp:115
bool dont_log(log_domain const &domain) const
Definition: log.hpp:122
std::string get_timestamp(const time_t &t, const std::string &format)
Definition: log.cpp:174
logger(char const *name, int severity)
Definition: log.hpp:118
std::ostream & operator()(log_domain const &domain, bool show_names=true, bool do_indent=false) const
Definition: log.cpp:208
logger & info()
Definition: log.cpp:91
scope_logger(log_domain const &domain, const char *str)
Definition: log.hpp:152
std::string get_name() const
Definition: log.hpp:132
void timestamps(bool t)
Definition: log.cpp:76
std::string str_
Definition: log.hpp:150
void do_indent() const
Definition: log.cpp:256
int get_severity() const
Definition: log.hpp:127
GLdouble GLdouble t
Definition: glew.h:1366
std::pair< const std::string, int > logd
Definition: log.hpp:94
bool broke_strict()
Definition: log.cpp:170
GLuint GLuint stream
Definition: glew.h:5239
bool set_log_domain_severity(std::string const &name, int severity)
Definition: log.cpp:117
void do_log_entry(log_domain const &domain, const std::string &str)
Definition: log.cpp:238
log_domain(char const *name)
Definition: log.cpp:109
int severity_
Definition: log.hpp:116
ptime ticks_
Definition: log.hpp:148
std::ostream * old_stream_
The previously set redirection.
Definition: log.hpp:91
scope_logger(log_domain const &domain, const std::string &str)
Definition: log.hpp:157
std::ostream * output_
Definition: log.hpp:149
logger & debug()
Definition: log.cpp:97
GLenum severity
Definition: glew.h:2497
log_domain & general()
Definition: log.cpp:103
Definition: pump.hpp:42
std::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:262
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1222
logger & err()
Definition: log.cpp:79
void set_strict_severity(int severity)
Definition: log.cpp:160
std::string get_timespan(const time_t &t)
Definition: log.cpp:182
bool get_log_domain_severity(std::string const &name, int &severity)
Definition: log.cpp:141
tredirect_output_setter(std::ostream &stream)
Constructor.
Definition: log.cpp:62
GLuint const GLchar * name
Definition: glew.h:1782
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glew.h:3448
logger & warn()
Definition: log.cpp:85
std::string list_logdomains(const std::string &filter)
Definition: log.cpp:150
Helper class to redirect the output of the logger in a certain scope.
Definition: log.hpp:71
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void do_log_exit()
Definition: log.cpp:247
void precise_timestamps(bool pt)
Definition: log.cpp:77
logd * domain_
Definition: log.hpp:99