1 | /*
|
2 | * Copyright (c) 2012 Dmitri Melikyan
|
3 | *
|
4 | * Permission is hereby granted, free of charge, to any person obtaining a
|
5 | * copy of this software and associated documentation files (the
|
6 | * "Software"), to deal in the Software without restriction, including
|
7 | * without limitation the rights to use, copy, modify, merge, publish,
|
8 | * distribute, sublicense, and/or sell copies of the Software, and to permit
|
9 | * persons to whom the Software is furnished to do so, subject to the
|
10 | * following conditions:
|
11 | *
|
12 | * The above copyright notice and this permission notice shall be included
|
13 | * in all copies or substantial portions of the Software.
|
14 | *
|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
16 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
18 | * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
19 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
20 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
|
21 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22 | */
|
23 |
|
24 |
|
25 | var nt;
|
26 |
|
27 | exports.init = function() {
|
28 | nt = global.nodetime;
|
29 |
|
30 | nt.on('sample', function(sample) {
|
31 | console.log(indent({sample: sample}));
|
32 | });
|
33 | };
|
34 |
|
35 |
|
36 | function indent(obj, depth) {
|
37 | if(!depth) depth = 0;
|
38 | if(depth > 20) return '';
|
39 |
|
40 | var tab = '';
|
41 | for(var i = 0; i < depth; i++) tab += "\t";
|
42 |
|
43 | var str = ''
|
44 | var arr = Array.isArray(obj);
|
45 |
|
46 | for(var prop in obj) {
|
47 | var val = obj[prop];
|
48 | if(val == undefined || prop.match(/^_/)) continue;
|
49 |
|
50 | var label = val._label || (arr ? ('[' + prop + ']') : prop);
|
51 |
|
52 | if(typeof val === 'string' || typeof val === 'number') {
|
53 | str += tab + label + ': \033[33m' + val + '\033[0m\n';
|
54 | }
|
55 | else if(typeof val === 'object') {
|
56 | str += tab + '\033[1m' + label + '\033[0m\n';
|
57 | str += indent(val, depth + 1);
|
58 | }
|
59 | }
|
60 |
|
61 | return str;
|
62 | }
|
63 |
|