source: imaps-frontend/node_modules/webpack/lib/logging/createConsoleLogger.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { LogType } = require("./Logger");
9
10/** @typedef {import("../../declarations/WebpackOptions").FilterItemTypes} FilterItemTypes */
11/** @typedef {import("../../declarations/WebpackOptions").FilterTypes} FilterTypes */
12/** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
13
14/** @typedef {function(string): boolean} FilterFunction */
15/** @typedef {function(string, LogTypeEnum, EXPECTED_ANY[]=): void} LoggingFunction */
16
17/**
18 * @typedef {object} LoggerConsole
19 * @property {function(): void} clear
20 * @property {function(): void} trace
21 * @property {(...args: EXPECTED_ANY[]) => void} info
22 * @property {(...args: EXPECTED_ANY[]) => void} log
23 * @property {(...args: EXPECTED_ANY[]) => void} warn
24 * @property {(...args: EXPECTED_ANY[]) => void} error
25 * @property {(...args: EXPECTED_ANY[]) => void=} debug
26 * @property {(...args: EXPECTED_ANY[]) => void=} group
27 * @property {(...args: EXPECTED_ANY[]) => void=} groupCollapsed
28 * @property {(...args: EXPECTED_ANY[]) => void=} groupEnd
29 * @property {(...args: EXPECTED_ANY[]) => void=} status
30 * @property {(...args: EXPECTED_ANY[]) => void=} profile
31 * @property {(...args: EXPECTED_ANY[]) => void=} profileEnd
32 * @property {(...args: EXPECTED_ANY[]) => void=} logTime
33 */
34
35/**
36 * @typedef {object} LoggerOptions
37 * @property {false|true|"none"|"error"|"warn"|"info"|"log"|"verbose"} level loglevel
38 * @property {FilterTypes|boolean} debug filter for debug logging
39 * @property {LoggerConsole} console the console to log to
40 */
41
42/**
43 * @param {FilterItemTypes} item an input item
44 * @returns {FilterFunction | undefined} filter function
45 */
46const filterToFunction = item => {
47 if (typeof item === "string") {
48 const regExp = new RegExp(
49 `[\\\\/]${item.replace(/[-[\]{}()*+?.\\^$|]/g, "\\$&")}([\\\\/]|$|!|\\?)`
50 );
51 return ident => regExp.test(ident);
52 }
53 if (item && typeof item === "object" && typeof item.test === "function") {
54 return ident => item.test(ident);
55 }
56 if (typeof item === "function") {
57 return item;
58 }
59 if (typeof item === "boolean") {
60 return () => item;
61 }
62};
63
64/**
65 * @enum {number}
66 */
67const LogLevel = {
68 none: 6,
69 false: 6,
70 error: 5,
71 warn: 4,
72 info: 3,
73 log: 2,
74 true: 2,
75 verbose: 1
76};
77
78/**
79 * @param {LoggerOptions} options options object
80 * @returns {LoggingFunction} logging function
81 */
82module.exports = ({ level = "info", debug = false, console }) => {
83 const debugFilters =
84 /** @type {FilterFunction[]} */
85 (
86 typeof debug === "boolean"
87 ? [() => debug]
88 : /** @type {FilterItemTypes[]} */ ([])
89 .concat(debug)
90 .map(filterToFunction)
91 );
92 /** @type {number} */
93 const loglevel = LogLevel[`${level}`] || 0;
94
95 /**
96 * @param {string} name name of the logger
97 * @param {LogTypeEnum} type type of the log entry
98 * @param {EXPECTED_ANY[]=} args arguments of the log entry
99 * @returns {void}
100 */
101 const logger = (name, type, args) => {
102 const labeledArgs = () => {
103 if (Array.isArray(args)) {
104 if (args.length > 0 && typeof args[0] === "string") {
105 return [`[${name}] ${args[0]}`, ...args.slice(1)];
106 }
107 return [`[${name}]`, ...args];
108 }
109 return [];
110 };
111 const debug = debugFilters.some(f => f(name));
112 switch (type) {
113 case LogType.debug:
114 if (!debug) return;
115 if (typeof console.debug === "function") {
116 console.debug(...labeledArgs());
117 } else {
118 console.log(...labeledArgs());
119 }
120 break;
121 case LogType.log:
122 if (!debug && loglevel > LogLevel.log) return;
123 console.log(...labeledArgs());
124 break;
125 case LogType.info:
126 if (!debug && loglevel > LogLevel.info) return;
127 console.info(...labeledArgs());
128 break;
129 case LogType.warn:
130 if (!debug && loglevel > LogLevel.warn) return;
131 console.warn(...labeledArgs());
132 break;
133 case LogType.error:
134 if (!debug && loglevel > LogLevel.error) return;
135 console.error(...labeledArgs());
136 break;
137 case LogType.trace:
138 if (!debug) return;
139 console.trace();
140 break;
141 case LogType.groupCollapsed:
142 if (!debug && loglevel > LogLevel.log) return;
143 if (!debug && loglevel > LogLevel.verbose) {
144 if (typeof console.groupCollapsed === "function") {
145 console.groupCollapsed(...labeledArgs());
146 } else {
147 console.log(...labeledArgs());
148 }
149 break;
150 }
151 // falls through
152 case LogType.group:
153 if (!debug && loglevel > LogLevel.log) return;
154 if (typeof console.group === "function") {
155 console.group(...labeledArgs());
156 } else {
157 console.log(...labeledArgs());
158 }
159 break;
160 case LogType.groupEnd:
161 if (!debug && loglevel > LogLevel.log) return;
162 if (typeof console.groupEnd === "function") {
163 console.groupEnd();
164 }
165 break;
166 case LogType.time: {
167 if (!debug && loglevel > LogLevel.log) return;
168 const [label, start, end] =
169 /** @type {[string, number, number]} */
170 (args);
171 const ms = start * 1000 + end / 1000000;
172 const msg = `[${name}] ${label}: ${ms} ms`;
173 if (typeof console.logTime === "function") {
174 console.logTime(msg);
175 } else {
176 console.log(msg);
177 }
178 break;
179 }
180 case LogType.profile:
181 if (typeof console.profile === "function") {
182 console.profile(...labeledArgs());
183 }
184 break;
185 case LogType.profileEnd:
186 if (typeof console.profileEnd === "function") {
187 console.profileEnd(...labeledArgs());
188 }
189 break;
190 case LogType.clear:
191 if (!debug && loglevel > LogLevel.log) return;
192 if (typeof console.clear === "function") {
193 console.clear();
194 }
195 break;
196 case LogType.status:
197 if (!debug && loglevel > LogLevel.info) return;
198 if (typeof console.status === "function") {
199 if (!args || args.length === 0) {
200 console.status();
201 } else {
202 console.status(...labeledArgs());
203 }
204 } else if (args && args.length !== 0) {
205 console.info(...labeledArgs());
206 }
207 break;
208 default:
209 throw new Error(`Unexpected LogType ${type}`);
210 }
211 };
212 return logger;
213};
Note: See TracBrowser for help on using the repository browser.