]>
Commit | Line | Data |
---|---|---|
1 | (function (global) { | |
2 | var Module; | |
3 | ||
4 | // Load up dependencies | |
5 | if (typeof require === 'function') { | |
6 | var Ne = require('neon'); | |
7 | Module = Ne.Module; | |
8 | } else { | |
9 | Module = global.Module; | |
10 | } | |
11 | ||
12 | var Cobalt = {}; | |
13 | Module(Cobalt, 'Formatter')({}); | |
14 | ||
15 | Cobalt.Formatter.Token = Module(Cobalt.Formatter, 'Token')({ | |
16 | formatString : "{{message}}", | |
17 | replaceRule : /{{(.*?)}}/g, | |
18 | separatorLength : 60, | |
19 | separatorType : "-", | |
20 | format : function (logObject, opts){ | |
21 | var indent, indentSize, | |
22 | separatorLength, separatorType, | |
23 | output; | |
24 | indentSize = logObject._indentLevel || 0; | |
25 | ||
26 | indent = Array(indentSize + 1).join(' '); | |
27 | ||
28 | if (logObject._separator) { | |
29 | separatorLength = logObject._separatorLength || this.separatorLength; | |
30 | separatorType = logObject._separatorType || this.separatorType; | |
31 | output = indent + Array(separatorLength - indentSize + 1).join(separatorType); | |
32 | } else { | |
33 | output = indent + this.parseFormatString(logObject, opts.formatString); | |
34 | } | |
35 | ||
36 | if (opts.ansiColor) { | |
37 | output = this.colorize(logObject._level, output); | |
38 | } | |
39 | ||
40 | return output; | |
41 | }, | |
42 | ||
43 | parseFormatString : function (logObject, formatString) { | |
44 | var resultString = ''; | |
45 | if (typeof formatString === 'undefined') { | |
46 | formatString = this.formatString; | |
47 | } | |
48 | ||
49 | resultString = formatString.replace(this.replaceRule, function(match, paren){ | |
50 | return logObject[paren] || "-"; | |
51 | }); | |
52 | ||
53 | return resultString; | |
54 | }, | |
55 | ||
56 | colorize : function (level, message) { | |
57 | switch(level) { | |
58 | case 0: | |
59 | case 1: | |
60 | case 2: | |
61 | case 3: | |
62 | return message.red; | |
63 | case 4: | |
64 | return message.yellow; | |
65 | case 5: | |
66 | case 6: | |
67 | return message.blue; | |
68 | default: | |
69 | return message; | |
70 | } | |
71 | } | |
72 | }); | |
73 | ||
74 | if (Cobalt.Formatter.Token.__objectSpy) { | |
75 | Cobalt.Formatter.Token.__objectSpy.destroy(); | |
76 | } | |
77 | ||
78 | if (typeof require === 'function') { | |
79 | global.Token = Cobalt.Formatter.Token; | |
80 | } else { | |
81 | global.Cobalt.Formatter.Token = Cobalt.Formatter.Token; | |
82 | } | |
83 | }(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); |