commit 2fb7cbc
uint
·
2026-01-25 19:30:15 +0000 UTC
parent 480252f
add logging
5 files changed,
+49,
-7
M
Makefile
+1,
-1
1@@ -1,6 +1,6 @@
2 CC ?= cc
3
4-CPPFLAGS =
5+CPPFLAGS = -D_POSIX_C_SOURCE=200809L
6 CFLAGS = -std=c99 -Wall -Wextra -O2 -Iserver/include
7 OUT = parados
8
+1,
-1
1@@ -6,7 +6,7 @@
2 static const char* media_dir = "/path/to/media";
3 static const char* server_addr = "127.0.0.1";
4 static const int server_port = 3579;
5-static const bool verbose_log = false;
6+static const bool verbose_log = true;
7
8 enum {
9 HTTP_REQ_MAX = 2048,
+8,
-1
1@@ -7,11 +7,13 @@
2 #include "config.h"
3 #include "http.h"
4
5+void logmsg(bool verbose, const char* tag, const char* fmt, ...);
6+
7 static int read_reqline(int c, char* method, size_t msz, char* path, size_t psz);
8 static void reply_text(int c, const char* status, const char* body);
9 static int write_all(int fd, const void* buf, size_t n);
10
11-int http_handle(char c)
12+int http_handle(int c)
13 {
14 char method[16];
15 char path[1024];
16@@ -20,18 +22,23 @@ int http_handle(char c)
17 reply_text(c, HTTP_400, "bad request\n");
18 return -1;
19 }
20+ logmsg(verbose_log, "HTTP", "request: %s %s", method, path);
21
22 if (strcmp(method, "GET") != 0) {
23+ logmsg(verbose_log, "HTTP", "method not allowed: %s", method);
24 reply_text(c, HTTP_405, "method not allowed\n");
25 return 0;
26 }
27
28 if (strcmp(path, "/ping") == 0) {
29+ logmsg(verbose_log, "HTTP", "route /ping");
30 reply_text(c, HTTP_200, "ok\n");
31 return 0;
32 }
33
34+ logmsg(verbose_log, "HTTP", "route not found: %s", path);
35 reply_text(c, HTTP_404, "not found\n");
36+
37 return 0;
38 }
39
+1,
-1
1@@ -10,7 +10,7 @@
2 #define HTTP_CLOSE "Connection: close\r\n"
3 #define HTTP_LENGTH "Content-Length: %zu\r\n"
4
5-int http_handle(char c);
6+int http_handle(int c);
7
8 #endif /* HTTP_H */
9
+38,
-3
1@@ -1,15 +1,19 @@
2-/* parados - simple media server
3+/* parados
4+ the simple home media server
5
6 this software is licensed under ISC
7 check LICENCE for more details
8 */
9
10 #include <errno.h>
11+#include <stdarg.h>
12+#include <stdbool.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/socket.h>
18+#include <time.h>
19 #include <unistd.h>
20
21 #include <arpa/inet.h>
22@@ -19,6 +23,7 @@
23 #include "http.h"
24
25 void die(const char* s, int e);
26+void logmsg(bool verbose, const char* tag, const char* fmt, ...);
27 void run(void);
28 void setup(void);
29 int write_all(int fd, const void* buf, size_t n);
30@@ -31,6 +36,36 @@ void die(const char* s, int e)
31 exit(e);
32 }
33
34+void logmsg(bool verbose, const char* tag, const char* fmt, ...)
35+{
36+ if (!verbose)
37+ return;
38+
39+ time_t t = time(NULL);
40+ struct tm tmv;
41+ char ts[9];
42+
43+ if (localtime_r(&t, &tmv))
44+ snprintf(
45+ ts, sizeof(ts),
46+ "%02d:%02d:%02d",
47+ tmv.tm_hour,
48+ tmv.tm_min,
49+ tmv.tm_sec
50+ );
51+ else
52+ snprintf(ts, sizeof(ts), "??:??:??");
53+
54+ fprintf(stderr, "%s [%s] ", ts, tag);
55+
56+ va_list ap;
57+ va_start(ap, fmt);
58+ vfprintf(stderr, fmt, ap);
59+ va_end(ap);
60+
61+ fputc('\n', stderr);
62+}
63+
64 void run(void)
65 {
66 for (;;) {
67@@ -40,6 +75,7 @@ void run(void)
68 continue;
69 continue;
70 }
71+ logmsg(verbose_log, "CORE", "connection accepted");
72
73 (void)http_handle(c);
74 shutdown(c, SHUT_WR);
75@@ -75,8 +111,7 @@ void setup(void)
76 if (ret < 0)
77 die("listen", EXIT_FAILURE);
78
79- if (verbose_log)
80- printf("listening on %s:%d\n", server_addr, server_port);
81+ logmsg(verbose_log, "CORE", "listening on %s:%d", server_addr, server_port);
82 }
83
84 int main(void)