Skip to content

Keep one logger for the whole process - #145

Merged
ethindp merged 1 commit into
ethindp:masterfrom
trypsynth:fix/single-logger
Sep 27, 2026
Merged

ethindp merged 1 commit into
ethindp:masterfrom
trypsynth:fix/single-logger

Conversation

@trypsynth

Copy link
Copy Markdown
Contributor

Since 7b78f08, logging has been broken for me:

  • prism_set_log_level returns PRISM_LOG_LEVEL_NONE instead of the previous level.
  • A handler set with prism_set_log_handler never receives a message, because each message goes to a new logger.
  • Each Logger starts a drain thread that never stops, so every call leaks a thread.

I made it so logger() creates the logger once, in a function-local static that a lambda initializes. Your commit did actually address something that seems legit to me, so the lambda does return nullptr when allocation fails instead of throwing.

Tested on x64 Windows 10. The following C program sets a handler and a level, logs 1000 messages, and counts its threads:

#include <prism.h>
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>

static int received;

static void PRISM_CALL count(void* userdata, PrismLogLevel level, const char* source, const char* message) {
	(void)userdata;
	(void)level;
	(void)source;
	(void)message;
	received++;
}

static int threads(void) {
	DWORD self = GetCurrentProcessId();
	HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
	THREADENTRY32 entry = {.dwSize = sizeof entry};
	int n = 0;
	for (BOOL ok = Thread32First(snapshot, &entry); ok; ok = Thread32Next(snapshot, &entry))
		if (entry.th32OwnerProcessID == self) n++;
	CloseHandle(snapshot);
	return n;
}

int main() {
	PrismLogHandler handler = {.fn = count, .userdata = NULL};
	prism_set_log_handler(handler);
	prism_set_log_level(PRISM_LOG_LEVEL_DEBUG);
	printf("previous level: %d (expected %d)\n", prism_set_log_level(PRISM_LOG_LEVEL_TRACE), PRISM_LOG_LEVEL_DEBUG);
	int before = threads();
	for (int i = 0; i < 1000; i++) prism_log(PRISM_LOG_LEVEL_INFO, "repro", "hello");
	prism_log_flush();
	printf("messages delivered: %d of 1000\n", received);
	printf("threads: %d before, %d after\n", before, threads());
	return 0;
}

Before:

previous level: 5 (expected 1)
messages delivered: 0 of 1000
threads: 7 before, 1008 after

After:

previous level: 1 (expected 1)
messages delivered: 1000 of 1000
threads: 5 before, 5 after

Since 7b78f08, logger() returns `new Logger` on every call instead of a
single static instance. Each call to prism_set_log_level,
prism_set_log_handler and prism_log, and every message a backend logs,
gets a logger of its own. The level and handler never reach the logger
a message goes to, so nothing is delivered. Each Logger also starts a
drain thread that never stops, so every call leaks a thread.

The lambda keeps the behavior 7b78f08 introduced of returning nullptr
when allocation fails instead of throwing.

Signed-off-by: Quin Gillespie <[email protected]>
@ethindp
ethindp merged commit a5dfd9a into ethindp:master Sep 27, 2026
110 of 111 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants