10017
|
1 |
/*
|
|
2 |
* Hedgewars, a free turn based strategy game
|
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
#ifndef LOGGING_H_
|
|
21 |
#define LOGGING_H_
|
|
22 |
|
|
23 |
#include <stdint.h>
|
|
24 |
#include <stdio.h>
|
|
25 |
#include <stdbool.h>
|
|
26 |
|
|
27 |
#define FLIB_LOGLEVEL_ALL -100
|
|
28 |
#define FLIB_LOGLEVEL_DEBUG -1
|
|
29 |
#define FLIB_LOGLEVEL_INFO 0
|
|
30 |
#define FLIB_LOGLEVEL_WARNING 1
|
|
31 |
#define FLIB_LOGLEVEL_ERROR 2
|
|
32 |
#define FLIB_LOGLEVEL_NONE 100
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Returns a pointer to a static buffer, don't free or store.
|
|
36 |
*/
|
|
37 |
char* flib_format_ip(uint32_t numip);
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Evaluates the expression cond. If it is true, a formatted error will be logged.
|
|
41 |
* Returns true if an error is logged, false otherwise (i.e. the boolean value of the argument)
|
|
42 |
* Usage: log_e_if(errorHasHappened, "Format string", formatArg, ...);
|
|
43 |
*/
|
|
44 |
#define log_e_if(cond, ...) _flib_fassert(__func__, FLIB_LOGLEVEL_ERROR, !(bool)(cond), __VA_ARGS__)
|
|
45 |
#define log_w_if(cond, ...) _flib_fassert(__func__, FLIB_LOGLEVEL_WARNING, !(bool)(cond), __VA_ARGS__)
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Helper macros for log_badargs_if
|
|
49 |
* The t parameters are the textual representation of the c parameters. They need to be passed
|
|
50 |
* explicitly, to prevent them from being expanded in prescan.
|
|
51 |
*/
|
|
52 |
#define _flib_lbi(c1,t1) log_e_if(c1, "Invalid Argument (%s)", t1)
|
|
53 |
#define _flib_lbi2(c1,t1,c2,t2) (_flib_lbi(c1,t1) || _flib_lbi(c2,t2))
|
|
54 |
#define _flib_lbi3(c1,t1,c2,t2,c3,t3) (_flib_lbi(c1,t1) || _flib_lbi2(c2,t2,c3,t3))
|
|
55 |
#define _flib_lbi4(c1,t1,c2,t2,c3,t3,c4,t4) (_flib_lbi(c1,t1) || _flib_lbi3(c2,t2,c3,t3,c4,t4))
|
|
56 |
#define _flib_lbi5(c1,t1,c2,t2,c3,t3,c4,t4,c5,t5) (_flib_lbi(c1,t1) || _flib_lbi4(c2,t2,c3,t3,c4,t4,c5,t5))
|
|
57 |
#define _flib_lbi6(c1,t1,c2,t2,c3,t3,c4,t4,c5,t5,c6,t6) (_flib_lbi(c1,t1) || _flib_lbi5(c2,t2,c3,t3,c4,t4,c5,t5,c6,t6))
|
|
58 |
|
|
59 |
/**
|
|
60 |
* These macros log an "Invalid Argument" error for the first of their arguments that evaluates to true.
|
|
61 |
* The text of the argument is included in the log message.
|
|
62 |
* The expression returns true if any of its arguments is true (i.e. if an argument error was logged).
|
|
63 |
*
|
|
64 |
* For example, log_badargs_if(x==NULL) will log "Invalid Argument (x==NULL)" and return true if x is NULL.
|
|
65 |
*/
|
|
66 |
#define log_badargs_if(c1) _flib_lbi(c1,#c1)
|
|
67 |
#define log_badargs_if2(c1, c2) _flib_lbi2(c1,#c1,c2,#c2)
|
|
68 |
#define log_badargs_if3(c1, c2, c3) _flib_lbi3(c1,#c1,c2,#c2,c3,#c3)
|
|
69 |
#define log_badargs_if4(c1, c2, c3, c4) _flib_lbi4(c1,#c1,c2,#c2,c3,#c3,c4,#c4)
|
|
70 |
#define log_badargs_if5(c1, c2, c3, c4, c5) _flib_lbi5(c1,#c1,c2,#c2,c3,#c3,c4,#c4,c5,#c5)
|
|
71 |
#define log_badargs_if6(c1, c2, c3, c4, c5, c6) _flib_lbi6(c1,#c1,c2,#c2,c3,#c3,c4,#c4,c5,#c5,c6,#c6)
|
|
72 |
|
|
73 |
#define log_oom_if(cond) log_e_if(cond, "Out of Memory")
|
|
74 |
|
|
75 |
#define flib_log_e(...) _flib_flog(__func__, FLIB_LOGLEVEL_ERROR, __VA_ARGS__)
|
|
76 |
#define flib_log_w(...) _flib_flog(__func__, FLIB_LOGLEVEL_WARNING, __VA_ARGS__)
|
|
77 |
#define flib_log_i(...) _flib_flog(__func__, FLIB_LOGLEVEL_INFO, __VA_ARGS__)
|
|
78 |
#define flib_log_d(...) _flib_flog(__func__, FLIB_LOGLEVEL_DEBUG, __VA_ARGS__)
|
|
79 |
|
|
80 |
bool _flib_fassert(const char *func, int level, bool cond, const char *fmt, ...);
|
|
81 |
void _flib_flog(const char *func, int level, const char *fmt, ...);
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Only log messages that are at least the indicated level
|
|
85 |
*/
|
|
86 |
void flib_log_setLevel(int level);
|
|
87 |
int flib_log_getLevel();
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Log to the indicated file. You can pass NULL to log to stdout.
|
|
91 |
* This overrides setCallback and vice versa.
|
|
92 |
*/
|
|
93 |
void flib_log_setFile(FILE *logfile);
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Returns whether messages of this level are logged at the moment.
|
|
97 |
*/
|
|
98 |
bool flib_log_isActive(int level);
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Allows logging through an arbitrary callback function. Useful for integrating into an
|
|
102 |
* existing logging system. This overrides setFile and vice versa.
|
|
103 |
*/
|
|
104 |
void flib_log_setCallback(void (*logCallback)(int level, const char *msg));
|
|
105 |
|
|
106 |
#endif /* LOGGING_H_ */
|