14 #include <fc_config.h>
20 #include <QLoggingCategory>
21 #include <QMutexLocker>
38 static QString log_level = QStringLiteral();
39 static bool fatal_assertions =
false;
41 static QBasicMutex mutex;
42 static void handle_message(QtMsgType type,
const QMessageLogContext &context,
44 static QtMessageHandler original_handler =
nullptr;
45 static QFile *log_file =
nullptr;
55 bool log_init(
const QString &level_str,
const QStringList &extra_rules)
58 log_level = level_str;
61 original_handler = qInstallMessageHandler(&handle_message);
66 auto handle = GetStdHandle(STD_OUTPUT_HANDLE);
67 if (handle != INVALID_HANDLE_VALUE) {
69 if (GetConsoleMode(handle, &mode)) {
70 mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
71 SetConsoleMode(handle, mode);
81 QStringLiteral(
"[%{type}] %{appname} (%{file}:%{line}) - %{message}"));
84 qSetMessagePattern(QStringLiteral(
"[%{type}] %{appname} - %{message}"));
89 auto rules = QStringList();
90 if (level_str == QStringLiteral(
"fatal")) {
93 QStringLiteral(
"*.critical = false"),
94 QStringLiteral(
"*.warning = false"),
95 QStringLiteral(
"*.info = false"),
96 QStringLiteral(
"*.debug = false"),
98 }
else if (level_str == QStringLiteral(
"critical")) {
100 QStringLiteral(
"*.critical = true"),
101 QStringLiteral(
"*.warning = false"),
102 QStringLiteral(
"*.info = false"),
103 QStringLiteral(
"*.debug = false"),
105 }
else if (level_str == QStringLiteral(
"warning")) {
107 QStringLiteral(
"*.critical = true"),
108 QStringLiteral(
"*.warning = true"),
109 QStringLiteral(
"*.info = false"),
110 QStringLiteral(
"*.debug = false"),
112 }
else if (level_str == QStringLiteral(
"info")) {
114 QStringLiteral(
"*.critical = true"),
115 QStringLiteral(
"*.warning = true"),
116 QStringLiteral(
"*.info = true"),
117 QStringLiteral(
"*.debug = false"),
118 QStringLiteral(
"qt.*.info = false"),
120 }
else if (level_str == QStringLiteral(
"debug")) {
122 QStringLiteral(
"*.critical = true"),
123 QStringLiteral(
"*.warning = true"),
124 QStringLiteral(
"*.info = true"),
125 QStringLiteral(
"*.debug = true"),
126 QStringLiteral(
"qt.*.info = false"),
127 QStringLiteral(
"qt.*.debug = false"),
133 qCritical(
_(
"\"%s\" is not a valid log level name (valid names are "
134 "fatal/critical/warning/info/debug)"),
135 qUtf8Printable(level_str));
139 rules += extra_rules;
140 QLoggingCategory::setFilterRules(rules.join(
'\n'));
142 qDebug() <<
"Applied logging rules" << rules;
152 static void handle_message(QtMsgType type,
const QMessageLogContext &context,
156 if (log_file !=
nullptr) {
157 QMutexLocker lock(&mutex);
158 log_file->write((
message + QStringLiteral(
"\n")).toLocal8Bit());
161 if (type == QtFatalMsg || type == QtCriticalMsg) {
167 if (original_handler !=
nullptr) {
168 original_handler(type, context,
message);
180 if (path.isEmpty()) {
186 auto *new_file =
new QFile(path);
187 if (!new_file->open(QIODevice::WriteOnly | QIODevice::Text)) {
190 qCritical().noquote()
191 << QString(
_(
"Could not open log file for writing: %1"))
192 .arg(new_file->errorString());
214 QMutexLocker locker(&mutex);
219 qInstallMessageHandler(original_handler);
237 int line,
const char *
function,
241 QMessageLogger logger(file, line, assert_category().categoryName());
242 logger.critical(
"Assertion %s failed", condition);
244 logger.critical().noquote() <<
message;
246 logger.critical().noquote()
247 << QString(
_(
"Please report this message at %1")).arg(BUG_URL);
249 logger.fatal(
"%s",
_(
"Assertion failed"));
260 qInfo() << qUtf8Printable(msg);
const QString & log_get_level()
Retrieves the log level passed to log_init (even if log_init failed).
void fc_assert_handle_failure(const char *condition, const char *file, int line, const char *function, const QString &message)
Handles a failed assertion.
void log_time(const QString &msg, bool log)
bool log_init(const QString &level_str, const QStringList &extra_rules)
Parses a log level string as provided by the user on the command line, and installs the corresponding...
bool fc_assert_are_fatal()
Checks whether the fc_assert* macros should raise on failed assertion.
void fc_assert_set_fatal(bool fatal)
Set what signal the assert* macros should raise on failed assertion (-1 to disable).
void log_set_file(const QString &path)
Redirects the log to a file.
void log_close()
Deinitialize logging module.
Q_LOGGING_CATEGORY(tileset_category, "freeciv.tileset")
Functions for handling the tilespec files which describe the files and contents of tilesets.