commit 7718242

uint  ·  2026-01-28 21:01:18 +0000 UTC
parent ba644a9
improve logging
5 files changed,  +38, -39
+0, -1
1@@ -173,7 +173,6 @@ log:
2 	char port[8];
3 	snprintf(port, sizeof(port), "%d", server_port);
4 	LOG(true, "CONF", "Server Port        %s", port);
5-
6 	LOG(true, "CONF", "Verbose Logging    %s", (verbose_log) ? "true" : "false");
7 	LOG(true, "CONF", "CORS origins       %s", cors_origin);
8 
+17, -17
  1@@ -490,7 +490,7 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
  2 
  3 	/* build absolute path */
  4 	if (join_path(full, sizeof(full), media_dir, it->path) < 0) {
  5-		LOG(verbose_log, "HTTP", "Path too long");
  6+		LOG(verbose_log, "HTTP", "Stream FAILED      Path too long");
  7 		reply_text(c, hdr, HTTP_500, "Server error\n");
  8 		return -1;
  9 	}
 10@@ -498,7 +498,7 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
 11 	/* open file */
 12 	int fd = open(full, O_RDONLY);
 13 	if (fd < 0) {
 14-		LOG(verbose_log, "HTTP", "Open failed: %s", it->path);
 15+		LOG(verbose_log, "HTTP", "Open FAILED        %s", it->path);
 16 		reply_text(c, hdr, HTTP_404, "Not found\n");
 17 		return 0;
 18 	}
 19@@ -587,9 +587,9 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
 20 		close(fd);
 21 
 22 		if (partial == RANGE_OK)
 23-			LOG(verbose_log, "HTTP", "Header %s [%zu-%zu/%zu]", it->path, start, end, total);
 24+			LOG(verbose_log, "HTTP", "Header             %s [%zu-%zu/%zu]", it->path, start, end, total);
 25 		else
 26-			LOG(verbose_log, "HTTP", "Header %s (%zu bytes)", it->path, total);
 27+			LOG(verbose_log, "HTTP", "Header             %s (%zu bytes)", it->path, total);
 28 
 29 		return 0;
 30 	}
 31@@ -622,9 +622,9 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
 32 	close(fd);
 33 
 34 	if (partial == RANGE_OK)
 35-		LOG(verbose_log, "HTTP", "Streamed %s [%zu-%zu/%zu]", it->path, start, end, total);
 36+		LOG(verbose_log, "HTTP", "Streamed           %s [%zu-%zu/%zu]", it->path, start, end, total);
 37 	else
 38-		LOG(verbose_log, "HTTP", "Streamed %s (%zu bytes)", it->path, total);
 39+		LOG(verbose_log, "HTTP", "Streamed           %s (%zu bytes)", it->path, total);
 40 
 41 	return 0;
 42 }
 43@@ -644,7 +644,7 @@ int http_handle(int c)
 44 		reply_text(c, hdr, HTTP_400, "bad request\n");
 45 		return -1;
 46 	}
 47-	LOG(verbose_log, "HTTP", "Request: %s %s", method, path);
 48+	LOG(verbose_log, "HTTP", "Request            %s %s", method, path);
 49 
 50 	int head_only = 0;
 51 	if (strcmp(method, "GET") == 0) {
 52@@ -658,13 +658,13 @@ int http_handle(int c)
 53 		return 0;
 54 	}
 55 	else {
 56-		LOG(verbose_log, "HTTP", "Method not allowed: %s", method);
 57+		LOG(verbose_log, "HTTP", "Method forbidden   %s", method);
 58 		reply_text(c, hdr, HTTP_405, "method not allowed\n");
 59 		return 0;
 60 	}
 61 
 62 	if (strcmp(path, "/ping") == 0) {
 63-		LOG(verbose_log, "HTTP", "Route /ping");
 64+		LOG(verbose_log, "HTTP", "Route              /ping");
 65 		reply_text(c, hdr, HTTP_200, "ok\n");
 66 		return 0;
 67 	}
 68@@ -678,7 +678,7 @@ int http_handle(int c)
 69 	}
 70 
 71 	if (strcmp(path, "/library") == 0) {
 72-		LOG(verbose_log, "HTTP", "Route /library");
 73+		LOG(verbose_log, "HTTP", "Route              /library");
 74 		struct json j;
 75 		struct library view;
 76 		memset(&view, 0, sizeof(view));
 77@@ -707,11 +707,11 @@ int http_handle(int c)
 78 			if (u)
 79 				free(view.items);
 80 
 81-			LOG(verbose_log, "JSON", "Encode failed");
 82-			reply_text(c, hdr, HTTP_500, "json failed\n");
 83+			LOG(verbose_log, "JSON", "Encode     FAILED");
 84+			reply_text(c, hdr, HTTP_500, "json encode failed\n");
 85 			return -1;
 86 		}
 87-		LOG(verbose_log, "JSON", "Encoded %zu bytes", j.len);
 88+		LOG(verbose_log, "JSON", "Encoded bytes      %zu bytes", j.len);
 89 
 90 		reply_json(c, hdr, HTTP_200, j.buf, j.len, !head_only);
 91 		json_free(&j);
 92@@ -723,7 +723,7 @@ int http_handle(int c)
 93 	}
 94 
 95 	if (strncmp(path, "/stream/", 8) == 0) {
 96-		LOG(verbose_log, "HTTP", "Route /stream");
 97+		LOG(verbose_log, "HTTP", "Route              /stream");
 98 
 99 		uint64_t id;
100 		if (parse_hex64(path + 8, &id) < 0) {
101@@ -745,7 +745,7 @@ int http_handle(int c)
102 	}
103 
104 	if (strncmp(path, "/meta/", 6) == 0) {
105-		LOG(verbose_log, "HTTP", "Route /meta");
106+		LOG(verbose_log, "HTTP", "Route              /meta");
107 
108 		uint64_t id;
109 		if (parse_hex64(path + 6, &id) < 0) {
110@@ -784,7 +784,7 @@ int http_handle(int c)
111 	}
112 
113 	if (strcmp(path, "/queue") == 0) {
114-		LOG(verbose_log, "HTTP", "Route /queue");
115+		LOG(verbose_log, "HTTP", "Route              /queue");
116 
117 		if (queue_write(c, hdr, head_only, u) < 0) {
118 			reply_text(c, hdr, HTTP_500, "server error\n");
119@@ -794,7 +794,7 @@ int http_handle(int c)
120 		return 0;
121 	}
122 
123-	LOG(verbose_log, "HTTP", "Route not found: %s", path);
124+	LOG(verbose_log, "HTTP", "Route not found    %s", path);
125 	reply_text(c, hdr, HTTP_404, "not found\n");
126 
127 	return 0;
+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-		LOG(verbose_log, "JSON", "Out of memory");
6+		LOG(verbose_log, "JSON", "realloc FAILED     OOM");
7 		return -1;
8 	}
9 
+2, -2
 1@@ -36,7 +36,7 @@ static int library_push(struct library* l, const char* rel)
 2 		size_t ncap = l->cap ? (l->cap * 2) : 64;
 3 		struct item* ni = realloc(l->items, ncap * sizeof(*ni));
 4 		if (!ni) {
 5-			LOG(verbose_log, "SCAN", "Out of memory");
 6+			LOG(verbose_log, "SCAN", "realloc FAILED     OOM");
 7 			return -1;
 8 		}
 9 		l->items = ni;
10@@ -71,7 +71,7 @@ static int scan_dir(struct library* l, const char* root, const char* rel)
11 
12 	d = opendir(full);
13 	if (!d) {
14-		LOG(verbose_log, "SCAN", "opendir failed: %s", full);
15+		LOG(verbose_log, "SCAN", "opendir FAILED     %s", full);
16 		return -1;
17 	}
18 
+18, -18
  1@@ -152,11 +152,11 @@ bool user_allows_path(const struct user* u, const char* relpath)
  2 int users_add_allow(const char* prefix)
  3 {
  4 	if (users.len == 0 || !prefix) {
  5-		LOG(true, "AUTH", "Missing user or prefix");
  6+		LOG(true, "AUTH", "AddAllow FAILED     No user or prefix");
  7 		return -1;
  8 	}
  9 
 10-	LOG(verbose_log, "AUTH", "Allow for %s: %s", users.v[users.len - 1].name, prefix);
 11+	LOG(verbose_log, "AUTH", "Allow directories  %s %s", users.v[users.len - 1].name, prefix);
 12 
 13 	return allow_push(&users.v[users.len - 1], prefix);
 14 }
 15@@ -168,7 +168,7 @@ const struct user* users_auth_from_hdr(const char* hdr)
 16 
 17 	char auth[512];
 18 	if (hdr_get_value(auth, hdr, "authorization") < 0) {
 19-		LOG(true, "AUTH", "No Authorization header");
 20+		LOG(true, "AUTH", "Authorization      Not found");
 21 		return NULL;
 22 	}
 23 
 24@@ -178,7 +178,7 @@ const struct user* users_auth_from_hdr(const char* hdr)
 25 		p++;
 26 
 27 	if (strncasecmp(p, "Basic", 5) != 0) {
 28-		LOG(verbose_log, "AUTH", "Authorization not Basic");
 29+		LOG(verbose_log, "AUTH", "Authorization      Not Basic");
 30 		return NULL;
 31 	}
 32 
 33@@ -187,19 +187,19 @@ const struct user* users_auth_from_hdr(const char* hdr)
 34 		p++;
 35 
 36 	if (*p == '\0') {
 37-		LOG(verbose_log, "AUTH", "Basic auth missing token");
 38+		LOG(verbose_log, "AUTH", "Basic Auth         Missing token");
 39 		return NULL;
 40 	}
 41 
 42 	unsigned char dec[512];
 43 	if (b64_decode(dec, sizeof(dec), p) < 0) {
 44-		LOG(verbose_log, "AUTH", "Basic auth b64 decode failed");
 45+		LOG(verbose_log, "AUTH", "Basic Auth         B64 decode failed");
 46 		return NULL;
 47 	}
 48 
 49 	char* sep = strchr((char*)dec, ':');
 50 	if (!sep) {
 51-		LOG(verbose_log, "AUTH", "Basic auth decoded but missing ':'");
 52+		LOG(verbose_log, "AUTH", "Basic Auth         Decoded but missing ':'");
 53 		return NULL;
 54 	}
 55 
 56@@ -209,26 +209,26 @@ const struct user* users_auth_from_hdr(const char* hdr)
 57 
 58 	const struct user* u = find_user(user);
 59 	if (!u) {
 60-		LOG(true, "AUTH", "Auth failed: unknown user '%s'", user);
 61+		LOG(true, "AUTH", "Login FAILED Unknown user '%s'", user);
 62 		return NULL;
 63 	}
 64 
 65 	/* empty password -> passwordless account */
 66 	if (u->pass[0] == '\0') {
 67 		if (pass[0] == '\0') {
 68-			LOG(true, "AUTH", "Auth ok: %s (passwordless)", user);
 69+			LOG(true, "AUTH", "Login OK           %s", user);
 70 			return u;
 71 		}
 72-		LOG(true, "AUTH", "Auth failed: %s (passwordless but pass given)", user);
 73+		LOG(true, "AUTH", "Login FAILED       %s", user);
 74 		return NULL;
 75 	}
 76 
 77 	if (!ct_equal(u->pass, pass)) {
 78-		LOG(true, "AUTH", "Auth failed: bad password for %s", user);
 79+		LOG(true, "AUTH", "Login FAILED Bad password %s", user);
 80 		return NULL;
 81 	}
 82 
 83-	LOG(verbose_log, "AUTH", "Auth ok: %s", user);
 84+	LOG(verbose_log, "AUTH", "Login OK           %s", user);
 85 	return u;
 86 }
 87 
 88@@ -253,7 +253,7 @@ void users_free(void)
 89 int users_push(const char* name)
 90 {
 91 	if (!name || name[0] == '\0') {
 92-		LOG(true, "AUTH", "Empty username");
 93+		LOG(true, "AUTH", "UserAdd FAILED     Empty username");
 94 		return -1;
 95 	}
 96 
 97@@ -261,7 +261,7 @@ int users_push(const char* name)
 98 		size_t ncap = users.cap ? (users.cap * 2) : 8;
 99 		struct user* nv = realloc(users.v, ncap * sizeof(*nv));
100 		if (!nv) {
101-			LOG(true, "AUTH", "realloc failed (cap=%zu -> %zu)", users.cap, ncap);
102+			LOG(true, "AUTH", "realloc FAILED     (cap=%zu -> %zu)", users.cap, ncap);
103 			return -1;
104 		}
105 		users.v = nv;
106@@ -271,7 +271,7 @@ int users_push(const char* name)
107 	memset(&users.v[users.len], 0, sizeof(users.v[users.len]));
108 	snprintf(users.v[users.len].name, sizeof(users.v[users.len].name), "%s", name);
109 
110-	LOG(verbose_log, "AUTH", "User added: %s", users.v[users.len].name);
111+	LOG(verbose_log, "AUTH", "UserAdd SUCCESS    %s", users.v[users.len].name);
112 
113 	users.len++;
114 	return 0;
115@@ -280,7 +280,7 @@ int users_push(const char* name)
116 int users_set_pass(const char* pass)
117 {
118 	if (users.len == 0) {
119-		LOG(true, "AUTH", "No user yet");
120+		LOG(true, "AUTH", "SetPass FAILED     No users");
121 		return -1;
122 	}
123 
124@@ -291,8 +291,8 @@ int users_set_pass(const char* pass)
125 	snprintf(users.v[users.len - 1].pass, sizeof(users.v[users.len - 1].pass), "%s", pass);
126 
127 	LOG(
128-		verbose_log, "AUTH", "Pass set for user: %s (%s)",
129-		users.v[users.len - 1].name, (pass[0] == '\0') ? "empty" : "non-empty"
130+		verbose_log, "AUTH", "Password set for   %s (%s)",
131+		users.v[users.len - 1].name, (pass[0] == '\0') ? "none" : ""
132 	);
133 	return 0;
134 }