Skip to content

Commit fb3896e

Browse files
authored
fix(log): implement Android logging in rn-llama.hpp as opposed to printf (#106)
* fix(log): implement Android logging in rn-llama.hpp as opposed to printf * fix: add back printf for non-android systems
1 parent d9f1e2e commit fb3896e

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

cpp/rn-llama.hpp

+31-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "llama.h"
99
#include "llama-impl.h"
1010
#include "sampling.h"
11+
#if defined(__ANDROID__)
12+
#include <android/log.h>
13+
#endif
1114

1215
namespace rnllama {
1316

@@ -106,16 +109,32 @@ static void llama_batch_add(llama_batch *batch, llama_token id, llama_pos pos, s
106109
static void log(const char *level, const char *function, int line,
107110
const char *format, ...)
108111
{
109-
printf("[%s] %s:%d ", level, function, line);
110-
111112
va_list args;
112-
va_start(args, format);
113-
vprintf(format, args);
114-
va_end(args);
115-
116-
printf("\n");
113+
#if defined(__ANDROID__)
114+
char prefix[256];
115+
snprintf(prefix, sizeof(prefix), "%s:%d %s", function, line, format);
116+
117+
va_start(args, format);
118+
android_LogPriority priority;
119+
if (strcmp(level, "ERROR") == 0) {
120+
priority = ANDROID_LOG_ERROR;
121+
} else if (strcmp(level, "WARNING") == 0) {
122+
priority = ANDROID_LOG_WARN;
123+
} else if (strcmp(level, "INFO") == 0) {
124+
priority = ANDROID_LOG_INFO;
125+
} else {
126+
priority = ANDROID_LOG_DEBUG;
127+
}
128+
__android_log_vprint(priority, "RNLlama", prefix, args);
129+
va_end(args);
130+
#else
131+
printf("[%s] %s:%d ", level, function, line);
132+
va_start(args, format);
133+
vprintf(format, args);
134+
va_end(args);
135+
printf("\n");
136+
#endif
117137
}
118-
119138
static bool rnllama_verbose = false;
120139

121140
#if RNLLAMA_VERBOSE != 1
@@ -311,6 +330,10 @@ struct llama_rn_context
311330
return false;
312331
}
313332
n_ctx = llama_n_ctx(ctx);
333+
334+
// We can uncomment for debugging or after this fix: https://github.com/ggerganov/llama.cpp/pull/11101
335+
// LOG_INFO("%s\n", common_params_get_system_info(params).c_str());
336+
314337
return true;
315338
}
316339

0 commit comments

Comments
 (0)