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 : I'll post on google+ later * Version: 1.0.1 ====== 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 ====== Changelog ====== 1.0.1 - Fixed a small bug with linenumbers ====== 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"); LL.bindClass("android.widget.Toast"); 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)); scrollDown(logScript.sv); } 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 ""; } } function scrollDown(sv){ sv.post(new Runnable() { run: function() { sv.fullScroll(ScrollView.FOCUS_DOWN); } }); } 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.setText(Html.fromHtml(read(logScript.logFilePath))); logScript.v.setMovementMethod(new ScrollingMovementMethod()); logScript.cv.setVerticalGrab(true); logScript.v.setVerticalScrollBarEnabled(true); if (!logScript.lineWrapping) { logScript.v.setHorizontallyScrolling(true); logScript.cv.setHorizontalGrab(true); } scrollDown(sv); 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(); if(logScript.sv == null){ Toast.makeText(LL.getContext(), "custom view not loaded", Toast.LENGTH_SHORT); function log(text, channel) {} }else{ logScript.v = logScript.sv.getChildAt(0); } logScript.text = LL.getCurrentScript().getText(); logScript.returns = ""; for(var i=0;i<logScript.text.substr(0, logScript.text.search("var logScript")).split("\n").length; i++){ logScript.returns += "\n"; } logScript.text = logScript.returns + 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.1450816210.txt.gz · Last modified: 2015/12/22 20:30 by cdfa