diff --git a/engine/src/android/com/jme3/app/AndroidHarness.java b/engine/src/android/com/jme3/app/AndroidHarness.java index 49c1a38f0..702c3a25d 100644 --- a/engine/src/android/com/jme3/app/AndroidHarness.java +++ b/engine/src/android/com/jme3/app/AndroidHarness.java @@ -31,6 +31,7 @@ import com.jme3.system.android.OGLESContext; import com.jme3.util.AndroidLogHandler; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; @@ -65,7 +66,7 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt */ @Deprecated protected boolean eglConfigVerboseLogging = false; - + /** * set to 2, 4 to enable multisampling. */ @@ -160,7 +161,8 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt @Override public void onCreate(Bundle savedInstanceState) { - LogManager.getLogManager().getLogger("").addHandler(new AndroidLogHandler()); + initializeLogHandler(); + logger.fine("onCreate"); super.onCreate(savedInstanceState); @@ -198,7 +200,7 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt settings.setSamples(antiAliasingSamples); settings.setResolution(disp.getWidth(), disp.getHeight()); settings.put(AndroidConfigChooser.SETTINGS_CONFIG_TYPE, eglConfigType); - + // Create application instance try { @@ -473,6 +475,24 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt } } + /** + * Removes the standard Android log handler due to an issue with not logging + * entries lower than INFO level and adds a handler that produces + * JME formatted log messages. + */ + protected void initializeLogHandler() { + Logger log = LogManager.getLogManager().getLogger(""); + for (Handler handler : log.getHandlers()) { + if (log.getLevel() != null && log.getLevel().intValue() <= Level.FINE.intValue()) { + Log.v("AndroidHarness", "Removing Handler class: " + handler.getClass().getName()); + } + log.removeHandler(handler); + } + Handler handler = new AndroidLogHandler(); + log.addHandler(handler); + handler.setLevel(Level.ALL); + } + public void initialize() { app.initialize(); if (handleExitHook) { diff --git a/engine/src/android/com/jme3/system/android/JmeAndroidSystem.java b/engine/src/android/com/jme3/system/android/JmeAndroidSystem.java index aab6a2b7c..40318040b 100644 --- a/engine/src/android/com/jme3/system/android/JmeAndroidSystem.java +++ b/engine/src/android/com/jme3/system/android/JmeAndroidSystem.java @@ -115,34 +115,7 @@ public class JmeAndroidSystem extends JmeSystemDelegate { } initialized = true; - try { -// JmeFormatter formatter = new JmeFormatter(); -// Handler consoleHandler = new AndroidLogHandler(); -// consoleHandler.setFormatter(formatter); -// -// Logger log = Logger.getLogger(""); -// for (Handler h : log.getHandlers()) { -// log.removeHandler(h); -// } -// log.addHandler(consoleHandler); - Logger log = Logger.getLogger(JmeAndroidSystem.class.getName()); - boolean bIsLogFormatSet = false; - do { - log.setLevel(Level.ALL); - if (log.getHandlers().length == 0) { - log = log.getParent(); - if (log != null) { - for (Handler h : log.getHandlers()) { - h.setFormatter(new JmeFormatter()); - h.setLevel(Level.ALL); - bIsLogFormatSet = true; - } - } - } - } while (log != null && !bIsLogFormatSet); - } catch (SecurityException ex) { - logger.log(Level.SEVERE, "Security error in creating log file", ex); - } + logger.log(Level.INFO, "Running on {0}", getFullName()); } diff --git a/engine/src/android/com/jme3/util/AndroidLogHandler.java b/engine/src/android/com/jme3/util/AndroidLogHandler.java index 805c5d9bf..9966be955 100644 --- a/engine/src/android/com/jme3/util/AndroidLogHandler.java +++ b/engine/src/android/com/jme3/util/AndroidLogHandler.java @@ -15,22 +15,31 @@ import java.util.logging.Logger; */ public class AndroidLogHandler extends Handler { - private static final Formatter THE_FORMATTER = new Formatter() { + private static final Formatter JME_FORMATTER = new JmeFormatter() { + + String lineSeperator = System.getProperty("line.separator"); + @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(); + public String format(LogRecord record) { + StringBuilder sb = new StringBuilder(); + + sb.append(record.getLevel().getLocalizedName()).append(" "); + sb.append(formatMessage(record)).append(lineSeperator); + + if (record.getThrown() != null) { + try { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + record.getThrown().printStackTrace(pw); + pw.close(); + sb.append(sw.toString()); + } catch (Exception ex) { + } } + + return sb.toString(); } + }; @Override @@ -44,14 +53,13 @@ public class AndroidLogHandler extends Handler { @Override public void publish(LogRecord record) { - try { - Level level = record.getLevel(); - String tag = record.getLoggerName(); - String msg = THE_FORMATTER.format(record); - int lv = getAndroidLevel(level); - - Log.println(lv, tag, msg); + int level = getAndroidLevel(record.getLevel()); +// String tag = loggerNameToTag(record.getLoggerName()); + String tag = record.getLoggerName(); + try { + String message = JME_FORMATTER.format(record); + Log.println(level, tag, message); } catch (RuntimeException e) { Log.e("AndroidHandler", "Error logging message.", e); } @@ -77,4 +85,26 @@ public class AndroidLogHandler extends Handler { return Log.DEBUG; } } + + /** + * Returns the short logger tag for the given logger name. + * Traditionally loggers are named by fully-qualified Java classes; this + * method attempts to return a concise identifying part of such names. + */ + public static String loggerNameToTag(String loggerName) { + // Anonymous logger. + if (loggerName == null) { + return "null"; + } + + int length = loggerName.length(); + int lastPeriod = loggerName.lastIndexOf("."); + + if (lastPeriod == -1) { + return loggerName; + } + + return loggerName.substring(lastPeriod + 1); + } + }