project_files/frontlib/util/logging.c
author alfadur
Thu, 25 Oct 2018 04:55:27 +0300
changeset 13960 0e2fd51c4a52
parent 10017 de822cd3df3a
permissions -rw-r--r--
fix some of Wuzzy's rage
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    20
#include "logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    21
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    22
#include <time.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    23
#include <stdio.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    24
#include <stdarg.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    25
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    26
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    27
static int flib_loglevel = FLIB_LOGLEVEL_INFO;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    28
static FILE *flib_logfile = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    29
void (*flib_logCallback)(int level, const char *msg) = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    30
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    31
char* flib_format_ip(uint32_t numip) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    32
    static char ip[16];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    33
    snprintf(ip, 16, "%u.%u.%u.%u", (unsigned)(numip>>24), (unsigned)((numip>>16)&0xff), (unsigned)((numip>>8)&0xff), (unsigned)(numip&0xff));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    34
    return ip;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    35
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    36
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    37
static inline FILE *flib_log_getfile() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    38
    if(flib_logfile==NULL) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    39
        return stdout;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    40
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    41
        return flib_logfile;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    42
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    43
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    44
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    45
static int log_time(char *buffer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    46
    time_t timer;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    47
    struct tm* tm_info;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    48
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    49
    time(&timer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    50
    tm_info = localtime(&timer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    51
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    52
    return strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    53
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    54
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    55
static char getPrefix(int level) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    56
    switch(level) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    57
    case FLIB_LOGLEVEL_ERROR: return 'E';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    58
    case FLIB_LOGLEVEL_WARNING: return 'W';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    59
    case FLIB_LOGLEVEL_INFO: return 'I';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    60
    case FLIB_LOGLEVEL_DEBUG: return 'D';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    61
    default: return '?';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    62
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    63
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    64
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    65
static void _flib_vflog(const char *func, int level, const char *fmt, va_list args) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    66
    if(level >= flib_loglevel) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    67
        char logbuffer[1024];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    68
        logbuffer[0] = getPrefix(level);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    69
        logbuffer[1] = ' ';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    70
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    71
        int pos = 2;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    73
        int len = log_time(logbuffer+pos);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    74
        if(len>=0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    75
            pos += len;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    76
            if(pos>sizeof(logbuffer)-1) pos = sizeof(logbuffer)-1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    77
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    78
            return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    79
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    80
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    81
        len = snprintf(logbuffer+pos, sizeof(logbuffer)-pos, " [%-30s] ", func);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    82
        if(len>=0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    83
            pos += len;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    84
            if(pos>sizeof(logbuffer)-1) pos = sizeof(logbuffer)-1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    85
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    86
            return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    87
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    88
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    89
        len = vsnprintf(logbuffer+pos, sizeof(logbuffer)-pos, fmt, args);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    90
        if(len>=0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    91
            pos += len;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    92
            if(pos>sizeof(logbuffer)-1) pos = sizeof(logbuffer)-1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    93
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    94
            return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    95
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    96
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    97
        if(flib_logCallback != NULL) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    98
            flib_logCallback(level, logbuffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
    99
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   100
            FILE *logfile = flib_log_getfile();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   101
            fputs(logbuffer, logfile);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   102
            fputc('\n', logfile);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   103
            fflush(logfile);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   104
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   105
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   106
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   107
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   108
void _flib_flog(const char *func, int level, const char *fmt, ...) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   109
    va_list argp;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   110
    va_start(argp, fmt);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   111
    _flib_vflog(func, level, fmt, argp);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   112
    va_end(argp);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   113
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   114
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   115
bool _flib_fassert(const char *func, int level, bool cond, const char *fmt, ...) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   116
    if(!cond) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   117
        va_list argp;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   118
        va_start(argp, fmt);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   119
        _flib_vflog(func, level, fmt, argp);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   120
        va_end(argp);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   121
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   122
    return !cond;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   123
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   124
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   125
int flib_log_getLevel() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   126
    return flib_loglevel;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   127
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   128
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   129
void flib_log_setLevel(int level) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   130
    flib_loglevel = level;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   131
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   132
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   133
void flib_log_setFile(FILE *file) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   134
    flib_logfile = file;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   135
    flib_logCallback = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   136
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   137
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   138
bool flib_log_isActive(int level) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   139
    return level >= flib_log_getLevel();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   140
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   141
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   142
void flib_log_setCallback(void (*logCallback)(int level, const char *msg)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   143
    flib_logCallback = logCallback;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   144
    flib_logfile = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7338
diff changeset
   145
}