Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
logging.h File Reference

spdlog initialization and logger access. More...

#include <entropic/entropic_export.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <chrono>
#include <filesystem>
#include <memory>
#include <string>
Include dependency graph for logging.h:

Go to the source code of this file.

Classes

class  entropic::log::HandleLogScope
 gh#59 (v2.3.1): RAII guard — sets thread's current handle_id. More...
 

Namespaces

namespace  entropic
 Activate model on GPU (WARM → ACTIVE).
 

Functions

ENTROPIC_EXPORT void entropic::log::init (spdlog::level::level_enum level=spdlog::level::info)
 Initialize the logging subsystem.
 
ENTROPIC_EXPORT void entropic::log::add_file_sink (const std::filesystem::path &path)
 Add a file sink to all loggers (truncated per run).
 
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > entropic::log::get (const std::string &name)
 Get or create a named logger.
 
ENTROPIC_EXPORT void entropic::log::setup_session (const std::filesystem::path &log_dir)
 Set up session logging for a project directory.
 
ENTROPIC_EXPORT void entropic::log::set_console_enabled (bool enabled)
 Enable or disable the stderr console sink process-wide.
 
ENTROPIC_EXPORT void entropic::log::register_handle_log (int handle_id, const std::filesystem::path &log_dir)
 gh#59 (v2.3.1): register a per-handle session.log file.
 
ENTROPIC_EXPORT void entropic::log::unregister_handle_log (int handle_id)
 gh#59 (v2.3.1): release a handle's session.log file sink.
 
ENTROPIC_EXPORT int entropic::log::current_handle_id ()
 gh#59 (v2.3.1): query the current thread's handle_id.
 
auto entropic::log::now ()
 Get current time for timing measurements.
 
double entropic::log::elapsed_ms (std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end)
 Compute elapsed milliseconds between two time points.
 
ENTROPIC_EXPORT std::string entropic::log::escape_content (const std::string &text)
 Escape content for safe spdlog formatting.
 
ENTROPIC_EXPORT void entropic::log::log_content (const std::shared_ptr< spdlog::logger > &logger, spdlog::level::level_enum level, const std::string &label, const std::string &text)
 Log full content with escaping applied.
 
ENTROPIC_EXPORT void entropic::log::log_timing (const std::shared_ptr< spdlog::logger > &logger, const std::string &label, double ms)
 Log a timing measurement.
 
ENTROPIC_EXPORT void entropic::log::log_tokens (const std::shared_ptr< spdlog::logger > &logger, int count, double time_ms)
 Log token count with throughput.
 
ENTROPIC_EXPORT void entropic::log::log_decision (const std::shared_ptr< spdlog::logger > &logger, const std::string &label, const std::string &key, const std::string &value)
 Log a decision/routing event.
 

Detailed Description

spdlog initialization and logger access.

Every .so in the entropic project uses spdlog for structured logging. This header establishes the initialization pattern used from v1.8.0 onward. All loggers are created through this interface.

Pattern
Each library creates a named logger via entropic::log::get("libname"). The facade initializes the root sink configuration (level, format, output). Libraries call get() lazily — sink config propagates automatically via spdlog's registry.
Thread safety
spdlog loggers are thread-safe. Initialization (init_logging) must happen once from a single thread before any logging calls.
Version
1.10.4

Definition in file logging.h.

Function Documentation

◆ add_file_sink()

void entropic::log::add_file_sink ( const std::filesystem::path &  path)

Add a file sink to all loggers (truncated per run).

Add a rotating file sink to all loggers.

Creates the parent directory, opens the file (truncating any existing content), adds the sink to the default logger, and applies it to every logger already registered. Loggers created after this call inherit the sink automatically via get().

Parameters
pathLog file path.
Version
2.0.1

Creates the log directory, adds the sink to the default logger, then walks every registered logger and adds the same sink. Future loggers created via get() inherit it automatically.

Parameters
pathLog file path.
max_sizeMax bytes before rotation.
max_filesRotated files to keep. @utility
Version
2.0.1

Definition at line 179 of file logging.cpp.

◆ current_handle_id()

int entropic::log::current_handle_id ( )

gh#59 (v2.3.1): query the current thread's handle_id.

gh#59 public entry — see header.

Returns 0 if no HandleLogScope is active on this thread. Mostly for tests; production code should rely on the dispatcher routing.

Version
2.3.1

@utility

Version
2.3.1

Definition at line 540 of file logging.cpp.

◆ elapsed_ms()

double entropic::log::elapsed_ms ( std::chrono::steady_clock::time_point  start,
std::chrono::steady_clock::time_point  end 
)
inline

Compute elapsed milliseconds between two time points.

Parameters
startStart time point.
endEnd time point.
Returns
Elapsed time in milliseconds. @utility
Version
1.10.4

Definition at line 203 of file logging.h.

◆ escape_content()

std::string entropic::log::escape_content ( const std::string &  text)

Escape content for safe spdlog formatting.

Replaces embedded newlines with \n, ANSI escape sequences with their hex representation, and curly braces (fmt specifiers) with doubled braces so spdlog/fmt does not interpret them.

Parameters
textRaw content (user input or model output).
Returns
Escaped string safe for spdlog format strings. @utility
Version
1.10.4

Replaces embedded newlines with \n, carriage returns with \r, ANSI escape sequences with [ESC...], and curly braces with doubled braces so spdlog/fmt does not interpret them as format specifiers.

Parameters
textRaw content.
Returns
Escaped string safe for spdlog. @utility
Version
1.10.4

Definition at line 409 of file logging.cpp.

◆ get()

std::shared_ptr< spdlog::logger > entropic::log::get ( const std::string &  name)

Get or create a named logger.

Get or create a named logger sharing current sinks.

Returns the existing logger if already created, or creates a new one sharing the root sink configuration.

Parameters
nameLogger name (e.g., "types", "inference", "mcp").
Returns
Shared pointer to the logger.
Example
auto log = entropic::log::get("inference");
log->info("Model loaded: {} ({:.1f} GB)", path, size_gb);
log->error("Load failed: {}", entropic_error_name(err));
ENTROPIC_EXPORT const char * entropic_error_name(entropic_error_t code)
Get the human-readable name for an error code.
Definition error.cpp:84
Version
1.8.0

New loggers inherit sinks from the default logger rather than the original s_sink. This ensures loggers created after test infrastructure replaces sinks via spdlog::apply_all() still write to the correct destinations (e.g., per-test file sinks).

Parameters
nameLogger name.
Returns
Shared pointer to the logger.
Version
2.3.7

Definition at line 211 of file logging.cpp.

◆ init()

void entropic::log::init ( spdlog::level::level_enum  level = spdlog::level::info)

Initialize the logging subsystem.

Call once at engine startup (in entropic_create). Sets the global log level and output format. Subsequent calls are no-ops.

Parameters
levelspdlog level (trace, debug, info, warn, error, critical).
Version
1.8.0

Uses stderr so that engine diagnostic output does not interleave with the stdout streaming token callback used by TUI consumers.

Parameters
levelspdlog level. Subsequent calls are no-ops. @utility
Version
2.0.7

Definition at line 145 of file logging.cpp.

◆ log_content()

void entropic::log::log_content ( const std::shared_ptr< spdlog::logger > &  logger,
spdlog::level::level_enum  level,
const std::string &  label,
const std::string &  text 
)

Log full content with escaping applied.

Parameters
loggerTarget logger.
levelspdlog level.
labelPrefix label (e.g., "Content", "Input").
textRaw text — escaped before formatting. @utility
Version
1.10.4
Parameters
loggerTarget logger.
levelspdlog level.
labelPrefix label.
textRaw text — escaped before formatting. @utility
Version
1.10.4

Definition at line 431 of file logging.cpp.

◆ log_decision()

void entropic::log::log_decision ( const std::shared_ptr< spdlog::logger > &  logger,
const std::string &  label,
const std::string &  key,
const std::string &  value 
)

Log a decision/routing event.

Parameters
loggerTarget logger.
labelDecision category (e.g., "Route", "Grammar").
keyDecision input.
valueDecision output. @utility
Version
1.10.4
Parameters
loggerTarget logger.
labelDecision category.
keyDecision input.
valueDecision output. @utility
Version
1.10.4

Definition at line 484 of file logging.cpp.

◆ log_timing()

void entropic::log::log_timing ( const std::shared_ptr< spdlog::logger > &  logger,
const std::string &  label,
double  ms 
)

Log a timing measurement.

Parameters
loggerTarget logger.
labelOperation label.
msElapsed milliseconds. @utility
Version
1.10.4

Definition at line 448 of file logging.cpp.

◆ log_tokens()

void entropic::log::log_tokens ( const std::shared_ptr< spdlog::logger > &  logger,
int  count,
double  time_ms 
)

Log token count with throughput.

Parameters
loggerTarget logger.
countToken count.
time_msGeneration time in milliseconds. @utility
Version
1.10.4

Definition at line 464 of file logging.cpp.

◆ now()

auto entropic::log::now ( )
inline

Get current time for timing measurements.

Returns
Steady clock time point. @utility
Version
1.10.4

Definition at line 193 of file logging.h.

◆ register_handle_log()

void entropic::log::register_handle_log ( int  handle_id,
const std::filesystem::path &  log_dir 
)

gh#59 (v2.3.1): register a per-handle session.log file.

gh#59 public entry — see header.

Replaces the old global-mutation behavior of add_file_sink for the multi-handle case. Each handle registers its own log_dir under a unique handle_id. The process-wide HandleAwareSink consults the calling thread's current_handle_id (set via HandleLogScope) and writes only to that handle's file. No cross-handle bleed.

Idempotent: registering the same id twice replaces the old sink. Safe to call before or after init().

Parameters
handle_idMonotonic handle identifier (engine_handle::log_id).
log_dirDirectory containing session.log + session_model.log.
Version
2.3.1

@utility

Version
2.3.1

Definition at line 500 of file logging.cpp.

◆ set_console_enabled()

void entropic::log::set_console_enabled ( bool  enabled)

Enable or disable the stderr console sink process-wide.

Enable/disable the stderr console sink process-wide.

When disabled, the shared stderr sink (s_sink) is stripped from the default logger and every registered logger, and a process-global flag makes get() filter it out of any logger created afterwards. Engine output then routes to the per-handle file sink only.

TUI consumers that paint to fd 2 (stderr) MUST disable this — an engine log line on fd 2 corrupts the painted screen. Default state (console enabled) is unchanged for operator/CLI consumers.

Parameters
enabledtrue to keep the console sink (default), false to remove it everywhere including future loggers.
Version
2.3.7
Parameters
enabledfalse strips s_sink everywhere + flags get(). @utility
Version
2.3.7

Definition at line 243 of file logging.cpp.

◆ setup_session()

void entropic::log::setup_session ( const std::filesystem::path &  log_dir)

Set up session logging for a project directory.

Adds a spdlog file sink for session.log and truncates session_model.log for a fresh session. Centralizes log file lifecycle that was previously inline in the facade.

Parameters
log_dirDirectory for session log files.
Version
2.0.1

Adds a file sink for session.log, removes the console sink so logs go to exactly one destination. Truncates session_model.log.

Parameters
log_dirDirectory for session log files. @utility
Version
2.0.6

Definition at line 305 of file logging.cpp.

◆ unregister_handle_log()

void entropic::log::unregister_handle_log ( int  handle_id)

gh#59 (v2.3.1): release a handle's session.log file sink.

gh#59 public entry — see header.

Called from entropic_destroy. Closes the underlying file sink. Safe to call on unregistered ids.

Parameters
handle_idIdentifier previously passed to register_handle_log.
Version
2.3.1

@utility

Version
2.3.1

Definition at line 530 of file logging.cpp.