soc.2012.android: 126f826c: Added logging routing to android logcat, ...

michael at soc.pidgin.im michael at soc.pidgin.im
Sat Jun 9 06:57:11 EDT 2012


----------------------------------------------------------------------
Revision: 126f826cddbc3f9020d99e4d1e3aab70396ac975
Parent:   257607fea4c29923037534e43b8a48acab44491e
Author:   michael at soc.pidgin.im
Date:     06/03/12 18:04:11
Branch:   im.pidgin.soc.2012.android
URL: http://d.pidgin.im/viewmtn/revision/info/126f826cddbc3f9020d99e4d1e3aab70396ac975

Changelog: 

Added logging routing to android logcat, so that log messages can be read.


Changes against parent 257607fea4c29923037534e43b8a48acab44491e

  added    android/workspace/im.pidgin.libpurple/native/logging.c
  added    android/workspace/im.pidgin.libpurple/native/logging.h
  patched  android/workspace/im.pidgin.libpurple/.cproject
  patched  android/workspace/im.pidgin.libpurple/native/PurpleInstance.c
  patched  android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/PurpleInstance.java
  patched  android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/PurpleLibraryLoader.java
  patched  android/workspace/im.pidgin.libpurple.testclient/src/im/pidgin/libpurple/testclient/PurpleTestClient.java

-------------- next part --------------
============================================================
--- android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/PurpleInstance.java	d58012cd32660a40f95582eaf5cf607e518e842f
+++ android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/PurpleInstance.java	ade1bafb6bb06ab816a9f891a2fc87793e5645da
@@ -1,5 +1,7 @@ package im.pidgin.libpurple.core;
 package im.pidgin.libpurple.core;
 
+import im.pidgin.libpurple.exceptions.PurpleRuntimeException;
+
 import java.io.File;
 
 /**
@@ -14,14 +16,67 @@ public class PurpleInstance {
  * 
  */
 public class PurpleInstance {
+	private static Object instanceActiveMutex = new Object();
+	private static boolean instanceActive = false;
 
-	public PurpleInstance() {
+	private PurpleInstance(String uiName, File baseDirectory) {
 		PurpleLibraryLoader.load();
+
+		setDebugEnabled(true);
+		
+		File pluginDirectory = new File(baseDirectory, "plugins");
+		if (!pluginDirectory.exists()) {
+			pluginDirectory.mkdirs();
+		}
+		addPluginSearchpath(pluginDirectory);
+
+		if (!init_native(uiName)) {
+			throw new PurpleRuntimeException(
+					"Initialization of the libpurple core failed.");
+		}
 	}
 
-	public static PurpleInstance createInstance(File baseDirectory) {
-		return null;
+	public void setDebugEnabled(boolean enabled) {
+		setDebugEnabled_native(enabled);
 	}
 
-	public native String getVersion_native();
+	private native void setDebugEnabled_native(boolean enabled);
+
+	public void addPluginSearchpath(File directory) {
+		addPluginSearchpath_native(directory.getAbsolutePath());
+	}
+
+	private native void addPluginSearchpath_native(String absolutePath);
+
+	/**
+	 * Initializes libpurple by setting the core ui ops and calling
+	 * purple_core_init(). To be called after eventloop was set up.
+	 * 
+	 * @param uiName
+	 */
+	private native boolean init_native(String uiName);
+
+	public static PurpleInstance createInstance(String uiName,
+			File baseDirectory) {
+		synchronized (instanceActiveMutex) {
+			if (instanceActive) {
+				throw new IllegalStateException(
+						"There is already a libpurple running");
+			}
+			instanceActive = true;
+		}
+
+		return new PurpleInstance(uiName, baseDirectory);
+	}
+
+	/**
+	 * Gets the current version of libpurple.
+	 * 
+	 * @return The version as version String.
+	 */
+	public String getVersion() {
+		return getVersion_native();
+	}
+
+	private native String getVersion_native();
 }
============================================================
--- android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/PurpleLibraryLoader.java	23c65524605799d029955751022ba1d359509cb8
+++ android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/PurpleLibraryLoader.java	c974e6d07153e70f5c8be326a62d221db153d207
@@ -13,9 +13,7 @@ public class PurpleLibraryLoader {
 public class PurpleLibraryLoader {
 	private static boolean loaded = false;
 	private static Object mutex = new Object();
-//	private Method loadLibraryMethod;
-//	private String[] libDirs;
-
+	
 	private PurpleLibraryLoader() {
 	}
 
@@ -23,6 +21,7 @@ public class PurpleLibraryLoader {
 		synchronized (mutex) {
 			if (!loaded) {
 				PurpleLibraryLoader purpleLibraryLoader = new PurpleLibraryLoader();
+				purpleLibraryLoader.loadLibrary("log", null);
 				purpleLibraryLoader.loadLibrary("ffi", "6");
 				purpleLibraryLoader.loadLibrary("charset", "1");
 				purpleLibraryLoader.loadLibrary("iconv", "2");
@@ -50,68 +49,4 @@ public class PurpleLibraryLoader {
 		}
 		System.loadLibrary(realname);
 	}
-//	private void loadLibrary(String name, String major) {
-//		if (libDirs == null) {
-//			String pathSeparator = System.getProperty("path.separator", ":");
-//			String javaLibraryPath = System.getProperty("java.library.path",
-//					".");
-//			String fileSep = System.getProperty("file.separator", "/");
-//
-//			libDirs = javaLibraryPath.split(pathSeparator);
-//
-//			for (int i = 0; i < libDirs.length; i++) {
-//				if (!libDirs[i].endsWith(fileSep)) {
-//					libDirs[i] += fileSep;
-//				}
-//			}
-//			System.out.println("Runtime paths: " + Arrays.toString(libDirs));
-//		}
-//
-//		String filename = "lib" + name + ".so";
-//		if (major != null) {
-//			filename += "." + major;
-//		}
-//
-//		for (String dir : libDirs) {
-//			File file = new File(dir + filename);
-//			if (file.exists()) {
-//				loadFile(file.getAbsolutePath());
-//				return;
-//			}
-//		}
-//		throw new UnsatisfiedLinkError("could not find " + filename);
-//	}
-//
-//	private void loadFile(String file) {
-//		if (loadLibraryMethod == null) {
-//			try {
-//				loadLibraryMethod = Runtime.class.getMethod("nativeLoad",
-//						String.class, ClassLoader.class);
-//				loadLibraryMethod.setAccessible(true);
-//			} catch (SecurityException e) {
-//				e.printStackTrace();
-//				throw new UnsatisfiedLinkError(
-//						"security error accessing nativeLoad()");
-//			} catch (NoSuchMethodException e) {
-//				e.printStackTrace();
-//				throw new UnsatisfiedLinkError("cannot find nativeLoad()");
-//			}
-//		}
-//		try {
-//			String error = (String) loadLibraryMethod.invoke(Runtime.class,
-//					file);
-//			if (error != null) {
-//				throw new UnsatisfiedLinkError(error);
-//			}
-//		} catch (IllegalArgumentException e) {
-//			e.printStackTrace();
-//			throw new UnsatisfiedLinkError("error invoking nativeLoad()");
-//		} catch (IllegalAccessException e) {
-//			e.printStackTrace();
-//			throw new UnsatisfiedLinkError("error accessing nativeLoad()");
-//		} catch (InvocationTargetException e) {
-//			e.printStackTrace();
-//			throw new UnsatisfiedLinkError("error invoking nativeLoad()");
-//		}
-//	}
 }
============================================================
--- android/workspace/im.pidgin.libpurple/.cproject	aef56829adcff808c5d636e71b1fe73fe2394274
+++ android/workspace/im.pidgin.libpurple/.cproject	b6da4f37e901a98d1c3372408fe1a042d23711f4
@@ -35,7 +35,7 @@
 								<option id="gnu.cpp.compiler.so.release.option.optimization.level.1740518659" name="Optimization Level" superClass="gnu.cpp.compiler.so.release.option.optimization.level" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
 								<option id="gnu.cpp.compiler.so.release.option.debugging.level.208987653" name="Debug Level" superClass="gnu.cpp.compiler.so.release.option.debugging.level" value="gnu.cpp.compiler.debugging.level.none" valueType="enumerated"/>
 							</tool>
-							<tool command="${ndk.root}/bin/${ndk.host}-gcc" id="cdt.managedbuild.tool.gnu.c.compiler.so.release.438648946" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.so.release">
+							<tool command="${ndk.root}/bin/${ndk.host}-gcc" commandLinePattern="${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}" id="cdt.managedbuild.tool.gnu.c.compiler.so.release.438648946" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.so.release">
 								<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.so.release.option.optimization.level.1239458496" name="Optimization Level" superClass="gnu.c.compiler.so.release.option.optimization.level" valueType="enumerated"/>
 								<option id="gnu.c.compiler.so.release.option.debugging.level.17581855" name="Debug Level" superClass="gnu.c.compiler.so.release.option.debugging.level" value="gnu.c.debugging.level.none" valueType="enumerated"/>
 								<option id="gnu.c.compiler.option.include.paths.105762210" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath">
@@ -44,12 +44,15 @@
 									<listOptionValue builtIn="false" value=""${workspace_loc:/im.pidgin.libpurple.build/build/prefix/include/glib-2.0}""/>
 									<listOptionValue builtIn="false" value=""${workspace_loc:/im.pidgin.libpurple.build/build/prefix/lib/glib-2.0/include}""/>
 								</option>
+								<option id="gnu.c.compiler.option.warnings.toerrors.482094137" name="Warnings as errors (-Werror)" superClass="gnu.c.compiler.option.warnings.toerrors" value="true" valueType="boolean"/>
+								<option id="gnu.c.compiler.option.misc.other.1988279070" name="Other flags" superClass="gnu.c.compiler.option.misc.other" value="-c -fmessage-length=0" valueType="string"/>
 								<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.877345300" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
 							</tool>
 							<tool command="${ndk.root}/bin/${ndk.host}-gcc" id="cdt.managedbuild.tool.gnu.c.linker.so.release.1449130208" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.so.release">
 								<option defaultValue="true" id="gnu.c.link.so.release.option.shared.788169912" name="Shared (-shared)" superClass="gnu.c.link.so.release.option.shared" valueType="boolean"/>
 								<option id="gnu.c.link.option.libs.1851430816" name="Libraries (-l)" superClass="gnu.c.link.option.libs" valueType="libs">
 									<listOptionValue builtIn="false" value="purple"/>
+									<listOptionValue builtIn="false" value="log"/>
 									<listOptionValue builtIn="false" value="glib-2.0"/>
 								</option>
 								<option id="gnu.c.link.option.paths.866605597" name="Library search path (-L)" superClass="gnu.c.link.option.paths" valueType="libPaths">
@@ -109,11 +112,16 @@
 									<listOptionValue builtIn="false" value=""${workspace_loc:/im.pidgin.libpurple.build/build/prefix/include/glib-2.0}""/>
 									<listOptionValue builtIn="false" value=""${workspace_loc:/im.pidgin.libpurple.build/build/prefix/lib/glib-2.0/include}""/>
 								</option>
+								<option id="gnu.c.compiler.option.warnings.toerrors.1752011746" name="Warnings as errors (-Werror)" superClass="gnu.c.compiler.option.warnings.toerrors" value="true" valueType="boolean"/>
+								<option id="gnu.c.compiler.option.warnings.wconversion.1396656653" name="Implicit conversion warnings (-Wconversion)" superClass="gnu.c.compiler.option.warnings.wconversion" value="false" valueType="boolean"/>
+								<option id="gnu.c.compiler.option.misc.other.1967504557" name="Other flags" superClass="gnu.c.compiler.option.misc.other" value="-c -fmessage-length=0" valueType="string"/>
 								<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.793154500" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
 							</tool>
 							<tool id="cdt.managedbuild.tool.gnu.c.linker.exe.debug.137759764" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.exe.debug">
 								<option id="gnu.c.link.option.libs.1999672232" name="Libraries (-l)" superClass="gnu.c.link.option.libs" valueType="libs">
-									<listOptionValue builtIn="false" value=""${workspace_loc:/im.pidgin.libpurple.build/build/prefix/lib/libpurple.so}""/>
+									<listOptionValue builtIn="false" value="purple"/>
+									<listOptionValue builtIn="false" value="log"/>
+									<listOptionValue builtIn="false" value="glib-2.0"/>
 								</option>
 								<option id="gnu.c.link.option.paths.727652150" name="Library search path (-L)" superClass="gnu.c.link.option.paths" valueType="libPaths">
 									<listOptionValue builtIn="false" value=""${workspace_loc:/im.pidgin.libpurple.build/build/prefix/lib}""/>
============================================================
--- android/workspace/im.pidgin.libpurple/native/PurpleInstance.c	8790ffc47fbf3f4df595909503282a06de658e39
+++ android/workspace/im.pidgin.libpurple/native/PurpleInstance.c	723071418aace3a5ace72a8de22d4f4b77fbe3a6
@@ -1,15 +1,98 @@
 #include <jni.h>
 #include <libpurple/core.h>
+#include <libpurple/plugin.h>
+#include <libpurple/blist.h>
+#include <libpurple/pounce.h>
 
 #include "PurpleInstance.h"
+#include "helpers.h"
+#include "logging.h"
 
+JavaObjectReference instanceReference;
+
+void initUi();
+
+PurpleCoreUiOps coreUiOps = {
+		NULL,
+		initLogging,
+		initUi,
+		NULL,
+		NULL,
+		NULL,
+		NULL
+};
+
+void initUi() {
+	//TODO...
+}
+
 /*
  * Class:     im_pidgin_libpurple_core_PurpleInstance
+ * Method:    setDebugEnabled_native
+ * Signature: (Z)V
+ */
+JNIEXPORT void JNICALL Java_im_pidgin_libpurple_core_PurpleInstance_setDebugEnabled_1native
+  (JNIEnv *env, jobject obj, jboolean enabled) {
+	setDebugEnabled(enabled);
+}
+
+
+/*
+ * Class:     im_pidgin_libpurple_core_PurpleInstance
+ * Method:    addPluginSearchpath_native
+ * Signature: (Ljava/lang/String;)V
+ */JNIEXPORT void JNICALL
+Java_im_pidgin_libpurple_core_PurpleInstance_addPluginSearchpath_1native(
+		JNIEnv *env, jobject obj, jstring path)
+{
+	const char* pathNative;
+
+	pathNative = (*env)->GetStringUTFChars(env, path, NULL);
+	g_return_if_fail(pathNative != NULL);
+	purple_plugins_add_search_path(pathNative);
+	(*env)->ReleaseStringUTFChars(env, path, pathNative);
+}
+
+/*
+ * Class:     im_pidgin_libpurple_core_PurpleInstance
+ * Method:    init_native
+ * Signature: ()V
+ */JNIEXPORT jboolean JNICALL
+Java_im_pidgin_libpurple_core_PurpleInstance_init_1native(JNIEnv *env,
+		jobject obj, jstring uiName)
+{
+	jboolean success;
+	const char* uiNameNative;
+
+	setJavaObject(&instanceReference, env, obj);
+
+	purple_core_set_ui_ops(&coreUiOps);
+
+	uiNameNative = (*env)->GetStringUTFChars(env, uiName, NULL);
+	g_return_val_if_fail(uiNameNative != NULL, JNI_FALSE);
+
+	success = purple_core_init(uiNameNative) ? JNI_TRUE : JNI_FALSE;
+	(*env)->ReleaseStringUTFChars(env, uiName, uiNameNative);
+
+	if (success) {
+		purple_set_blist(purple_blist_new());
+		purple_blist_load();
+		purple_plugins_load_saved("/plugins/loaded");
+		purple_pounces_load();
+
+	}
+
+	return success;
+}
+
+/*
+ * Class:     im_pidgin_libpurple_core_PurpleInstance
  * Method:    getVersion_native
  * Signature: ()Ljava/lang/String;
- */
-JNIEXPORT jstring JNICALL Java_im_pidgin_libpurple_core_PurpleInstance_getVersion_1native
-  (JNIEnv *env, jobject obj) {
+ */JNIEXPORT jstring JNICALL
+Java_im_pidgin_libpurple_core_PurpleInstance_getVersion_1native(JNIEnv *env,
+		jobject obj)
+{
 	const char* version = purple_core_get_version();
 	return (*env)->NewStringUTF(env, version);
 }
============================================================
--- android/workspace/im.pidgin.libpurple.testclient/src/im/pidgin/libpurple/testclient/PurpleTestClient.java	cdb7bc17f35a9c8b790088bf7b149bc794b74e7e
+++ android/workspace/im.pidgin.libpurple.testclient/src/im/pidgin/libpurple/testclient/PurpleTestClient.java	1f1cc746bf002a3c2f388fcbb405a2616cddab40
@@ -14,6 +14,6 @@ public class PurpleTestClient extends Ac
         
         TextView text = (TextView) findViewById(R.id.main_text);
         
-        text.setText("Purple Version: " + new PurpleInstance().getVersion_native());
+        text.setText("Purple Version: " + PurpleInstance.createInstance("testclient", getFilesDir()).getVersion());
     }
 }
\ No newline at end of file
============================================================
--- /dev/null	
+++ android/workspace/im.pidgin.libpurple/native/logging.c	0516713327e9c3ac96b7f65f5198b8796422f3c4
@@ -0,0 +1,81 @@
+/*
+ * logging.c
+ *
+ *  Created on: 29.05.2012
+ *      Author: michaelz
+ */
+
+#include <libpurple/debug.h>
+#include <android/log.h>
+#include "logging.h"
+
+gboolean debugEnabled;
+
+static void
+debugPrint(PurpleDebugLevel level, const char *category, const char *arg_s)
+{
+	__android_log_print(ANDROID_LOG_DEBUG, category, arg_s);
+}
+
+static void
+glibLogHandler(const gchar *domain, GLogLevelFlags flags, const gchar *msg,
+		gpointer user_data)
+{
+	int level = ANDROID_LOG_UNKNOWN;
+
+	if ((flags & G_LOG_LEVEL_ERROR) == G_LOG_LEVEL_ERROR)
+		level = ANDROID_LOG_ERROR;
+	else if ((flags & G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL)
+		level = ANDROID_LOG_ERROR;
+	else if ((flags & G_LOG_LEVEL_WARNING) == G_LOG_LEVEL_WARNING)
+		level = ANDROID_LOG_WARN;
+	else if ((flags & G_LOG_LEVEL_MESSAGE) == G_LOG_LEVEL_MESSAGE)
+		level = ANDROID_LOG_DEFAULT;
+	else if ((flags & G_LOG_LEVEL_INFO) == G_LOG_LEVEL_INFO)
+		level = ANDROID_LOG_INFO;
+	else if ((flags & G_LOG_LEVEL_DEBUG) == G_LOG_LEVEL_DEBUG)
+		level = ANDROID_LOG_DEBUG;
+
+	if (msg != NULL) {
+		__android_log_print(level, domain != NULL ? domain : "g_log", msg);
+	}
+}
+
+static gboolean
+isDebugEnabled()
+{
+	return debugEnabled;
+}
+
+PurpleDebugUiOps debugUiOps = { debugPrint, isDebugEnabled, NULL, NULL, NULL,
+		NULL, };
+
+void
+setDebugEnabled(gboolean enabled)
+{
+	debugEnabled = enabled;
+}
+
+void
+initLogging()
+{
+#define REGISTER_G_LOG_HANDLER(name) \
+	g_log_set_handler((name), G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL \
+					  | G_LOG_FLAG_RECURSION, \
+					  glibLogHandler, NULL)
+
+	/* Register the glib/gtk log handlers. */
+	REGISTER_G_LOG_HANDLER(NULL);
+	REGISTER_G_LOG_HANDLER("Gdk");
+	REGISTER_G_LOG_HANDLER("Gtk");
+	REGISTER_G_LOG_HANDLER("GdkPixbuf");
+	REGISTER_G_LOG_HANDLER("GLib");
+	REGISTER_G_LOG_HANDLER("GModule");
+	REGISTER_G_LOG_HANDLER("GLib-GObject");
+	REGISTER_G_LOG_HANDLER("GThread");
+#ifdef USE_GSTREAMER
+	REGISTER_G_LOG_HANDLER("GStreamer");
+#endif
+	purple_debug_set_ui_ops(&debugUiOps);
+}
+
============================================================
--- /dev/null	
+++ android/workspace/im.pidgin.libpurple/native/logging.h	c46dfa3e36e7c79eb4e02417308b722d684b6932
@@ -0,0 +1,17 @@
+/*
+ * logging.h
+ *
+ *  Created on: 29.05.2012
+ *      Author: michaelz
+ */
+
+#ifndef LOGGING_H_
+#define LOGGING_H_
+#include <glib.h>
+
+void setDebugEnabled(gboolean enabled);
+
+void initLogging();
+
+
+#endif /* LOGGING_H_ */


More information about the Commits mailing list