The Battle for Wesnoth  1.19.0-dev
log.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2024
3  by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 /**
18  * @file
19  * Standard logging facilities (interface).
20  *
21  * To use one of the standard log channels, put something like the following at the start
22  * of your .cpp file:
23  *
24  * static lg::log_domain log_display("display");
25  * \#define ERR_DP LOG_STREAM(err, log_display)
26  * \#define LOG_DP LOG_STREAM(info, log_display)
27  *
28  * Then stream logging info to ERR_DP, or LOG_DP, as if it were an ostream like std::cerr.
29  * (In general it will actually be std::cerr at runtime when logging is enabled.)
30  *
31  * LOG_DP << "Found a window resize event: ...";
32  *
33  * Please do not use iomanip features like std::hex directly on the logger. Because of the
34  * design of the logger, this will result in all of the loggers (in fact std::cerr) being
35  * imbued with std::hex. Please use a formatter instead.
36  *
37  * \#include "formatter.hpp"
38  *
39  * LOG_DP << (formatter() << "The random seed is: '" << std::hex << seed << "'\n").str();
40  *
41  * It might be nice if somehow the logger class / macros could support using iomanip
42  * things directly, but right now it doesn't, and it seems that it would complicate the
43  * design greatly enough that it doesn't seem worth it.
44  */
45 
46 #pragma once
47 
48 #ifndef __func__
49  #ifdef __FUNCTION__
50  #define __func__ __FUNCTION__
51  #endif
52 #endif
53 
54 #include <iosfwd> // needed else all files including log.hpp need to do it.
55 #include <optional>
56 #include <string>
57 #include <utility>
58 #include <ctime>
59 #include <cstdint>
60 
61 #include "formatter.hpp"
62 
63 namespace lg {
64 
65 // Prefix and extension for log files.
66 // This is used to find old files to delete.
67 const std::string log_file_prefix = "wesnoth-";
68 const std::string log_file_suffix = ".log";
69 // stdout file for Windows
70 const std::string out_log_file_suffix = ".out.log";
71 
72 // Maximum number of older log files to keep intact. Other files are deleted.
73 // Note that this count does not include the current log file!
74 // double for Windows due to the separate .log and .out.log files
75 const unsigned max_logs = 8
76 #ifdef _WIN32
77 *2
78 #endif
79 ;
80 
81 enum class severity
82 {
83  LG_NONE=-1,
84  LG_ERROR=0,
85  LG_WARN=1,
86  LG_INFO=2,
87  LG_DEBUG=3
88 };
89 std::ostringstream& operator<<(std::ostringstream& oss, lg::severity severity);
90 
91 /**
92  * Helper class to redirect the output of the logger in a certain scope.
93  *
94  * The main usage of the redirection is for the unit tests to validate the
95  * output on the logger with the expected output.
96  */
98 {
99 public:
100 
101  /**
102  * Constructor.
103  *
104  * @param stream The stream to direct the output to.
105  */
106  explicit redirect_output_setter(std::ostream& stream);
107 
109 
110 private:
111 
112  /**
113  * The previously set redirection.
114  *
115  * This value is stored here to be restored in this destructor.
116  */
117  std::ostream* old_stream_;
118 };
119 
120 class logger;
121 
122 typedef std::pair<const std::string, severity> logd;
123 
124 class log_domain {
126 public:
127  explicit log_domain(char const *name, severity severity = severity::LG_WARN);
128  friend class logger;
129 };
130 
131 bool set_log_domain_severity(const std::string& name, severity severity);
132 bool set_log_domain_severity(const std::string& name, const logger &lg);
133 bool get_log_domain_severity(const std::string& name, severity &severity);
134 std::string list_log_domains(const std::string& filter);
135 
137 void set_strict_severity(const logger &lg);
138 bool broke_strict();
139 
140 /**
141  * Do the initial redirection to a log file if the logs directory is writable.
142  * Also performs log rotation to delete old logs.
143  * NOTE: This runs before command line arguments are processed.
144  * Therefore the log file is initially written under the default userdata directory
145  */
146 void set_log_to_file();
147 /**
148  * Move the log file to another directory.
149  * Used if a custom userdata directory is given as a command line option to move it to the new location.
150  */
151 void move_log_file();
152 /**
153  * Checks that a dummy file can be written to and deleted from the logs directory.
154  */
156 /**
157  * Returns the result set by check_log_dir_writable().
158  * Will not be set if called before log redirection is done.
159  *
160  * @return true if the log directory is writable, false otherwise.
161  */
162 std::optional<bool> log_dir_writable();
163 
164 /**
165  * Use the defined prefix and suffix to determine if a filename is a log file.
166  *
167  * @return true if it's a log file, false otherwise
168  */
169 bool is_not_log_file(const std::string& filename);
170 /**
171  * Check how many log files exist and delete the oldest when there's too many.
172  */
173 void rotate_logs(const std::string& log_dir);
174 /**
175  * Generate a unique file name using the current timestamp and a randomly generated number.
176  *
177  * @return A unique file name to use for the current log file.
178  */
179 std::string unique_log_filename();
180 
181 // A little "magic" to surround the logging operation in a mutex.
182 // This works by capturing the output first to a stringstream formatter, then
183 // locking a mutex and dumping it to the stream all in one go.
184 // By doing this we can avoid rare deadlocks if a function whose output is streamed
185 // calls logging of its own.
186 // We overload operator| only because it has lower precedence than operator<<
187 // Any other lower-precedence operator would have worked just as well.
189  std::ostream& stream_;
190  int indent_ = 0;
191  bool timestamp_ = false;
192  std::string prefix_;
193  bool auto_newline_ = true;
194 public:
195  log_in_progress(std::ostream& stream);
196  void operator|(formatter&& message);
197  void set_indent(int level);
198  void enable_timestamp();
199  void set_prefix(const std::string& prefix);
200  void set_auto_newline(bool enabled);
201 };
202 
203 class logger {
204  char const *name_;
206 public:
207  logger(char const *name, severity severity): name_(name), severity_(severity) {}
208  log_in_progress operator()(const log_domain& domain,
209  bool show_names = true, bool do_indent = false, bool show_timestamps = true, bool break_strict = true, bool auto_newline = true) const;
210 
211  bool dont_log(const log_domain& domain) const
212  {
213  return severity_ > domain.domain_->second;
214  }
215 
217  {
218  return severity_;
219  }
220 
221  std::string get_name() const
222  {
223  return name_;
224  }
225 };
226 
227 void timestamps(bool);
228 void precise_timestamps(bool);
229 std::string get_timestamp(const std::time_t& t, const std::string& format="%Y%m%d %H:%M:%S ");
230 std::string get_timespan(const std::time_t& t);
231 std::string sanitize_log(const std::string& logstr);
232 std::string get_log_file_path();
233 
234 logger &err(), &warn(), &info(), &debug();
235 log_domain& general();
236 
238 {
239  int64_t ticks_;
241  std::string str_;
242 public:
243  scope_logger(const log_domain& domain, const char* str)
244  : ticks_(0)
245  , domain_(domain)
246  , str_()
247  {
248  if (!debug().dont_log(domain)) do_log_entry(str);
249  }
250  scope_logger(const log_domain& domain, const std::string& str)
251  : ticks_(0)
252  , domain_(domain)
253  , str_()
254  {
255  if (!debug().dont_log(domain)) do_log_entry(str);
256  }
258  {
259  if (!str_.empty()) do_log_exit();
260  }
261 private:
262  void do_log_entry(const std::string& str) noexcept;
263  void do_log_exit() noexcept;
264 };
265 
266 /**
267  * Use this to show WML errors in the ingame chat.
268  * After every WML event the errors are shown to the user so they can inform the campaign maintainer.
269  */
270 std::stringstream& log_to_chat();
271 
272 } // namespace lg
273 
274 #define log_scope(description) lg::scope_logger scope_logging_object__(lg::general(), description);
275 #define log_scope2(domain,description) lg::scope_logger scope_logging_object__(domain, description);
276 
277 #define LOG_STREAM(level, domain) if (lg::level().dont_log(domain)) ; else lg::level()(domain) | formatter()
278 
279 // Don't prefix the logdomain to messages on this stream
280 #define LOG_STREAM_NAMELESS(level, domain) if (lg::level().dont_log(domain)) ; else lg::level()(domain, false) | formatter()
281 
282 // Like LOG_STREAM_NAMELESS except doesn't add newlines automatically
283 #define LOG_STREAM_NAMELESS_STREAMING(level, domain) if (lg::level().dont_log(domain)) ; else lg::level()(domain, false, false, true, true, false) | formatter()
284 
285 // When using log_scope/log_scope2 it is nice to have all output indented.
286 #define LOG_STREAM_INDENT(level,domain) if (lg::level().dont_log(domain)) ; else lg::level()(domain, true, true) | formatter()
287 
288 // If you have an explicit logger object and want to ignore the logging level, use this.
289 // Meant for cases where you explicitly call dont_log to avoid an expensive operation if the logging is disabled.
290 #define FORCE_LOG_TO(logger, domain) logger(domain) | formatter()
291 
292 // always log (since it's at the error level) to the general log stream
293 // outputting the log domain and timestamp is disabled
294 // meant as a replacement to using cerr/cout, but that goes through the same logging infrastructure as everything else
295 #define PLAIN_LOG lg::err()(lg::general(), false, false, false, false, true) | formatter()
296 #define STREAMING_LOG lg::err()(lg::general(), false, false, false, false, false) | formatter()
double t
Definition: astarsearch.cpp:63
std::ostringstream wrapper.
Definition: formatter.hpp:40
logd * domain_
Definition: log.hpp:125
log_domain(char const *name, severity severity=severity::LG_WARN)
Definition: log.cpp:333
void operator|(formatter &&message)
Definition: log.cpp:487
std::string prefix_
Definition: log.hpp:192
std::ostream & stream_
Definition: log.hpp:189
void set_auto_newline(bool enabled)
Definition: log.cpp:517
void set_prefix(const std::string &prefix)
Definition: log.cpp:513
void enable_timestamp()
Definition: log.cpp:509
bool auto_newline_
Definition: log.hpp:193
void set_indent(int level)
Definition: log.cpp:505
log_in_progress(std::ostream &stream)
Definition: log.cpp:483
char const * name_
Definition: log.hpp:204
std::string get_name() const
Definition: log.hpp:221
logger(char const *name, severity severity)
Definition: log.hpp:207
log_in_progress operator()(const log_domain &domain, bool show_names=true, bool do_indent=false, bool show_timestamps=true, bool break_strict=true, bool auto_newline=true) const
Definition: log.cpp:453
severity get_severity() const
Definition: log.hpp:216
severity severity_
Definition: log.hpp:205
bool dont_log(const log_domain &domain) const
Definition: log.hpp:211
Helper class to redirect the output of the logger in a certain scope.
Definition: log.hpp:98
std::ostream * old_stream_
The previously set redirection.
Definition: log.hpp:117
redirect_output_setter(std::ostream &stream)
Constructor.
Definition: log.cpp:285
const log_domain & domain_
Definition: log.hpp:240
void do_log_entry(const std::string &str) noexcept
Definition: log.cpp:521
std::string str_
Definition: log.hpp:241
scope_logger(const log_domain &domain, const char *str)
Definition: log.hpp:243
void do_log_exit() noexcept
Definition: log.cpp:531
scope_logger(const log_domain &domain, const std::string &str)
Definition: log.hpp:250
int64_t ticks_
Definition: log.hpp:239
Definition: pump.hpp:41
bool get_log_domain_severity(const std::string &name, severity &severity)
Definition: log.cpp:366
logger & err()
Definition: log.cpp:302
severity
Definition: log.hpp:82
std::string list_log_domains(const std::string &filter)
Definition: log.cpp:375
log_domain & general()
Definition: log.cpp:328
bool broke_strict()
Definition: log.cpp:395
std::string get_timespan(const std::time_t &t)
Definition: log.cpp:406
logger & debug()
Definition: log.cpp:320
const std::string out_log_file_suffix
Definition: log.hpp:70
void rotate_logs(const std::string &log_dir)
Check how many log files exist and delete the oldest when there's too many.
Definition: log.cpp:97
std::string unique_log_filename()
Generate a unique file name using the current timestamp and a randomly generated number.
Definition: log.cpp:128
void set_log_to_file()
Do the initial redirection to a log file if the logs directory is writable.
Definition: log.cpp:229
void move_log_file()
Move the log file to another directory.
Definition: log.cpp:173
logger & warn()
Definition: log.cpp:308
void timestamps(bool t)
Definition: log.cpp:299
std::string sanitize_log(const std::string &logstr)
Definition: log.cpp:435
const unsigned max_logs
Definition: log.hpp:75
void check_log_dir_writable()
Checks that a dummy file can be written to and deleted from the logs directory.
Definition: log.cpp:141
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
Definition: log.cpp:544
const std::string log_file_prefix
Definition: log.hpp:67
std::pair< const std::string, severity > logd
Definition: log.hpp:120
std::string get_timestamp(const std::time_t &t, const std::string &format)
Definition: log.cpp:399
bool is_not_log_file(const std::string &fn)
Use the defined prefix and suffix to determine if a filename is a log file.
Definition: log.cpp:91
void precise_timestamps(bool pt)
Definition: log.cpp:300
std::optional< bool > log_dir_writable()
Returns the result set by check_log_dir_writable().
Definition: log.cpp:275
logger & info()
Definition: log.cpp:314
bool set_log_domain_severity(const std::string &name, severity severity)
Definition: log.cpp:342
void set_strict_severity(severity severity)
Definition: log.cpp:385
std::string get_log_file_path()
Definition: log.cpp:280
const std::string log_file_suffix
Definition: log.hpp:68
std::ostringstream & operator<<(std::ostringstream &oss, const lg::severity severity)
Definition: log.cpp:85