Javascript file logging

This forum is for discussing Mailtraq's Scripting features. Get help with Mailtraq scripts, templates and external ActiveX scripting.

Javascript file logging

Postby Martin Clayton » Mon Jan 31st, 2011 1:37pm

This example shows a way of setting up file logging under javascript:
Code: Select all
<@LANGUAGE=Javascript@>
<%

function logfile_init(file) {

    // return text steam object for writing or null on error
    // uses database dir unless a path is specified in 'file'

    var filepath     = '' + file,                                        // path optional
        fso          = new ActiveXObject("Scripting.FileSystemObject"),
        io_mode      = 8,                                                // 1 read, 2 write, 8 append
        can_create   = true,                                             // file creation  permission
        tristate     = 0,                                                // -2 default, -1 Unicode, 0 ASCII
        text_stream  = {};                                               // fso text stream object
       
    // locate file in \database unless a path (or drive) is specified
    if (! /[\/:]/.test(filepath)) {
        filepath = Server.DatabasePath + filepath;
    }
             
    try {
        text_stream = fso.OpenTextFile(filepath, io_mode, can_create, tristate);
    } catch (e) {
        text_stream = null;
        // Request.SetHeader("X-MTQ-DEBUG-ScriptError", "Can't handle '" + filepath + "'");
    }
   
    return text_stream;
}

var LOG = logfile_init("D:\\examples\\mtq-js_test-001.log");             // escape any backslashes
if (LOG) {
    LOG.WriteLine("-------------------------------------" + "\r\n" +
                  Server.Software + "/" + Server.Version  + "\r\n" +
                  "-------------------------------------");
   
    // do stuff
   
    LOG.Write("At the third");
    LOG.WriteLine(" beep");   
                 
    LOG.Close();
}
%>


To add timing counters you could start your script with this:
Code: Select all
var time_start = new Date(),
    elapsed;
function duration() {
    return (new Date() - time_start);
}


... and then call the duration() function at selected points:
Code: Select all
elapsed = duration();

... to return the elapsed time in milliseconds.
Cheers, Martin
User avatar
Martin Clayton
Expert User
 
Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK

Return to Mailtraq Scripting

Who is online

Users browsing this forum: No registered users and 2 guests

cron