Freeciv-3.2
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 <stdarg.h>
21#include <stdlib.h>
22
23/* utility */
24#include "support.h" /* bool type and fc__attribute */
25
26#define MAX_LEN_LOG_LINE 5120
27
30 LOG_ERROR, /* non-fatal errors */
33 LOG_VERBOSE, /* not shown by default */
34 LOG_DEBUG /* suppressed unless DEBUG defined */
35};
36
37/* If one wants to compare autogames with lots of code changes, the line
38 * numbers can cause a lot of noise. In that case set this to a fixed
39 * value. */
40#define __FC_LINE__ __LINE__
41
42/* Dummy log message. */
43extern const char *nologmsg;
44#define NOLOGMSG nologmsg
45
46/* Preparation of the log message, i.e. add a backtrace. */
48 const char *where, const char *msg);
49
50/* A function type to enable custom output of log messages other than
51 * via fputs(stderr). Eg, to the server console while handling prompts,
52 * rfcstyle, client notifications; Eg, to the client window output window?
53 */
54typedef void (*log_callback_fn)(enum log_level, const char *, bool file_too);
55
56/* A function type to generate a custom prefix for the log messages, e.g.
57 * add the turn and/or time of the log message. */
58typedef const char *(*log_prefix_fn)(void);
59
60void log_init(const char *filename, enum log_level initial_level,
63void log_close(void);
64bool log_parse_level_str(const char *level_str, enum log_level *ret_level);
65
70enum log_level log_get_level(void);
71const char *log_level_name(enum log_level lvl);
72#ifdef FREECIV_DEBUG
74 const char *file, int line);
75#endif
76
77void vdo_log(const char *file, const char *function, int line,
79 char *buf, int buflen, const char *message, va_list args);
80void do_log(const char *file, const char *function, int line,
82 const char *message, ...)
84
85#ifdef FREECIV_DEBUG
86#define log_do_output_for_level(level) \
87 log_do_output_for_level_at_location(level, __FILE__, __FC_LINE__)
88#else
89#define log_do_output_for_level(level) (log_get_level() >= level)
90#endif /* FREECIV_DEBUG */
91
92
93/* The log macros */
94#define log_base(level, message, ...) \
95 if (log_do_output_for_level(level)) { \
96 do_log(__FILE__, __FUNCTION__, __FC_LINE__, FALSE, \
97 level, message, ## __VA_ARGS__); \
98 }
99/* This one doesn't need check, fatal messages are always displayed. */
100#define log_fatal(message, ...) \
101 do_log(__FILE__, __FUNCTION__, __FC_LINE__, FALSE, \
102 LOG_FATAL, message, ## __VA_ARGS__);
103#define log_error(message, ...) \
104 log_base(LOG_ERROR, message, ## __VA_ARGS__)
105#define log_warn(message, ...) \
106 log_base(LOG_WARN, message, ## __VA_ARGS__)
107#define log_normal(message, ...) \
108 log_base(LOG_NORMAL, message, ## __VA_ARGS__)
109#define log_verbose(message, ...) \
110 log_base(LOG_VERBOSE, message, ## __VA_ARGS__)
111#ifdef FREECIV_DEBUG
112# define log_debug(message, ...) \
113 log_base(LOG_DEBUG, message, ## __VA_ARGS__)
114#else
115# define log_debug(message, ...) /* Do nothing. */
116#endif /* FREECIV_DEBUG */
117#ifdef FREECIV_TESTMATIC
118#define log_testmatic(message, ...) \
119 log_base(LOG_ERROR, message, ## __VA_ARGS__)
120#define log_testmatic_alt(altlvl, message, ...) \
121 log_base(LOG_ERROR, message, ## __VA_ARGS__)
122#else /* FREECIV_TESTMATIC */
123#define log_testmatic(message, ...) /* Do nothing. */
124#define log_testmatic_alt(altlvl, message, ...) \
125 log_base(altlvl, message, ## __VA_ARGS__)
126#endif /* FREECIV_TESTMATIC */
127
128#define log_va_list(level, msg, args) \
129 if (log_do_output_for_level(level)) { \
130 char __buf_[1024]; \
131 vdo_log(__FILE__, __FUNCTION__, __FC_LINE__, FALSE, \
132 level, __buf_, sizeof(__buf_), msg, args); \
133 }
134
135/* Used by game debug command */
136#define log_test log_normal
137#define log_packet log_verbose
138#define log_packet_detailed log_debug
139#define LOG_TEST LOG_NORMAL /* needed by citylog_*() functions */
140
141
142/* Assertions. */
144#ifdef FREECIV_NDEBUG
145/* Disable the assertion failures, but not the tests! */
146#define fc_assert_fail(...) (void) 0
147#else
148void fc_assert_fail(const char *file, const char *function, int line,
149 const char *assertion, const char *message, ...)
151#endif /* FREECIV_NDEBUG */
152
153#define fc_assert_full(file, function, line, \
154 condition, action, message, ...) \
155 if (!(condition)) { \
156 fc_assert_fail(file, function, line, #condition, \
157 message, ## __VA_ARGS__); \
158 action; \
159 } \
160 (void) 0 /* Force the usage of ';' at the end of the call. */
161
162#ifdef FREECIV_NDEBUG
163/* Disabled. */
164#define fc_assert(...) (void) 0
165#define fc_assert_msg(...) (void) 0
166#define fc_assert_action(...) (void) 0
167#define fc_assert_ret(...) (void) 0
168#define fc_assert_ret_val(...) (void) 0
169#define fc_assert_exit(...) (void) 0
170#define fc_assert_action_msg(...) (void) 0
171#define fc_assert_ret_msg(...) (void) 0
172#define fc_assert_ret_val_msg(...) (void) 0
173#define fc_assert_exit_msg(...) (void) 0
174#else
175/* Like assert(). */
176#define fc_assert(condition) \
177 ((condition) ? (void) 0 \
178 : fc_assert_fail(__FILE__, __FUNCTION__, __FC_LINE__, #condition, \
179 NOLOGMSG, NOLOGMSG))
180/* Like assert() with extra message. */
181#define fc_assert_msg(condition, message, ...) \
182 ((condition) ? (void) 0 \
183 : fc_assert_fail(__FILE__, __FUNCTION__, __FC_LINE__, \
184 #condition, message, ## __VA_ARGS__))
185
186/* Do action on failure. */
187#define fc_assert_action(condition, action) \
188 fc_assert_full(__FILE__, __FUNCTION__, __FC_LINE__, condition, action, \
189 NOLOGMSG, NOLOGMSG)
190/* Return on failure. */
191#define fc_assert_ret(condition) \
192 fc_assert_action(condition, return)
193/* Return a value on failure. */
194#define fc_assert_ret_val(condition, val) \
195 fc_assert_action(condition, return val)
196/* Exit on failure. */
197#define fc_assert_exit(condition) \
198 fc_assert_action(condition, exit(EXIT_FAILURE))
199
200/* Do action on failure with extra message. */
201#define fc_assert_action_msg(condition, action, message, ...) \
202 fc_assert_full(__FILE__, __FUNCTION__, __FC_LINE__, condition, action, \
203 message, ## __VA_ARGS__)
204/* Return on failure with extra message. */
205#define fc_assert_ret_msg(condition, message, ...) \
206 fc_assert_action_msg(condition, return, message, ## __VA_ARGS__)
207/* Return a value on failure with extra message. */
208#define fc_assert_ret_val_msg(condition, val, message, ...) \
209 fc_assert_action_msg(condition, return val, message, ## __VA_ARGS__)
210/* Exit on failure with extra message. */
211#define fc_assert_exit_msg(condition, message, ...) \
212 fc_assert_action(condition, \
213 log_fatal(message, ## __VA_ARGS__); exit(EXIT_FAILURE))
214#endif /* FREECIV_NDEBUG */
215
216#ifdef __cplusplus
217#ifdef FREECIV_CXX11_STATIC_ASSERT
218#define FC_STATIC_ASSERT(cond, tag) static_assert(cond, #tag)
219#endif /* FREECIV_CXX11_STATIC_ASSERT */
220#else /* __cplusplus */
221#ifdef FREECIV_C11_STATIC_ASSERT
222#define FC_STATIC_ASSERT(cond, tag) _Static_assert(cond, #tag)
223#endif /* FREECIV_C11_STATIC_ASSERT */
224#ifdef FREECIV_STATIC_STRLEN
225#define FC_STATIC_STRLEN_ASSERT(cond, tag) FC_STATIC_ASSERT(cond, tag)
226#else /* FREECIV_STATIC_STRLEN */
227#define FC_STATIC_STRLEN_ASSERT(cond, tag)
228#endif /* FREECIV_STATIC_STRLEN */
229#endif /* __cplusplus */
230
231#ifndef FC_STATIC_ASSERT
232/* Static (compile-time) assertion.
233 * "tag" is a semi-meaningful C identifier which will appear in the
234 * compiler error message if the assertion fails. */
235#define FC_STATIC_ASSERT(cond, tag) \
236 enum { static_assert_ ## tag = 1 / (!!(cond)) }
237#endif
238
239#ifdef __cplusplus
240}
241#endif /* __cplusplus */
242
243#endif /* FC__LOG_H */
char * incite_cost
Definition comments.c:75
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:58
void log_close(void)
Definition log.c:270
void(* log_callback_fn)(enum log_level, const char *, bool file_too)
Definition log.h:54
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:47
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:28
@ LOG_ERROR
Definition log.h:30
@ LOG_DEBUG
Definition log.h:34
@ LOG_NORMAL
Definition log.h:32
@ LOG_FATAL
Definition log.h:29
@ LOG_VERBOSE
Definition log.h:33
@ LOG_WARN
Definition log.h:31
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