Add more logging

This commit is contained in:
kamkow1
2025-07-06 00:19:41 +02:00
parent 5ee7e02f7a
commit e88dc0426a
3 changed files with 28 additions and 2 deletions

View File

@ -22,10 +22,12 @@ void add_baked_resource(char *key, const uchar *data, size_t size)
}
write(fd, data, size);
shput(baked_resources.value, key, ((Baked_Resource_Value){ .memfd = fd, .bufptr = (void *)data }));
LOGI("Added baked resource: %s\n", key);
}
void init_baked_resources(void)
{
LOGI("Initializing baked resources...\n");
struct zip_t *zip = zip_stream_open(bundle_zip_data, bundle_zip_size, BUNDLE_ZIP_COMPRESSION, 'r');
size_t n = zip_entries_total(zip);
for (size_t i = 0; i < n; i++) {
@ -41,24 +43,29 @@ void init_baked_resources(void)
zip_entry_close(zip);
}
zip_stream_close(zip);
LOGI("baked resources done\n");
}
void free_baked_resources(void)
{
LOGI("freeing baked resouces\n");
for (size_t i = 0; i < shlen(baked_resources.value); i++) {
close(baked_resources.value[i].value.memfd);
free(baked_resources.value[i].key);
free(baked_resources.value[i].value.bufptr);
}
shfree(baked_resources.value);
LOGI("baked resources done\n");
}
bool get_baked_resource_path(char *key, char *buf, size_t size)
{
LOGI("Request for baked resource path: %s\n", key);
lockx(&baked_resources);
if (shgeti(baked_resources.value, key) != -1) {
Baked_Resource_Value brv = shget(baked_resources.value, key);
snprintf(buf, size, "/proc/%d/fd/%d", getpid(), brv.memfd);
LOGI("Memfd path %s\n", buf);
unlockx(&baked_resources);
return true;
}

6
main.c
View File

@ -110,7 +110,9 @@ void *route_thread_function(void *param)
void event_handler(struct mg_connection *conn, int ev, void *ev_data)
{
if (ev == MG_EV_HTTP_MSG) {
LOGI("HTTP EVENT\n");
struct mg_http_message *msg = (struct mg_http_message *)ev_data;
LOGI("Route: %.*s\n", msg->uri.len, msg->uri.buf);
Route_Thread_Data *data = calloc(1, sizeof(*data));
data->message = mg_strdup(msg->message);
@ -118,8 +120,10 @@ void event_handler(struct mg_connection *conn, int ev, void *ev_data)
data->mgr = conn->mgr;
data->arena = malloc(sizeof(*data->arena));
*data->arena = arena_get();
LOGI("starting handler thread...\n");
run_in_thread(&route_thread_function, data);
} else if (ev == MG_EV_WAKEUP) {
LOGI("WAKEUP EVENT\n");
struct mg_str *data = (struct mg_str *)ev_data;
Route_Result *result = *(Route_Result **)data->buf;
Arena *arena = result->arena;
@ -140,6 +144,8 @@ void event_handler(struct mg_connection *conn, int ev, void *ev_data)
arena_destroy(arena);
free(arena);
LOGI("WAKEUP done, arena cleaned\n");
}
}

View File

@ -68,12 +68,16 @@ bool gpp_run(Allocator *alloc, char *path, NString_List *env, String_Builder *ou
cmd_append_alloc(alloc, &cmd, env->items[i]);
}
return cmd_run_collect_alloc(alloc, &cmd, out) == 0;
bool ok = cmd_run_collect_alloc(alloc, &cmd, out) == 0;
size_t len = out->count > 100 ? 100 : out->count;
LOGI("Output (first 100): \n-------------------------------\n%.*s\n-------------------------------\n", len, out->items);
return ok;
}
#if MY_DEBUG
void route_build_id(Allocator *alloc, struct mg_http_message *msg, Route_Result *result, void *context_data)
{
LOGI("handler build_id (%.*s)\n", msg->uri.len, msg->uri.buf);
// TODO: Somehow use our arena in here. This will require a slight rework of the way cJSON works.
cJSON *root = cJSON_CreateObject();
defer { cJSON_Delete(root); }
@ -90,11 +94,13 @@ void route_build_id(Allocator *alloc, struct mg_http_message *msg, Route_Result
cJSON_AddItemToObject(root, "build_id", cJSON_CreateString(sb.items));
make_application_json(alloc, result, 200, root);
LOGI("handler build_id done\n");
}
#endif
void route_page_not_found(Allocator *alloc, struct mg_http_message *msg, Route_Result *result, void *context_data)
{
LOGI("handler page not found (%.*s)\n", msg->uri.len, msg->uri.buf);
NString_List env = {0};
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DURL=%.*s", msg->uri.len, msg->uri.buf)));
@ -103,6 +109,7 @@ void route_page_not_found(Allocator *alloc, struct mg_http_message *msg, Route_R
char path[PATH_MAX] = {0};
if (!get_baked_resource_path("page-missing.html", path, sizeof(path))) {
LOGE("Failed to get baked resource page-missing.html\n");
make_internal_server_error(alloc, result);
return;
}
@ -126,16 +133,19 @@ void route_page_not_found(Allocator *alloc, struct mg_http_message *msg, Route_R
} else {
make_text(alloc, result, "html", 200, out.items);
}
LOGI("handler page not found done\n");
}
void route_home(Allocator *alloc, struct mg_http_message *msg, Route_Result *result, void *context_data)
{
LOGI("handler home (%.*s)\n", msg->uri.len, msg->uri.buf);
NString_List env = {0};
String_Builder out = {0};
char path[PATH_MAX] = {0};
if (!get_baked_resource_path("home.html", path, sizeof(path))) {
LOGE("Failed to get baked resource home.html\n");
make_internal_server_error(alloc, result);
return;
}
@ -159,10 +169,12 @@ void route_home(Allocator *alloc, struct mg_http_message *msg, Route_Result *res
} else {
make_text(alloc, result, "html", 200, out.items);
}
LOGI("handler home done\n");
}
void route_generic_blog(Allocator *alloc, struct mg_http_message *msg, Route_Result *result, void *context_data)
{
LOGI("handler generic blog (%.*s)\n", msg->uri.len, msg->uri.buf);
NString_List env = {0};
String_Builder out = {0};
@ -171,6 +183,7 @@ void route_generic_blog(Allocator *alloc, struct mg_http_message *msg, Route_Res
char md_path[PATH_MAX] = {0};
if (!get_baked_resource_path(resource->key, md_path, sizeof(md_path))) {
LOGE("Failed to get baked resource %s\n", resource->key);
make_internal_server_error(alloc, result);
return;
}
@ -232,12 +245,12 @@ void route_generic_blog(Allocator *alloc, struct mg_http_message *msg, Route_Res
} else {
make_text(alloc, result, "html", 200, out.items);
}
LOGI("handler generic blog done\n");
}
void collect_blogs(Baked_Resource *resource, void *udata)
{
struct { Allocator *alloc; String_Builder *sb; } *ud = udata;
printf("%s\n", resource->key);
if ((strlen(resource->key) >= strlen("blog-"))
&& strncmp(resource->key, "blog-", strlen("blog-")) == 0) {
sb_append_nstr_alloc(ud->alloc, ud->sb,