Logging through primal::log #6

Closed
UnflavoredMeson/Primal_Plus_Plus
opened 2026-11-24 20:15:48 +00:00 by UnflavoredMeson · 0 comments
Owner

Why

Every later system reports problems. With one logging API from the start, nothing is written against printf or OutputDebugString and converted later, and the editor (S7) can show the same log.

Decided

  • log::debug/info/warning/error(log::Category::x, "format {}", args) in Engine/Core/Log.h, with std::format syntax. No trailing \n, no "Vulkan:" prefixes: the category says it.
  • A header, not a module. The fork tried logging as a C++ module and reverted it: macros (DXCall), headers and game code all need to call it.
  • A logger thread; a console sink (stderr, plus the debugger on Windows) and a log file next to the executable; a ring buffer that the editor reads later.
  • error() waits until the message is written (so it is seen before a crash); don't call it every frame.
  • Tools/check_log_usage.py fails on direct printf/std::cout/OutputDebugString.
  • Command-line tools whose output a build reads use log::set_console_style(log::ConsoleStyle::plain).

Read first

  • Multithreading, section 6 (logging) and the logging decisions in section 8.
  • UnflavoredMeson/Primal#19.
  • Engine/Core/EngineWin32.cpp on master (where output goes today).

Steps

  • 1. Write tests in UnitTests/LogTests.cpp for formatting, categories and the ring buffer (a test sink that records messages).
  • 2. Write Log.h/Log.cpp with a synchronous sink until the tests pass.
  • 3. Add the logger thread and the file sink; run the tests under ThreadSanitizer (-fsanitize=thread) once.
  • 4. Replace every OutputDebugString/printf in the engine and tools.
  • 5. Write check_log_usage.py and add it to your check script.

Done when

  • UnitTests pass; the log tests pass under TSan.
  • check_log_usage.py reports nothing.
  • EngineTest writes EngineTest.log next to the executable on both OSes.

Claude can help with

  • Reviewing the logger thread's shutdown order (the classic bug: messages lost at exit).
  • Writing check_log_usage.py.

Learn

  • How std::format checks format strings at compile time (std::format_string).
  • Why a logger thread needs a flush on error().
## Why Every later system reports problems. With one logging API from the start, nothing is written against `printf` or `OutputDebugString` and converted later, and the editor (S7) can show the same log. ## Decided - `log::debug/info/warning/error(log::Category::x, "format {}", args)` in `Engine/Core/Log.h`, with `std::format` syntax. No trailing `\n`, no "Vulkan:" prefixes: the category says it. - **A header, not a module.** The fork tried logging as a C++ module and reverted it: macros (`DXCall`), headers and game code all need to call it. - A logger thread; a console sink (stderr, plus the debugger on Windows) and a log file next to the executable; a ring buffer that the editor reads later. - `error()` waits until the message is written (so it is seen before a crash); don't call it every frame. - `Tools/check_log_usage.py` fails on direct `printf`/`std::cout`/`OutputDebugString`. - Command-line tools whose output a build reads use `log::set_console_style(log::ConsoleStyle::plain)`. ## Read first - [Multithreading](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Multithreading), section 6 (logging) and the logging decisions in section 8. - `UnflavoredMeson/Primal#19`. - `Engine/Core/EngineWin32.cpp` on `master` (where output goes today). ## Steps - [x] 1. Write tests in `UnitTests/LogTests.cpp` for formatting, categories and the ring buffer (a test sink that records messages). - [x] 2. Write `Log.h`/`Log.cpp` with a synchronous sink until the tests pass. - [x] 3. Add the logger thread and the file sink; run the tests under ThreadSanitizer (`-fsanitize=thread`) once. - [x] 4. Replace every `OutputDebugString`/`printf` in the engine and tools. - [x] 5. Write `check_log_usage.py` and add it to your check script. ## Done when - [x] UnitTests pass; the log tests pass under TSan. - [x] `check_log_usage.py` reports nothing. - [x] EngineTest writes `EngineTest.log` next to the executable on both OSes. ## Claude can help with - Reviewing the logger thread's shutdown order (the classic bug: messages lost at exit). - Writing `check_log_usage.py`. ## Learn - How `std::format` checks format strings at compile time (`std::format_string`). - Why a logger thread needs a flush on `error()`.
Sign in to join this conversation.
No assignees
No project
No milestone
Blocks
#16 Job system module primal.jobs
UnflavoredMeson/Primal_Plus_Plus
#24 ShaderCompilerTool at build time
UnflavoredMeson/Primal_Plus_Plus
Depends on
#5 Unit tests with doctest
UnflavoredMeson/Primal_Plus_Plus
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
UnflavoredMeson/Primal_Plus_Plus#6
No description provided.