Skip to content

Commit

Permalink
minor-fix for call stack
Browse files Browse the repository at this point in the history
  • Loading branch information
baryon committed Mar 8, 2012
1 parent 79ee090 commit 082dd42
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -347,6 +347,10 @@ module.exports = function(conf) {

## History

### 0.3.1

* minor-fix for call stack

### 0.3.0

* support custom format and filter for special method
Expand Down
63 changes: 32 additions & 31 deletions lib/console.js
Expand Up @@ -22,15 +22,16 @@ module.exports = (function() {
}
return obj;
};

_config = _union(_config, arguments);

_config.format = Array.isArray(_config.format) ? _config.format : [_config.format];
_config.format = Array.isArray(_config.format) ? _config.format
: [ _config.format ];

var _isFunction = function (x) {
return Object.prototype.toString.call(x) == '[object Function]';
var _isFunction = function(x) {
return Object.prototype.toString.call(x) == '[object Function]';
};

var _log = function(title, args) {
var msg = msgformat.apply(this, args);
var data = {
Expand All @@ -39,41 +40,40 @@ module.exports = (function() {
title : title
};
data.method = data.path = data.line = data.pos = data.file = '<unknown>';
try {
throw new Error();
} catch (e) {
data.stack = e.stack.split('\n').slice(3);

var reg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/ig;
var sp = reg.exec(data.stack[0]);
if (sp && sp.length === 5) {
data.method = sp[1];
data.path = sp[2];
data.line = sp[3];
data.pos = sp[4];
var paths = data.path.split('/');
data.file = paths[paths.length - 1];
}

data.stack = (new Error()).stack.split('\n').slice(3);

var reg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/ig;
var sp = reg.exec(data.stack[0]);
if (sp && sp.length === 5) {
data.method = sp[1];
data.path = sp[2];
data.line = sp[3];
data.pos = sp[4];
var paths = data.path.split('/');
data.file = paths[paths.length - 1];
}

var format = _config.format[0];
if(_config.format.length===2 && _config.format[1][title])
if (_config.format.length === 2 && _config.format[1][title])
format = _config.format[1][title];

data.output = tim(format, data);
if (_config.filters) {
var filters = Array.isArray(_config.filters) ? _config.filters
: [_config.filters];
if (filters.length>0) {
: [ _config.filters ];
if (filters.length > 0) {
var i, len = filters.length;
if(!_isFunction(filters[len-1])){
len -= 1;
if(filters[len][title]){
if (!_isFunction(filters[len - 1])) {
len -= 1;
if (filters[len][title]) {
filters = filters[len][title]
filters = Array.isArray(filters) ? filters : [ filters ];
filters = Array.isArray(filters) ? filters
: [ filters ];
len = filters.length;
}
}

for (i = 0; i < len; i += 1) {
data.output = filters[i](data.output, data);
if (!data.output)
Expand All @@ -88,12 +88,13 @@ module.exports = (function() {
var _self = {};

var len = _config.methods.length;
if(typeof(_config.level)=='string')
_config.level=_config.methods.indexOf(_config.level);
if (typeof (_config.level) == 'string')
_config.level = _config.methods.indexOf(_config.level);
for ( var i = 0; i < len; ++i) {
var method = _config.methods[i];
if (i < _config.level)
_self[method] = (function(){});
_self[method] = (function() {
});
else
_self[method] = (function(title) {
return (function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "tracer",
"description": "A powerful and customizable logging library for node.js. support color console with timestamp, line number, method name, file name and call stack. you can set transport to file, stream, database(ex: mongodb and clouddb, simpledb). keywords: log, logger, trace ",
"homepage": "http://github.com/baryon/tracer",
"version": "0.3.0",
"version": "0.3.1",
"author": "LI Long <lilong@gmail.com>",
"dependencies": {
"dateformat": "1.0.2-1.2.3",
Expand Down

0 comments on commit 082dd42

Please sign in to comment.