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
3.0
rem..om 12 years ago
parent 5f95807432
commit bfb1b21d6b
  1. 4
      engine/src/android/com/jme3/app/AndroidHarness.java
  2. 72
      engine/src/android/com/jme3/util/AndroidLogHandler.java

@ -13,7 +13,6 @@ import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.jme3.asset.DesktopAssetManager;
import com.jme3.audio.AudioRenderer; import com.jme3.audio.AudioRenderer;
import com.jme3.audio.android.AndroidAudioRenderer; import com.jme3.audio.android.AndroidAudioRenderer;
import com.jme3.input.JoyInput; 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.AndroidConfigChooser.ConfigType;
import com.jme3.system.android.JmeAndroidSystem; import com.jme3.system.android.JmeAndroidSystem;
import com.jme3.system.android.OGLESContext; import com.jme3.system.android.OGLESContext;
import com.jme3.util.AndroidLogHandler;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -159,6 +160,7 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
LogManager.getLogManager().getLogger("").addHandler(new AndroidLogHandler());
logger.fine("onCreate"); logger.fine("onCreate");
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

@ -1,17 +1,38 @@
package com.jme3.util; package com.jme3.util;
import android.util.Log; 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.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* Converts from Java based logging ({@link Logger} to Android based * Converts from Java based logging ({@link Logger} to Android based logging
* logging {@link Log}. * {@link Log}.
*/ */
public class AndroidLogHandler extends Handler { 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 @Override
public void close() { public void close() {
} }
@ -22,21 +43,38 @@ public class AndroidLogHandler extends Handler {
@Override @Override
public void publish(LogRecord record) { public void publish(LogRecord record) {
Level level = record.getLevel();
String clsName = record.getSourceClassName(); try {
String msg = String.format(record.getMessage(), record.getParameters()); Level level = record.getLevel();
Throwable t = record.getThrown(); String tag = record.getLoggerName();
String msg = THE_FORMATTER.format(record);
if (level == Level.FINE) { int lv = getAndroidLevel(level);
Log.i(clsName, msg, t);
} else if (level == Level.SEVERE) { Log.println(lv, tag, msg);
Log.e(clsName, msg, t);
} else if (level == Level.WARNING) { } catch (RuntimeException e) {
Log.w(clsName, msg, t); Log.e("AndroidHandler", "Error logging message.", e);
} 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); /**
* 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;
} }
} }
} }

Loading…
Cancel
Save