project_files/frontlib/util/logging.c
author Medo <smaxein@googlemail.com>
Mon, 11 Jun 2012 00:02:17 +0200
changeset 7182 076aba32abd3
parent 7179 f84805e6df03
child 7224 5143861c83bd
permissions -rw-r--r--
Small improvements to the frontend lib for better debugging
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "logging.h"
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
     3
#include <time.h>
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
     4
#include <stdio.h>
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
     5
#include <stdarg.h>
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
     6
#include <stdlib.h>
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
     7
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     8
static int flib_loglevel = FLIB_LOGLEVEL_INFO;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     9
static FILE *flib_logfile = NULL;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    10
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
char* flib_format_ip(uint32_t numip) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
	static char ip[16];
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    13
	snprintf(ip, 16, "%u.%u.%u.%u", (unsigned)(numip>>24), (unsigned)((numip>>16)&0xff), (unsigned)((numip>>8)&0xff), (unsigned)(numip&0xff));
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
	return ip;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
}
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    16
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    17
static inline FILE *flib_log_getfile() {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    18
	if(flib_logfile==NULL) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    19
		return stdout;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    20
	} else {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    21
		return flib_logfile;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    22
	}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    23
}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    24
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    25
static void log_time() {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    26
    time_t timer;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    27
    char buffer[25];
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    28
    struct tm* tm_info;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    29
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    30
    time(&timer);
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    31
    tm_info = localtime(&timer);
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    32
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    33
    strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info);
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    34
    fprintf(flib_log_getfile(), "%s", buffer);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    35
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    36
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    37
static void flib_vflog(const char *prefix, int level, const char *fmt, va_list args) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    38
	FILE *logfile = flib_log_getfile();
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    39
	if(level >= flib_loglevel) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    40
		fprintf(logfile, "%s ", prefix);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    41
		log_time(logfile);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    42
		fprintf(logfile, "  ", prefix);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    43
		vfprintf(logfile, fmt, args);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    44
		fprintf(logfile, "\n");
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    45
		fflush(logfile);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    46
	}
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    47
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    48
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    49
void flib_log_e(const char *fmt, ...) {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    50
	va_list argp;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    51
	va_start(argp, fmt);
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    52
	flib_vflog("E", FLIB_LOGLEVEL_ERROR, fmt, argp);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    53
	va_end(argp);
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    54
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    55
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    56
void flib_log_w(const char *fmt, ...) {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    57
	va_list argp;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    58
	va_start(argp, fmt);
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    59
	flib_vflog("W", FLIB_LOGLEVEL_WARNING, fmt, argp);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    60
	va_end(argp);
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    61
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    62
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    63
void flib_log_i(const char *fmt, ...) {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    64
	va_list argp;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    65
	va_start(argp, fmt);
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    66
	flib_vflog("I", FLIB_LOGLEVEL_INFO, fmt, argp);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    67
	va_end(argp);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    68
}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    69
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    70
void flib_log_d(const char *fmt, ...) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    71
	va_list argp;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    72
	va_start(argp, fmt);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    73
	flib_vflog("D", FLIB_LOGLEVEL_DEBUG, fmt, argp);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    74
	va_end(argp);
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    75
}
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    76
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    77
int flib_log_getLevel() {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    78
	return flib_loglevel;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    79
}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    80
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    81
void flib_log_setLevel(int level) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    82
	flib_loglevel = level;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    83
}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    84
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    85
void flib_log_setFile(FILE *file) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    86
	flib_logfile = file;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    87
}