commit 28b2d38

uint  ·  2026-07-21 17:31:00 +0000 UTC
parent 9743d43
add log levels, flush log output, put newline character on fd not stderr
2 files changed,  +27, -5
+8, -2
 1@@ -12,8 +12,14 @@ typedef enum {
 2 	MG_ERR_LAST
 3 } MGErrorCodes;
 4 
 5-void mg_die(int ec, const char* fmt, ...);
 6-void mg_log(FILE* fd, const char* fmt, ...);
 7+typedef enum {
 8+	LOG_INF, /* info */
 9+	LOG_ERR, /* error */
10+	LOG_DBG  /* debug */
11+} LogType;
12+
13+void mg_die(MGErrorCodes ec, const char* fmt, ...);
14+void mg_log(LogType lt, const char* fmt, ...);
15 
16 #endif /* UTIL_H */
17 
+19, -3
 1@@ -10,10 +10,11 @@ static void vlog(FILE* fd, const char* fmt, va_list ap)
 2 {
 3 	fprintf(fd, "magnolia: ");
 4 	vfprintf(fd, fmt, ap);
 5-	fputc('\n', stderr);
 6+	fputc('\n', fd);
 7+	fflush(fd);
 8 }
 9 
10-void mg_die(int ec, const char* fmt, ...)
11+void mg_die(MGErrorCodes ec, const char* fmt, ...)
12 {
13 	va_list ap;
14 	va_start(ap, fmt);
15@@ -24,9 +25,24 @@ void mg_die(int ec, const char* fmt, ...)
16 	exit(ec);
17 }
18 
19-void mg_log(FILE* fd, const char* fmt, ...)
20+void mg_log(LogType lt, const char* fmt, ...)
21 {
22+#ifdef DEBUG
23+	int debug = 1;
24+#else
25+	int debug = 0;
26+#endif
27+	FILE* fd;
28 	va_list ap;
29+
30+	if (lt == LOG_DBG && !debug)
31+		return;
32+
33+	if (lt == LOG_DBG || lt == LOG_ERR)
34+		fd = stderr;
35+	else
36+		fd = stdout;
37+
38 	va_start(ap, fmt);
39 	vlog(fd, fmt, ap);
40 	va_end(ap);