UNPKG

2.73 kBJavaScriptView Raw
1var util = require('util')
2var colors = require('./colors')
3
4function wrap(log,length,res){
5 if(!res) res=[]
6 if(log.length <= length){
7 res.push(log)
8 return res
9 }else{
10 res.push(log.substr(0,length))
11 return wrap(log.substr(length),length,res)
12 }
13}
14
15var trimEnd = '…';
16var ansiEscapes = /\x1b\[(\d+([A-GJKSTm]|;\d+[Hf])|6n|s|u|\?25[lh])/g;
17
18function stripANSI(str) {
19 return str.replace(ansiEscapes, '');
20}
21
22// Try to leave the escape sequences intact if possible, but strip them
23// if we need to trim the line so that we don't put the terminal in a weird
24// state by stripping a reset code.
25function trim(line, n) {
26 var stripped = stripANSI(line);
27 if (stripped.length <= n) {
28 return line;
29 } else {
30 return stripped.substr(0,n) + trimEnd;
31 }
32}
33
34function Console(logger) {
35 logger = logger || console
36 this.padding = 25
37
38 this.trimline = 10
39 this.wrapline = 500
40
41 this.fmt = function fmt(){
42 return util.format.apply(null,arguments);
43 }
44
45 this.pad = function pad(string,n){
46 var l = string.length;
47 var d = n - l;
48 var o = string;
49 for(i=l;i<n;i++){
50 o += " "
51 }
52 return o
53 }
54
55 this.trim = trim;
56
57 // Process Specific Loggers //
58 this.info = function info(key,proc,string){
59 var stamp = (new Date().toLocaleTimeString()) + " " + key;
60 logger.log(proc.color(this.pad(stamp,this.padding)),
61 colors.cyan(string));
62 }
63
64 this.Info = function info(key,proc,string){
65 var stamp = (new Date().toLocaleTimeString()) + " " + key;
66 logger.log(proc.color(this.pad(stamp,this.padding)),
67 colors.bright_cyan(string));
68 }
69
70 this.log = function log(key,proc,string){
71 var self = this;
72 string.split(/\n/).forEach(function(line){
73
74 if (line.trim().length==0) return;
75
76 var stamp = (new Date().toLocaleTimeString()) + " " + key;
77
78 if(self.trimline>0){
79 line = self.trim(line,self.trimline);
80 }
81
82 var delimiter = " | "
83
84 var wrapline
85 if(self.wrapline==0){
86 wrapline = line.length
87 }else{
88 wrapline = self.wrapline
89 }
90
91 wrap(line,wrapline).forEach(function(l){
92 logger.log(proc.color(self.pad(stamp,self.padding) + delimiter),l.trim());
93 delimiter = " | > "
94 })
95
96 });
97 }
98
99 // Foreman Loggers //
100
101 this.Alert = function Alert(){
102 logger.log( colors.green('[OKAY] '+ this.fmt.apply(null,arguments)) );
103 }
104
105 this.Done = function Info(){
106 logger.log( colors.cyan('[DONE] ' + this.fmt.apply(null,arguments)) );
107 }
108
109 this.Warn = function Warn(){
110 logger.warn( colors.yellow('[WARN] ' + this.fmt.apply(null,arguments)) );
111 }
112
113 this.Error = function Error(){
114 logger.error( colors.bright_red('[FAIL] ' + this.fmt.apply(null,arguments)) );
115 }
116
117}
118
119
120module.exports = Console
121Console.Console = new Console()