Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AGenUI AGenUI Runtime Logger Injection

From Leeroopedia
Knowledge Sources
Domains Software_Architecture, Cross_Platform
Last Updated 2026-10-24 17:21 GMT

Overview

A dependency inversion pattern where the core engine depends on an abstract logger interface, allowing each platform (iOS, Android, HarmonyOS) to inject its own native logging implementation at runtime.

Description

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules; both should depend on abstractions. In a cross-platform C++ SDK, the core engine needs diagnostic logging but cannot know whether the host uses Android Log, Apple `os_log`, or Harmony HiLog.

This principle solves the problem by defining an abstract `IRuntimeLogger` interface in the C++ core. All internal logging macros (`AGENUI_LOG`) route through a global pointer to this interface. The platform layer implements the interface and injects its concrete instance via `setRuntimeLogger()`. If no logger is injected, a built-in default falls back to `printf` or platform-native logging.

A key optimization is the minimum log level filter: the interface exposes `getMinLevel()`, checked at the call site before variadic formatting. Messages below the threshold are dropped without formatting their arguments, making it a hot-path performance hook.

Usage

Use this principle when:

  • A cross-platform C++ core needs logging but cannot depend on platform-specific logging APIs
  • The host application wants to route SDK logs into its own telemetry pipeline
  • Log verbosity must be controlled at runtime without recompilation
  • Performance-sensitive code needs log-level filtering before string formatting

Theoretical Basis

The dependency inversion structure:

Without DIP:  Core → AndroidLog  (tight coupling, not portable)
With DIP:     Core → IRuntimeLogger ← PlatformImpl  (both depend on abstraction)

The log-level filter is a guard clause evaluated before the costly variadic formatting:

// Abstract interface — the inversion point
class IRuntimeLogger {
    virtual void log(LogLevel level, const char* tag,
                     const char* func, int line, const char* format, ...) = 0;
    virtual LogLevel getMinLevel() const { return LOG_LEVEL_DEBUG; }
};

// Call-site guard: skip formatting entirely if below threshold
#define AGENUI_LOG(fmt, ...) \
    if (gRuntimeLogger && level >= gRuntimeLogger->getMinLevel()) \
        gRuntimeLogger->log(level, TAG, __func__, __LINE__, fmt, ##__VA_ARGS__)

Practical Guide

  • Implement `IRuntimeLogger` on the platform side (e.g., bridging to Android `Log.d`).
  • Call `engine->setRuntimeLogger(&myLogger)` before any surface activity.
  • Override `getMinLevel()` to return an `std::atomic`-backed field so the level can change at runtime.
  • Pass `nullptr` to `setRuntimeLogger()` to restore the built-in default logger.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment