From bfb1b21d6b973569176d0f21d585542da79c3a4a Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Tue, 5 Mar 2013 19:02:26 +0000 Subject: [PATCH] Android : FINE or lower log levels are now displayed in output when configured so git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10468 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../android/com/jme3/app/AndroidHarness.java | 4 +- .../com/jme3/util/AndroidLogHandler.java | 72 ++++++++++++++----- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/engine/src/android/com/jme3/app/AndroidHarness.java b/engine/src/android/com/jme3/app/AndroidHarness.java index 49886ead7..49c1a38f0 100644 --- a/engine/src/android/com/jme3/app/AndroidHarness.java +++ b/engine/src/android/com/jme3/app/AndroidHarness.java @@ -13,7 +13,6 @@ import android.view.ViewGroup.LayoutParams; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; -import com.jme3.asset.DesktopAssetManager; import com.jme3.audio.AudioRenderer; import com.jme3.audio.android.AndroidAudioRenderer; import com.jme3.input.JoyInput; @@ -29,9 +28,11 @@ import com.jme3.system.android.AndroidConfigChooser; import com.jme3.system.android.AndroidConfigChooser.ConfigType; import com.jme3.system.android.JmeAndroidSystem; import com.jme3.system.android.OGLESContext; +import com.jme3.util.AndroidLogHandler; import java.io.PrintWriter; import java.io.StringWriter; import java.util.logging.Level; +import java.util.logging.LogManager; import java.util.logging.Logger; /** @@ -159,6 +160,7 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt @Override public void onCreate(Bundle savedInstanceState) { + LogManager.getLogManager().getLogger("").addHandler(new AndroidLogHandler()); logger.fine("onCreate"); super.onCreate(savedInstanceState); diff --git a/engine/src/android/com/jme3/util/AndroidLogHandler.java b/engine/src/android/com/jme3/util/AndroidLogHandler.java index 51885cd0d..805c5d9bf 100644 --- a/engine/src/android/com/jme3/util/AndroidLogHandler.java +++ b/engine/src/android/com/jme3/util/AndroidLogHandler.java @@ -1,17 +1,38 @@ package com.jme3.util; import android.util.Log; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; /** - * Converts from Java based logging ({@link Logger} to Android based - * logging {@link Log}. + * Converts from Java based logging ({@link Logger} to Android based logging + * {@link Log}. */ public class AndroidLogHandler extends Handler { + private static final Formatter THE_FORMATTER = new Formatter() { + @Override + public String format(LogRecord r) { + Throwable thrown = r.getThrown(); + if (thrown != null) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + sw.write(r.getMessage()); + sw.write("\n"); + thrown.printStackTrace(pw); + pw.flush(); + return sw.toString(); + } else { + return r.getMessage(); + } + } + }; + @Override public void close() { } @@ -22,21 +43,38 @@ public class AndroidLogHandler extends Handler { @Override public void publish(LogRecord record) { - Level level = record.getLevel(); - String clsName = record.getSourceClassName(); - String msg = String.format(record.getMessage(), record.getParameters()); - Throwable t = record.getThrown(); - - if (level == Level.FINE) { - Log.i(clsName, msg, t); - } else if (level == Level.SEVERE) { - Log.e(clsName, msg, t); - } else if (level == Level.WARNING) { - Log.w(clsName, msg, t); - } else if (level == Level.CONFIG) { - Log.d(clsName, msg, t); - } else if (level == Level.FINE || level == Level.FINER || level == Level.FINEST) { - Log.v(clsName, msg, t); + + try { + Level level = record.getLevel(); + String tag = record.getLoggerName(); + String msg = THE_FORMATTER.format(record); + int lv = getAndroidLevel(level); + + Log.println(lv, tag, msg); + + } catch (RuntimeException e) { + Log.e("AndroidHandler", "Error logging message.", e); + } + } + + /** + * Converts a {@link java.util.logging.Logger} logging level into an Android + * one. + * + * @param level The {@link java.util.logging.Logger} logging level. + * + * @return The resulting Android logging level. + */ + static int getAndroidLevel(Level level) { + int value = level.intValue(); + if (value >= 1000) { // SEVERE + return Log.ERROR; + } else if (value >= 900) { // WARNING + return Log.WARN; + } else if (value >= 800) { // INFO + return Log.INFO; + } else { + return Log.DEBUG; } } }