small3dlib uint/magnolia / source / util.c
 1#include <stdarg.h>
 2#include <stdio.h>
 3#include <stdlib.h>
 4
 5#include "util.h"
 6
 7static void vlog(FILE* fd, const char* fmt, va_list ap);
 8
 9static void vlog(FILE* fd, const char* fmt, va_list ap)
10{
11	fprintf(fd, "Magnolia: ");
12	vfprintf(fd, fmt, ap);
13	fputc('\n', fd);
14	fflush(fd);
15}
16
17void mg_die(MGErrorCodes ec, const char* fmt, ...)
18{
19	va_list ap;
20	va_start(ap, fmt);
21	vlog(stderr, fmt, ap);
22	va_end(ap);
23
24	fprintf(stderr, "Died with ErrorCode [%d]\n", ec);
25	exit(ec);
26}
27
28void mg_log(LogType lt, const char* fmt, ...)
29{
30#ifndef NDEBUG
31	int debug = 1;
32#else
33	int debug = 0;
34#endif
35	FILE* fd;
36	va_list ap;
37
38	if (lt == LOG_DBG && !debug)
39		return;
40
41	if (lt == LOG_DBG || lt == LOG_ERR)
42		fd = stderr;
43	else
44		fd = stdout;
45
46	va_start(ap, fmt);
47	vlog(fd, fmt, ap);
48	va_end(ap);
49}
50