Freeciv21
Develop your civilization from humble roots to a global empire
log.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 // Qt
16 #include <QLoggingCategory>
17 #include <QString>
18 #include <QtGlobal>
19 
20 #include <stdlib.h>
21 
22 constexpr auto LOG_FATAL = QtFatalMsg;
23 constexpr auto LOG_ERROR = QtCriticalMsg;
24 constexpr auto LOG_WARN = QtWarningMsg;
25 constexpr auto LOG_NORMAL = QtInfoMsg;
26 constexpr auto LOG_VERBOSE = QtDebugMsg;
27 constexpr auto LOG_DEBUG = QtDebugMsg;
28 
29 /* If one wants to compare autogames with lots of code changes, the line
30  * numbers can cause a lot of noise. In that case set this to a fixed
31  * value. */
32 #define __FC_LINE__ __LINE__
33 
34 void log_close();
35 bool log_init(const QString &level_str = QStringLiteral("info"),
36  const QStringList &extra_rules = {});
37 void log_set_file(const QString &path);
38 const QString &log_get_level();
39 
40 // The log macros
41 #define log_base(level, message, ...) \
42  do { \
43  switch (level) { \
44  case QtFatalMsg: \
45  qFatal(message, ##__VA_ARGS__); \
46  break; \
47  case QtCriticalMsg: \
48  qCritical(message, ##__VA_ARGS__); \
49  break; \
50  case QtWarningMsg: \
51  qWarning(message, ##__VA_ARGS__); \
52  break; \
53  case QtInfoMsg: \
54  qInfo(message, ##__VA_ARGS__); \
55  break; \
56  case QtDebugMsg: \
57  qDebug(message, ##__VA_ARGS__); \
58  break; \
59  } \
60  } while (false)
61 
62 #ifdef FREECIV_DEBUG
63 #define log_debug(message, ...) qDebug(message, ##__VA_ARGS__)
64 #else
65 #define log_debug(message, ...) \
66  { \
67  }
68 #endif
69 
70 // Used by game debug command
71 #define log_test qInfo
72 #define log_packet qDebug
73 #define log_packet_detailed log_debug
74 #define LOG_TEST LOG_NORMAL // needed by citylog_*() functions
75 
76 // Assertions.
77 Q_DECLARE_LOGGING_CATEGORY(assert_category)
78 
79 void fc_assert_set_fatal(bool fatal_assertions);
80 bool fc_assert_are_fatal();
81 
82 void fc_assert_handle_failure(const char *condition, const char *file,
83  int line, const char *function,
84  const QString &message = QString());
85 
86 // Like assert().
87 // The lambda below is used to allow returning a value from a multi-line
88 // macro. We need a macro for line number reporting to work.
89 #define fc_assert(condition) \
90  ((condition) \
91  ? ((void) 0) \
92  : fc_assert_handle_failure(#condition, QT_MESSAGELOG_FILE, \
93  QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC))
94 
95 // Like assert() with extra message.
96 #define fc_assert_msg(condition, message, ...) \
97  ((condition) \
98  ? ((void) 0) \
99  : fc_assert_handle_failure( \
100  #condition, QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, \
101  QT_MESSAGELOG_FUNC, QString::asprintf(message, ##__VA_ARGS__)))
102 
103 // Do action on failure.
104 #define fc_assert_action(condition, action) \
105  if (!(condition)) { \
106  fc_assert_handle_failure(#condition, QT_MESSAGELOG_FILE, \
107  QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC); \
108  action; \
109  }
110 
111 // Return on failure.
112 #define fc_assert_ret(condition) fc_assert_action(condition, return )
113 // Return a value on failure.
114 #define fc_assert_ret_val(condition, val) \
115  fc_assert_action(condition, return val)
116 // Exit on failure.
117 #define fc_assert_exit(condition) \
118  fc_assert_action(condition, exit(EXIT_FAILURE))
119 
120 // Do action on failure with extra message.
121 #define fc_assert_action_msg(condition, action, message, ...) \
122  if (!(condition)) { \
123  fc_assert_handle_failure(#condition, QT_MESSAGELOG_FILE, \
124  QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, \
125  QString::asprintf(message, ##__VA_ARGS__)); \
126  action; \
127  }
128 // Return on failure with extra message.
129 #define fc_assert_ret_msg(condition, message, ...) \
130  fc_assert_action_msg(condition, return, message, ##__VA_ARGS__)
131 // Return a value on failure with extra message.
132 #define fc_assert_ret_val_msg(condition, val, message, ...) \
133  fc_assert_action_msg(condition, return val, message, ##__VA_ARGS__)
134 // Exit on failure with extra message.
135 #define fc_assert_exit_msg(condition, message, ...) \
136  fc_assert_action(condition, qFatal(message, ##__VA_ARGS__); \
137  exit(EXIT_FAILURE))
138 
139 #ifdef __cplusplus
140 #ifdef FREECIV_CXX11_STATIC_ASSERT
141 #define FC_STATIC_ASSERT(cond, tag) static_assert(cond, #tag)
142 #endif // FREECIV_CXX11_STATIC_ASSERT
143 #else // __cplusplus
144 #ifdef FREECIV_C11_STATIC_ASSERT
145 #define FC_STATIC_ASSERT(cond, tag) _Static_assert(cond, #tag)
146 #endif // FREECIV_C11_STATIC_ASSERT
147 #ifdef FREECIV_STATIC_STRLEN
148 #define FC_STATIC_STRLEN_ASSERT(cond, tag) FC_STATIC_ASSERT(cond, tag)
149 #else // FREECIV_STATIC_STRLEN
150 #define FC_STATIC_STRLEN_ASSERT(cond, tag)
151 #endif // FREECIV_STATIC_STRLEN
152 #endif // __cplusplus
153 
154 #ifndef FC_STATIC_ASSERT
155 /* Static (compile-time) assertion.
156  * "tag" is a semi-meaningful C identifier which will appear in the
157  * compiler error message if the assertion fails. */
158 #define FC_STATIC_ASSERT(cond, tag) \
159  enum { static_assert_##tag = 1 / (!!(cond)) }
160 #endif
161 
162 void log_time(const QString &msg, bool log = false);
constexpr auto LOG_DEBUG
Definition: log.h:27
const QString & log_get_level()
Retrieves the log level passed to log_init (even if log_init failed).
Definition: log.cpp:207
constexpr auto LOG_ERROR
Definition: log.h:23
bool log_init(const QString &level_str=QStringLiteral("info"), const QStringList &extra_rules={})
Parses a log level string as provided by the user on the command line, and installs the corresponding...
Definition: log.cpp:55
constexpr auto LOG_VERBOSE
Definition: log.h:26
constexpr auto LOG_NORMAL
Definition: log.h:25
void fc_assert_set_fatal(bool fatal_assertions)
Set what signal the assert* macros should raise on failed assertion (-1 to disable).
Definition: log.cpp:226
bool fc_assert_are_fatal()
Checks whether the fc_assert* macros should raise on failed assertion.
Definition: log.cpp:231
constexpr auto LOG_WARN
Definition: log.h:24
void log_set_file(const QString &path)
Redirects the log to a file.
Definition: log.cpp:177
void log_close()
Deinitialize logging module.
Definition: log.cpp:212
void log_time(const QString &msg, bool log=false)
Definition: log.cpp:253
void fc_assert_handle_failure(const char *condition, const char *file, int line, const char *function, const QString &message=QString())
Handles a failed assertion.
Definition: log.cpp:236
constexpr auto LOG_FATAL
Definition: log.h:22