android: add LogListener
A Java object to send logs on the android side.
This commit is contained in:
parent
5af2632d4f
commit
aff070bcbb
|
@ -276,7 +276,8 @@ libjava_a_SOURCES = \
|
||||||
noinst_LIBRARIES += libandroid.a
|
noinst_LIBRARIES += libandroid.a
|
||||||
libandroid_a_SOURCES = \
|
libandroid_a_SOURCES = \
|
||||||
src/android/Context.cxx src/android/Context.hxx \
|
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
|
libandroid_a_CPPFLAGS = $(AM_CPPFLAGS) -Iandroid/build/include
|
||||||
|
|
||||||
noinst_LIBRARIES += libmain.a
|
noinst_LIBRARIES += libmain.a
|
||||||
|
|
|
@ -25,6 +25,12 @@ import android.content.Context;
|
||||||
* Bridge to native code.
|
* Bridge to native code.
|
||||||
*/
|
*/
|
||||||
public class Bridge {
|
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();
|
public static native void shutdown();
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ public class Main extends Activity implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void run() {
|
@Override public void run() {
|
||||||
Bridge.run(this);
|
Bridge.run(this, null);
|
||||||
quitHandler.sendMessage(quitHandler.obtainMessage());
|
quitHandler.sendMessage(quitHandler.obtainMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,8 @@
|
||||||
|
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
#include <android/log.h>
|
#include <android/log.h>
|
||||||
|
#include "android/LogListener.hxx"
|
||||||
|
#include "Main.hxx"
|
||||||
|
|
||||||
static int
|
static int
|
||||||
ToAndroidLogLevel(LogLevel log_level)
|
ToAndroidLogLevel(LogLevel log_level)
|
||||||
|
@ -177,6 +179,9 @@ Log(const Domain &domain, LogLevel level, const char *msg)
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
__android_log_print(ToAndroidLogLevel(level), "MPD",
|
__android_log_print(ToAndroidLogLevel(level), "MPD",
|
||||||
"%s: %s", domain.GetName(), msg);
|
"%s: %s", domain.GetName(), msg);
|
||||||
|
if (logListener != nullptr)
|
||||||
|
logListener->OnLog(Java::GetEnv(), ToAndroidLogLevel(level),
|
||||||
|
"%s: %s", domain.GetName(), msg);
|
||||||
#else
|
#else
|
||||||
|
|
||||||
if (level < log_threshold)
|
if (level < log_threshold)
|
||||||
|
|
|
@ -92,6 +92,7 @@
|
||||||
#include "java/File.hxx"
|
#include "java/File.hxx"
|
||||||
#include "android/Environment.hxx"
|
#include "android/Environment.hxx"
|
||||||
#include "android/Context.hxx"
|
#include "android/Context.hxx"
|
||||||
|
#include "android/LogListener.hxx"
|
||||||
#include "fs/StandardDirectory.hxx"
|
#include "fs/StandardDirectory.hxx"
|
||||||
#include "fs/FileSystem.hxx"
|
#include "fs/FileSystem.hxx"
|
||||||
#include "org_musicpd_Bridge.h"
|
#include "org_musicpd_Bridge.h"
|
||||||
|
@ -128,6 +129,7 @@ static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10;
|
||||||
|
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
Context *context;
|
Context *context;
|
||||||
|
LogListener *logListener;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Instance *instance;
|
Instance *instance;
|
||||||
|
@ -676,16 +678,19 @@ try {
|
||||||
|
|
||||||
gcc_visibility_default
|
gcc_visibility_default
|
||||||
JNIEXPORT void JNICALL
|
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::Init(env);
|
||||||
Java::File::Initialise(env);
|
Java::File::Initialise(env);
|
||||||
Environment::Initialise(env);
|
Environment::Initialise(env);
|
||||||
|
|
||||||
context = new Context(env, _context);
|
context = new Context(env, _context);
|
||||||
|
if (_logListener != nullptr)
|
||||||
|
logListener = new LogListener(env, _logListener);
|
||||||
|
|
||||||
mpd_main(0, nullptr);
|
mpd_main(0, nullptr);
|
||||||
|
|
||||||
|
delete logListener;
|
||||||
delete context;
|
delete context;
|
||||||
Environment::Deinitialise(env);
|
Environment::Deinitialise(env);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,10 @@ class Context;
|
||||||
struct Instance;
|
struct Instance;
|
||||||
|
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
|
#include "android/LogListener.hxx"
|
||||||
|
|
||||||
extern Context *context;
|
extern Context *context;
|
||||||
|
extern LogListener *logListener;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern Instance *instance;
|
extern Instance *instance;
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
|
@ -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
|
Loading…
Reference in New Issue