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
9/* write formatted log message to file */
10static void vlog(FILE* fd, const char* fmt, va_list ap)
11{
12 fprintf(fd, "Magnolia: ");
13 vfprintf(fd, fmt, ap);
14 fputc('\n', fd);
15 fflush(fd);
16}
17
18void mg_die(MGErrorCodes ec, const char* fmt, ...)
19{
20 va_list ap;
21 va_start(ap, fmt);
22 vlog(stderr, fmt, ap);
23 va_end(ap);
24
25 fprintf(stderr, "Died with ErrorCode [%d]\n", ec);
26 exit(ec);
27}
28
29void mg_log(LogType lt, const char* fmt, ...)
30{
31#ifndef NDEBUG
32 int debug = 1;
33#else
34 int debug = 0;
35#endif
36 FILE* fd;
37 va_list ap;
38
39 if (lt == LOG_DBG && !debug)
40 return;
41
42 if (lt == LOG_DBG || lt == LOG_ERR) {
43 fd = stderr;
44 }
45 else {
46 fd = stdout;
47 }
48
49 va_start(ap, fmt);
50 vlog(fd, fmt, ap);
51 va_end(ap);
52}