====== Differences ====== This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
script_customviewlog [2016/11/12 12:13] cdfa |
script_customviewlog [2016/11/18 15:24] (current) cdfa |
||
|---|---|---|---|
| Line 3: | Line 3: | ||
| * Author : cdfa | * Author : cdfa | ||
| * Link : https://plus.google.com/100430440392224314007/posts/ht4aZS8LSWg | * Link : https://plus.google.com/100430440392224314007/posts/ht4aZS8LSWg | ||
| - | * Version: 1.0.6 | + | * Version: 1.0.6.3 |
| ====== How to use the script ====== | ====== How to use the script ====== | ||
| Line 54: | Line 54: | ||
| 1.0.6 | 1.0.6 | ||
| * Your code isn't executed in eval anymore! | * Your code isn't executed in eval anymore! | ||
| + | |||
| + | 1.0.6.1 | ||
| + | * Fixed a bug with the script name | ||
| + | |||
| + | 1.0.6.2 | ||
| + | * Fixed a few bugs related to unreachable logfile | ||
| + | |||
| + | 1.0.6.3 | ||
| + | * Fixed date | ||
| ====== Things to keep in mind ====== | ====== Things to keep in mind ====== | ||
| Line 67: | Line 76: | ||
| //CONFIG | //CONFIG | ||
| var logScript = { | var logScript = { | ||
| - | logFilePath: "/storage/sdcard0/LightningLauncher/script/logScriptLog.html", | + | logFilePath: "/storage/sdcard0/LightningLauncher/script/logScriptLog.html" |
| - | lineWrapping: true // might require app restart | + | , lineWrapping: true // might require app restart |
| - | , | + | , bgColor: 0xff191919 |
| - | bgColor: 0xff191919, | + | , textColors: ["#d0d0d0", "#ee7600"] // the script iterates through these colors with each new "session" |
| - | textColors: ["#d0d0d0", "#ee7600"] // the script iterates through these colors with each new "session" | + | , logLevel: { |
| - | , | + | |
| - | logLevel: { | + | |
| NORMAL: { | NORMAL: { | ||
| - | name: "[NORMAL]", | + | name: "[NORMAL]" |
| - | color: "#ffffff" | + | , color: "#ffffff" |
| - | }, | + | |
| - | DEBUG: { | + | |
| - | name: "[DEBUG]", | + | |
| - | color: "#0000ff" | + | |
| - | }, | + | |
| - | ERROR: { | + | |
| - | name: "[ERROR]", | + | |
| - | color: "#ff0000" | + | |
| } | } | ||
| - | }, | + | , DEBUG: { |
| - | defaultLogLevel: "NORMAL", | + | name: "[DEBUG]" |
| - | saveMode: true | + | , color: "#0000ff" |
| + | } | ||
| + | , ERROR: { | ||
| + | name: "[ERROR]" | ||
| + | , color: "#ff0000" | ||
| + | } | ||
| + | } | ||
| + | , defaultLogLevel: "NORMAL" | ||
| + | , saveMode: true | ||
| // END CONFIG | // END CONFIG | ||
| - | , | + | , currentScript: getCurrentScript() |
| - | getScript: function() { | + | , getScript: function() { |
| return this.script = this.script || getScriptByName("logScript"); | return this.script = this.script || getScriptByName("logScript"); | ||
| - | }, | + | } |
| - | scrollDown: function(sv) { | + | , scrollDown: function(sv) { |
| sv.post(new Runnable() { | sv.post(new Runnable() { | ||
| run: function() { | run: function() { | ||
| Line 100: | Line 107: | ||
| } | } | ||
| }); | }); | ||
| - | }, | + | } |
| - | getCVItem: function() { | + | , getCVItem: function() { |
| - | if (!this.cvItem) { | + | if(!this.cvItem) { |
| this.cvItem = getActiveScreen().getAllItemsById(this.getScript().getTag("cvId"))[0]; | this.cvItem = getActiveScreen().getAllItemsById(this.getScript().getTag("cvId"))[0]; | ||
| - | if (!this.cvItem) throw new Error("Custom view not found!") | + | if(!this.cvItem) throw new Error("Custom view not found!") |
| } | } | ||
| return this.cvItem | return this.cvItem | ||
| - | }, | + | } |
| - | getCV: function() { | + | , getCV: function() { |
| - | if (!this.cv) { | + | if(!this.cv) { |
| this.cv = this.getCVItem().getView() | this.cv = this.getCVItem().getView() | ||
| - | if (!this.cv) throw new Error("Custom view not loaded!"); | + | if(!this.cv) throw new Error("Custom view not loaded!"); |
| } | } | ||
| return this.cv | return this.cv | ||
| - | }, | + | } |
| - | getSV: function() { | + | , getSV: function() { |
| - | if (!this.sv) { | + | if(!this.sv) { |
| var cv = this.getCV() | var cv = this.getCV() | ||
| - | for (var i = 0; i < cv.getChildCount(); i++) { | + | for(var i = 0; i < cv.getChildCount(); i++) { |
| this.sv = cv.getChildAt(i); | this.sv = cv.getChildAt(i); | ||
| - | if (this.sv.getTag() == "sv") | + | if(this.sv.getTag() == "sv") |
| break; | break; | ||
| } | } | ||
| } | } | ||
| return this.sv | return this.sv | ||
| - | }, | + | } |
| - | getTV: function() { | + | , getTV: function() { |
| - | if (!this.tv) | + | if(!this.tv) |
| this.tv = this.getSV().getChildAt(0); | this.tv = this.getSV().getChildAt(0); | ||
| return this.tv | return this.tv | ||
| - | }, | + | } |
| - | createCVItem: function(c) { | + | , createCVItem: function(c) { |
| var script = getCurrentScript(); | var script = getCurrentScript(); | ||
| var scriptId = script.getId(); | var scriptId = script.getId(); | ||
| Line 143: | Line 150: | ||
| prop.commit(); | prop.commit(); | ||
| return cv; | return cv; | ||
| - | }, | + | } |
| - | isTVAvailable: function() { | + | , isTVAvailable: function() { |
| try { | try { | ||
| logScript.getTV() | logScript.getTV() | ||
| return true; | return true; | ||
| - | } catch (e) { | + | } catch(e) { |
| - | if (e.message != "Custom view not found!" && e.message != "Custom view not loaded!") { | + | if(e.message != "Custom view not found!" && e.message != "Custom view not loaded!") { |
| throw e; | throw e; | ||
| } | } | ||
| Line 176: | Line 183: | ||
| logLevel = logLevel || logScript.logLevel[logScript.defaultLogLevel]; | logLevel = logLevel || logScript.logLevel[logScript.defaultLogLevel]; | ||
| - | for (var i = 2; i < arguments.length; i++) { | + | for(var i = 2; i < arguments.length; i++) { |
| var newText = text.replace("{}", arguments[i]); | var newText = text.replace("{}", arguments[i]); | ||
| - | if (newText == text) { | + | if(newText == text) { |
| break; | break; | ||
| } else { | } else { | ||
| Line 186: | Line 193: | ||
| var date = new Date(); | var date = new Date(); | ||
| - | var month = date.getMonth() + 1; | + | var logText = "<font color=#add8e6>" + date.getDate() + "-" + (date.getMonth() + 1) + |
| - | var scriptName = getCurrentScript().getName(); | + | |
| - | var logText = "<font color=#add8e6>" + date.getDate() + "-" + month + | + | |
| "</font>" + " <font color=#ffff00>" + date.toTimeString().slice(0, 8) + | "</font>" + " <font color=#ffff00>" + date.toTimeString().slice(0, 8) + | ||
| - | "</font>" + " <font color=#00ff00>" + scriptName + "</font>" + | + | "</font>" + " <font color=#00ff00>" + logScript.currentScript.getName() + "</font>" + |
| " <font color=" + logLevel.color + ">" + logLevel.name + "</font>" + ": " + | " <font color=" + logLevel.color + ">" + logLevel.name + "</font>" + ": " + | ||
| " <font color=" + logScript.textColors[logScript.ind] + ">" + text + | " <font color=" + logScript.textColors[logScript.ind] + ">" + text + | ||
| "</font>"; | "</font>"; | ||
| - | if (logScript.isTVAvailable()) { | + | if(logScript.isTVAvailable()) { |
| var tv = logScript.getTV() | var tv = logScript.getTV() | ||
| var tvOldLength = tv.length() | var tvOldLength = tv.length() | ||
| tv.append(Html.fromHtml(logText).append("\n")); | tv.append(Html.fromHtml(logText).append("\n")); | ||
| logScript.scrollDown(logScript.getSV()); | logScript.scrollDown(logScript.getSV()); | ||
| - | if (logScript.saveMode) { | + | if(logScript.saveMode) { |
| var script = logScript.getScript() | var script = logScript.getScript() | ||
| var prevLogInd = script.getTag("prevLogInd") || -1 | var prevLogInd = script.getTag("prevLogInd") || -1 | ||
| var prevLog = JSON.parse(script.getTag("log" + prevLogInd)) | var prevLog = JSON.parse(script.getTag("log" + prevLogInd)) | ||
| var tvNewLength = tv.length() | var tvNewLength = tv.length() | ||
| - | if (prevLog && prevLog[1] == tvOldLength) { | + | if(prevLog && prevLog[1] == tvOldLength) { |
| prevLog[1] = tv.length() | prevLog[1] = tv.length() | ||
| script.setTag("log" + prevLogInd, JSON.stringify(prevLog)); | script.setTag("log" + prevLogInd, JSON.stringify(prevLog)); | ||
| Line 224: | Line 229: | ||
| } | } | ||
| - | return (function() { | + | return(function() { |
| - | var script = getCurrentScript(); | + | |
| function read(filePath) { | function read(filePath) { | ||
| try { | try { | ||
| Line 232: | Line 235: | ||
| var s = ""; | var s = ""; | ||
| var l; | var l; | ||
| - | while ((l = r.readLine()) != null) s += (l + "\n"); | + | while((l = r.readLine()) != null) s += (l + "\n"); |
| return s; | return s; | ||
| - | } catch (e) { | + | } catch(e) { |
| return ""; | return ""; | ||
| } | } | ||
| } | } | ||
| - | if (script.getName() == "logScript") { | + | if(logScript.currentScript.getName() == "logScript") { |
| try { | try { | ||
| typeof item; | typeof item; | ||
| - | } catch (e) { | + | } catch(e) { |
| // in create | // in create | ||
| var cv = item; | var cv = item; | ||
| Line 257: | Line 260: | ||
| tv.setVerticalScrollBarEnabled(true); | tv.setVerticalScrollBarEnabled(true); | ||
| - | if (!logScript.lineWrapping) { | + | if(!logScript.lineWrapping) { |
| tv.setHorizontallyScrolling(true); | tv.setHorizontallyScrolling(true); | ||
| cv.setHorizontalGrab(true); | cv.setHorizontalGrab(true); | ||
| Line 271: | Line 274: | ||
| tv.setText(""); | tv.setText(""); | ||
| logScript.getScript().setTag("prevLogInd", null) | logScript.getScript().setTag("prevLogInd", null) | ||
| - | } catch (e) {} | + | } catch(e) {} |
| } | } | ||
| }); | }); | ||
| Line 286: | Line 289: | ||
| var e = getEvent(); | var e = getEvent(); | ||
| var src = e.getSource(); | var src = e.getSource(); | ||
| - | switch (src) { | + | switch(src) { |
| case "MENU_APP": | case "MENU_APP": | ||
| //in container | //in container | ||
| Line 302: | Line 305: | ||
| case "I_RESUMED": | case "I_RESUMED": | ||
| // in resume | // in resume | ||
| - | var tv = logScript.getTV() | + | var tv = logScript.getTV(); |
| - | if (tv.getText().length() == 0 || new File(logScript.logFilePath).lastModified() > logScript.getScript().getTag("lastLoaded")) { | + | try { |
| + | var logFile = new File(logScript.logFilePath); | ||
| + | } catch(e) {} | ||
| + | |||
| + | if(logFile !== null && tv.getText().length() == 0 || logFile.lastModified() > logScript.getScript().getTag("lastLoaded")) { | ||
| var savedLogText = read(logScript.logFilePath) | var savedLogText = read(logScript.logFilePath) | ||
| - | if (savedLogText.length > 30000) { | + | if(savedLogText.length > 30000) { |
| savedLogText = savedLogText.substring(savedLogText.length - 20000, savedLogText.length) | savedLogText = savedLogText.substring(savedLogText.length - 20000, savedLogText.length) | ||
| savedLogText = savedLogText.substring(savedLogText.indexOf("<br>"), savedLogText.length); | savedLogText = savedLogText.substring(savedLogText.indexOf("<br>"), savedLogText.length); | ||
| Line 315: | Line 322: | ||
| pw.print(""); | pw.print(""); | ||
| pw.write(savedLogText); | pw.write(savedLogText); | ||
| - | } catch (e) { | + | } catch(e) {} finally { |
| - | alert("At line " + e.lineNumber + ": " + e); | + | if(pw) pw.close(); |
| - | } finally { | + | |
| - | pw.close(); | + | |
| } | } | ||
| logScript.getScript().setTag("lastLoaded", new Date().getTime()) | logScript.getScript().setTag("lastLoaded", new Date().getTime()) | ||
| Line 330: | Line 335: | ||
| var i = 0; | var i = 0; | ||
| var script = logScript.getScript() | var script = logScript.getScript() | ||
| - | while (unsavedLog = script.getTag("log" + i)) { | + | while(unsavedLog = script.getTag("log" + i)) { |
| var unsavedLog = JSON.parse(unsavedLog) | var unsavedLog = JSON.parse(unsavedLog) | ||
| var fullText = logScript.getTV().getEditableText(); | var fullText = logScript.getTV().getEditableText(); | ||
| - | if (fullText && fullText.length() != 0 && fullText.length() >= unsavedLog[1]) { | + | if(fullText && fullText.length() != 0 && fullText.length() >= unsavedLog[1]) { |
| var unsavedLogTextString = Html.toHtml(fullText.subSequence(unsavedLog[0], unsavedLog[1])); | var unsavedLogTextString = Html.toHtml(fullText.subSequence(unsavedLog[0], unsavedLog[1])); | ||
| bfWriter.append(unsavedLogTextString.substring(unsavedLogTextString.indexOf('>') + 1, unsavedLogTextString.lastIndexOf('<'))); | bfWriter.append(unsavedLogTextString.substring(unsavedLogTextString.indexOf('>') + 1, unsavedLogTextString.lastIndexOf('<'))); | ||
| Line 341: | Line 346: | ||
| } | } | ||
| script.setTag("prevLogInd", null); | script.setTag("prevLogInd", null); | ||
| - | } catch (e) { | + | } catch(e) {} finally { |
| - | alert("At line " + e.lineNumber + ": " + e); | + | if(bfWriter) bfWriter.close(); |
| - | } finally { | + | |
| - | bfWriter.close(); | + | |
| } | } | ||
| break; | break; | ||
| Line 352: | Line 355: | ||
| logScript.ind = parseInt(logScript.getScript().getTag("prevColorIndex") || "0"); | logScript.ind = parseInt(logScript.getScript().getTag("prevColorIndex") || "0"); | ||
| var tvAvailable = logScript.isTVAvailable() | var tvAvailable = logScript.isTVAvailable() | ||
| - | if (!tvAvailable) | + | if(!tvAvailable) |
| logScript.logText = ""; | logScript.logText = ""; | ||
| - | var text = script.getText(); | + | var text = logScript.currentScript.getText(); |
| var returns = ""; | var returns = ""; | ||
| - | for (var i = 0; i < text.substr(0, text.search("var logScript")).split("\n").length; i++) { | + | for(var i = 0; i < text.substr(0, text.search("var logScript")).split("\n").length; i++) { |
| returns += "\n"; | returns += "\n"; | ||
| } | } | ||
| text = text.substr(text.search("logScriptEnd") + 12); | text = text.substr(text.search("logScriptEnd") + 12); | ||
| - | if (text.charAt(0) == "*") { | + | if(text.charAt(0) == "*") { |
| text = text.slice(2, text.length); | text = text.slice(2, text.length); | ||
| returns = returns.slice(1, returns.length); | returns = returns.slice(1, returns.length); | ||
| } | } | ||
| var counter = 0; | var counter = 0; | ||
| - | for (var i = 0; i < text.length; i++) { | + | for(var i = 0; i < text.length; i++) { |
| - | if (text.charAt(i) == "{") | + | if(text.charAt(i) == "{") |
| counter++; | counter++; | ||
| - | else if (text.charAt(i) == "}") | + | else if(text.charAt(i) == "}") |
| counter--; | counter--; | ||
| - | if (counter < 0) { | + | if(counter < 0) { |
| break; | break; | ||
| } | } | ||
| Line 380: | Line 383: | ||
| try { | try { | ||
| return cleanEval(text); | return cleanEval(text); | ||
| - | } catch (e) { | + | } catch(e) { |
| log("At line " + e.lineNumber + ": " + e, logScript.logLevel.ERROR); | log("At line " + e.lineNumber + ": " + e, logScript.logLevel.ERROR); | ||
| } finally { | } finally { | ||
| Line 386: | Line 389: | ||
| } | } | ||
| - | if (!tvAvailable) { | + | if(!tvAvailable) { |
| try { | try { | ||
| var bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); | var bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); | ||
| bfWriter.append(logScript.logText) | bfWriter.append(logScript.logText) | ||
| - | } catch (e) { | + | } catch(e) { |
| alert("At line " + e.lineNumber + ": " + e); | alert("At line " + e.lineNumber + ": " + e); | ||
| } finally { | } finally { | ||
| - | bfWriter.close(); | + | if(bfWriter) bfWriter.close(); |
| } | } | ||
| } | } | ||