User Tools

Site Tools


script_customviewlog

====== Differences ====== This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
script_customviewlog [2015/12/22 20:30]
cdfa
script_customviewlog [2016/11/18 15:24] (current)
cdfa
Line 2: Line 2:
   * 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   * 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   * Author : cdfa
-  * Link : I'll post on google+ later +  * Link : https://​plus.google.com/​100430440392224314007/​posts/​ht4aZS8LSWg 
-  * Version: 1.0.1+  * Version: 1.0.6.3
  
 ====== How to use the script ====== ====== How to use the script ======
Line 11: Line 11:
 ====== (copy/​paste) ====== ====== (copy/​paste) ======
 <sxh javascript>​ <sxh javascript>​
-var logScript = LL.getScriptByName('​logScript'​);​ +function log(){}try{return(function(){var logScript=getScriptByName('​logScript'​);​if(logScript){eval('​function ​run(){'​+logScript.getText()+'​}'​);​return run();}})();}catch(e){alert("At line "+e.lineNumber+":​ "+e);}/*logScriptEnd*/
-if(logScript!=null){ return ​eval('(function() {' + logScript.getText() + '}())'); }else{throw("logScript not found!");} //logScriptEnd+
 </​sxh>​ </​sxh>​
  
-(before this gets executed you can't use the log functionerrors won't get caught)+(before this gets executed you can't use the log function ​and errors won't get caught)
  
-log function+usage
-log(text, logLevel)+log(text, logLevel, replacements..)
  
-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)+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 please report any bugs/​feature suggestion in the google plus post
Line 26: Line 30:
 ====== Changelog ====== ====== Changelog ======
 1.0.1 1.0.1
-  ​Fixed a small bug with linenumbers+  ​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. 
 + 
 +1.0.5.1 
 +  * Small fix for the log getting too big. 
 +  * You can now run the script from an item to create a custom view with the same dimensions as the item. 
 + 
 +1.0.6 
 +  * 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 ====== 
 +  * The log won't catch syntax errors 
 +  * 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 ====== ====== logScript ======
 <sxh javascript>​ <sxh javascript>​
 +/​*-----------
 +put this in script to use logScript
  
 +function log(){}try{return(function(){var logScript=getScriptByName('​logScript'​);​if(logScript){eval('​function run(){'​+logScript.getText()+'​}'​);​return run();​}})();​}catch(e){alert("​At line "​+e.lineNumber+":​ "​+e);​}/​*logScriptEnd*/​
 +//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: { 
 +    NORMAL: { 
 +      name: "​[NORMAL]"​ 
 +      , color: "#​ffffff"​ 
 +    } 
 +    , DEBUG: { 
 +      name: "​[DEBUG]"​ 
 +      , color: "#​0000ff"​ 
 +    } 
 +    , ERROR: { 
 +      name: "​[ERROR]"​ 
 +      , color: "#​ff0000"​ 
 +    } 
 +  } 
 +  , defaultLogLevel:​ "​NORMAL"​ 
 +  , saveMode: true 
 +    // END CONFIG 
 +  , currentScript:​ getCurrentScript() 
 +  , getScript: function() { 
 +    return this.script = this.script || getScriptByName("​logScript"​);​ 
 +  } 
 +  , scrollDown: function(sv) { 
 +    sv.post(new Runnable() { 
 +      run: function() { 
 +        sv.fullScroll(ScrollView.FOCUS_DOWN);​ 
 +      } 
 +    }); 
 +  } 
 +  , getCVItem: function() { 
 +    if(!this.cvItem) { 
 +      this.cvItem = getActiveScreen().getAllItemsById(this.getScript().getTag("​cvId"​))[0];​ 
 +      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) { 
 +      var cv = this.getCV() 
 +      for(var i = 0; i < cv.getChildCount();​ i++) { 
 +        this.sv = 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 
 +  } 
 +  , createCVItem:​ function(c) { 
 +    var script = getCurrentScript();​ 
 +    var scriptId = script.getId();​ 
 +    var cv = c.addCustomView(0,​ 0); 
 +    var prop = cv.getProperties().edit();​ 
 +    script.setTag("​cvId",​ cv.getId());​ 
 +    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();​ 
 +    return cv; 
 +  } 
 +  , isTVAvailable:​ function() { 
 +    try { 
 +      logScript.getTV() 
 +      return true; 
 +    } catch(e) { 
 +      if(e.message != "​Custom view not found!"​ && e.message != "​Custom view not loaded!"​) { 
 +        throw e; 
 +      } 
 +      return false; 
 +    } 
 +  }
 }; };
  
-LL.bindClass("​java.io.File"​);​ +bindClass("​java.io.File"​);​ 
-LL.bindClass("​java.io.FileOutputStream"); +bindClass("​java.io.FileWriter"); 
-LL.bindClass("​java.io.OutputStreamWriter"); +bindClass("​java.io.PrintWriter"); 
-LL.bindClass("​android.widget.TextView"​);​ +bindClass("​java.io.BufferedWriter"​);​ 
-LL.bindClass("​android.text.method.ScrollingMovementMethod"​);​ +bindClass("​android.widget.TextView"​);​ 
-LL.bindClass("​android.text.Html"​);​ +bindClass("​android.text.method.ScrollingMovementMethod"​);​ 
-LL.bindClass("​java.io.BufferedReader"​);​ +bindClass("​android.text.Html"​);​ 
-LL.bindClass("​java.io.FileReader"​);​ +bindClass("​java.io.BufferedReader"​);​ 
-LL.bindClass("​android.widget.ScrollView"​);​ +bindClass("​java.io.FileReader"​);​ 
-LL.bindClass("​android.view.View"​);​ +bindClass("​android.widget.ScrollView"​);​ 
-LL.bindClass("​java.lang.Runnable"​);​ +bindClass("​android.view.View"​);​ 
-LL.bindClass("​android.widget.Toast");+bindClass("​java.lang.Runnable"​);​ 
 +bindClass("​android.widget.Button"​);​ 
 +bindClass("​android.widget.FrameLayout"​);​ 
 +bindClass("​android.view.Gravity");
  
-function log(text, ​channel) { +function log(text, ​logLevel /**/ ) { 
- var date new Date(); +  ​logLevel ​logLevel || logScript.logLevel[logScript.defaultLogLevel];
- 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) { +  for(var i = 2; i < arguments.length;​ i++) { 
- try +    var newText = text.replace("​{}", arguments[i]);​ 
- var = new BufferedReader(new FileReader(filePath)); +    if(newText == text) { 
- var = "";​ +      break; 
- var l+    } else { 
- while ​((r.readLine()) !nulls += (l + "\n"); +      text = newText; 
- return s; +    } 
- catch (e{ +  } 
- return ​"";​ + 
- }+  var date = new Date(); 
 +  var logText = "<​font color=#​add8e6>"​ + date.getDate() + "​-"​ + (date.getMonth(+ 1) + 
 +    "</​font>"​ + " <font color=#​ffff00>"​ + date.toTimeString().slice(0,​ 8) + 
 +    "</​font>"​ + " <font color=#​00ff00>"​ + logScript.currentScript.getName() + "</​font>"​ + 
 +    " <font color="​ + logLevel.color + ">"​ + logLevel.name + "</​font>"​ + ": " + 
 +    " <font color="​ + logScript.textColors[logScript.ind] + ">"​ + text + 
 +    "</​font>"​
 + 
 +  if(logScript.isTVAvailable()) { 
 +    ​var tv logScript.getTV() 
 +    var tvOldLength = tv.length() 
 +    tv.append(Html.fromHtml(logText).append("\n"))
 +    ​logScript.scrollDown(logScript.getSV())
 +    if(logScript.saveMode) { 
 +      var script = logScript.getScript(
 +      var prevLogInd ​script.getTag("​prevLogInd"​|| -1 
 +      var prevLog = JSON.parse(script.getTag("​log"​ + prevLogInd)
 +      var tvNewLength ​tv.length() 
 +      if(prevLog && prevLog[1] ​== tvOldLength) { 
 +        prevLog[1] = tv.length() 
 +        script.setTag("log" ​+ prevLogInd, JSON.stringify(prevLog)); 
 +      else { 
 +        prevLogInd++ 
 +        script.setTag("​log"​ + prevLogInd, JSON.stringify([tvOldLength,​ tvNewLength])
 +        ​script.setTag("prevLogInd", prevLogInd) 
 +      } 
 +    } 
 +  } else { 
 +    logScript.logText += logText
 +  }
 } }
  
-function ​scrollDown(sv){ +function ​cleanEval(text) { 
- sv.post(new Runnable() { +  eval('function ​execute() {' + text + '​}'​); 
- run: ​function() { +  ​execute();
- sv.fullScroll(ScrollView.FOCUS_DOWN); +
- +
- });+
 } }
  
-if (LL.getCurrentScript().getName() == "logScript") { +return(function() 
- var script ​LL.getCurrentScript();+  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 "";​ 
 +    } 
 +  }
  
- try { +  if(logScript.currentScript.getName() == "​logScript"​) { 
- typeof item; +    ​try { 
- } catch (e) { +      typeof item; 
- // in create +    } catch(e) { 
- logScript.cv = item; +      // in create 
- var context = LL.getContext();​ +      ​var ​cv = item; 
- logScript.v ​= new TextView(context);​ +      var context = getActiveScreen().getContext();​ 
- var sv = new ScrollView(context);​ +      var tv = new TextView(context);​ 
- sv.addView(logScript.v);+      var sv = new ScrollView(context);​ 
 +      sv.addView(tv); 
 +      sv.setTag("​sv"​);
  
- logScript.v.setText(Html.fromHtml(read(logScript.logFilePath)));​ +      tv.setMovementMethod(new ScrollingMovementMethod());​ 
- logScript.v.setMovementMethod(new ScrollingMovementMethod());​ +      tv.setTextColor(0xffffffff)
- 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(); +      cv.setVerticalGrab(true); 
- var src = e.getSource(); +      tv.setVerticalScrollBarEnabled(true); 
- if (src == "MENU_APP") { + 
- //in container +      ​if(!logScript.lineWrapping) { 
- var scriptId ​script.getId(); +        tv.setHorizontallyScrolling(true);​ 
- var e = LL.getEvent();​ +        cv.setHorizontalGrab(true);​ 
- var = e.getContainer(); +      } 
- var cv = c.addCustomView(e.getTouchX(),​ e.getTouchY());​ + 
- var prop cv.getProperties().edit(); +      var button ​new Button(context);​ 
- prop.getBox("​i.box"​).setColor("​c", "​n",​ logScript.bgColor); +      button.setText("clear log"); 
- prop.setString("v.onCreate"​scriptId); +      button.setOnClickListener(new View.OnClickListener() { 
- prop.setEventHandler("i.resumed"​EventHandler.RUN_SCRIPT, scriptId); +        ​onClick:​ function(v) { 
- prop.commit(); +          new File(logScript.logFilePath).delete();​ 
- script.setTag("cvId", cv.getId()); +          try { 
-else +            logScript.getTV();​ 
- var cv LL.getItemById(script.getTag("​cvId")); +            tv.setText(""​);​ 
- if (cv == null) { +            logScript.getScript().setTag("​prevLogInd",​ null) 
- script.setTag("​cvId"​null); +          } catch(e) {} 
- } else { +        } 
- cv.getView().getChildAt(0).setText(Html.fromHtml(read(logScript.logFilePath)));​ +      }); 
- } +      button.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,​ FrameLayout.LayoutParams.WRAP_CONTENT,​ Gravity.TOP | Gravity.RIGHT));​ 
- } + 
-else +      ​var layout ​new FrameLayout(context);​ 
- //in eval() +      layout.addView(sv); 
- logScript.cv = LL.getItemById(LL.getScriptByName("logScript").getTag("​cvId"​)); +      ​layout.addView(button);​ 
- if (logScript.cv == null) throw ("No custom view found!") + 
- logScript.ind ​parseInt(logScript.cv.getTag("​prevColorIndex"​|| "​0"​); +      logScript.scrollDown(sv);​ 
- logScript.htmlText ​= ""​; +      return layout; 
- logScript.sv = logScript.cv.getView(); +    } 
- if(logScript.sv == null){ + 
- Toast.makeText(LL.getContext(), "custom view not loaded", ​Toast.LENGTH_SHORT); +    ​var e = getEvent();​ 
- function log(textchannel) {} +    var src = e.getSource(); 
- }else{ +    ​switch(src) { 
- logScript.= logScript.sv.getChildAt(0); +      case "​MENU_APP":​ 
- } +        //in container 
- logScript.text = LL.getCurrentScript().getText();​ +        ​var cv = logScript.createCVItem(e.getContainer()) 
- logScript.returns = "";​ +        cv.setPosition(e.getTouchX(),​ e.getTouchY()) 
- for(var i=0;i<logScript.text.substr(0, ​logScript.text.search("​var logScript"​)).split("​\n"​).length;​ i++){ +        break
-   logScript.returns += "​\n";​ +      case "​MENU_ITEM":​ 
-+        ​var it e.getItem()
- logScript.text = logScript.returns + logScript.text.substr(logScript.text.search("​logScriptEnd"​) + 12); +        var c = it.getParent(); 
- try +        var cv = logScript.createCVItem(c); 
- return eval('(function() {' ​logScript.text + '}())'); +        cv.setSize(it.getWidth()it.getHeight()); 
- } catch (e) { +        cv.setPosition(it.getPositionX()it.getPositionY()
- log("​At line " + e.lineNumber + ": " + e, "ERROR"); +        c.removeItem(it); 
- } finally { +        ​break;​ 
- logScript.cv.setTag("​prevColorIndex",​ logScript.ind == logScript.textColors.length - +      case "I_RESUMED"
- 1 ? 0 : logScript.ind + 1); +        // in resume 
- var out = null; +        var tv = logScript.getTV()
- try { +        try { 
- out = new FileOutputStream(logScript.logFilePath,​ true)+          var logFile = new File(logScript.logFilePath); 
- var osw = new OutputStreamWriter(out); +        catch(e) ​{} 
- osw.write(logScript.htmlText); + 
- } catch (e) { +        if(logFile !== null && tv.getText().length() == 0 || logFile.lastModified() > logScript.getScript().getTag("​lastLoaded"​)) ​
- log(e"ERROR"); +          var savedLogText = read(logScript.logFilePath) 
- } finally { + 
- if (osw != nullosw.flush();​ +          if(savedLogText.length > 30000) { 
- if (out != null) out.close(); +            ​savedLogText = savedLogText.substring(savedLogText.length - 20000savedLogText.length
- +            ​savedLogText = savedLogText.substring(savedLogText.indexOf("<​br>"​), savedLogText.length);​ 
-+          } 
-}+          tv.setText(Html.fromHtml(savedLogText));​ 
 +          try { 
 +            var pw = new PrintWriter(new File(logScript.logFilePath))
 +            pw.print(""​); 
 +            ​pw.write(savedLogText);​ 
 +          catch(e) {finally ​
 +            if(pwpw.close(); 
 +          } 
 +          ​logScript.getScript().setTag("lastLoaded", new Date().getTime()) 
 +        } 
 +        logScript.scrollDown(logScript.getSV()); 
 +        break; 
 +      case "I_PAUSED": 
 +        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.getTV().getEditableText(); 
 +            if(fullText && fullText.length() !0 && fullText.length() >unsavedLog[1]) { 
 +              var unsavedLogTextString = Html.toHtml(fullText.subSequence(unsavedLog[0],​ unsavedLog[1])); 
 +              bfWriter.append(unsavedLogTextString.substring(unsavedLogTextString.indexOf('>'​) + 1unsavedLogTextString.lastIndexOf('<'​)));​ 
 +            } 
 +            script.setTag("log" ​+ inull) 
 +            i++
 +          } 
 +          script.setTag("​prevLogInd"​null); 
 +        } catch(e) {} finally { 
 +          if(bfWriter) bfWriter.close();​ 
 +        } 
 +        break; 
 +    } 
 +  ​} else { 
 +    //in eval() 
 +    ​logScript.ind parseInt(logScript.getScript().getTag("​prevColorIndex"​) || "0"); 
 +    var tvAvailable = logScript.isTVAvailable() 
 +    ​if(!tvAvailable) 
 +      ​logScript.logText = "";​ 
 + 
 +    var text = logScript.currentScript.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);​ 
 +    } 
 +    var counter = 0; 
 +    for(var i = 0; i < text.length;​ i++) { 
 +      if(text.charAt(i) == "​{"​) 
 +        counter++; 
 +      else if(text.charAt(i) == "​}"​) 
 +        counter--;​ 
 + 
 +      if(counter < 0) { 
 +        break; 
 +      } 
 +    } 
 +    ​text = returns ​text.slice(0, i)
 + 
 +    try { 
 +      return cleanEval(text); 
 +    } catch(e) { 
 +      log("​At line " + e.lineNumber + ": " + e, logScript.logLevel.ERROR); 
 +    } finally { 
 +      logScript.getScript().setTag("​prevColorIndex",​ logScript.ind == logScript.textColors.length - 1 ? 0 : logScript.ind + 1); 
 +    } 
 + 
 +    if(!tvAvailable) { 
 +      ​try { 
 +        var bfWriter ​= new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); 
 +        ​bfWriter.append(logScript.logText
 +      } catch(e) { 
 +        alert("At line " + e.lineNumber + "" ​+ e); 
 +      } finally { 
 +        if(bfWriterbfWriter.close(); 
 +      
 +    
 +  } 
 +})();
 </​sxh>​ </​sxh>​
script_customviewlog.1450816210.txt.gz · Last modified: 2015/12/22 20:30 by cdfa