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 : https://plus.google.com/100430440392224314007/posts/ht4aZS8LSWg * Version: 1.0.3 ====== 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 1.0.2 * Fixed a bug that occured when a script causes the container to reload, because of which logs before then wouldn't be saved and displayed after the reload. 1.0.3 * fixed slow writing speed which caused duplicates and wrong order of logs ===== Things to keep in mind ===== * your code is executed in eval(), so some weird stuff might happen * The log won't catch errors thrown by the compiler 1.0.4 * There is now a clear log button, so you don't have to delete it wherever you are saving it * You can now reduce your whole script to one line without the line that starts this script breaking your script ====== logScript ====== <sxh javascript> /*----------- put this in script to use logScript var logScript = LL.getScriptByName('logScript'); if(logScript!=null){ return eval('(function() {' + logScript.getText() + '}())'); }else{throw("logScript not found!");} //logScriptEnd -----------*/ 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.FileWriter"); LL.bindClass("java.io.BufferedWriter"); 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.v.append(Html.fromHtml(logText)); scrollDown(logScript.sv); try { if (bfWriter == null) var bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); bfWriter.append(logText); } catch (e) { alert(e); } bfWriter.flush() } 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.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); if(typeof bfWriter != "undefined") bfWriter.flush(); } }/*----------- put this in script to use logScript var logScript = LL.getScriptByName('logScript'); if(logScript!=null){ return eval('(function() {' + logScript.getText() + '}())'); }else{throw("logScript not found!");} /*logScriptEnd*/ //CONFIG 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" // END CONFIG , scrollDown : function(sv){ sv.post(new Runnable() { run: function() { sv.fullScroll(ScrollView.FOCUS_DOWN); } }); } , initCv: function(){ if(this.cv == null) this.cv = LL.getItemById(LL.getScriptByName("logScript").getTag("cvId")); if (this.cv == null) throw ("No custom view found!") } , initSv: function(){ if(this.cv == null) this.initCv(); var cv = this.cv.getView(); //if(cv != null){} for(var i = 0; i < cv.getChildCount(); i++){ this.sv = cv.getChildAt(i); if(this.sv.getTag()=="sv") break; } //} } , initTv: function(){ if(this.sv == null) this.initSv() this.tv = this.sv.getChildAt(0); } }; LL.bindClass("java.io.File"); LL.bindClass("java.io.FileWriter"); LL.bindClass("java.io.BufferedWriter"); 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.Button"); LL.bindClass("android.widget.FrameLayout"); LL.bindClass("android.view.Gravity"); 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>"; if(logScript.sv != null){ logScript.tv.append(Html.fromHtml(logText)); logScript.scrollDown(logScript.sv); } try { if (logScript.bfWriter == null) logScript.bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); logScript.bfWriter.append(logText); } catch (e) { alert(e); } } 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 var cv = item; var context = LL.getContext(); var tv = new TextView(context); var sv = new ScrollView(context); sv.addView(tv); sv.setTag("sv"); tv.setText(Html.fromHtml(read(logScript.logFilePath))); tv.setMovementMethod(new ScrollingMovementMethod()); cv.setVerticalGrab(true); tv.setVerticalScrollBarEnabled(true); if (!logScript.lineWrapping) { tv.setHorizontallyScrolling(true); cv.setHorizontalGrab(true); } var button = new Button(context); button.setText("clear log"); button.setOnClickListener(new View.OnClickListener() { onClick: function(v) { new File(logScript.logFilePath).delete(); try{ logScript.initTv(); tv.setText(""); }catch(e){} } }); button.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT)); var layout = new FrameLayout(context); layout.addView(sv); layout.addView(button); logScript.scrollDown(sv); return layout; } 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 { // in resume try{ logScript.initTv(); logScript.tv.setText(Html.fromHtml(read(logScript.logFilePath))); }catch(e){} } } else { //in eval() try{logScript.initTv();}catch(e){} logScript.ind = parseInt(logScript.cv.getTag("prevColorIndex") || "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.text.substr(logScript.text.search("logScriptEnd") + 12); if(logScript.text.charAt(0) == "*") logScript.text = logScript.text.slice(2, logScript.text.length); logScript.text = logScript.returns + logScript.text; alert(logScript.text); 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); if(typeof logScript.bfWriter != "undefined") logScript.bfWriter.flush(); } } </sxh>

script_customviewlog.1467895835.txt.gz · Last modified: 2016/07/07 12:50 by cdfa