commit bc0aec2
uint
·
2026-01-25 21:16:25 +0000 UTC
parent a148208
update debug logging, update Makefile logging is now inline functions in log.h Makefile has 2 build options, release and debug
6 files changed,
+88,
-50
M
Makefile
+7,
-4
1@@ -1,12 +1,15 @@
2 CC ?= cc
3 CPPFLAGS = -D_POSIX_C_SOURCE=200809L
4-CFLAGS = -std=c99 -Wall -Wextra -O2 -Iserver/include
5+CFLAGS = -std=c99 -Wall -Wextra -Iserver/include
6 OUT = parados
7
8-all: parados
9+all: release
10
11-parados:
12- $(CC) $(CPPFLAGS) $(CFLAGS) server/*.c -o $(OUT)
13+release:
14+ $(CC) $(CPPFLAGS) -DNDEBUG $(CFLAGS) -O2 server/*.c -o $(OUT)
15+
16+debug:
17+ $(CC) $(CPPFLAGS) -DDEBUG $(CFLAGS) -g server/*.c -o $(OUT)
18
19 clean:
20 rm -f $(OUT)
+9,
-9
1@@ -24,39 +24,39 @@ int http_handle(int c)
2 reply_text(c, HTTP_400, "bad request\n");
3 return -1;
4 }
5- logmsg(verbose_log, "HTTP", "request: %s %s", method, path);
6+ LOG(verbose_log, "HTTP", "request: %s %s", method, path);
7
8 if (strcmp(method, "GET") != 0) {
9- logmsg(verbose_log, "HTTP", "method not allowed: %s", method);
10+ LOG(verbose_log, "HTTP", "method not allowed: %s", method);
11 reply_text(c, HTTP_405, "method not allowed\n");
12 return 0;
13 }
14
15 if (strcmp(path, "/ping") == 0) {
16- logmsg(verbose_log, "HTTP", "route /ping");
17+ LOG(verbose_log, "HTTP", "route /ping");
18 reply_text(c, HTTP_200, "ok\n");
19 return 0;
20 }
21
22 if (strcmp(path, "/library") == 0) {
23- logmsg(verbose_log, "HTTP", "route /library");
24+ LOG(verbose_log, "HTTP", "route /library");
25 struct library l;
26 struct json j;
27
28 if (scan_library(&l, media_dir) < 0) {
29- logmsg(verbose_log, "SCAN", "scan failed");
30+ LOG(verbose_log, "SCAN", "scan failed");
31 reply_text(c, HTTP_500, "scan failed\n");
32 return -1;
33 }
34- logmsg(verbose_log, "SCAN", "found %zu items", l.len);
35+ LOG(verbose_log, "SCAN", "found %zu items", l.len);
36
37 if (json_library(&j, &l) < 0) {
38- logmsg(verbose_log, "JSON", "encode failed");
39+ LOG(verbose_log, "JSON", "encode failed");
40 scan_library_free(&l);
41 reply_text(c, HTTP_500, "json failed\n");
42 return -1;
43 }
44- logmsg(verbose_log, "JSON", "encoded %zu bytes", j.len);
45+ LOG(verbose_log, "JSON", "encoded %zu bytes", j.len);
46
47 scan_library_free(&l);
48 reply_json(c, HTTP_200, j.buf, j.len);
49@@ -65,7 +65,7 @@ int http_handle(int c)
50 return 0;
51 }
52
53- logmsg(verbose_log, "HTTP", "route not found: %s", path);
54+ LOG(verbose_log, "HTTP", "route not found: %s", path);
55 reply_text(c, HTTP_404, "not found\n");
56
57 return 0;
+66,
-1
1@@ -1,9 +1,74 @@
2 #ifndef LOG_H
3 #define LOG_H
4
5+#include <stdarg.h>
6 #include <stdbool.h>
7+#include <stdio.h>
8+#include <time.h>
9
10-void logmsg(bool verbose, const char* tag, const char* fmt, ...);
11+#include "config.h"
12+
13+static inline const char* log_time_hhmmss(char out[9])
14+{
15+ time_t t = time(NULL);
16+ struct tm tmv;
17+
18+#if defined(_WIN32)
19+ localtime_s(&tmv, &t);
20+#else
21+ localtime_r(&t, &tmv);
22+#endif
23+
24+ snprintf(out, 9, "%02d:%02d:%02d", tmv.tm_hour, tmv.tm_min, tmv.tm_sec);
25+ return out;
26+}
27+
28+static inline void log_vprint(bool verbose, const char* tag,
29+#if defined(DEBUG)
30+ const char* file, int line, const char* func,
31+#endif
32+ const char* fmt, va_list ap)
33+{
34+ if (!verbose)
35+ return;
36+
37+ char ts[9];
38+ fprintf(stderr, "%s [%s] ", log_time_hhmmss(ts), tag);
39+
40+#if defined(DEBUG)
41+ fprintf(stderr, "[%s : %d : %s()] ", file, line, func);
42+#endif
43+
44+ vfprintf(stderr, fmt, ap);
45+
46+ fputc('\n', stderr);
47+}
48+
49+static inline void log_print(bool verbose, const char* tag,
50+#if defined(DEBUG)
51+ const char* file, int line, const char* func,
52+#endif
53+ const char* fmt, ...)
54+{
55+ va_list ap;
56+ va_start(ap, fmt);
57+
58+ log_vprint(verbose, tag,
59+#if defined(DEBUG)
60+ file, line, func,
61+#endif
62+ fmt, ap);
63+
64+ va_end(ap);
65+}
66+
67+#if defined(DEBUG)
68+#define LOG(verbose_log, tag, fmt, ...) \
69+ log_print((verbose_log), (tag), __FILE__, __LINE__, __func__, (fmt), ##__VA_ARGS__)
70+#else
71+#define LOG(verbose_log, tag, fmt, ...) \
72+ log_print((verbose_log), (tag), (fmt), ##__VA_ARGS__)
73+#endif
74
75 #endif /* LOG_H */
76
+1,
-1
1@@ -25,7 +25,7 @@ static int json_grow(struct json* j, size_t need)
2
3 char* nb = realloc(j->buf, ncap);
4 if (!nb) {
5- logmsg(verbose_log, "JSON", "out of memory");
6+ LOG(verbose_log, "JSON", "out of memory");
7 return -1;
8 }
9
+2,
-32
1@@ -36,36 +36,6 @@ void die(const char* s, int e)
2 exit(e);
3 }
4
5-void logmsg(bool verbose, const char* tag, const char* fmt, ...)
6-{
7- if (!verbose)
8- return;
9-
10- time_t t = time(NULL);
11- struct tm tmv;
12- char ts[9];
13-
14- if (localtime_r(&t, &tmv))
15- snprintf(
16- ts, sizeof(ts),
17- "%02d:%02d:%02d",
18- tmv.tm_hour,
19- tmv.tm_min,
20- tmv.tm_sec
21- );
22- else
23- snprintf(ts, sizeof(ts), "??:??:??");
24-
25- fprintf(stderr, "%s [%s] ", ts, tag);
26-
27- va_list ap;
28- va_start(ap, fmt);
29- vfprintf(stderr, fmt, ap);
30- va_end(ap);
31-
32- fputc('\n', stderr);
33-}
34-
35 void run(void)
36 {
37 for (;;) {
38@@ -75,7 +45,7 @@ void run(void)
39 continue;
40 continue;
41 }
42- logmsg(verbose_log, "CORE", "connection accepted");
43+ LOG(verbose_log, "CORE", "connection accepted");
44
45 (void)http_handle(c);
46 shutdown(c, SHUT_WR);
47@@ -111,7 +81,7 @@ void setup(void)
48 if (ret < 0)
49 die("listen", EXIT_FAILURE);
50
51- logmsg(verbose_log, "CORE", "listening on %s:%d", server_addr, server_port);
52+ LOG(verbose_log, "CORE", "listening on %s:%d", server_addr, server_port);
53 }
54
55 int main(void)
+3,
-3
1@@ -36,7 +36,7 @@ static int join_path(char* out, size_t outsz, const char* a, const char* b)
2 size_t bl = strlen(b);
3
4 if (al + 1 + bl + 1 > outsz) {
5- logmsg(verbose_log, "SCAN", "path too long");
6+ LOG(verbose_log, "SCAN", "path too long");
7 return -1;
8 }
9
10@@ -54,7 +54,7 @@ static int library_push(struct library* l, const char* rel)
11 size_t ncap = l->cap ? (l->cap * 2) : 64;
12 struct item* ni = realloc(l->items, ncap * sizeof(*ni));
13 if (!ni) {
14- logmsg(verbose_log, "SCAN", "out of memory");
15+ LOG(verbose_log, "SCAN", "out of memory");
16 return -1;
17 }
18 l->items = ni;
19@@ -89,7 +89,7 @@ static int scan_dir(struct library* l, const char* root, const char* rel)
20
21 d = opendir(full);
22 if (!d) {
23- logmsg(verbose_log, "SCAN", "opendir failed: %s", full);
24+ LOG(verbose_log, "SCAN", "opendir failed: %s", full);
25 return -1;
26 }
27