Action

Debug

Posted by RoyRogers, Last update over 4 years ago

debug.js function to log to external file
Captures console.log() to file in iCloud Drive.

Steps

  • script

    // debug.js
    // RV 2020-03-07 at 23:07
    
    // Function to log to external file
    // Captures console.log() to file in iCloud Drive.
    
    // load js library from iCloud drive:
    // require('debug.js'); // USE THIS at start of code!!
    
    // insert next line anywhere in code for debug: 
    // console.log(11, 'sample', 108, etc); // line # first!
    // supply line number first, and then any variables
    // separated by commas.
    
    // Copy script below to iCloud Drive:
    // as '/Drafts/Library/Scripts/debug.js'
    
    // ------------- start of file ------------------
    // debug.js
    // Captures console.log() to file:
    var captLog = '';
    var clearLog = true; // clear log on first run
    
    (function(){
        var oldLog = console.log;
        console.log = function (message) {
            captLog = Array.prototype.join.call(arguments, '\n\t');
            oldLog.apply(console, arguments);
            writeLog()
        };
    })();
    
    function writeLog() {
      // function to send log to external file
      const now = new Date();
      const tm = now.toString("yyyy-MM-dd HH:mm");
      
      let msg = 'line: ' + captLog;
      let shortMsg = msg.split('\n\t').join(': ').slice(0, 20);
      
      // For interactive log, use either (or none) of these two lines:
      // app.displayInfoMessage(shortMsg)
      alert(msg)
      
      let path = '/debug_log.txt';
      let fm = FileManager.create(false);
      log = fm.readString(path);
      if (clearLog) {
        log = tm + msg; // new log
        clearLog = false;
      } else {
        log = log + '\n' + tm + ': ' + msg; // append to log
      }
      let success = fm.writeString(path, log);
    }
    // -------------- end of file --------------
    
    // Sample. To be used anywhere in code:
    // code … 
    console.log(60, 'sample: open this script for instructions!', 108, ['string', 15, false]); 
    // code … 
    
    

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.