PDA

View Full Version : Where am I?



MCSchlemme
29 May 2012, 5:50 AM
I'd like to create a utility that will help me when debugging. I use the chrome debugger, however sometimes I just want a quick console.log message.

I know I can code like this: console.log('In controller.js init function' or 'In app.js launch function'). I'd like to have a generic function that mimic the above examples and look something like this "myConsole.log(whereEverIHappenToBe)".

Is this possible?

Thanks...

burnnat
29 May 2012, 6:23 AM
Yes, it is possible. Essentially the same thing is done in Ext.Error.raise(), fetching the name of the parent class and method where the error was raised. Looking at the source for Ext.Error.raise(), you can see how this is done:


raise: function(err){
// ...
var method = this.raise.caller;

if (method) {
if (method.$name) {
err.sourceMethod = method.$name;
}
if (method.$owner) {
err.sourceClass = method.$owner.$className;
}
}

// ...
},

You can use the same technique in your log method to get the parent class and method names.

MCSchlemme
29 May 2012, 6:43 AM
Yes, it is possible. Essentially the same thing is done in Ext.Error.raise(), fetching the name of the parent class and method where the error was raised. Looking at the source for Ext.Error.raise(), you can see how this is done:


raise: function(err){
// ...
var method = this.raise.caller;

if (method) {
if (method.$name) {
err.sourceMethod = method.$name;
}
if (method.$owner) {
err.sourceClass = method.$owner.$className;
}
}

// ...
},

You can use the same technique in your log method to get the parent class and method names.

I tried coping the "
var method = this.raise.caller;" and then did a console.log(method), and it doesn't like the ".caller". Am I missing something?

Thanks...

burnnat
29 May 2012, 6:54 AM
You need to replace "this.raise" with a reference to the enclosing function. For example, here's a method named "log":


Ext.define('MyConsole', {
singleton: true,

log: function() {
var method = this.log.caller;
// ... etc ...
}
});

Ext.define('OtherClass', {
doStuff: function() {
MyConsole.log();
}
});

MCSchlemme
29 May 2012, 6:55 AM
I will give that a try.

Thank...