The Battle for Wesnoth  1.19.1+dev
Classes | Namespaces | Macros | Typedefs | Enumerations | Functions | Variables
log.hpp File Reference

Standard logging facilities (interface). More...

#include <iosfwd>
#include <optional>
#include <string>
#include <utility>
#include <ctime>
#include <cstdint>
#include "formatter.hpp"
Include dependency graph for log.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.


class  lg::redirect_output_setter
 Helper class to redirect the output of the logger in a certain scope. More...
class  lg::log_domain
class  lg::log_in_progress
class  lg::logger
class  lg::scope_logger




#define log_scope(description)   lg::scope_logger scope_logging_object__(lg::general(), description);
#define log_scope2(domain, description)   lg::scope_logger scope_logging_object__(domain, description);
#define LOG_STREAM(level, domain)   if (lg::level().dont_log(domain)) ; else lg::level()(domain) | formatter()
#define LOG_STREAM_NAMELESS(level, domain)   if (lg::level().dont_log(domain)) ; else lg::level()(domain, false) | formatter()
#define LOG_STREAM_NAMELESS_STREAMING(level, domain)   if (lg::level().dont_log(domain)) ; else lg::level()(domain, false, false, true, true, false) | formatter()
#define LOG_STREAM_INDENT(level, domain)   if (lg::level().dont_log(domain)) ; else lg::level()(domain, true, true) | formatter()
#define FORCE_LOG_TO(logger, domain)   logger(domain) | formatter()
#define PLAIN_LOG   lg::err()(lg::general(), false, false, false, false, true) | formatter()
#define STREAMING_LOG   lg::err()(lg::general(), false, false, false, false, false) | formatter()


typedef std::pair< const std::string, severity > lg::logd


enum class  lg::severity {
  lg::LG_NONE =-1 , lg::LG_ERROR =0 , lg::LG_WARN =1 , lg::LG_INFO =2 ,
  lg::LG_DEBUG =3


std::ostringstream & lg::operator<< (std::ostringstream &oss, const lg::severity severity)
bool lg::set_log_domain_severity (const std::string &name, severity severity)
bool lg::set_log_domain_severity (const std::string &name, const logger &lg)
bool lg::get_log_domain_severity (const std::string &name, severity &severity)
std::string lg::list_log_domains (const std::string &filter)
void lg::set_strict_severity (severity severity)
void lg::set_strict_severity (const logger &lg)
bool lg::broke_strict ()
void lg::set_log_to_file ()
 Do the initial redirection to a log file if the logs directory is writable. More...
void lg::move_log_file ()
 Move the log file to another directory. More...
void lg::check_log_dir_writable ()
 Checks that a dummy file can be written to and deleted from the logs directory. More...
std::optional< bool > lg::log_dir_writable ()
 Returns the result set by check_log_dir_writable(). More...
bool lg::is_not_log_file (const std::string &filename)
 Use the defined prefix and suffix to determine if a filename is a log file. More...
void lg::rotate_logs (const std::string &log_dir)
 Check how many log files exist and delete the oldest when there's too many. More...
std::string lg::unique_log_filename ()
 Generate a unique file name using the current timestamp and a randomly generated number. More...
void lg::timestamps (bool t)
void lg::precise_timestamps (bool pt)
std::string lg::get_timestamp (const std::time_t &t, const std::string &format)
std::string lg::get_timespan (const std::time_t &t)
std::string lg::sanitize_log (const std::string &logstr)
std::string lg::get_log_file_path ()
logger & lg::err ()
logger & lg::warn ()
logger & lg::info ()
logger & lg::debug ()
log_domain & lg::general ()
std::stringstream & lg::log_to_chat ()
 Use this to show WML errors in the ingame chat. More...


const std::string lg::log_file_prefix = "wesnoth-"
const std::string lg::log_file_suffix = ".log"
const std::string lg::out_log_file_suffix = ".out" + log_file_suffix
const unsigned lg::max_logs = 8

Detailed Description

Standard logging facilities (interface).

To use one of the standard log channels, put something like the following at the start of your .cpp file:

static lg::log_domain log_display("display"); #define ERR_DP LOG_STREAM(err, log_display) #define LOG_DP LOG_STREAM(info, log_display)

Then stream logging info to ERR_DP, or LOG_DP, as if it were an ostream like std::cerr. (In general it will actually be std::cerr at runtime when logging is enabled.)

LOG_DP << "Found a window resize event: ...";

Please do not use iomanip features like std::hex directly on the logger. Because of the design of the logger, this will result in all of the loggers (in fact std::cerr) being imbued with std::hex. Please use a formatter instead.

#include "formatter.hpp"

LOG_DP << (formatter() << "The random seed is: '" << std::hex << seed << "'\n").str();

It might be nice if somehow the logger class / macros could support using iomanip things directly, but right now it doesn't, and it seems that it would complicate the design greatly enough that it doesn't seem worth it.

Definition in file log.hpp.

Macro Definition Documentation


#define FORCE_LOG_TO (   logger,
)    logger(domain) | formatter()

Definition at line 290 of file log.hpp.

◆ log_scope

#define log_scope (   description)    lg::scope_logger scope_logging_object__(lg::general(), description);

Definition at line 274 of file log.hpp.

◆ log_scope2

#define log_scope2 (   domain,
)    lg::scope_logger scope_logging_object__(domain, description);

Definition at line 275 of file log.hpp.


#define LOG_STREAM (   level,
)    if (lg::level().dont_log(domain)) ; else lg::level()(domain) | formatter()

Definition at line 277 of file log.hpp.


#define LOG_STREAM_INDENT (   level,
)    if (lg::level().dont_log(domain)) ; else lg::level()(domain, true, true) | formatter()

Definition at line 286 of file log.hpp.


#define LOG_STREAM_NAMELESS (   level,
)    if (lg::level().dont_log(domain)) ; else lg::level()(domain, false) | formatter()

Definition at line 280 of file log.hpp.


)    if (lg::level().dont_log(domain)) ; else lg::level()(domain, false, false, true, true, false) | formatter()

Definition at line 283 of file log.hpp.


#define PLAIN_LOG   lg::err()(lg::general(), false, false, false, false, true) | formatter()

Definition at line 295 of file log.hpp.


#define STREAMING_LOG   lg::err()(lg::general(), false, false, false, false, false) | formatter()

Definition at line 296 of file log.hpp.