diff --git a/common/log.cpp b/common/log.cpp index 4ccdbd17cd726..7436f0d296140 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -416,6 +416,10 @@ void common_log_add(struct common_log * log, enum ggml_log_level level, const ch va_end(args); } +void common_log_add_v(struct common_log * log, enum ggml_log_level level, const char * fmt, va_list args) { + log->add(level, fmt, args); +} + void common_log_set_file(struct common_log * log, const char * file) { log->set_file(file); } diff --git a/common/log.h b/common/log.h index f329b434c9395..ffba6b9fe1709 100644 --- a/common/log.h +++ b/common/log.h @@ -49,6 +49,8 @@ void common_log_free (struct common_log * log); LOG_ATTRIBUTE_FORMAT(3, 4) void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...); +void common_log_add_v(struct common_log * log, enum ggml_log_level level, const char * fmt, va_list args); + // defaults: file = NULL, colors = false, prefix = false, timestamps = false // // regular log output: diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h index c1ed1a21c81c4..a0e04a1d29c2d 100644 --- a/ggml/include/ggml.h +++ b/ggml/include/ggml.h @@ -208,6 +208,7 @@ #include #include #include +#include #define GGML_FILE_MAGIC 0x67676d6c // "ggml" #define GGML_FILE_VERSION 2 diff --git a/tools/mtmd/mtmd-cli.cpp b/tools/mtmd/mtmd-cli.cpp index 3e19e95958a2f..9531527beeb55 100644 --- a/tools/mtmd/mtmd-cli.cpp +++ b/tools/mtmd/mtmd-cli.cpp @@ -266,6 +266,13 @@ static int eval_message(mtmd_cli_context & ctx, common_chat_msg & msg) { return 0; } +static void mtmd_log_callback(enum ggml_log_level level, const char * fmt, ...) { + va_list args; + va_start(args, fmt); + common_log_add_v(common_log_main(), level, fmt, args); + va_end(args); +} + int main(int argc, char ** argv) { ggml_time_init(); @@ -277,6 +284,7 @@ int main(int argc, char ** argv) { } common_init(); + mtmd_set_log_callback(mtmd_log_callback); if (params.mmproj.path.empty()) { show_additional_info(argc, argv); @@ -285,7 +293,7 @@ int main(int argc, char ** argv) { } mtmd_cli_context ctx(params); - LOG("%s: loading model: %s\n", __func__, params.model.path.c_str()); + LOG_INF("%s: loading model: %s\n", __func__, params.model.path.c_str()); bool is_single_turn = !params.prompt.empty() && !params.image.empty(); diff --git a/tools/mtmd/mtmd-helper.cpp b/tools/mtmd/mtmd-helper.cpp index 89e3355bbab27..3fe71da7bf36a 100644 --- a/tools/mtmd/mtmd-helper.cpp +++ b/tools/mtmd/mtmd-helper.cpp @@ -32,8 +32,24 @@ #define STB_IMAGE_IMPLEMENTATION #include "stb/stb_image.h" -#define LOG_INF(...) fprintf(stdout, __VA_ARGS__) -#define LOG_ERR(...) fprintf(stderr, __VA_ARGS__) +static void log_callback_default(enum ggml_log_level level, const char * fmt, ...) { + va_list args; + va_start(args, fmt); + if (level >= GGML_LOG_LEVEL_ERROR) { + vfprintf(stderr, fmt, args); + } else { + vfprintf(stdout, fmt, args); + } + va_end(args); + +} +static mtmd_log_callback_t log_callback = log_callback_default; + +void mtmd_set_log_callback(mtmd_log_callback_t callback) { + log_callback = callback; +} +#define LOG_INF(...) (*log_callback)(GGML_LOG_LEVEL_INFO, __VA_ARGS__) +#define LOG_ERR(...) (*log_callback)(GGML_LOG_LEVEL_ERROR, __VA_ARGS__) size_t mtmd_helper_get_n_tokens(const mtmd_input_chunks * chunks) { size_t n_tokens = 0; diff --git a/tools/mtmd/mtmd-helper.h b/tools/mtmd/mtmd-helper.h index 5c0edc6937eee..5f43b13d1378a 100644 --- a/tools/mtmd/mtmd-helper.h +++ b/tools/mtmd/mtmd-helper.h @@ -80,6 +80,9 @@ MTMD_API int32_t mtmd_helper_decode_image_chunk(mtmd_context * ctx, int32_t n_batch, llama_pos * new_n_past); +typedef void (*mtmd_log_callback_t)(enum ggml_log_level level, const char * fmt, ...); +MTMD_API void mtmd_set_log_callback(mtmd_log_callback_t callback); + #ifdef __cplusplus } // extern "C" #endif