-
Notifications
You must be signed in to change notification settings - Fork 6
/
log.c
36 lines (32 loc) · 911 Bytes
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "log.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
void
log_internal(char type, const char *format, ...) {
if (log_implementation != NULL) {
va_list ap;
va_start(ap, format);
log_implementation(type, format, ap);
va_end(ap);
}
}
// The default logging implementation simply prints to stderr.
static void
log_stderr(char type, const char *format, va_list ap) {
char *message = NULL;
vasprintf(&message, format, ap);
assert(message != NULL);
const char *logtype = "";
const char *separator = ": ";
switch (type) {
case 'D': logtype = "Debug"; break;
case 'I': logtype = "Info"; break;
case 'W': logtype = "Warning"; break;
case 'E': logtype = "Error"; break;
default: separator = "";
}
fprintf(stderr, "%s%s%s\n", logtype, separator, message);
free(message);
}
void (*log_implementation)(char type, const char *format, va_list ap) = log_stderr;