| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { LogType } = require("./Logger"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 14:23:40 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").FilterItemTypes} FilterItemTypes */ | 
					
						
							| 
									
										
										
										
											2019-07-24 16:51:04 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").FilterTypes} FilterTypes */ | 
					
						
							|  |  |  | /** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */ | 
					
						
							| 
									
										
										
										
											2019-07-22 14:23:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | /** @typedef {function(string): boolean} FilterFunction */ | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  |  * @typedef {Object} LoggerOptions | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  |  * @property {false|true|"none"|"error"|"warn"|"info"|"log"|"verbose"} level loglevel | 
					
						
							|  |  |  |  * @property {FilterTypes|boolean} debug filter for debug logging | 
					
						
							|  |  |  |  * @property {Console & { status?: Function, logTime?: Function }} console the console to log to | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-07-22 14:23:40 +08:00
										 |  |  |  * @param {FilterItemTypes} item an input item | 
					
						
							| 
									
										
										
										
											2020-03-10 09:59:46 +08:00
										 |  |  |  * @returns {FilterFunction} filter function | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | const filterToFunction = item => { | 
					
						
							|  |  |  | 	if (typeof item === "string") { | 
					
						
							|  |  |  | 		const regExp = new RegExp( | 
					
						
							|  |  |  | 			`[\\\\/]${item.replace( | 
					
						
							|  |  |  | 				// eslint-disable-next-line no-useless-escape
 | 
					
						
							|  |  |  | 				/[-[\]{}()*+?.\\^$|]/g, | 
					
						
							|  |  |  | 				"\\$&" | 
					
						
							|  |  |  | 			)}([\\\\/]|$|!|\\?)`
 | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		return ident => regExp.test(ident); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (item && typeof item === "object" && typeof item.test === "function") { | 
					
						
							|  |  |  | 		return ident => item.test(ident); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (typeof item === "function") { | 
					
						
							|  |  |  | 		return item; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (typeof item === "boolean") { | 
					
						
							|  |  |  | 		return () => item; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-09-10 15:15:20 +08:00
										 |  |  |  * @enum {number} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | const LogLevel = { | 
					
						
							| 
									
										
										
										
											2019-07-22 16:35:26 +08:00
										 |  |  | 	none: 6, | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 	false: 6, | 
					
						
							|  |  |  | 	error: 5, | 
					
						
							|  |  |  | 	warn: 4, | 
					
						
							|  |  |  | 	info: 3, | 
					
						
							|  |  |  | 	log: 2, | 
					
						
							|  |  |  | 	true: 2, | 
					
						
							|  |  |  | 	verbose: 1 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {LoggerOptions} options options object | 
					
						
							|  |  |  |  * @returns {function(string, LogTypeEnum, any[]): void} logging function | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | module.exports = ({ level = "info", debug = false, console }) => { | 
					
						
							| 
									
										
										
										
											2019-07-22 14:23:40 +08:00
										 |  |  | 	const debugFilters = | 
					
						
							|  |  |  | 		typeof debug === "boolean" | 
					
						
							|  |  |  | 			? [() => debug] | 
					
						
							|  |  |  | 			: /** @type {FilterItemTypes[]} */ ([]) | 
					
						
							|  |  |  | 					.concat(debug) | 
					
						
							|  |  |  | 					.map(filterToFunction); | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 	/** @type {number} */ | 
					
						
							|  |  |  | 	const loglevel = LogLevel[`${level}`] || 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} name name of the logger | 
					
						
							|  |  |  | 	 * @param {LogTypeEnum} type type of the log entry | 
					
						
							|  |  |  | 	 * @param {any[]} args arguments of the log entry | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	const logger = (name, type, args) => { | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 		const labeledArgs = () => { | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 			if (Array.isArray(args)) { | 
					
						
							|  |  |  | 				if (args.length > 0 && typeof args[0] === "string") { | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 					return [`[${name}] ${args[0]}`, ...args.slice(1)]; | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 					return [`[${name}]`, ...args]; | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				return []; | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 		const debug = debugFilters.some(f => f(name)); | 
					
						
							|  |  |  | 		switch (type) { | 
					
						
							|  |  |  | 			case LogType.debug: | 
					
						
							|  |  |  | 				if (!debug) return; | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 				// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				if (typeof console.debug === "function") { | 
					
						
							|  |  |  | 					// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 					console.debug(...labeledArgs()); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					console.log(...labeledArgs()); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case LogType.log: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.log) return; | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 				console.log(...labeledArgs()); | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case LogType.info: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.info) return; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 				console.info(...labeledArgs()); | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case LogType.warn: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.warn) return; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 				console.warn(...labeledArgs()); | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case LogType.error: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.error) return; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 				console.error(...labeledArgs()); | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case LogType.trace: | 
					
						
							|  |  |  | 				if (!debug) return; | 
					
						
							|  |  |  | 				console.trace(); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 			case LogType.groupCollapsed: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.log) return; | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.verbose) { | 
					
						
							|  |  |  | 					// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 					if (typeof console.groupCollapsed === "function") { | 
					
						
							|  |  |  | 						// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 						console.groupCollapsed(...labeledArgs()); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						console.log(...labeledArgs()); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			// falls through
 | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 			case LogType.group: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.log) return; | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 				// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				if (typeof console.group === "function") { | 
					
						
							|  |  |  | 					// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 					console.group(...labeledArgs()); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					console.log(...labeledArgs()); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case LogType.groupEnd: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.log) return; | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 				// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				if (typeof console.groupEnd === "function") { | 
					
						
							|  |  |  | 					// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 					console.groupEnd(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 			case LogType.time: { | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				if (!debug && loglevel > LogLevel.log) return; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 				const ms = args[1] * 1000 + args[2] / 1000000; | 
					
						
							|  |  |  | 				const msg = `[${name}] ${args[0]}: ${ms}ms`; | 
					
						
							|  |  |  | 				if (typeof console.logTime === "function") { | 
					
						
							|  |  |  | 					console.logTime(msg); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					console.log(msg); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 			case LogType.profile: | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 				// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				if (typeof console.profile === "function") { | 
					
						
							|  |  |  | 					// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 					console.profile(...labeledArgs()); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case LogType.profileEnd: | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 				// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				if (typeof console.profileEnd === "function") { | 
					
						
							|  |  |  | 					// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 					console.profileEnd(...labeledArgs()); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case LogType.clear: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.log) return; | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | 				// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 				if (typeof console.clear === "function") { | 
					
						
							|  |  |  | 					// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 					console.clear(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 			case LogType.status: | 
					
						
							|  |  |  | 				if (!debug && loglevel > LogLevel.info) return; | 
					
						
							|  |  |  | 				if (typeof console.status === "function") { | 
					
						
							|  |  |  | 					if (args.length === 0) { | 
					
						
							|  |  |  | 						console.status(); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						console.status(...labeledArgs()); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					if (args.length !== 0) { | 
					
						
							|  |  |  | 						console.info(...labeledArgs()); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2019-07-22 04:28:46 +08:00
										 |  |  | 			default: | 
					
						
							|  |  |  | 				throw new Error(`Unexpected LogType ${type}`); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	return logger; | 
					
						
							| 
									
										
										
										
											2019-07-19 18:31:18 +08:00
										 |  |  | }; |