Skip to content

Commit

Permalink
Issue 5932: FBTrace scoped logging
Browse files Browse the repository at this point in the history
  • Loading branch information
janodvarko committed Sep 21, 2012
1 parent 6a04745 commit 5144308
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions extension/content/firebug/lib/trace.js
Expand Up @@ -10,6 +10,8 @@ const Cu = Components.utils;
// ********************************************************************************************* // // ********************************************************************************************* //
// Firebug Trace - FBTrace // Firebug Trace - FBTrace


var TraceAPI = ["dump", "sysout", "setScope", "matchesNode", "time", "timeEnd"];

var scope = {}; var scope = {};


try try
Expand All @@ -22,7 +24,6 @@ catch (err)
{ {
getTracer: function(prefDomain) getTracer: function(prefDomain)
{ {
var TraceAPI = ["dump", "sysout", "setScope", "matchesNode", "time", "timeEnd"];
var TraceObj = {}; var TraceObj = {};
for (var i=0; i<TraceAPI.length; i++) for (var i=0; i<TraceAPI.length; i++)
TraceObj[TraceAPI[i]] = function() {}; TraceObj[TraceAPI[i]] = function() {};
Expand All @@ -32,8 +33,52 @@ catch (err)
} }


// ********************************************************************************************* // // ********************************************************************************************* //
// Wrapper

/**
* Wraps tracer for given option. Logs made throug the wrapper will automatially
* be checked against the option and only displayed if the option is true.
* If FBTrace console isn't installed all options are false and there is no
* additional performance penalty.
*/
function TraceWrapper(tracer, option)
{
function createMethodWrapper(method)
{
return function()
{
// Check the option before the log is passed to the tracing console.
if (tracer[option])
tracer[method].apply(tracer, arguments);
}
}

for (var i=0; i<TraceAPI.length; i++)
{
var method = TraceAPI[i];
this[method] = createMethodWrapper(method);
}
}

// ********************************************************************************************* //

var tracer = scope.traceConsoleService.getTracer("extensions.firebug");

/**
* Support for scoped logging.
*
* Example:
* FBTrace = FBTrace.to("DBG_NET");
*
* // This log will be displayed only if DBG_NET option is on
* FBTrace.sysout("net.initialiaze");
*/
tracer.to = function(option)
{
return new TraceWrapper(this, option);
}


return scope.traceConsoleService.getTracer("extensions.firebug"); return tracer;


// ********************************************************************************************* // // ********************************************************************************************* //
}); });

0 comments on commit 5144308

Please sign in to comment.