Implementation:Ollama Ollama Llama Log
| Knowledge Sources | |
|---|---|
| Domains | Logging, Infrastructure |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Implements the asynchronous logging system for llama.cpp with a background worker thread, color support, and configurable verbosity filtering.
Description
Uses a common_log struct with an internal worker thread that processes log entries from a ring buffer queue. Log entries (common_log_entry) carry a timestamp, log level, and message. The worker thread dequeues entries and writes them to stdout/stderr (or an optional file) based on level. Supports ANSI color codes (auto-detected via isatty), optional timestamps and level prefixes, and verbosity filtering via common_log_verbosity_thold. Provides pause/resume control for the worker thread, and a singleton accessor (common_log_main) with automatic cleanup on exit.
Usage
Use this for all logging within llama.cpp. The asynchronous design prevents logging from blocking inference operations.
Code Reference
Source Location
- Repository: Ollama
- File: llama/llama.cpp/common/log.cpp
- Lines: 1-446
Signature
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 {
common_log();
common_log(size_t capacity);
~common_log();
void add(enum ggml_log_level level, const char * fmt, va_list args);
void resume();
void pause();
void set_file(const char * file);
void set_colors(log_colors colors);
void set_prefix(bool prefix);
void set_timestamps(bool timestamps);
};
Import
#include "log.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| level | ggml_log_level | Yes | Log level (DEBUG, INFO, WARN, ERROR, NONE) |
| fmt | const char * | Yes | Printf-style format string |
| args | va_list | Yes | Variadic arguments for format string |
| verbosity | int | No | Verbosity threshold (lower = fewer messages) |
Outputs
| Name | Type | Description |
|---|---|---|
| output | FILE * | Messages written to stdout/stderr or log file |
Usage Examples
#include "log.h"
// Use convenience macros (most common)
LOG_INF("Model loaded: %s\n", model_path);
LOG_WRN("Context size reduced to %d\n", n_ctx);
LOG_ERR("Failed to load model\n");
LOG_DBG("Token %d decoded in %lld us\n", token_id, elapsed);
// Configure logging
common_log * log = common_log_main();
common_log_set_file(log, "output.log");
common_log_set_colors(log, LOG_COLORS_ENABLED);
common_log_set_prefix(log, true);
common_log_set_timestamps(log, true);