Skip to main content

Debug Logging

Img2Num uses spdlog for compile-time gated debug logging. spdlog is header-only and is part of every build (C++, C, WASM and Python). What changes between builds is the log level, which you choose when you compile.

TL;DR
  • This is only relevant for C and C++ code in Img2Num.
  • Include internal/log.h and call the IMG2NUM_LOG_* macros.
  • Pass the level when you build, e.g. just cmake build Debug TRACE.
  • A default Release build prints nothing - logging is stripped at compile time.
  • std::cout, printf and friends are banned in library code and caught by clang-tidy (see below).

Using the logging macros​

Include the header, which lives at core/include/internal/log.h:

#include "internal/log.h"

It provides one macro per level, from most to least verbose. Messages use spdlog's {}-style formatting and are written to stdout by the img2num logger.

MacroTypical use
IMG2NUM_LOG_TRACE(...)Very fine-grained tracing, usually per iteration
IMG2NUM_LOG_DEBUG(...)Diagnostic detail useful while developing
IMG2NUM_LOG_INFO(...)High-level progress and state changes
IMG2NUM_LOG_WARN(...)Something unexpected but recoverable
IMG2NUM_LOG_ERROR(...)An operation failed
IMG2NUM_LOG_CRITICAL(...)The process cannot continue
Example
IMG2NUM_LOG_INFO("GPU initialized");
IMG2NUM_LOG_WARN("Falling back to CPU");
IMG2NUM_LOG_ERROR("Fatal: {}", error_message);
IMG2NUM_LOG_DEBUG("Processing pixel {}", index);
note

Macros below the compiled-in level are removed entirely, so their arguments are never evaluated. Keep side effects out of log calls.

Choosing the log level​

The level is a compile-time setting controlled by the CMake option IMG2NUM_LOG_LEVEL. It accepts:

AUTO (default) â‹…\cdot TRACE â‹…\cdot DEBUG â‹…\cdot INFO â‹…\cdot WARN â‹…\cdot ERROR â‹…\cdot CRITICAL â‹…\cdot OFF

With AUTO, the level is derived from the build type:

Build typeResolved levelEffect
DebugTRACEEverything is logged
Release, MinSizeRelOFFLogging is fully stripped
Anything else (e.g. RelWithDebInfo)INFOINFO and above
Changing the level requires a rebuild

Because the level is baked in at compile time, a default Release build prints no logs. If you change IMG2NUM_LOG_LEVEL or the build type, you must rebuild.

Enabling logging per build​

The C/C++, Python, and JS/WASM just build recipes take the build type first and the log level second. Both are optional and default to Release and AUTO.

Paste this inside the Docker shell
just cmake build Debug TRACE

Verifying it worked​

Build with Debug and run the C++ console example. It should print log lines; a Release build of the same example should print none.

Paste this inside the Docker shell
just cmake build Debug TRACE
just console-cpp <path-to-image>

Why spdlog?​

  • Zero overhead when OFF - logging is gated at compile time, not at runtime.
  • Safe by default - debug output cannot accidentally ship in a default Release build.
  • One API everywhere - the same macros work in the C++ core, C bindings and Python bindings.
  • Header-only - used the same way in every build, including WASM.

How the "no iostream" rule is enforced​

Raw iostream and stdio output is banned in library code. The rule is enforced by clang-tidy, configured in .clang-tidy - not by a GitHub Actions workflow that greps for std::cout. Two checks do the work, and both are treated as errors:

CheckWhat it blocks
portability-restrict-system-includes<iostream>, <ostream>, <istream>, <cstdio>, <stdio.h>, <print>, <syncstream> - i.e. std::cout and std::cerr
bugprone-unsafe-functionsprintf, fprintf, puts, putchar, vprintf, perror - and points you to the logging macros