1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs");
|
4 | const path = require("path");
|
5 |
|
6 | let chalk;
|
7 | let printRed;
|
8 | let printYellow;
|
9 | let printGray;
|
10 | try {
|
11 | chalk = require('chalk');
|
12 | printRed = chalk.red;
|
13 | printYellow = chalk.yellow;
|
14 | printGray = chalk.gray;
|
15 | }
|
16 | catch (e) {
|
17 | printRed = printYellow = printGray = (msg) => {
|
18 | return msg;
|
19 | };
|
20 | }
|
21 | var LogLevel;
|
22 | (function (LogLevel) {
|
23 | LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
|
24 | LogLevel[LogLevel["WARN"] = 1] = "WARN";
|
25 | LogLevel[LogLevel["INFO"] = 2] = "INFO";
|
26 | LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
|
27 | })(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
|
28 | var WriteTo;
|
29 | (function (WriteTo) {
|
30 | WriteTo[WriteTo["CONSOLE"] = 0] = "CONSOLE";
|
31 | WriteTo[WriteTo["FILE"] = 1] = "FILE";
|
32 | WriteTo[WriteTo["BOTH"] = 2] = "BOTH";
|
33 | WriteTo[WriteTo["NONE"] = 3] = "NONE";
|
34 | })(WriteTo = exports.WriteTo || (exports.WriteTo = {}));
|
35 | let logFile = 'protractor.log';
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | class Logger {
|
41 | |
42 |
|
43 |
|
44 |
|
45 | constructor(id) {
|
46 | this.id = id;
|
47 | }
|
48 | |
49 |
|
50 |
|
51 |
|
52 | static set(config) {
|
53 | if (config.troubleshoot) {
|
54 | Logger.logLevel = LogLevel.DEBUG;
|
55 | }
|
56 | else if (config.logLevel) {
|
57 | Logger.logLevel = LogLevel[config.logLevel];
|
58 | }
|
59 | }
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 | static setWrite(writeTo, opt_logFile) {
|
66 | if (opt_logFile) {
|
67 | logFile = opt_logFile;
|
68 | }
|
69 | Logger.writeTo = writeTo;
|
70 | if (Logger.writeTo == WriteTo.FILE || Logger.writeTo == WriteTo.BOTH) {
|
71 | Logger.fd = fs.openSync(path.resolve(logFile), 'a');
|
72 | Logger.firstWrite = false;
|
73 | }
|
74 | }
|
75 | |
76 |
|
77 |
|
78 |
|
79 | info(...msgs) {
|
80 | this.log_(LogLevel.INFO, msgs);
|
81 | }
|
82 | |
83 |
|
84 |
|
85 |
|
86 | debug(...msgs) {
|
87 | this.log_(LogLevel.DEBUG, msgs);
|
88 | }
|
89 | |
90 |
|
91 |
|
92 |
|
93 | warn(...msgs) {
|
94 | this.log_(LogLevel.WARN, msgs);
|
95 | }
|
96 | |
97 |
|
98 |
|
99 |
|
100 | error(...msgs) {
|
101 | this.log_(LogLevel.ERROR, msgs);
|
102 | }
|
103 | |
104 |
|
105 |
|
106 |
|
107 |
|
108 | log_(logLevel, msgs) {
|
109 | switch (Logger.logLevel) {
|
110 | case LogLevel.ERROR:
|
111 | if (logLevel <= LogLevel.ERROR) {
|
112 | this.print_(logLevel, msgs);
|
113 | }
|
114 | break;
|
115 | case LogLevel.WARN:
|
116 | if (logLevel <= LogLevel.WARN) {
|
117 | this.print_(logLevel, msgs);
|
118 | }
|
119 | break;
|
120 | case LogLevel.INFO:
|
121 | if (logLevel <= LogLevel.INFO) {
|
122 | this.print_(logLevel, msgs);
|
123 | }
|
124 | break;
|
125 | case LogLevel.DEBUG:
|
126 | if (logLevel <= LogLevel.DEBUG) {
|
127 | this.print_(logLevel, msgs);
|
128 | }
|
129 | break;
|
130 | default:
|
131 | throw new Error('Invalid log level');
|
132 | }
|
133 | }
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | print_(logLevel, msgs) {
|
141 | let consoleLog = '';
|
142 | let fileLog = '';
|
143 | if (Logger.showTimestamp) {
|
144 | consoleLog += Logger.timestamp_(WriteTo.CONSOLE);
|
145 | fileLog += Logger.timestamp_(WriteTo.FILE);
|
146 | }
|
147 | consoleLog += Logger.level_(logLevel, this.id, WriteTo.CONSOLE);
|
148 | fileLog += Logger.level_(logLevel, this.id, WriteTo.FILE);
|
149 | if (Logger.showId) {
|
150 | consoleLog += Logger.id_(logLevel, this.id, WriteTo.CONSOLE);
|
151 | fileLog += Logger.id_(logLevel, this.id, WriteTo.FILE);
|
152 | }
|
153 | consoleLog += ' -';
|
154 | fileLog += ' - ';
|
155 | switch (Logger.writeTo) {
|
156 | case WriteTo.CONSOLE:
|
157 | msgs.unshift(consoleLog);
|
158 | console.log.apply(console, msgs);
|
159 | break;
|
160 | case WriteTo.FILE:
|
161 |
|
162 | if (!Logger.firstWrite) {
|
163 | fs.writeSync(Logger.fd, '\n');
|
164 | Logger.firstWrite = true;
|
165 | }
|
166 | fileLog += ' ' + Logger.msgToFile_(msgs);
|
167 | fs.writeSync(Logger.fd, fileLog + '\n');
|
168 | break;
|
169 | case WriteTo.BOTH:
|
170 |
|
171 | if (!Logger.firstWrite) {
|
172 | fs.writeSync(Logger.fd, '\n');
|
173 | Logger.firstWrite = true;
|
174 | }
|
175 | fileLog += ' ' + Logger.msgToFile_(msgs);
|
176 | fs.writeSync(Logger.fd, fileLog + '\n');
|
177 | msgs.unshift(consoleLog);
|
178 | console.log.apply(console, msgs);
|
179 | break;
|
180 | case WriteTo.NONE:
|
181 | break;
|
182 | }
|
183 | }
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 | static timestamp_(writeTo) {
|
190 | let d = new Date();
|
191 | let ts = '[';
|
192 | let hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
|
193 | let minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
|
194 | let seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
|
195 | if (writeTo == WriteTo.CONSOLE) {
|
196 | ts += printGray(hours + ':' + minutes + ':' + seconds) + ']';
|
197 | }
|
198 | else {
|
199 | ts += hours + ':' + minutes + ':' + seconds + ']';
|
200 | }
|
201 | ts += ' ';
|
202 | return ts;
|
203 | }
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | static id_(logLevel, id, writeTo) {
|
211 | if (writeTo === WriteTo.FILE) {
|
212 | return '/' + id;
|
213 | }
|
214 | else if (logLevel === LogLevel.ERROR) {
|
215 | return printRed('/' + id);
|
216 | }
|
217 | else if (logLevel === LogLevel.WARN) {
|
218 | return printYellow('/' + id);
|
219 | }
|
220 | else {
|
221 | return '/' + id;
|
222 | }
|
223 | }
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | static level_(logLevel, id, writeTo) {
|
231 | let level = LogLevel[logLevel].toString();
|
232 | if (writeTo === WriteTo.FILE) {
|
233 | return level[0];
|
234 | }
|
235 | else if (logLevel === LogLevel.ERROR) {
|
236 | return printRed(level[0]);
|
237 | }
|
238 | else if (logLevel === LogLevel.WARN) {
|
239 | return printYellow(level[0]);
|
240 | }
|
241 | else {
|
242 | return level[0];
|
243 | }
|
244 | }
|
245 | |
246 |
|
247 |
|
248 |
|
249 |
|
250 | static msgToFile_(msgs) {
|
251 | let log = '';
|
252 | for (let pos = 0; pos < msgs.length; pos++) {
|
253 | let msg = msgs[pos];
|
254 | let ret;
|
255 | if (typeof msg === 'object') {
|
256 | ret = JSON.stringify(msg);
|
257 | }
|
258 | else {
|
259 | ret = msg;
|
260 | }
|
261 | if (pos !== msgs.length - 1) {
|
262 | ret += ' ';
|
263 | }
|
264 | log += ret;
|
265 | }
|
266 | return log;
|
267 | }
|
268 | }
|
269 | Logger.logLevel = LogLevel.INFO;
|
270 | Logger.showTimestamp = true;
|
271 | Logger.showId = true;
|
272 | Logger.writeTo = WriteTo.CONSOLE;
|
273 | Logger.firstWrite = false;
|
274 | exports.Logger = Logger;
|
275 |
|
\ | No newline at end of file |