forked from egorFiNE/node-stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtraLog.js
71 lines (54 loc) · 1.6 KB
/
ExtraLog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require('./ExtraNumber');
require('./ExtraDate');
var util = require('util');
/**
Actually it's a printf substitute with support for prices and unixtimes. Contains two functions.
*/
function ExtraLog() {}
var formatRegExp = /%[sdjpTD%]/g;
/**
Similar to <code>util.format()</code> but also supports the following tokens:
* **%p** human readable price;
* **%D** the argument is a unixtime, print the daystamp of it;
* **%T** the argument is a unixtime, print the time of it;
Example:
var s = ExtraLog.format("%D %T sold at %p", 1322590084, 1322590084, 233700);
// 20111129 20:08:04 sold at 23.37
*/
ExtraLog.format = function(f) {
var i=1, x;
var args = arguments;
var len = args.length;
var str = String(f).replace(formatRegExp, function(x) {
if (i >= len) { return x; }
switch (x) {
case '%D':return Date.parseUnixtime(args[i++]).toFormat('YYYYMMDD');
case '%T':return Date.parseUnixtime(args[i++]).toFormat('HH24:MI:SS');
case '%s':return String(args[i++]);
case '%d':return Number(args[i++]);
case '%p':return parseInt(args[i++]).humanReadablePrice();
case '%j':return JSON.stringify(args[i++]);
case '%%':return '%';
default:
return x;
}
});
for (x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') {
str += ' ' + x;
} else {
str += ' ' + util.inspect(x);
}
}
return str;
};
/**
Similar to <code>console.log</code> but uses <code>ExtraLog.format()</code>.
*/
ExtraLog.log = function() {
process.stdout.write(ExtraLog.format.apply(this, arguments) + '\n');
};
module.exports = {
format: ExtraLog.format,
log: ExtraLog.log
};