]> git.bts.cx Git - benzene.git/blob - src_platform/sdl/bz/debug/log.c
Sprites
[benzene.git] / src_platform / sdl / bz / debug / log.c
1 #include <bz/debug/log_internal.h>
2
3 #include <stb_sprintf.h>
4
5 #include <fcntl.h>
6 #include <stddef.h>
7 #include <stdlib.h>
8 #include <time.h>
9 #include <unistd.h>
10
11 #define _OUTPUT_PRELUDE(dst, name, filename, lineNumber) { \
12 time_t t; \
13 time(&t); \
14 struct tm *timeinfo = localtime(&t); \
15 output(dst, name",%02d:%02d:%02d,\"%s\"@%d,", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, filename, lineNumber); \
16 }
17
18 static char buffer[STB_SPRINTF_MIN];
19
20 static int errorFD = STDERR_FILENO;
21 static int outputFD = STDOUT_FILENO;
22
23 static char *sprintfCallback(char const *buf, void *user, int len) {
24 int fd = (int)((long)user);
25 write(fd, buf, (size_t)len);
26 return buffer;
27 }
28
29 static void output(int fd, const char *fmt, ...) {
30 va_list args;
31 va_start(args, fmt);
32 stbsp_vsprintfcb(sprintfCallback, (void *)((long)fd), buffer, fmt, args);
33 va_end(args);
34 }
35
36 void _bzLog(const char *filename, unsigned long lineNumber, const char *fmt, ...) {
37 va_list args;
38 va_start(args, fmt);
39 _OUTPUT_PRELUDE(outputFD, "LOG", filename, lineNumber);
40 stbsp_vsprintfcb(sprintfCallback, (void *)((long)outputFD), buffer, fmt, args);
41 output(outputFD, "\n");
42 va_end(args);
43 }
44
45 void _bzError(const char *filename, unsigned long lineNumber, const char *fmt, ...) {
46 va_list args;
47 va_start(args, fmt);
48 _OUTPUT_PRELUDE(errorFD, "ERROR", filename, lineNumber);
49 stbsp_vsprintfcb(sprintfCallback, (void *)((long)errorFD), buffer, fmt, args);
50 output(errorFD, "\n");
51 va_end(args);
52 }
53
54 void bzLogInit(const char *outputFilePath) {
55 if (outputFilePath != NULL) {
56 outputFD = errorFD = open(outputFilePath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
57 }
58 }
59
60 void bzLogTeardown() {
61 //close();
62 }