Freeciv-3.3
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13#ifndef FC__LOG_H
14#define FC__LOG_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20#include <assert.h>
21#include <stdarg.h>
22#include <stdlib.h>
23
24/* utility */
25#include "support.h" /* bool type and fc__attribute */
26
27#define MAX_LEN_LOG_LINE 5120
28
31 LOG_ERROR, /* non-fatal errors */
34 LOG_VERBOSE, /* not shown by default */
35 LOG_DEBUG /* suppressed unless DEBUG defined */
36};
37
38/* If one wants to compare autogames with lots of code changes, the line
39 * numbers can cause a lot of noise. In that case set this to a fixed
40 * value. */
41#define __FC_LINE__ __LINE__
42
43/* Dummy log message. */
44extern const char *nologmsg;
45#define NOLOGMSG nologmsg
46
47/* Preparation of the log message, i.e. add a backtrace. */
49 const char *where, const char *msg);
50
51/* A function type to enable custom output of log messages other than
52 * via fputs(stderr). Eg, to the server console while handling prompts,
53 * rfcstyle, client notifications; Eg, to the client window output window?
54 */
55typedef void (*log_callback_fn)(enum log_level, const char *, bool file_too);
56
57/* A function type to generate a custom prefix for the log messages, e.g.
58 * add the turn and/or time of the log message. */
59typedef const char *(*log_prefix_fn)(void);
60
61void log_init(const char *filename, enum log_level initial_level,
64void log_close(void);
65bool log_parse_level_str(const char *level_str, enum log_level *ret_level);
66
71enum log_level log_get_level(void);
72const char *log_level_name(enum log_level lvl);
73#ifdef FREECIV_DEBUG
75 const char *file, int line);
76#endif
77
78void vdo_log(const char *file, const char *function, int line,
80 char *buf, int buflen, const char *message, va_list args);
81void do_log(const char *file, const char *function, int line,
83 const char *message, ...)
85
86#ifdef FREECIV_DEBUG
87#define log_do_output_for_level(level) \
88 log_do_output_for_level_at_location(level, __FILE__, __FC_LINE__)
89#else
90#define log_do_output_for_level(level) (log_get_level() >= level)
91#endif /* FREECIV_DEBUG */
92
93
94/* The log macros */
95#define log_base(level, message, ...) \
96 if (log_do_output_for_level(level)) { \
97 do_log(__FILE__, __FUNCTION__, __FC_LINE__, FALSE, \
98 level, message, ## __VA_ARGS__); \
99 }
100/* This one doesn't need check, fatal messages are always displayed. */
101#define log_fatal(message, ...) \
102 do_log(__FILE__, __FUNCTION__, __FC_LINE__, FALSE, \
103 LOG_FATAL, message, ## __VA_ARGS__);
104#define log_error(message, ...) \
105 log_base(LOG_ERROR, message, ## __VA_ARGS__)
106#define log_warn(message, ...) \
107 log_base(LOG_WARN, message, ## __VA_ARGS__)
108#define log_normal(message, ...) \
109 log_base(LOG_NORMAL, message, ## __VA_ARGS__)
110#define log_verbose(message, ...) \
111 log_base(LOG_VERBOSE, message, ## __VA_ARGS__)
112#ifdef FREECIV_DEBUG
113# define log_debug(message, ...) \
114 log_base(LOG_DEBUG, message, ## __VA_ARGS__)
115#else
116# define log_debug(message, ...) /* Do nothing. */
117#endif /* FREECIV_DEBUG */
118#ifdef FREECIV_TESTMATIC
119#define log_testmatic(message, ...) \
120 log_base(LOG_ERROR, message, ## __VA_ARGS__)
121#define log_testmatic_alt(altlvl, message, ...) \
122 log_base(LOG_ERROR, message, ## __VA_ARGS__)
123#else /* FREECIV_TESTMATIC */
124#define log_testmatic(message, ...) /* Do nothing. */
125#define log_testmatic_alt(altlvl, message, ...) \
126 log_base(altlvl, message, ## __VA_ARGS__)
127#endif /* FREECIV_TESTMATIC */
128
129#define log_va_list(level, msg, args) \
130 if (log_do_output_for_level(level)) { \
131 char __buf_[1024]; \
132 vdo_log(__FILE__, __FUNCTION__, __FC_LINE__, FALSE, \
133 level, __buf_, sizeof(__buf_), msg, args); \
134 }
135
136/* Used by game debug command */
137#define log_test log_normal
138#define log_packet log_verbose
139#define log_packet_detailed log_debug
140#define LOG_TEST LOG_NORMAL /* needed by citylog_*() functions */
141
142
143/* Assertions. */
145#ifdef FREECIV_NDEBUG
146/* Disable the assertion failures, but not the tests! */
147#define fc_assert_fail(...) (void) 0
148#else
149void fc_assert_fail(const char *file, const char *function, int line,
150 const char *assertion, const char *message, ...)
152#endif /* FREECIV_NDEBUG */
153
154#define fc_assert_full(file, function, line, \
155 condition, action, message, ...) \
156 if (!(condition)) { \
157 fc_assert_fail(file, function, line, #condition, \
158 message, ## __VA_ARGS__); \
159 action; \
160 } \
161 (void) 0 /* Force the usage of ';' at the end of the call. */
162
163#ifdef FREECIV_NDEBUG
164/* Disabled. */
165#define fc_assert(...) (void) 0
166#define fc_assert_msg(...) (void) 0
167#define fc_assert_action(...) (void) 0
168#define fc_assert_ret(...) (void) 0
169#define fc_assert_ret_val(...) (void) 0
170#define fc_assert_exit(...) (void) 0
171#define fc_assert_action_msg(...) (void) 0
172#define fc_assert_ret_msg(...) (void) 0
173#define fc_assert_ret_val_msg(...) (void) 0
174#define fc_assert_exit_msg(...) (void) 0
175#else
176/* Like assert(). */
177#define fc_assert(condition) \
178 ((condition) ? (void) 0 \
179 : fc_assert_fail(__FILE__, __FUNCTION__, __FC_LINE__, #condition, \
180 NOLOGMSG, NOLOGMSG))
181/* Like assert() with extra message. */
182#define fc_assert_msg(condition, message, ...) \
183 ((condition) ? (void) 0 \
184 : fc_assert_fail(__FILE__, __FUNCTION__, __FC_LINE__, \
185 #condition, message, ## __VA_ARGS__))
186
187/* Do action on failure. */
188#define fc_assert_action(condition, action) \
189 fc_assert_full(__FILE__, __FUNCTION__, __FC_LINE__, condition, action, \
190 NOLOGMSG, NOLOGMSG)
191/* Return on failure. */
192#define fc_assert_ret(condition) \
193 fc_assert_action(condition, return)
194/* Return a value on failure. */
195#define fc_assert_ret_val(condition, val) \
196 fc_assert_action(condition, return val)
197/* Exit on failure. */
198#define fc_assert_exit(condition) \
199 fc_assert_action(condition, exit(EXIT_FAILURE))
200
201/* Do action on failure with extra message. */
202#define fc_assert_action_msg(condition, action, message, ...) \
203 fc_assert_full(__FILE__, __FUNCTION__, __FC_LINE__, condition, action, \
204 message, ## __VA_ARGS__)
205/* Return on failure with extra message. */
206#define fc_assert_ret_msg(condition, message, ...) \
207 fc_assert_action_msg(condition, return, message, ## __VA_ARGS__)
208/* Return a value on failure with extra message. */
209#define fc_assert_ret_val_msg(condition, val, message, ...) \
210 fc_assert_action_msg(condition, return val, message, ## __VA_ARGS__)
211/* Exit on failure with extra message. */
212#define fc_assert_exit_msg(condition, message, ...) \
213 fc_assert_action(condition, \
214 log_fatal(message, ## __VA_ARGS__); exit(EXIT_FAILURE))
215#endif /* FREECIV_NDEBUG */
216
217#ifdef __cplusplus
218#ifdef FREECIV_CXX11_STATIC_ASSERT
219#define FC_STATIC_ASSERT(cond, tag) static_assert(cond, #tag)
220#endif /* FREECIV_CXX11_STATIC_ASSERT */
221#else /* __cplusplus */
222#ifdef FREECIV_C23_STATIC_ASSERT
223#define FC_STATIC_ASSERT(cond, tag) static_assert(cond, #tag)
224#elif defined(FREECIV_C11_STATIC_ASSERT)
225#define FC_STATIC_ASSERT(cond, tag) _Static_assert(cond, #tag)
226#endif /* FREECIV_C11_STATIC_ASSERT */
227#ifdef FREECIV_STATIC_STRLEN
228#define FC_STATIC_STRLEN_ASSERT(cond, tag) FC_STATIC_ASSERT(cond, tag)
229#else /* FREECIV_STATIC_STRLEN */
230#define FC_STATIC_STRLEN_ASSERT(cond, tag)
231#endif /* FREECIV_STATIC_STRLEN */
232#endif /* __cplusplus */
233
234#ifndef FC_STATIC_ASSERT
235/* Static (compile-time) assertion.
236 * "tag" is a semi-meaningful C identifier which will appear in the
237 * compiler error message if the assertion fails. */
238#define FC_STATIC_ASSERT(cond, tag) \
239 enum { static_assert_ ## tag = 1 / (!!(cond)) }
240#endif
241
242#ifdef __cplusplus
243}
244#endif /* __cplusplus */
245
246#endif /* FC__LOG_H */
char * incite_cost
Definition comments.c:76
void fc_assert_fail(const char *file, const char *function, int line, const char *assertion, const char *message,...) fc__attribute((__format__(__printf__
const char *(* log_prefix_fn)(void)
Definition log.h:59
void log_close(void)
Definition log.c:270
void(* log_callback_fn)(enum log_level, const char *, bool file_too)
Definition log.h:55
const char * nologmsg
Definition log.c:72
void log_init(const char *filename, enum log_level initial_level, log_callback_fn callback, log_prefix_fn prefix, int fatal_assertions)
Definition log.c:245
void fc_assert_set_fatal(int fatal_assertions)
Definition log.c:531
void do_log(const char *file, const char *function, int line, bool print_from_where, enum log_level level, const char *message,...) fc__attribute((__format__(__printf__
void log_set_level(enum log_level level)
Definition log.c:314
void(* log_pre_callback_fn)(enum log_level, bool print_from_where, const char *where, const char *msg)
Definition log.h:48
log_callback_fn log_set_callback(log_callback_fn callback)
Definition log.c:290
const char * log_level_name(enum log_level lvl)
Definition log.c:330
log_pre_callback_fn log_set_pre_callback(log_pre_callback_fn precallback)
Definition log.c:278
void vdo_log(const char *file, const char *function, int line, bool print_from_where, enum log_level level, char *buf, int buflen, const char *message, va_list args)
Definition log.c:403
log_level
Definition log.h:29
@ LOG_ERROR
Definition log.h:31
@ LOG_DEBUG
Definition log.h:35
@ LOG_NORMAL
Definition log.h:33
@ LOG_FATAL
Definition log.h:30
@ LOG_VERBOSE
Definition log.h:34
@ LOG_WARN
Definition log.h:32
bool log_parse_level_str(const char *level_str, enum log_level *ret_level)
Definition log.c:86
log_prefix_fn log_set_prefix(log_prefix_fn prefix)
Definition log.c:302
enum log_level log_get_level(void)
Definition log.c:322
static int fatal_assertions
Definition ruledit.cpp:58
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define fc__attribute(x)
Definition support.h:99