Skip to content

common: add LOG_JSON macro to log structured data - #28586

Merged
ngxson merged 2 commits into
masterfrom
xsn/log_jsonl_fit
Sep 12, 2026
Merged

ngxson merged 2 commits into
masterfrom
xsn/log_jsonl_fit

Conversation

@ngxson

@ngxson ngxson commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Overview

Cont #28437 and supersede #23676

The way it works:

  • log.h exposes a LOG_JSON macro, note that it does NOT replace other LOG_* macros, but is purely an additive change
  • code can use this to write structured data
  • this will only handled if --log-jsonl is set
  • downstream app can parse jsonl output and filter by event type

Normal log lines will have type == "log", but specific struct will have specific type

As an example, common_memory_break_print now outputs this when --log-jsonl is set:

{"type":"fit_memory_breakdown","data":{"unit":"MiB","rows":[{"kind":"device","name":"MTL0","description":"Apple M5 Max","total":110100,"free":110100,"self":5749,"model":2883,"context":2148,"compute":718,"unaccounted":-5749},{"kind":"host","name":"Host","self":2531,"model":2208,"context":0,"compute":323}]}}

Requirements

@ngxson

ngxson commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator Author

I'm merging this today as it's a nice-to-have addition, no impacts to existing code.

For context, this is similar to --json mode on HF CLI, as @julien-c suggested a while ago, allowing downstream apps to use llama.cpp tools without linking it internally.

The long-term plan of this feature is to allow an external app to interact with llama-server via JSON messages (stdin / stdout), cc @ServeurpersoCom for visibility

@ggerganov ggerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the logs that would be consumed by third-parties would need to carry a version per each log to be able to handle breaking changes.

@ngxson

ngxson commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

@ggerganov I thought about versioning but probably it's not very important for now. JSON is quite forgiving, so breaking changes are probably rare (i.e. removing a field)

We can also add a code review rule says "try avoiding breaking changes in JSON output" in general, which also apply to all HTTP JSON responses from server, but let's see a bit later

@ngxson
ngxson merged commit 8e33095 into master Sep 12, 2026
24 of 26 checks passed
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
* add LOG_JSON macro

* fit: add demo LOG_JSON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants