From b009c7bed22ef6502cfdf5082339590c234046ac Mon Sep 17 00:00:00 2001 From: Tim Gunn <2896311+Tornado-Tim@users.noreply.github.com> Date: Mon, 6 Apr 2020 22:01:15 +1200 Subject: [PATCH] Move the APS logger to its own package --- .../plugins/aps/logger/LoggerCallback.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.java new file mode 100644 index 0000000000..815cf2fa24 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.java @@ -0,0 +1,63 @@ +package info.nightscout.androidaps.plugins.aps.logger; + +import org.mozilla.javascript.ScriptableObject; + +import javax.inject.Inject; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; + +/** + * Created by adrian on 15/10/17. + */ + + +public class LoggerCallback extends ScriptableObject { + + @Inject + AAPSLogger aapsLogger; + + private static StringBuffer errorBuffer = new StringBuffer(); + private static StringBuffer logBuffer = new StringBuffer(); + + + public LoggerCallback() { + //empty constructor needed for Rhino + errorBuffer = new StringBuffer(); + logBuffer = new StringBuffer(); + MainApp.instance().androidInjector().inject(this); + } + + @Override + public String getClassName() { + return "LoggerCallback"; + } + + public void jsConstructor() { + //empty constructor on JS site; could work as setter + } + + public void jsFunction_log(Object obj1) { + aapsLogger.debug(LTag.APS, obj1.toString().trim()); + logBuffer.append(obj1.toString()); + } + + public void jsFunction_error(Object obj1) { + aapsLogger.error(LTag.APS, obj1.toString().trim()); + errorBuffer.append(obj1.toString()); + } + + + public static String getScriptDebug() { + String ret = ""; + if (errorBuffer.length() > 0) { + ret += "e:\n" + errorBuffer.toString(); + } + if (ret.length() > 0 && logBuffer.length() > 0) ret += '\n'; + if (logBuffer.length() > 0) { + ret += "d:\n" + logBuffer.toString(); + } + return ret; + } +}