Implementation:Ggml org Llama cpp Log
| Knowledge Sources | |
|---|---|
| Domains | Logging, Infrastructure |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Implements the asynchronous logging system with a worker thread that buffers and dispatches log messages to stderr and optional file output.
Description
The common_log struct uses a worker thread that reads from a message queue (vector of common_log_entry). Log entries carry a level (debug/info/warn/error/output), timestamp, prefix flag, and message text. common_log_add formats messages via vsnprintf and pushes them to the queue, with the worker thread printing to stderr (filtered by verbosity threshold) and optionally to a log file (unfiltered). The system supports colored output via ANSI codes (auto-detected or forced), timestamp prefixes, and log level indicators (D/I/W/E/O).
Usage
Use this module through the LOG_DBG, LOG_INF, LOG_WRN, LOG_ERR macros throughout the codebase. The singleton common_log_main() provides a global instance with automatic cleanup at exit. The async design prevents logging from blocking inference performance.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/log.cpp
- Lines: 1-446
Signature
int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
void common_log_set_verbosity_thold(int verbosity);
struct common_log_entry {
enum ggml_log_level level;
bool prefix;
int64_t timestamp;
std::vector<char> msg;
bool is_end;
void print(FILE * file = nullptr) const;
};
struct common_log {
// Worker thread, message queue, mutex, condition variable
// Methods: add, resume, pause, set_file, set_colors, set_prefix, set_timestamps
};
common_log * common_log_main();
Import
#include "common.h"
#include "log.h"
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| level | enum ggml_log_level | Yes | Log level: GGML_LOG_LEVEL_DEBUG, _INFO, _WARN, _ERROR |
| prefix | bool | Yes | Whether to prepend timestamp and level indicator |
| msg | const char * (format string) | Yes | Printf-style format string for the log message |
| verbosity | int | No | Threshold for filtering messages on stderr output |
Outputs
| Name | Type | Description |
|---|---|---|
| stderr output | text | Formatted log messages printed to stderr (filtered by verbosity) |
| file output | text | All log messages written to optional log file (unfiltered) |
Usage Examples
#include "log.h"
// Use logging macros (most common usage)
LOG_DBG("Loading model from %s\n", path.c_str());
LOG_INF("Model loaded: %d layers, %d parameters\n", n_layers, n_params);
LOG_WRN("Context size %d exceeds maximum, clamping to %d\n", ctx_size, max_ctx);
LOG_ERR("Failed to allocate %zu bytes for KV cache\n", required_bytes);
// Configure logging
common_log_set_verbosity_thold(LOG_DEFAULT_DEBUG); // Show debug messages
common_log * log = common_log_main();
log->set_file("llama.log"); // Enable file logging
log->set_colors(true); // Enable ANSI color output
log->set_timestamps(true); // Enable timestamp prefixes