Skip to content

Commit 2bb9a83

Browse files
committed
Add facilities to integrate llama.cpp logging with other logging stacks
This adds the ability for llama.cpp logging to integrate with other logging stacks by allowing the setting of a log callback to which all logging calls are forwarded.
1 parent adc7634 commit 2bb9a83

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

common/log.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#endif // defined(_WIN32)
2222

2323
int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
24+
static ggml_log_callback g_external_log_callback = nullptr;
25+
static void * g_external_log_user_data = nullptr;
26+
static bool g_stdout_enabled = true;
2427

2528
void common_log_set_verbosity_thold(int verbosity) {
2629
common_log_verbosity_thold = verbosity;
@@ -101,14 +104,18 @@ struct common_log_entry {
101104
return;
102105
}
103106

104-
fcur = stdout;
107+
if (g_stdout_enabled) {
108+
fcur = stdout;
105109

106-
if (level != GGML_LOG_LEVEL_NONE) {
107-
fcur = stderr;
110+
if (level != GGML_LOG_LEVEL_NONE) {
111+
fcur = stderr;
112+
}
113+
} else {
114+
fcur = nullptr; // suppress stdio prints
108115
}
109116
}
110117

111-
if (level != GGML_LOG_LEVEL_NONE && level != GGML_LOG_LEVEL_CONT && prefix) {
118+
if (fcur && level != GGML_LOG_LEVEL_NONE && level != GGML_LOG_LEVEL_CONT && prefix) {
112119
if (timestamp) {
113120
// [M.s.ms.us]
114121
fprintf(fcur, "%s%d.%02d.%03d.%03d%s ",
@@ -130,13 +137,22 @@ struct common_log_entry {
130137
}
131138
}
132139

133-
fprintf(fcur, "%s", msg.data());
140+
if (fcur) {
141+
fprintf(fcur, "%s", msg.data());
142+
}
134143

135-
if (level == GGML_LOG_LEVEL_WARN || level == GGML_LOG_LEVEL_ERROR || level == GGML_LOG_LEVEL_DEBUG) {
144+
if (fcur && (level == GGML_LOG_LEVEL_WARN || level == GGML_LOG_LEVEL_ERROR || level == GGML_LOG_LEVEL_DEBUG)) {
136145
fprintf(fcur, "%s", g_col[COMMON_LOG_COL_DEFAULT]);
137146
}
138147

139-
fflush(fcur);
148+
if (fcur) {
149+
fflush(fcur);
150+
}
151+
152+
// forward to external callback only once per entry (when not printing to the file sink)
153+
if (!file && g_external_log_callback) {
154+
g_external_log_callback(level, msg.data(), g_external_log_user_data);
155+
}
140156
}
141157
};
142158

@@ -442,3 +458,12 @@ void common_log_set_prefix(struct common_log * log, bool prefix) {
442458
void common_log_set_timestamps(struct common_log * log, bool timestamps) {
443459
log->set_timestamps(timestamps);
444460
}
461+
462+
void common_log_set_callback(ggml_log_callback log_callback, void * user_data) {
463+
g_external_log_callback = log_callback;
464+
g_external_log_user_data = user_data;
465+
}
466+
467+
void common_log_set_stdout_enabled(bool enabled) {
468+
g_stdout_enabled = enabled;
469+
}

common/log.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ void common_log_set_colors (struct common_log * log, log_colors colors); // n
7676
void common_log_set_prefix (struct common_log * log, bool prefix); // whether to output prefix to each log
7777
void common_log_set_timestamps(struct common_log * log, bool timestamps); // whether to output timestamps in the prefix
7878

79+
// Redirect log messages to an external callback in addition to, or instead of,
80+
// writing to stdout/stderr. When a callback is set, it will be invoked for every
81+
// log entry produced by common_log_add.
82+
// If NULL is supplied, no callback will be invoked.
83+
// The callback type matches ggml's logging callback for compatibility.
84+
void common_log_set_callback(ggml_log_callback log_callback, void * user_data);
85+
86+
// Convenience overload when no user_data is needed
87+
static inline void common_log_set_callback(ggml_log_callback log_callback) {
88+
common_log_set_callback(log_callback, NULL);
89+
}
90+
91+
// Enable or disable printing to stdout/stderr. When disabled, logs will still
92+
// be forwarded to the callback (if set) and any configured log file, but will
93+
// not be written to the standard streams.
94+
void common_log_set_stdout_enabled(bool enabled);
95+
7996
// helper macros for logging
8097
// use these to avoid computing log arguments if the verbosity of the log is higher than the threshold
8198
//

0 commit comments

Comments
 (0)