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.5 ====== 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){try{return eval('(function(){'+logScript.getText()+'})()');}catch(e){if(e.message!="Custom view not found!"&&e.message!="Custom view not loaded!"){alert("At line "+e.lineNumber+": "+e);}function log(){}}}else{function log(){}}/*logScriptEnd*/ </sxh> (before this gets executed you can't use the log function, errors won't get caught) log function: log(text, logLevel, replacements..) Loglevels are accessed through logScript.logLevel. If you need some number or something to be inside the the text you can enter "{}" where the text should be and it will get replaced by the extra argument you passed. So for example: log("This {} has id: {}", logScript.logLevel.NORMAL, item.getType(), item.getId()) will write: This Shortcut had id: 18894. 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 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 1.0.5 * The logs are now saved on pause of the custom view instead of after the script has finished running. (But for some reason the paused event doesn't get fired when you restart the app, so keep that in mind) * you can now pass replacements to the log function (see above) * Some Errors in the logscript won't cause your script to break anymore. * Be sure to update the line that enables logscript. ===== 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 * For some reason the paused event doesn't get fired when you restart the app, so new logs since the last pause won't be saved ====== logScript ====== <sxh javascript> /*----------- put this in script to use logScript var logScript=LL.getScriptByName('logScript');if(logScript){try{return eval('(function(){'+logScript.getText()+'})()');}catch(e){if(e.message!="Custom view not found!"&&e.message!="Custom view not loaded!"){alert("At line "+e.lineNumber+": "+e);}function log(){}}}else{function log(){}}/*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" , logLevel:{ NORMAL:{name:"[NORMAL]", color:"#ffffff"} , DEBUG:{name:"[DEBUG]", color:"#0000ff"} , ERROR:{name:"[ERROR]", color:"#ff0000"} } , defaultLogLevel: "NORMAL" , saveMode: true // END CONFIG , getScript:function(){ return this.script = this.script || LL.getScriptByName("logScript"); } , scrollDown:function(sv){ sv.post(new Runnable(){ run:function(){ sv.fullScroll(ScrollView.FOCUS_DOWN); } }); } , getCVItem:function(){ if(!this.cvItem){ this.cvItem = LL.getItemById(this.getScript().getTag("cvId")); if(!this.cvItem) throw new Error("Custom view not found!") } return this.cvItem } , getCV:function(){ if(!this.cv){ this.cv = this.getCVItem().getView() if(!this.cv) throw new Error("Custom view not loaded!"); } return this.cv } , getSV:function(){ if(!this.sv){ for(var i=0; i<this.getCV().getChildCount(); i++){ this.sv=this.cv.getChildAt(i); if(this.sv.getTag()=="sv") break; } } return this.sv } , getTV:function(){ if(!this.tv) this.tv = this.getSV().getChildAt(0); return this.tv } }; 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, logLevel /**/){ logLevel = logLevel || logScript.logLevel[logScript.defaultLogLevel]; for(var i=2; i<arguments.length; i++){ newText = text.replace("{}", arguments[i]); if(newText==text){ break; }else{ text = newText; } } var date=new Date(); var month=date.getMonth()+1; var scriptName=LL.getCurrentScript().getName(); var logText="<font color=#add8e6>"+date.getDate()+"-"+month+ "</font>"+" <font color=#ffff00>"+date.toTimeString().slice(0, 8)+ "</font>"+" <font color=#00ff00>"+scriptName+"</font>"+ " <font color="+logLevel.color+">"+logLevel.name+"</font>"+": "+ " <font color="+logScript.textColors[logScript.ind]+">"+text+ "</font>"; if(logScript.sv){ var tvOldLength = logScript.tv.length() logScript.tv.append(Html.fromHtml(logText).append("\n")); logScript.scrollDown(logScript.sv); if(logScript.saveMode){ var script = logScript.getScript() var prevLogInd = script.getTag("prevLogInd")||-1 var prevLog = JSON.parse(script.getTag("log" + prevLogInd)) var tvNewLength = logScript.tv.length() if(prevLog && prevLog[1]==tvOldLength){ prevLog[1] = logScript.tv.length() script.setTag("log" + prevLogInd, JSON.stringify(prevLog)); }else{ prevLogInd++ script.setTag("log" + prevLogInd, JSON.stringify([tvOldLength, tvNewLength])) script.setTag("prevLogInd", prevLogInd) } } } } 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.setMovementMethod(new ScrollingMovementMethod()); tv.setTextColor(0xffffffff) 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.getTV(); tv.setText(""); logScript.getScript().setTag("prevLogInd", null) }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).commit(); prop.setEventHandler("i.resumed", EventHandler.RUN_SCRIPT, scriptId); prop.setEventHandler("i.paused", EventHandler.RUN_SCRIPT, scriptId); prop.commit(); script.setTag("cvId", cv.getId()); }else if(src=="I_RESUMED"){ // in resume logScript.getTV(); logScript.tv.setText(Html.fromHtml(read(logScript.logFilePath))); } else if(src=="I_PAUSED"){ logScript.getTV(); try{ var bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); var unsavedLog; var i = 0; var script = logScript.getScript() while(unsavedLog = script.getTag("log"+i)){ var unsavedLog = JSON.parse(unsavedLog) var fullText = logScript.tv.getEditableText(); if(fullText && fullText.length()!=0){ var unsavedLogTextString = Html.toHtml(fullText.subSequence(unsavedLog[0], unsavedLog[1])); bfWriter.append(unsavedLogTextString.substring(unsavedLogTextString.indexOf('>') + 1, unsavedLogTextString.lastIndexOf('<'))); } script.setTag("log"+i, null) i++; } script.setTag("prevLogInd", null); }catch(e){ alert(e); } finally{ bfWriter.flush(); } } }else{ //in eval() logScript.getTV(); logScript.ind=parseInt(logScript.cvItem.getTag("prevColorIndex")||"0"); try{ return eval('(function() {' + (function(){ var text=LL.getCurrentScript().getText(); var returns=""; for(var i=0; i<text.substr(0, text.search("var logScript")).split("\n").length; i++){ returns+="\n"; } text=text.substr(text.search("logScriptEnd")+12); if(text.charAt(0)=="*"){ text=text.slice(2, text.length); returns=returns.slice(1, returns.length); } return returns+text; })() +'}())'); }catch(e){ log("At line "+e.lineNumber+": "+e, logScript.logLevel.ERROR); }finally{ logScript.cvItem.setTag("prevColorIndex", logScript.ind==logScript.textColors.length-1?0:logScript.ind+1); } } </sxh>

script_customviewlog.1470684123.txt.gz · Last modified: 2016/08/08 19:22 by cdfa