From b69ca1ea840a14554298840347ae3db043513972 Mon Sep 17 00:00:00 2001 From: acscpt <18481385+acscpt@users.noreply.github.com> Date: Tue, 21 Apr 2026 19:18:22 +0200 Subject: [PATCH] Add -log-stderr flag to route log output to stderr Separating diagnostic log output from stdout allows clean capture of emulated terminal output when running with -terminal -headless, without relying on line-level filtering at the consumer. Default behaviour is unchanged. The flag is a no-op when -no-log-stdout is also set, since that gate disables terminal logging entirely; this is noted in the help text. --- log.c | 17 +++++++++++++++-- log.h | 1 + main.c | 4 ++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/log.c b/log.c index 719107bb..79e80578 100644 --- a/log.c +++ b/log.c @@ -8,6 +8,13 @@ #include static int s_do_log_to_stdout = 1; + +// +// Redirects the stdout log stream to stderr. No effect when +// s_do_log_to_stdout is 0 -- that gate disables terminal logging entirely. +// +static int s_do_log_to_stderr = 0; + static struct util_file* s_p_log_file = NULL; static const char* @@ -74,6 +81,11 @@ log_set_do_log_to_stdout(int do_log_to_stdout) { s_do_log_to_stdout = do_log_to_stdout; } +void +log_set_log_to_stderr(void) { + s_do_log_to_stderr = 1; +} + static void log_do_log_va_list(int module, int severity, const char* p_msg, va_list args) { char msg[256]; @@ -92,11 +104,12 @@ log_do_log_va_list(int module, int severity, const char* p_msg, va_list args) { } if (s_do_log_to_stdout) { - ret = fprintf(stdout, "%s:%s:%s\n", p_severity_str, p_module_str, msg); + FILE* p_stream = s_do_log_to_stderr ? stderr : stdout; + ret = fprintf(p_stream, "%s:%s:%s\n", p_severity_str, p_module_str, msg); if (ret <= 0) { util_bail("fprintf failed"); } - ret = fflush(stdout); + ret = fflush(p_stream); if (ret != 0) { util_bail("fflush failed"); } diff --git a/log.h b/log.h index 7ffe5765..0960a325 100644 --- a/log.h +++ b/log.h @@ -29,6 +29,7 @@ enum log_severity { void log_set_log_filename(const char* p_filename); void log_set_do_log_to_stdout(int do_log_to_stdout); +void log_set_log_to_stderr(void); void log_do_log(int module, int severity, const char* p_msg, ...) __attribute__((format(printf, 3, 4))); diff --git a/main.c b/main.c index 9f365276..abe2b584 100644 --- a/main.c +++ b/main.c @@ -353,6 +353,8 @@ beebjit_main(void) { test_map_flag = 1; } else if (!strcmp(arg, "-no-log-stdout")) { log_set_do_log_to_stdout(0); + } else if (!strcmp(arg, "-log-stderr")) { + log_set_log_to_stderr(); } else if (!strcmp(arg, "-nula")) { nula_flag = 1; } else if (!strcmp(arg, "-version") || @@ -380,6 +382,8 @@ beebjit_main(void) { "-fast : run CPU as fast as host can; lowers accuracy.\n" "-accurate : run accurately; only does anything with -fast.\n" "-log-file : log to file as well as stdout.\n" +"-log-stderr : send log output to stderr rather than default stdout.\n" +" Ignored if -no-log-stdout is set.\n" "-1770 : emulate a 1770 instead of an 8271 floppy controller.\n" "-master : set up a Master 128 with MOS 3.20.\n" "-mos35 : set up a Master 128 with MOS 3.50.\n"