jni: introduce a logging util to filter different logging levels on different build types
This commit is contained in:
parent
3fa3c15c5c
commit
33987b56fa
|
|
@ -6,6 +6,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sampling.h>
|
#include <sampling.h>
|
||||||
|
|
||||||
|
#include "logging.h"
|
||||||
#include "chat.h"
|
#include "chat.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "llama.h"
|
#include "llama.h"
|
||||||
|
|
@ -20,16 +21,6 @@ static std::string join(const std::vector<T> &values, const std::string &delim)
|
||||||
return str.str();
|
return str.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Logging utils
|
|
||||||
*/
|
|
||||||
#define TAG "ai-chat"
|
|
||||||
#define LOGv(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
|
|
||||||
#define LOGd(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
|
|
||||||
#define LOGi(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
|
|
||||||
#define LOGw(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
|
|
||||||
#define LOGe(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LLama resources: context, model, batch and sampler
|
* LLama resources: context, model, batch and sampler
|
||||||
*/
|
*/
|
||||||
|
|
@ -48,33 +39,11 @@ static llama_batch g_batch;
|
||||||
static common_chat_templates_ptr g_chat_templates;
|
static common_chat_templates_ptr g_chat_templates;
|
||||||
static common_sampler * g_sampler;
|
static common_sampler * g_sampler;
|
||||||
|
|
||||||
static void log_callback(ggml_log_level level, const char *fmt, void *data) {
|
|
||||||
int priority;
|
|
||||||
switch (level) {
|
|
||||||
case GGML_LOG_LEVEL_ERROR:
|
|
||||||
priority = ANDROID_LOG_ERROR;
|
|
||||||
break;
|
|
||||||
case GGML_LOG_LEVEL_WARN:
|
|
||||||
priority = GGML_LOG_LEVEL_WARN;
|
|
||||||
break;
|
|
||||||
case GGML_LOG_LEVEL_INFO:
|
|
||||||
priority = GGML_LOG_LEVEL_INFO;
|
|
||||||
break;
|
|
||||||
case GGML_LOG_LEVEL_DEBUG:
|
|
||||||
priority = GGML_LOG_LEVEL_DEBUG;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
priority = ANDROID_LOG_DEFAULT;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
__android_log_print(priority, TAG, fmt, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
JNIEXPORT void JNICALL
|
JNIEXPORT void JNICALL
|
||||||
Java_com_arm_aichat_internal_InferenceEngineImpl_init(JNIEnv *env, jobject /*unused*/, jstring nativeLibDir) {
|
Java_com_arm_aichat_internal_InferenceEngineImpl_init(JNIEnv *env, jobject /*unused*/, jstring nativeLibDir) {
|
||||||
// Set llama log handler to Android
|
// Set llama log handler to Android
|
||||||
llama_log_set(log_callback, nullptr);
|
llama_log_set(aichat_android_log_callback, nullptr);
|
||||||
|
|
||||||
// Loading all CPU backend variants
|
// Loading all CPU backend variants
|
||||||
const auto *path_to_backend = env->GetStringUTFChars(nativeLibDir, 0);
|
const auto *path_to_backend = env->GetStringUTFChars(nativeLibDir, 0);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
//
|
||||||
|
// Created by Han Yin on 10/31/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef AICHAT_LOGGING_H
|
||||||
|
#define AICHAT_LOGGING_H
|
||||||
|
|
||||||
|
#endif //AICHAT_LOGGING_H
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <android/log.h>
|
||||||
|
|
||||||
|
#ifndef LOG_TAG
|
||||||
|
#define LOG_TAG "ai-chat"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LOG_MIN_LEVEL
|
||||||
|
#if defined(NDEBUG)
|
||||||
|
#define LOG_MIN_LEVEL ANDROID_LOG_WARN
|
||||||
|
#else
|
||||||
|
#define LOG_MIN_LEVEL ANDROID_LOG_VERBOSE
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static inline int ai_should_log(int prio) {
|
||||||
|
return __android_log_is_loggable(prio, LOG_TAG, LOG_MIN_LEVEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if LOG_MIN_LEVEL <= ANDROID_LOG_VERBOSE
|
||||||
|
#define LOGv(...) do { if (ai_should_log(ANDROID_LOG_VERBOSE)) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__); } while (0)
|
||||||
|
#else
|
||||||
|
#define LOGv(...) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if LOG_MIN_LEVEL <= ANDROID_LOG_DEBUG
|
||||||
|
#define LOGd(...) do { if (ai_should_log(ANDROID_LOG_DEBUG)) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__); } while (0)
|
||||||
|
#else
|
||||||
|
#define LOGd(...) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define LOGi(...) do { if (ai_should_log(ANDROID_LOG_INFO )) __android_log_print(ANDROID_LOG_INFO , LOG_TAG, __VA_ARGS__); } while (0)
|
||||||
|
#define LOGw(...) do { if (ai_should_log(ANDROID_LOG_WARN )) __android_log_print(ANDROID_LOG_WARN , LOG_TAG, __VA_ARGS__); } while (0)
|
||||||
|
#define LOGe(...) do { if (ai_should_log(ANDROID_LOG_ERROR)) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__); } while (0)
|
||||||
|
|
||||||
|
static inline int android_log_prio_from_ggml(enum ggml_log_level level) {
|
||||||
|
switch (level) {
|
||||||
|
case GGML_LOG_LEVEL_ERROR: return ANDROID_LOG_ERROR;
|
||||||
|
case GGML_LOG_LEVEL_WARN: return ANDROID_LOG_WARN;
|
||||||
|
case GGML_LOG_LEVEL_INFO: return ANDROID_LOG_INFO;
|
||||||
|
case GGML_LOG_LEVEL_DEBUG: return ANDROID_LOG_DEBUG;
|
||||||
|
default: return ANDROID_LOG_DEFAULT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void aichat_android_log_callback(enum ggml_log_level level,
|
||||||
|
const char* text,
|
||||||
|
void* /*user*/) {
|
||||||
|
const int prio = android_log_prio_from_ggml(level);
|
||||||
|
if (!ai_should_log(prio)) return;
|
||||||
|
__android_log_write(prio, LOG_TAG, text);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue