Permalink
Browse files

Merge pull request #4 from kilianc/master

Add option for right alignment callsite info
  • Loading branch information...
2 parents 0de89b4 + 5fa78e5 commit 8a54bf706d001f1eef166b99391e952e4be10982 @rauchg rauchg committed May 5, 2012
Showing with 159 additions and 34 deletions.
  1. +64 −11 README.md
  2. +43 −8 console-trace.js
  3. +52 −15 test.js
View
@@ -1,4 +1,3 @@
-
# console-trace
Extends the native Node.JS `console` object to prefix logging functions
@@ -7,31 +6,80 @@ with the [CallSite](http://github.com/visionmedia/callsite) information.
To read more about runtime stack trace introspection you can refer to [this
article](http://www.devthought.com/2011/12/22/a-string-is-not-an-error/#beyond).
-![](http://f.cl.ly/items/0r3t3i0P0D1w2K242p1U/Image%202012.03.26%2011:42:35%20AM.png)
+![](http://f.cl.ly/items/1T2K0H0i2H2J0C3q3H2u/console-trace.png)
+
+## Installation
+
+ $ npm install console-trace
+
+### Syntax:
+
+```javascript
+require('console-trace')([options])
+```
+
+### Available Options:
+
+* __always__ - (`Boolean`: defaults to false) always print the callsite info even without accessing methods from the `t` or `traced` getters.
+* __cwd__ - (`String`: defaults to `process.cwd()`) the path that will be stripped from the callsite info
+* __colors__ - (`Boolean|Object`: defaults to true) terminal colors support flag or a custom color object
+* __right__ - (`Boolean`: defaults to false) callsite alignment flag, when true prints infos on the right
-## How to use
+### Examples:
-```js
+```javascript
require('console-trace')
```
You can add the `t` or `traced` getter to your calls to obtain a stacktrace:
-```js
+```javascript
console.t.log('a');
console.traced.log('a');
```
You can also make every console call trace:
-```js
-console.traceAlways = true;
+```javascript
+require('console-trace')({
+ always: true,
+})
+
+...
+
+console.log('a'); // tracing
+console.error('a'); // tracing
```
-To disable colors completely, set the following:
+You can align the callsite infos to the right
-```js
-console.traceColors = false;
+```javascript
+require('console-trace')({
+ always: true,
+ right: true
+})
+
+...
+
+console.log('a'); // tracing right
+console.error('a'); // tracing right
+```
+
+You can change defaults colors too
+
+```javascript
+require('./console-trace')({
+ always: true,
+ colors: {
+ warn: '35',
+ info: '32'
+ }
+})
+
+...
+
+console.warn('a'); // magenta
+console.info('a'); // green
```
To customize the string that's prefixed to the calls, override the
@@ -42,6 +90,11 @@ To customize the string that's prefixed to the calls, override the
If you have more sophisticated logging needs, or don't wish to extend
`console`, I suggest you look at [tracer](https://github.com/baryon/tracer).
+## Credits
+
+ * [Guillermo Rauch](https://github.com/guille)
+ * [Kilian Ciuffolo](https://github.com/kilianc)
+
## License
(The MIT License)
@@ -65,4 +118,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -5,16 +5,37 @@
var callsite = require('callsite')
, tty = require('tty')
+ , isatty = Boolean(tty.isatty() && process.stdout.getWindowSize)
+ , defaultColors = { log: '90', error: '91', warn: '93', info: '96' }
+
+console.traceOptions = Object.create(null);
+console.traceOptions.cwd = process.cwd() + '/';
+console.traceOptions.colors = true;
+
+/**
+ * Store custom options
+ *
+ * @param {Object} options
+ * @api public
+ */
+
+module.exports = function (options) {
+ if (options) {
+ options.cwd = options.cwd || console.traceOptions.cwd;
+ console.traceOptions = options;
+ }
+}
/**
* Overrides the console methods.
*/
-;['error', 'log', 'info', 'warn'].forEach(function (name) {
+;['error', 'log', 'info', 'warn', 'trace'].forEach(function (name) {
var fn = console[name];
console[name] = function () {
- if (console._trace || console.traceAlways) {
- arguments[0] = console.traceFormat(__stack[1], name) + arguments[0];
+ if (console._trace || console.traceOptions.always) {
+ var pad = (arguments[0] && !console.traceOptions.right || !isatty ? ' ' : '');
+ arguments[0] = console.traceFormat(__stack[1], name) + pad + arguments[0];
}
console._trace = false;
return fn.apply(this, arguments);
@@ -30,14 +51,28 @@ var callsite = require('callsite')
*/
console.traceFormat = function (call, method) {
- var basename = call.getFileName().replace(process.cwd() + '/', '')
- , str = '[' + basename + ':' + call.getLineNumber() + '] '
+ var basename = call.getFileName().replace(console.traceOptions.cwd, '')
+ , str = '[' + basename + ':' + call.getLineNumber() + ']'
+ , color = '99'
- if (false === console.traceColors || tty.isatty()) {
- return '\033[' + ('error' == method ? '91' : '90') + 'm' + str + '\033[39m';
- } else {
+ if (!isatty) {
return str;
}
+
+ if (console.traceOptions.colors !== false) {
+ color = console.traceOptions.colors === true ? defaultColors[method] : console.traceOptions.colors[method];
+ }
+
+ if (console.traceOptions.right) {
+ var rowWidth = process.stdout.getWindowSize()[0];
+ return '\033[s' + // save current position
+ '\033[' + rowWidth + 'D' + // move to the start of the line
+ '\033[' + (rowWidth - str.length) + 'C' + // align right
+ '\033[' + color + 'm' + str + '\033[39m' +
+ '\033[u'; // restore current position
+ } else {
+ return '\033[' + color + 'm' + str + '\033[39m';
+ }
}
/**
View
@@ -1,25 +1,62 @@
-
require('./console-trace')
+require('./console-trace')() // should work even if I require it twice
+
+process.stdout.write('\n');
+
+;['error', 'log', 'info', 'warn'].forEach(function (name) {
+ process.stdout.write(' ');
+ console[name]('regular console.%s, no clue where it came from', name);
+});
+
+process.stdout.write('\n---------------------------------------------------------\n\n');
+
+;['error', 'log', 'info', 'warn'].forEach(function (name) {
+ process.stdout.write(' ');
+ console.traced[name]('this is a traced console.%s', name);
+});
+
+process.stdout.write('\n---------------------------------------------------------\n\n');
+
+console.traceOptions.colors = false;
+
+;['error', 'log', 'info', 'warn'].forEach(function (name) {
+ process.stdout.write(' ');
+ console.traced[name]('this is an uncolored traced console.%s', name);
+});
-// visual pad
-console.log('');
+process.stdout.write('\n---------------------------------------------------------\n\n');
-console.error(' regular console.error, no clue where it came from');
+console.traceOptions.right = true;
+console.traceOptions.colors = true;
-// visual pad
-process.stdout.write(' ');
+;['error', 'log', 'info', 'warn'].forEach(function (name) {
+ process.stdout.write(' ');
+ console.traced[name]('this is a colored right aligned traced console.%s', name);
+});
-console.traced.error('this is a traced console.error');
+process.stdout.write('\n---------------------------------------------------------\n\n');
-// visual pad
-console.log('');
+console.traceOptions.always = true;
+console.traceOptions.colors = false;
-console.log(' regular console.log, no clue where it came from');
+;['error', 'log', 'info', 'warn'].forEach(function (name) {
+ process.stdout.write(' ');
+ console[name]('this is an uncolored right aligned traced console.%s', name);
+});
-// visual pad
-process.stdout.write(' ');
+process.stdout.write('\n---------------------------------------------------------\n\n');
-console.traced.log('this is a traced console.log');
+require('./console-trace')({
+ colors: {
+ log: '35',
+ warn: '35',
+ error: '35',
+ trace: '35',
+ info: '35'
+ }
+})
-// pad
-console.log('');
+;['error', 'log', 'info', 'warn', 'trace'].forEach(function (name) {
+ process.stdout.write(' ');
+ console.traced[name]('this is a magenta traced console.' + name);
+});

0 comments on commit 8a54bf7

Please sign in to comment.