|
|
@ -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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|