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