User Tools

Site Tools


script_customviewlog

**This is an old revision of the document!** ----

A PCRE internal error occured. This might be caused by a faulty plugin

====== About the script ====== * Purpose : With this script you can create a log in a custom view which lets you log things. Any errors in your script will also be written to this log * Author : cdfa * Link : gimme a minute * Version: 1.0 ====== How to use the script ====== If you're in the script repository app (you should be) click search and import and chose "logScript". Tick the "Lightning Menu" box and make sure it says "logScript" on top. (this is very important!! If you really want to name it differently modify the script where is says "if (LL.getCurrentScript().getName() == "logScript")" and replace logScript with the new name) Run this script in a container to set up the log view. If you want to use the log function and automatic error logging put exactly these two lines somewhere in the top of your script: ====== (copy/paste) ====== <sxh javascript> var logScript = LL.getScriptByName('logScript'); if(logScript!=null){ return eval('(function() {' + logScript.getText() + '}())'); }else{throw("logScript not found!");} //logScriptEnd </sxh> (before this gets executed you can't use the log function, errors won't get caught) log function: log(text, logLevel) the logLevel can be anything you want, but only "ERROR" and "debug" have custom colors. (you can add your own if you modify the script just a bit) please report any bugs/feature suggestion in the google plus post ====== logScript ====== <sxh javascript> var logScript = { logFilePath: "/storage/sdcard0/LightningLauncher/script/logScriptLog.html", lineWrapping: true, // might require app restart bgColor: 0xff191919, textColors: ["#d0d0d0", "#ee7600"] // the script iterates through these colors with each new "session" }; LL.bindClass("java.io.File"); LL.bindClass("java.io.FileOutputStream"); LL.bindClass("java.io.OutputStreamWriter"); LL.bindClass("android.widget.TextView"); LL.bindClass("android.text.method.ScrollingMovementMethod"); LL.bindClass("android.text.Html"); LL.bindClass("java.io.BufferedReader"); LL.bindClass("java.io.FileReader"); LL.bindClass("android.widget.ScrollView"); LL.bindClass("android.view.View"); LL.bindClass("java.lang.Runnable"); function log(text, channel) { var date = new Date(); var month = date.getMonth() + 1; var scriptName = LL.getCurrentScript().getName(); if (channel == null) channel = "normalLog"; switch (channel) { case "ERROR": var channelColor = "#ff0000"; break; case "debug": var channelColor = "#0000ff" break; default: var channelColor = "#ffffff"; break; } var logText = "<br><font color=#add8e6>" + date.getDate() + "-" + month + "</font>" + " <font color=#ffff00>" + date.toTimeString().slice(0, 8) + "</font>" + " <font color=#00ff00>" + scriptName + "</font>" + " <font color=" + channelColor + ">" + channel + "</font>" + ": " + " <font color=" + logScript.textColors[logScript.ind] + ">" + text + "</font>"; logScript.htmlText += logText; logScript.v.append(Html.fromHtml(logText)); logScript.sv.post(new Runnable() { run: function() { logScript.sv.fullScroll(ScrollView.FOCUS_DOWN); } }); } function read(filePath) { try { var r = new BufferedReader(new FileReader(filePath)); var s = ""; var l; while ((l = r.readLine()) != null) s += (l + "\n"); return s; } catch (e) { return ""; } } if (LL.getCurrentScript().getName() == "logScript") { var script = LL.getCurrentScript(); try { typeof item; } catch (e) { // in create logScript.cv = item; var context = LL.getContext(); logScript.v = new TextView(context); var sv = new ScrollView(context); sv.addView(logScript.v); logScript.v.setMovementMethod(new ScrollingMovementMethod()); logScript.cv.setVerticalGrab(true); logScript.v.setVerticalScrollBarEnabled(true); if (!logScript.lineWrapping) { logScript.v.setHorizontallyScrolling(true); logScript.cv.setHorizontalGrab(true); } return sv; } var e = LL.getEvent(); var src = e.getSource(); if (src == "MENU_APP") { //in container var scriptId = script.getId(); var e = LL.getEvent(); var c = e.getContainer(); var cv = c.addCustomView(e.getTouchX(), e.getTouchY()); var prop = cv.getProperties().edit(); prop.getBox("i.box").setColor("c", "n", logScript.bgColor); prop.setString("v.onCreate", scriptId); prop.setEventHandler("i.resumed", EventHandler.RUN_SCRIPT, scriptId); prop.commit(); script.setTag("cvId", cv.getId()); } else { var cv = LL.getItemById(script.getTag("cvId")); if (cv == null) { script.setTag("cvId", null); } else { cv.getView().getChildAt(0).setText(Html.fromHtml(read(logScript.logFilePath))); } } } else { //in eval() logScript.cv = LL.getItemById(LL.getScriptByName("logScript").getTag("cvId")); if (logScript.cv == null) throw ("No custom view found!") logScript.ind = parseInt(logScript.cv.getTag("prevColorIndex") || "0"); logScript.htmlText = ""; logScript.sv = logScript.cv.getView(); logScript.v = logScript.sv.getChildAt(0); logScript.text = LL.getCurrentScript().getText(); logScript.text = "\n" + logScript.text.substr(logScript.text.search("logScriptEnd") + 12); try { return eval('(function() {' + logScript.text + '}())'); } catch (e) { log("At line " + e.lineNumber + ": " + e, "ERROR"); } finally { logScript.cv.setTag("prevColorIndex", logScript.ind == logScript.textColors.length - 1 ? 0 : logScript.ind + 1); var out = null; try { out = new FileOutputStream(logScript.logFilePath, true); var osw = new OutputStreamWriter(out); osw.write(logScript.htmlText); } catch (e) { log(e, "ERROR"); } finally { if (osw != null) osw.flush(); if (out != null) out.close(); } } } </sxh>

script_customviewlog.1450613635.txt.gz · Last modified: 2015/12/20 12:13 by cdfa