From d79070e90755b3b8e456d179b9bd7fd2551b57c7 Mon Sep 17 00:00:00 2001 From: a-ghorbani Date: Mon, 6 Jan 2025 11:38:53 +0100 Subject: [PATCH 1/2] fix(log): implement Android logging in rn-llama.hpp as opposed to printf --- cpp/rn-llama.hpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/cpp/rn-llama.hpp b/cpp/rn-llama.hpp index 63f9d5b..db66b38 100644 --- a/cpp/rn-llama.hpp +++ b/cpp/rn-llama.hpp @@ -8,6 +8,7 @@ #include "llama.h" #include "llama-impl.h" #include "sampling.h" +#include namespace rnllama { @@ -106,14 +107,29 @@ static void llama_batch_add(llama_batch *batch, llama_token id, llama_pos pos, s static void log(const char *level, const char *function, int line, const char *format, ...) { - printf("[%s] %s:%d ", level, function, line); + // Create the log tag prefix with function:line-number + char prefix[256]; + snprintf(prefix, sizeof(prefix), "%s:%d ", function, line); + // add the message + char final_msg[4096]; va_list args; va_start(args, format); - vprintf(format, args); + vsnprintf(final_msg, sizeof(final_msg), format, args); va_end(args); - printf("\n"); + android_LogPriority priority; + if (strcmp(level, "ERROR") == 0) { + priority = ANDROID_LOG_ERROR; + } else if (strcmp(level, "WARNING") == 0) { + priority = ANDROID_LOG_WARN; + } else if (strcmp(level, "INFO") == 0) { + priority = ANDROID_LOG_INFO; + } else { + priority = ANDROID_LOG_DEBUG; + } + + __android_log_print(priority, "RNLlama", "%s%s", prefix, final_msg); } static bool rnllama_verbose = false; @@ -311,6 +327,10 @@ struct llama_rn_context return false; } n_ctx = llama_n_ctx(ctx); + + // We can uncomment for debugging or after this fix: https://github.com/ggerganov/llama.cpp/pull/11101 + // LOG_INFO("%s\n", common_params_get_system_info(params).c_str()); + return true; } From 1e8a3345d9c12166ca8ced66f3a26bded641c936 Mon Sep 17 00:00:00 2001 From: a-ghorbani Date: Mon, 6 Jan 2025 12:08:34 +0100 Subject: [PATCH 2/2] fix: add back printf for non-android systems --- cpp/rn-llama.hpp | 49 +++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/cpp/rn-llama.hpp b/cpp/rn-llama.hpp index db66b38..9a89a08 100644 --- a/cpp/rn-llama.hpp +++ b/cpp/rn-llama.hpp @@ -8,7 +8,9 @@ #include "llama.h" #include "llama-impl.h" #include "sampling.h" +#if defined(__ANDROID__) #include +#endif namespace rnllama { @@ -107,31 +109,32 @@ static void llama_batch_add(llama_batch *batch, llama_token id, llama_pos pos, s static void log(const char *level, const char *function, int line, const char *format, ...) { - // Create the log tag prefix with function:line-number - char prefix[256]; - snprintf(prefix, sizeof(prefix), "%s:%d ", function, line); - - // add the message - char final_msg[4096]; va_list args; - va_start(args, format); - vsnprintf(final_msg, sizeof(final_msg), format, args); - va_end(args); - - android_LogPriority priority; - if (strcmp(level, "ERROR") == 0) { - priority = ANDROID_LOG_ERROR; - } else if (strcmp(level, "WARNING") == 0) { - priority = ANDROID_LOG_WARN; - } else if (strcmp(level, "INFO") == 0) { - priority = ANDROID_LOG_INFO; - } else { - priority = ANDROID_LOG_DEBUG; - } - - __android_log_print(priority, "RNLlama", "%s%s", prefix, final_msg); + #if defined(__ANDROID__) + char prefix[256]; + snprintf(prefix, sizeof(prefix), "%s:%d %s", function, line, format); + + va_start(args, format); + android_LogPriority priority; + if (strcmp(level, "ERROR") == 0) { + priority = ANDROID_LOG_ERROR; + } else if (strcmp(level, "WARNING") == 0) { + priority = ANDROID_LOG_WARN; + } else if (strcmp(level, "INFO") == 0) { + priority = ANDROID_LOG_INFO; + } else { + priority = ANDROID_LOG_DEBUG; + } + __android_log_vprint(priority, "RNLlama", prefix, args); + va_end(args); + #else + printf("[%s] %s:%d ", level, function, line); + va_start(args, format); + vprintf(format, args); + va_end(args); + printf("\n"); + #endif } - static bool rnllama_verbose = false; #if RNLLAMA_VERBOSE != 1