: asapien/signet

commit 847eac9620cbff7d42c334dfa5a4f87bea598a95
parent 7399e3892e467d07797ca10a6d49acbbd7a3cf5e
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 19 Sep 2026 03:26:24 +0530

01: Syslog logging in signet-api and pim-api

Add comprehensive error logging to both FastCGI applications:
- Database query/execution failures logged with PostgreSQL error text
- All non-2xx HTTP responses logged with method, path, user, and error message
- Errors logged to both syslog (LOCAL0) and stderr for dev visibility
- New logging utilities: log_open(), log_err(), log_request()
- Updated AGENTS.md with documentation on finding logs in dev and production

The logging infrastructure allows debugging of failures that were previously
invisible when stderr was detached by kfcgi. In development, logs are visible
in container output; in production, configure syslog to capture LOCAL0 messages.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
8 files changed, 135 insertions(+), 7 deletions(-)
M
AGENTS.md
|
5
+++++
M
pim/pim-api.c
|
28
+++++++++++++++++++++++++---
M
src/db.c
|
5
+++--
M
src/http.c
|
30
++++++++++++++++++++++++++++++
M
src/http.h
|
3
+++
M
src/signet-api.c
|
27
+++++++++++++++++++++++++--
M
src/util.c
|
36
++++++++++++++++++++++++++++++++++++
M
src/util.h
|
8
++++++++
diff --git a/AGENTS.md b/AGENTS.md
@@ -105,6 +105,11 @@ sleep 6 && podman exec signet /src/test/e2e.sh        # must end with "all passe
 * Portable files can be syntax-checked on macOS:
   `cc -std=c99 -D_DARWIN_C_SOURCE -Isrc -fsyntax-only src/{conf,util,ttf}.c`,
   `node --check www/app.js`.
+* **Error logging:** API failures (database errors, validation failures, 4xx/5xx
+  responses) are logged to syslog (facility LOCAL0) and to stderr. In dev:
+  `podman exec signet journalctl -u signet-api` or check `podman logs signet`
+  for stderr output. In production, configure syslog to capture
+  `LOCAL0.notice` and above.
 
 Extend `test/e2e.sh` with every API behaviour change; it is the regression
 suite. After any change to `render.c`, `pdfw.c`, `ttf.c` or `fitText`, also do
diff --git a/pim/pim-api.c b/pim/pim-api.c
@@ -88,6 +88,20 @@ h_upsert(struct kreq *r, PGconn *pg)
 		send_json(r, KHTTP_200, "{\"ok\":true}");
 }
 
+static const char *
+http_method_str(enum kmethod m)
+{
+	switch (m) {
+	case KMETHOD_GET: return "GET";
+	case KMETHOD_POST: return "POST";
+	case KMETHOD_HEAD: return "HEAD";
+	case KMETHOD_PUT: return "PUT";
+	case KMETHOD_DELETE: return "DELETE";
+	case KMETHOD_OPTIONS: return "OPTIONS";
+	default: return "?";
+	}
+}
+
 static void
 dispatch(struct kreq *r)
 {

@@ -96,14 +110,17 @@ dispatch(struct kreq *r)
 	PGconn *pg;
 	PGresult *res;
 	int n;
+	const char *method = http_method_str(r->method);
 
 	ident_read(r, &cf, &id);
 	if (id.user == NULL) {
 		send_err(r, KHTTP_401, "no authenticated user");
+		log_request_error(method, r->fullpath, id.user, 401, "no authenticated user");
 		return;
 	}
 	if ((pg = db_get(&cf)) == NULL) {
 		send_err(r, KHTTP_503, "database unavailable");
+		log_request_error(method, r->fullpath, id.user, 503, "database unavailable");
 		return;
 	}
 	n = http_path(r, buf, sizeof(buf), seg, 2);

@@ -111,9 +128,10 @@ dispatch(struct kreq *r)
 		res = db_q(pg, "SELECT coalesce(json_agg(t), '[]')::text FROM ("
 		    "SELECT entity_code, attr_key, attr_value FROM pim_attributes "
 		    "ORDER BY entity_code, attr_key) t", 0, NULL);
-		if (res == NULL)
+		if (res == NULL) {
 			send_err(r, KHTTP_500, "database error");
-		else {
+			log_request_error(method, r->fullpath, id.user, 500, "database error");
+		} else {
 			send_json(r, KHTTP_200, PQgetvalue(res, 0, 0));
 			PQclear(res);
 		}

@@ -121,14 +139,17 @@ dispatch(struct kreq *r)
 	}
 	if (!http_post_ok(r) || !ident_has_role(&id, &cf, "pim_admin")) {
 		send_err(r, KHTTP_403, "pim_admin role required");
+		log_request_error(method, r->fullpath, id.user, 403, "pim_admin role required");
 		return;
 	}
 	if (n == 1 && strcmp(seg[0], "attrs") == 0)
 		h_upsert(r, pg);
 	else if (n == 1 && strcmp(seg[0], "resync") == 0)
 		h_resync(r, pg);
-	else
+	else {
 		send_err(r, KHTTP_404, "not found");
+		log_request_error(method, r->fullpath, id.user, 404, "not found");
+	}
 }
 
 int

@@ -141,6 +162,7 @@ main(void)
 
 	if (conf_load(&cf, path ? path : CONF_PATH_DEFAULT) == -1)
 		return 1;
+	log_open("pim-api");
 	if (khttp_fcgi_init(&fcgi, NULL, 0, pages, 1, 0) != KCGI_OK)
 		return 1;
 	while (khttp_fcgi_parse(fcgi, &r) == KCGI_OK) {
diff --git a/src/db.c b/src/db.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 
 #include "db.h"
+#include "util.h"
 
 static PGconn *pg;
 static redisContext *rc;

@@ -37,7 +38,7 @@ redis_get(const struct conf *cf)
 	rc = redisConnect(conf_get(cf, "redis.host", "127.0.0.1"),
 	    atoi(conf_get(cf, "redis.port", "6379")));
 	if (rc == NULL || rc->err) {
-		fprintf(stderr, "signet: redis: %s\n", rc ? rc->errstr : "oom");
+		log_err("redis: %s", rc ? rc->errstr : "oom");
 		redis_drop();
 	}
 	return rc;

@@ -58,7 +59,7 @@ db_q(PGconn *c, const char *sql, int n, const char *const *params)
 	ExecStatusType st = PQresultStatus(res);
 
 	if (st != PGRES_TUPLES_OK && st != PGRES_COMMAND_OK) {
-		fprintf(stderr, "signet: sql: %s", PQerrorMessage(c));
+		log_err("sql: %s", PQerrorMessage(c));
 		PQclear(res);
 		return NULL;
 	}
diff --git a/src/http.c b/src/http.c
@@ -132,3 +132,33 @@ send_file(struct kreq *r, const char *path, const char *mime, const char *name)
 		khttp_write(r, buf, n);
 	fclose(f);
 }
+
+static int
+http_status_code(enum khttp code)
+{
+	switch (code) {
+	case KHTTP_200: return 200;
+	case KHTTP_201: return 201;
+	case KHTTP_204: return 204;
+	case KHTTP_301: return 301;
+	case KHTTP_302: return 302;
+	case KHTTP_304: return 304;
+	case KHTTP_400: return 400;
+	case KHTTP_401: return 401;
+	case KHTTP_403: return 403;
+	case KHTTP_404: return 404;
+	case KHTTP_405: return 405;
+	case KHTTP_409: return 409;
+	case KHTTP_500: return 500;
+	case KHTTP_501: return 501;
+	case KHTTP_503: return 503;
+	default: return 500;
+	}
+}
+
+void
+log_request_error(const char *method, const char *path,
+    const char *user, int status, const char *msg)
+{
+	log_request(method, path, user, status, msg);
+}
diff --git a/src/http.h b/src/http.h
@@ -31,5 +31,8 @@ void send_json(struct kreq *, enum khttp, const char *body);
 void send_err(struct kreq *, enum khttp, const char *msg);
 void send_file(struct kreq *, const char *path, const char *mime,
     const char *download_name);
+/* log a request response; call after send_err for errors */
+void log_request_error(const char *method, const char *path,
+    const char *user, int status, const char *msg);
 
 #endif
diff --git a/src/signet-api.c b/src/signet-api.c
@@ -933,6 +933,20 @@ h_dam(struct kreq *r)
 
 /* --- dispatch -------------------------------------------------------------- */
 
+static const char *
+http_method_str(enum kmethod m)
+{
+	switch (m) {
+	case KMETHOD_GET: return "GET";
+	case KMETHOD_POST: return "POST";
+	case KMETHOD_HEAD: return "HEAD";
+	case KMETHOD_PUT: return "PUT";
+	case KMETHOD_DELETE: return "DELETE";
+	case KMETHOD_OPTIONS: return "OPTIONS";
+	default: return "?";
+	}
+}
+
 static void
 dispatch(struct kreq *r)
 {

@@ -940,18 +954,22 @@ dispatch(struct kreq *r)
 	char buf[512], *seg[5];
 	PGconn *pg;
 	int n, get = r->method == KMETHOD_GET;
+	const char *method = http_method_str(r->method);
 
 	ident_read(r, &cf, &id);
 	if (id.user == NULL) {
 		send_err(r, KHTTP_401, "no authenticated user");
+		log_request_error(method, r->fullpath, id.user, 401, "no authenticated user");
 		return;
 	}
 	if (!get && !http_post_ok(r)) {
 		send_err(r, KHTTP_403, "POST with X-Requested-With: signet required");
+		log_request_error(method, r->fullpath, id.user, 403, "POST with X-Requested-With: signet required");
 		return;
 	}
 	if ((pg = db_get(&cf)) == NULL) {
 		send_err(r, KHTTP_503, "database unavailable");
+		log_request_error(method, r->fullpath, id.user, 503, "database unavailable");
 		return;
 	}
 	n = http_path(r, buf, sizeof(buf), seg, 5);

@@ -983,10 +1001,14 @@ dispatch(struct kreq *r)
 			h_doc_decide(r, pg, &id, seg[1]);
 		else if (n == 3 && !get && strcmp(seg[2], "delete") == 0)
 			h_doc_delete(r, pg, &id, seg[1]);
-		else
+		else {
 			send_err(r, KHTTP_404, "not found");
-	} else
+			log_request_error(method, r->fullpath, id.user, 404, "not found");
+		}
+	} else {
 		send_err(r, KHTTP_404, "not found");
+		log_request_error(method, r->fullpath, id.user, 404, "not found");
+	}
 }
 
 int

@@ -1001,6 +1023,7 @@ main(void)
 		fprintf(stderr, "signet-api: cannot read configuration\n");
 		return 1;
 	}
+	log_open("signet-api");
 	umask(007);
 	if (khttp_fcgi_init(&fcgi, NULL, 0, pages, 1, 0) != KCGI_OK)
 		return 1;
diff --git a/src/util.c b/src/util.c
@@ -4,9 +4,11 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <syslog.h>
 #include <unistd.h>
 
 #include "util.h"

@@ -313,3 +315,37 @@ utf8_next(const char **sp)
 	*sp += n + 1;
 	return cp;
 }
+
+/* --- logging -------------------------------------------------------------- */
+
+void
+log_open(const char *ident)
+{
+	openlog(ident, LOG_PID, LOG_LOCAL0);
+}
+
+void
+log_err(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vsyslog(LOG_ERR, fmt, ap);
+	va_end(ap);
+	fprintf(stderr, "signet: ");
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+	fprintf(stderr, "\n");
+}
+
+void
+log_request(const char *method, const char *path, const char *user,
+    int status, const char *msg)
+{
+	if (status < 200 || status >= 300) {
+		syslog(LOG_WARNING, "%s %s (user=%s status=%d): %s",
+		    method ? method : "?", path ? path : "/",
+		    user ? user : "?", status, msg ? msg : "");
+	}
+}
diff --git a/src/util.h b/src/util.h
@@ -27,4 +27,12 @@ int is_safe_relpath(const char *s);
 /* decode one UTF-8 code point, advance *s; returns 0xFFFD on bad input */
 uint32_t utf8_next(const char **s);
 
+/* initialize syslog with the given identity (e.g., "signet-api") */
+void log_open(const char *ident);
+/* log to syslog; for errors, include the PostgreSQL/Redis error text */
+void log_err(const char *fmt, ...);
+/* log request failures (non-2xx responses) */
+void log_request(const char *method, const char *path, const char *user,
+    int status, const char *msg);
+
 #endif