how to test trace.js example in meld.js aspect directory?

95 views
Skip to first unread message

Leo Cono

unread,
Jan 20, 2015, 8:18:37 AM1/20/15
to cuj...@googlegroups.com
I am running Eclipse with the NodeClipse plug-in.
When I run trace.js example in the meld.js aspect directory I do not see anything written to the console.

How do you test trace.js?

Brian Cavalier

unread,
Jan 21, 2015, 6:56:45 AM1/21/15
to cuj...@googlegroups.com
Hi Leo,

The file aspect/trace.js isn't a runnable example.  It's a module that exports an aspect that can be applied, using meld, to objects/methods/functions.  There is an example of using the trace aspect in the docs here:


There are also examples of using aspect/trace.js in its unit test, which is in test/aspect/trace-test.js.  The unit tests can be run with `npm test` from the top-level meld dir.

Leo Cono

unread,
Jan 21, 2015, 7:28:49 AM1/21/15
to cuj...@googlegroups.com
I created a simple joinpoint hello world example and I got the following error in the console window:
I got the following error on execution of this line: console.log('jp.method ' + jp.method);

hello Leo

C:\Users\leo\workspace\node-demo\trace.js:58

console.log('jp.method ' + jp.method);

^

TypeError: Cannot read property 'method' of undefined

at User.doTrace (C:\Users\leo\workspace\node-demo\trace.js:58:32)

at Object.<anonymous> (C:\Users\leo\workspace\node-demo\app.js:7:6)

at Module._compile (module.js:456:26)

at Object.Module._extensions..js (module.js:474:10)

at Module.load (module.js:356:32)

at Function.Module._load (module.js:312:12)

at Module.runMain [as _onTimeout] (module.js:497:10)

at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

 

Here is the application java script file that calls the trace.js module file:

app.js

var node_framework = require('./trace.js');

var user = new node_framework.User('Leo');

user.sayHello();

user.doTrace();

 
 
here is the javascript module file:
trace.js

var meld, joinpoint, depth, padding, simpleReporter, reporter;

meld = require('./meld.js');

joinpoint = meld.joinpoint;

function indent(depth) {

if(depth > padding.length) {

padding += padding;

}

return padding.slice(0, depth-1);

}

 

// Call stack depth tracking for the default reporter

depth = 0;

// Padding characters for indenting traces. This will get expanded as needed

padding = '................................';

simpleReporter = {

onCall: function(info) {

console.log(indent(++depth) + info.method + ' CALL ', info.args);

},

onReturn: function(info) {

console.log(indent(depth--) + info.method + ' RETURN ', info.result);

},

onThrow: function(info) {

console.log(indent(depth--) + info.method + ' THROW ' + info.exception);

}

};

var User = function(name){

this.name = name;

};

User.prototype.sayHello = function(){

console.log('hello ' + this.name);

};

User.prototype.doTrace = function(){

if(!reporter) {

reporter = simpleReporter;

}

var jp = joinpoint();

//reporter.onCall && reporter.onCall({ method: jp.method, target: jp.target, args: jp.args.slice() });

//var jp = joinpoint();

//reporter.onReturn && reporter.onReturn({ method: jp.method, target: jp.target, result: result });

//var jp = joinpoint();

//reporter.onThrow && reporter.onThrow({ method: jp.method, target: jp.target, exception: exception });

console.log('hello ' + this.name);

console.log('jp.method ' + jp.method);

console.log('jp.target ' + jp.target);

console.log('exception ' + exception);

};

exports.User = User;





On Tuesday, January 20, 2015 at 8:18:37 AM UTC-5, Leo Cono wrote:

Brian Cavalier

unread,
Jan 23, 2015, 2:04:17 PM1/23/15
to cuj...@googlegroups.com
Hi Leo,

The problem here is that you're calling meld.joinpoint() at an invalid time.  meld.joinpoint() must only be called from within an advice function which has been added using any of meld.before/after/around/etc/etc.  This is due to the nature of what meld.joinpoint() *means*.  It returns context information about the method or function which has current been intercepted.  Hence, calling it outside of that situation makes no sense, thus it returns undefined in those cases ... meaning "there is no active joinpoint".

Since no methods are being intercepted in the example, there will never be an active joinpoint.

I apologize if the documentation wasn't clear about that, but I hope that helps.

Leo Cono

unread,
Feb 11, 2015, 1:01:03 PM2/11/15
to
NodeClipse Eclipse plug-in for Node.js does not work with the embedded V8 Debugger.
Instead I accessed the JavaScript Application module exports to do reflection with Node.js apps since "this" object is empty.
Here is the code:

app.js code:
var business_object = require('./user.js');

var node_framework = require('./trace-meldaround.js');

var user = new business_object.User('Leo');


var trace = new node_framework.Trace('Hello Meld App');

trace.doTrace();

user.sayHello();

user.sayGoodBye();


user.js code:

var User = function(name){
    this.name = name;
    };

    User.prototype.sayHello = function(){
        console.log('hello ' + this.name);
    };

    User.prototype.sayGoodBye = function(){
        console.log('Good Bye ' + this.name);
    };

    exports.User = User;

 

trace-meldaround.js Code:
var meld, joinPoint;


meld = require('./meld.js');
joinPoint = meld.joinpoint;

var log4js = require('./node_framework/log4js-node-master/lib/log4js'); // include log4js

log4js.configure({ // configure to use all types in different files.
    appenders: [
        {   type: 'file',
            filename: '../logs/error.log', // specify the path where u want logs folder error.log
            category: 'error',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: 'file',
            filename: '../logs/info.log', // specify the path where u want logs folder info.log
            category: 'info',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: 'file',
            filename: '../logs/debug.log', // specify the path where u want logs folder debug.log
            category: 'debug',
            maxLogSize: 20480,
            backups: 10
        }
    ]
});

var loggerinfo = log4js.getLogger('info'); // initialize the var to use.
var loggererror = log4js.getLogger('error'); // initialize the var to use.
var loggerdebug = log4js.getLogger('debug'); // initialize the var to use.

var Trace = function(AppName){
    this.appName = AppName;
    };


    Trace.prototype.doTrace = function(){
   
        function sayMeld(jp) {           
            console.log('meldaround function called for Application : ' + Trace.constructor.AppName);

            console.log('JoinPoint Method: ' +  jp.method);
            console.log('JoinPoint Target: ' + jp.target);
            console.log('JoinPoint Args: ' + jp.args.toString());
            console.log('JoinPoint Result: ' + jp.result);
            console.log('JoinPoint Exception: ' + jp.exception);
            loggerinfo.info('Calling Method: ' + jp.method);
            loggererror.info('This is Error Logger');
            loggerdebug.info('JoinPoint Method: ' +  jp.method);
            loggerdebug.info('JoinPoint Target: ' + jp.target);
            loggerdebug.info('JoinPoint Args: ' + jp.args.toString());
            loggerdebug.info('JoinPoint Result: ' + jp.result);
            loggerdebug.info('JoinPoint Exception: ' + jp.exception);           
        }
       
       
        var appObjects = module.parent.children;       
        var methods = [];
        var objects = [];

        for (var i=0; i<appObjects.length;i++){   
            if (typeof appObjects[i] == 'object'){
                    objects.push(appObjects[i]);
                   
                    var funcObj = appObjects[i].exports;
                    if (typeof funcObj == 'object')
                    {
                        for (var key in funcObj){
                            var k =0;
                            var prototypeObj = funcObj[key].prototype;
                            methods[i] = [];
                            for (var proto in prototypeObj){                                                                                                
                                 methods[i][k] = proto;
                                 k++;
                            }                                                                               
                        }
                    }                   
                }
        }

       
        for (var j=0; j<objects.length; j++){
            var ObjectName;
            for (var key2 in objects[j].exports){               
                ObjectName = key2;
                console.log(objects[j].exports[ObjectName].prototype);
               
                for (var n=0; n<methods[j].length; n++){
                    if (ObjectName != 'Trace'){
                    meld.around(objects[j].exports[ObjectName].prototype, methods[j][n], function(joinPoint){
                        sayMeld(joinPoint);
                       
                        joinPoint.proceed();
                    });   
                    }
                }

            }                       
    }
       
    };

    exports.Trace = Trace;




Best,
Reply all
Reply to author
Forward
0 new messages