Implementation:Ollama Ollama Llama Log API
| Knowledge Sources | |
|---|---|
| Domains | Logging, Infrastructure |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Header declaring the logging API, ANSI color macros, log level constants, and convenience logging macros for llama.cpp.
Description
Defines ANSI color escape code macros (LOG_COL_RED, LOG_COL_GREEN, etc.), log level constants (LOG_LEVEL_DEBUG=4 through LOG_LEVEL_OUTPUT=0), and the common_log opaque struct API: common_log_init, common_log_main (singleton), common_log_pause/common_log_resume, common_log_free, and common_log_add. Provides the LOG_TMPL macro that checks verbosity threshold before computing log arguments, and convenience macros LOG_DBG, LOG_INF, LOG_WRN, LOG_ERR for each level.
Usage
Include this header in any llama.cpp source file that needs logging. The verbosity threshold check in LOG_TMPL avoids unnecessary string formatting when log output would be suppressed, which is important for performance.
Code Reference
Source Location
- Repository: Ollama
- File: llama/llama.cpp/common/log.h
- Lines: 1-119
Signature
extern int common_log_verbosity_thold;
void common_log_set_verbosity_thold(int verbosity);
void common_log_default_callback(enum ggml_log_level level, const char * text, void * user_data);
struct common_log * common_log_init();
struct common_log * common_log_main();
void common_log_pause (struct common_log * log);
void common_log_resume(struct common_log * log);
void common_log_free (struct common_log * log);
void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...);
void common_log_set_file (struct common_log * log, const char * file);
void common_log_set_colors (struct common_log * log, log_colors colors);
void common_log_set_prefix (struct common_log * log, bool prefix);
void common_log_set_timestamps(struct common_log * log, bool timestamps);
void common_log_flush (struct common_log * log);
#define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG, __VA_ARGS__)
#define LOG_INF(...) LOG_TMPL(GGML_LOG_LEVEL_INFO, LOG_LEVEL_INFO, __VA_ARGS__)
#define LOG_WRN(...) LOG_TMPL(GGML_LOG_LEVEL_WARN, LOG_LEVEL_WARN, __VA_ARGS__)
#define LOG_ERR(...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, LOG_LEVEL_ERROR, __VA_ARGS__)
Import
#include "log.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| log | struct common_log * | Yes | Log instance (use common_log_main() for singleton) |
| level | ggml_log_level | Yes | Log level for the message |
| fmt | const char * | Yes | Printf-style format string |
Outputs
| Name | Type | Description |
|---|---|---|
| log | struct common_log * | Initialized log instance |
Usage Examples
#include "log.h"
// Basic logging with macros
LOG_INF("Loading model from %s\n", path);
LOG_DBG("Batch size: %d tokens\n", n_tokens);
LOG_WRN("Memory usage exceeds threshold\n");
LOG_ERR("Failed to allocate buffer: %zu bytes\n", size);
// Set verbosity (only show warnings and errors)
common_log_set_verbosity_thold(LOG_LEVEL_WARN);