Jump to content

TIP: Buffered Tracing - console.log


Dewdman42

Recommended Posts

Scripter is well known to have an annoying problem which is that quite often Trace messages will get dropped when there is other midi activity needing to be serviced at a higher priority. This can make it difficult to get complete tracing, when this all too familiar message is displayed in Scripter's console window:

...console bandwidth exceeded, thinning some traces...
 
This snippet of code provides buffered tracing. What that means is that all Trace messages will be sent first to a buffer and then later sent to the console window when Scripter is not busy. The result is that ALL tracing messages will be sent out, none will be lost.

To use this, just copy the following little snippet of code into your Scripter script.

var console = {
   maxFlush: 20,
   maxLen:   1000,
   b:[],
   log: function(msg) {this.b.push(msg)},
   flush: function() {
       var i=0;
       while(i<=this.maxFlush && this.b.length>0) {
           var line=this.b.shift();
           if(typeof line != "string") {
               line = line.toString();
           }
           var tl = line.length;
           var p = 0;
           var sub=line.substr(p,this.maxLen);
           Trace(sub);
           p += this.maxLen;
           while(p < tl) {
               sub = line.substr(p,this.maxLen);
               Trace("..." + sub);
               p += this.maxLen;
           }
           i++;
       }
   }
};
function Idle() {
   console.flush();
}
 
After that, use the following code to log messages to the console window instead of Trace:
// Intead of Trace(msg) use this:
console.log("Hello World");

// Instead of event.trace() use this:
console.log(event.toString());
 
Note that this provided console object is similar to what is found in most implementations of javascript, but is missing from Scripter. Most javascript implementations will have a console object and a log function within that console object. So this little snippet mimics that behavior.

If you still get the thinning message when using console.log(), then adjust the value of maxFlush in the code snippet. The default is set to flush out 20 messages at a time, but if that is too much for your system to handle, you can lower this value a little bit until no messages are lost.

  • Like 1
Link to comment
Share on other sites

  • 4 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...