project_files/frontlib/util/logging.c
author Medo <smaxein@googlemail.com>
Wed, 27 Jun 2012 18:02:45 +0200
changeset 7275 15f722e0b96f
parent 7224 5143861c83bd
child 7314 6171f0bad318
permissions -rw-r--r--
frontlib: Getting there :) Added commandline client for testing
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
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    37
static const char *getPrefix(int level) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    38
	switch(level) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    39
	case FLIB_LOGLEVEL_ERROR: return "E";
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    40
	case FLIB_LOGLEVEL_WARNING: return "W";
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    41
	case FLIB_LOGLEVEL_INFO: return "I";
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    42
	case FLIB_LOGLEVEL_DEBUG: return "D";
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    43
	default: return "?";
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    44
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    45
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    46
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    47
static void _flib_vflog(const char *func, int level, const char *fmt, va_list args) {
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    48
	FILE *logfile = flib_log_getfile();
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    49
	if(level >= flib_loglevel) {
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    50
		fprintf(logfile, "%s ", getPrefix(level));
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    51
		log_time(logfile);
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    52
		fprintf(logfile, " [%-30s] ", func);
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    53
		vfprintf(logfile, fmt, args);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    54
		fprintf(logfile, "\n");
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    55
		fflush(logfile);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    56
	}
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    57
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    58
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    59
void _flib_flog(const char *func, int level, const char *fmt, ...) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    60
	va_list argp;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    61
	va_start(argp, fmt);
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    62
	_flib_vflog(func, level, fmt, argp);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    63
	va_end(argp);
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    64
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    65
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    66
bool _flib_fassert(const char *func, int level, bool cond, const char *fmt, ...) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    67
	if(!cond) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    68
		va_list argp;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    69
		va_start(argp, fmt);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    70
		_flib_vflog(func, level, fmt, argp);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    71
		va_end(argp);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    72
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    73
	return !cond;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    74
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    75
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    76
bool _flib_assert_params(const char *func, bool cond) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    77
	return _flib_fassert(func, FLIB_LOGLEVEL_ERROR, cond, "Invalid parameter to function");
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    78
}
7182
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
int flib_log_getLevel() {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    81
	return flib_loglevel;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    82
}
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
void flib_log_setLevel(int level) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    85
	flib_loglevel = level;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    86
}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    87
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    88
void flib_log_setFile(FILE *file) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    89
	flib_logfile = file;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    90
}
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    91
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    92
bool flib_log_isActive(int level) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    93
	return level >= flib_log_getLevel();
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    94
}