api-impl: use liblog for android.util.Log; use Log.v for debugging prints and Log.w for stub tracing

This commit is contained in:
Mis012
2023-09-06 17:42:24 +02:00
parent 49861ce4dd
commit 90cb1c925a
11 changed files with 181 additions and 54 deletions

View File

@@ -16,7 +16,7 @@
JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1bitmap_1from_1path(JNIEnv *env, jobject this, jobject path)
{
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(_CSTRING(path), NULL);
printf(">>> made pixbuf from path: >%s<, >%p<\n", _CSTRING(path), pixbuf);
android_log_printf(ANDROID_LOG_VERBOSE, "["__FILE__"]", ">>> made pixbuf from path: >%s<, >%p<\n", _CSTRING(path), pixbuf);
sk_imageinfo_t info = {
.width = gdk_pixbuf_get_width(pixbuf),

View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include "defines.h"
#include "util.h"
/* copied from AOSP:
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
JNIEXPORT jint JNICALL Java_android_util_Log_println_1native(JNIEnv* env, jobject this, jint bufID, jint priority, jstring tagObj, jstring msgObj)
{
const char* tag = NULL;
const char* msg = NULL;
if (msgObj == NULL) {
// jniThrowNullPointerException(env, "println needs a message");
fprintf(stderr, "Log.println_native: println needs a message\n");
return -1;
}
if (bufID < 0 || bufID >= LOG_ID_MAX) {
// jniThrowNullPointerException(env, "bad bufID");
fprintf(stderr, "Log.println_native: bad bufID\n");
return -1;
}
if (tagObj != NULL)
tag = (*env)->GetStringUTFChars(env, tagObj, NULL);
msg = (*env)->GetStringUTFChars(env, msgObj, NULL);
int res = __android_log_buf_write(bufID, priority, tag, msg);
if (tag != NULL)
(*env)->ReleaseStringUTFChars(env, tagObj, tag);
(*env)->ReleaseStringUTFChars(env, msgObj, msg);
return res;
}

View File

@@ -29,11 +29,11 @@ extern "C" {
#define android_util_Log_LOG_ID_SYSTEM 3L
/*
* Class: android_util_Log
* Method: isLoggable
* Signature: (Ljava/lang/String;I)Z
* Method: println_native
* Signature: (IILjava/lang/String;Ljava/lang/String;)I
*/
JNIEXPORT jboolean JNICALL Java_android_util_Log_isLoggable
(JNIEnv *, jclass, jstring, jint);
JNIEXPORT jint JNICALL Java_android_util_Log_println_1native
(JNIEnv *, jclass, jint, jint, jstring, jstring);
#ifdef __cplusplus
}

View File

@@ -1,3 +1,6 @@
#include <dlfcn.h>
#include <pthread.h>
#include "util.h"
struct handle_cache handle_cache = {0};
@@ -140,3 +143,50 @@ void extract_from_apk(const char *path, const char *target) {
JNIEnv *env = get_jni_env();
(*env)->CallStaticObjectMethod(env, handle_cache.asset_manager.class, handle_cache.asset_manager.extractFromAPK, _JSTRING(path), _JSTRING(target));
}
/* logging with fallback to stderr */
typedef int __android_log_vprint_type(int prio, const char *tag, const char *fmt, va_list ap);
static int fallback_verbose_log(int prio, const char *tag, const char *fmt, va_list ap)
{
int ret;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex);
static char buf[1024];
ret = vsnprintf(buf, sizeof(buf), fmt, ap);
fprintf(stderr, "%lu: %s\n", pthread_self(), buf);
pthread_mutex_unlock(&mutex);
return ret;
}
static int android_log_vprintf(int prio, const char *tag, const char *fmt, va_list ap)
{
static __android_log_vprint_type *_android_log_vprintf = NULL;
if(!_android_log_vprintf) {
_android_log_vprintf = dlsym(RTLD_DEFAULT, "__android_log_vprint");
if(!_android_log_vprintf) {
_android_log_vprintf = &fallback_verbose_log;
}
}
return _android_log_vprintf(prio, tag, fmt, ap);
}
int android_log_printf(android_LogPriority prio, const char *tag, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = android_log_vprintf(prio, tag, fmt, ap);
va_end(ap);
return ret;
}

View File

@@ -107,4 +107,32 @@ void extract_from_apk(const char *path, const char *target);
void prepare_main_looper(JNIEnv* env);
/* we don't (currently?) install the headers for liblog */
typedef enum {
LOG_ID_MAIN = 0,
LOG_ID_RADIO = 1,
LOG_ID_EVENTS = 2,
LOG_ID_SYSTEM = 3,
LOG_ID_MAX
} log_id_t;
typedef enum android_LogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
} android_LogPriority;
/* TODO: do we really need the bufID, or can we use our function below which has a stderr fallback */
int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
/* defined in util.c */
int android_log_printf(android_LogPriority prio, const char *tag, const char *fmt, ...);
#endif