android: add LogListener

A Java object to send logs on the android side.
This commit is contained in:
Thomas Guillem 2014-11-02 16:52:43 +01:00 committed by Max Kellermann
parent 5af2632d4f
commit aff070bcbb
8 changed files with 102 additions and 4 deletions

View File

@ -276,7 +276,8 @@ libjava_a_SOURCES = \
noinst_LIBRARIES += libandroid.a
libandroid_a_SOURCES = \
src/android/Context.cxx src/android/Context.hxx \
src/android/Environment.cxx src/android/Environment.hxx
src/android/Environment.cxx src/android/Environment.hxx \
src/android/LogListener.cxx src/android/LogListener.hxx
libandroid_a_CPPFLAGS = $(AM_CPPFLAGS) -Iandroid/build/include
noinst_LIBRARIES += libmain.a

View File

@ -25,6 +25,12 @@ import android.content.Context;
* Bridge to native code.
*/
public class Bridge {
public static native void run(Context context);
/* used by jni */
public interface LogListener {
public void onLog(int priority, String msg);
}
public static native void run(Context context, LogListener logListener);
public static native void shutdown();
}

View File

@ -69,7 +69,7 @@ public class Main extends Activity implements Runnable {
}
@Override public void run() {
Bridge.run(this);
Bridge.run(this, null);
quitHandler.sendMessage(quitHandler.obtainMessage());
}
}

View File

@ -34,6 +34,8 @@
#ifdef ANDROID
#include <android/log.h>
#include "android/LogListener.hxx"
#include "Main.hxx"
static int
ToAndroidLogLevel(LogLevel log_level)
@ -177,6 +179,9 @@ Log(const Domain &domain, LogLevel level, const char *msg)
#ifdef ANDROID
__android_log_print(ToAndroidLogLevel(level), "MPD",
"%s: %s", domain.GetName(), msg);
if (logListener != nullptr)
logListener->OnLog(Java::GetEnv(), ToAndroidLogLevel(level),
"%s: %s", domain.GetName(), msg);
#else
if (level < log_threshold)

View File

@ -92,6 +92,7 @@
#include "java/File.hxx"
#include "android/Environment.hxx"
#include "android/Context.hxx"
#include "android/LogListener.hxx"
#include "fs/StandardDirectory.hxx"
#include "fs/FileSystem.hxx"
#include "org_musicpd_Bridge.h"
@ -128,6 +129,7 @@ static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10;
#ifdef ANDROID
Context *context;
LogListener *logListener;
#endif
Instance *instance;
@ -676,16 +678,19 @@ try {
gcc_visibility_default
JNIEXPORT void JNICALL
Java_org_musicpd_Bridge_run(JNIEnv *env, jclass, jobject _context)
Java_org_musicpd_Bridge_run(JNIEnv *env, jclass, jobject _context, jobject _logListener)
{
Java::Init(env);
Java::File::Initialise(env);
Environment::Initialise(env);
context = new Context(env, _context);
if (_logListener != nullptr)
logListener = new LogListener(env, _logListener);
mpd_main(0, nullptr);
delete logListener;
delete context;
Environment::Deinitialise(env);
}

View File

@ -25,7 +25,10 @@ class Context;
struct Instance;
#ifdef ANDROID
#include "android/LogListener.hxx"
extern Context *context;
extern LogListener *logListener;
#endif
extern Instance *instance;

View File

@ -0,0 +1,46 @@
/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "LogListener.hxx"
#include "java/Class.hxx"
#include "java/String.hxx"
#include "util/AllocatedString.hxx"
#include "util/FormatString.hxx"
void
LogListener::OnLog(JNIEnv *env, int priority, const char *fmt, ...) const
{
assert(env != nullptr);
Java::Class cls(env, env->GetObjectClass(Get()));
jmethodID method = env->GetMethodID(cls, "onLog",
"(ILjava/lang/String;)V");
assert(method);
va_list args;
va_start(args, fmt);
const auto log = FormatStringV(fmt, args);
va_end(args);
env->CallVoidMethod(Get(), method, priority,
Java::String(env, log.c_str()).Get());
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_ANDROID_LOG_LISTENER_HXX
#define MPD_ANDROID_LOG_LISTENER_HXX
#include "java/Object.hxx"
class LogListener : public Java::Object {
public:
LogListener(JNIEnv *env, jobject obj):Java::Object(env, obj) {}
void OnLog(JNIEnv *env, int priority, const char *fmt, ...) const;
};
#endif