@zenflux/core 0.1.12 ā 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/zenflux-core.cjs +6 -7
- package/dist/zenflux-core.mjs +6 -7
- package/package.json +3 -4
- package/src/bases/controller-base.ts +0 -96
- package/src/bases/index.ts +0 -6
- package/src/bases/initialize-base.ts +0 -32
- package/src/bases/object-base.ts +0 -55
- package/src/clients/http.ts +0 -152
- package/src/command-bases/command-base.ts +0 -82
- package/src/command-bases/command-internal.ts +0 -11
- package/src/command-bases/command-public.ts +0 -12
- package/src/command-bases/command-restful.ts +0 -55
- package/src/command-bases/index.ts +0 -8
- package/src/errors/command-already-registered.ts +0 -13
- package/src/errors/command-not-found.ts +0 -12
- package/src/errors/controller-already-registered.ts +0 -13
- package/src/errors/controller-already-set.ts +0 -13
- package/src/errors/error-with-metadata.ts +0 -14
- package/src/errors/force-method-implementation.ts +0 -15
- package/src/errors/index-public.ts +0 -1
- package/src/errors/index.ts +0 -10
- package/src/exports-index.ts +0 -13
- package/src/exports.ts +0 -58
- package/src/index.ts +0 -18
- package/src/interfaces/commands.ts +0 -17
- package/src/interfaces/config.ts +0 -23
- package/src/interfaces/index.ts +0 -8
- package/src/interfaces/logger.ts +0 -19
- package/src/interfaces/restful.ts +0 -23
- package/src/managers/commands.ts +0 -268
- package/src/managers/controllers.ts +0 -40
- package/src/managers/export.ts +0 -30
- package/src/managers/index.ts +0 -8
- package/src/managers/internal.ts +0 -11
- package/src/managers/restful.ts +0 -117
- package/src/modules/debugger.ts +0 -89
- package/src/modules/event-bus/event-bus.ts +0 -204
- package/src/modules/logger/config.ts +0 -44
- package/src/modules/logger/implementation.ts +0 -287
- package/src/modules/logger/index.ts +0 -2
- package/src/modules/logger/logger-browser-infra.ts +0 -344
- package/src/modules/logger/types/picocolors.d.ts +0 -15
- package/src/modules/logger/utils/index.ts +0 -3
- package/src/modules/logger/utils/object.ts +0 -19
- package/src/modules/logger/utils/rgb.ts +0 -34
- package/src/modules/mcp-server/mcp-server.ts +0 -139
- package/src/modules/mcp-server/mcp-service.ts +0 -80
- package/src/modules/service/service-base.ts +0 -55
- package/src/modules/service/service-locator.ts +0 -205
- package/src/modules/service/service-with-dependencies-base.ts +0 -51
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import pc from "picocolors";
|
|
2
|
-
|
|
3
|
-
import { EventBus } from "../event-bus/event-bus";
|
|
4
|
-
import { ObjectBase } from "../../bases";
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
getLoggerLogLevel,
|
|
8
|
-
getLoggerLogLevelString,
|
|
9
|
-
isLoggerDebugEnabled,
|
|
10
|
-
isLoggerDisabled,
|
|
11
|
-
isLoggerPreviousSourceDisabled
|
|
12
|
-
} from "./config";
|
|
13
|
-
|
|
14
|
-
import { LoggerBrowserInfra } from "./logger-browser-infra";
|
|
15
|
-
|
|
16
|
-
import { reduceCircularReferences } from "./utils";
|
|
17
|
-
|
|
18
|
-
import type * as interfaces from "../../interfaces";
|
|
19
|
-
|
|
20
|
-
const DEFAULT_LOG_PREFIX = pc.white( "āŖ - [LOG]" ),
|
|
21
|
-
DEFAULT_INFO_PREFIX = pc.blue( "šµ - [INFO]" ),
|
|
22
|
-
DEFAULT_DEBUG_PREFIX = pc.gray( "š¤ - [DEBUG]" ),
|
|
23
|
-
DEFAULT_WARN_PREFIX = pc.yellow( "š” - [WARN]" ),
|
|
24
|
-
DEFAULT_ERROR_PREFIX = pc.red( "š“ - [ERROR]" ),
|
|
25
|
-
DEFAULT_ADMIN_PREFIX = pc.bold( "š£ - [ADMIN]" );
|
|
26
|
-
|
|
27
|
-
const registeredNames: Record<string, boolean> = {};
|
|
28
|
-
|
|
29
|
-
type ICaller = interfaces.TCaller;
|
|
30
|
-
|
|
31
|
-
interface LoggerOptions {
|
|
32
|
-
skipEventBusHook?: boolean;
|
|
33
|
-
repeatedly?: boolean;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class Logger extends LoggerBrowserInfra implements interfaces.ILogger {
|
|
37
|
-
private static lastLogTime: number = Date.now();
|
|
38
|
-
|
|
39
|
-
private readonly ownerName: string;
|
|
40
|
-
|
|
41
|
-
private messagePrefixes: string[] = [];
|
|
42
|
-
|
|
43
|
-
private config: LoggerOptions = {};
|
|
44
|
-
|
|
45
|
-
public static getName(): string {
|
|
46
|
-
return "ZenFlux/Core/Modules/Logger";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public static getLogLevelString(): string {
|
|
50
|
-
return getLoggerLogLevelString();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public static getLogLevel(): number {
|
|
54
|
-
return getLoggerLogLevel();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public static isDebugEnabled() {
|
|
58
|
-
return isLoggerDebugEnabled();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public constructor( owner: any, options?: LoggerOptions ) {
|
|
62
|
-
const { repeatedly = false, skipEventBusHook } = options || {};
|
|
63
|
-
|
|
64
|
-
super( owner, { repeatedly } );
|
|
65
|
-
|
|
66
|
-
this.config = { skipEventBusHook };
|
|
67
|
-
this.ownerName = Logger.resolveOwnerName( owner );
|
|
68
|
-
|
|
69
|
-
if ( registeredNames[ this.ownerName ] ) {
|
|
70
|
-
throw new Error( `Logger for '${ this.ownerName }' already exists` );
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
registeredNames[ this.ownerName ] = true;
|
|
74
|
-
|
|
75
|
-
if ( isLoggerDisabled() ) {
|
|
76
|
-
this.disableLoggers();
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if ( isLoggerPreviousSourceDisabled() ) {
|
|
81
|
-
this.getPreviousSource = () => "";
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
switch ( getLoggerLogLevel() ) {
|
|
85
|
-
case 0:
|
|
86
|
-
this.error = () => {};
|
|
87
|
-
// falls through
|
|
88
|
-
case 1:
|
|
89
|
-
this.warn = () => {};
|
|
90
|
-
// falls through
|
|
91
|
-
case 2:
|
|
92
|
-
this.admin = () => {};
|
|
93
|
-
// falls through
|
|
94
|
-
case 3:
|
|
95
|
-
this.info = () => {};
|
|
96
|
-
// falls through
|
|
97
|
-
case 4:
|
|
98
|
-
this.log = () => {};
|
|
99
|
-
// falls through
|
|
100
|
-
case 5:
|
|
101
|
-
this.debug = () => {};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if ( !this.config.skipEventBusHook ) {
|
|
105
|
-
EventBus.$.registerMultiInstances( this, [ this.outputEvent ] );
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public addMessagePrefix( prefix: string ) {
|
|
110
|
-
this.messagePrefixes.push( prefix );
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public log( caller: ICaller, message: string, ...params: any[] ): void {
|
|
114
|
-
this.writeLog( DEFAULT_LOG_PREFIX, caller, message, ...params );
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public info( caller: ICaller, message: string, ...params: any[] ): void {
|
|
118
|
-
this.writeLog( DEFAULT_INFO_PREFIX, caller, message, ...params );
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public debug( caller: ICaller, message: string, ...params: any[] ): void {
|
|
122
|
-
this.writeLog( DEFAULT_DEBUG_PREFIX, caller, message, ...params );
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public warn( caller: ICaller, message: string, ...params: any[] ): void {
|
|
126
|
-
this.writeLog( DEFAULT_WARN_PREFIX, caller, message, ...params );
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
public error( caller: ICaller, message: string, ...params: any[] ): void {
|
|
130
|
-
this.writeLog( DEFAULT_ERROR_PREFIX, caller, message, ...params );
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
public admin( caller: ICaller, message: string, ...params: any[] ): void {
|
|
134
|
-
this.writeLog( DEFAULT_ADMIN_PREFIX, caller, message, ...params );
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
public startsEmpty( caller: interfaces.TCaller ) {
|
|
138
|
-
this.printFunctionNotify( "ā¶", caller, "" );
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
public startsWith( caller: interfaces.TCaller, params: string | object ) {
|
|
142
|
-
this.printObjectEfficient( "ā¶", caller, params );
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
public dump( caller: interfaces.TCaller, params: { [ key: string ]: object | string } = {}, notice = "" ) {
|
|
146
|
-
for ( const key in params ) {
|
|
147
|
-
if ( typeof params[ key ] === "object" ) {
|
|
148
|
-
params[ key ] = JSON.stringify(
|
|
149
|
-
LoggerBrowserInfra.useObjectMapper( params[ key ] as object ),
|
|
150
|
-
reduceCircularReferences()
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
this.printInLineElement( "dp", caller, key, params[ key ], notice );
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
public drop( caller: interfaces.TCaller, according: { [ key: string ]: string }, data: any ) {
|
|
159
|
-
for ( const key in according ) {
|
|
160
|
-
this.printInLineElement( "dr", caller, key, according[ key ], "corresponding" );
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
this.output( data );
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* TODO: Should respect debug levels, when to throw...
|
|
168
|
-
*/
|
|
169
|
-
public throw( caller: interfaces.TCaller, output: string, name: string = "null", params = {} ) {
|
|
170
|
-
this.printFunctionNotify( "tw", caller, output );
|
|
171
|
-
|
|
172
|
-
if ( params ) {
|
|
173
|
-
this.printInNextLineObject( "tw", caller, name, params );
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
throw new Error().stack;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
public beep() {
|
|
180
|
-
console.log( "\x07" );
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
public clone() {
|
|
184
|
-
return Object.assign( Object.create( Object.getPrototypeOf( this ) ), this );
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
public outputEvent( _prefix: string, _timeDiff: string, _source: string, _messagePrefix: string, _message: string, _params: any[] ): void {
|
|
188
|
-
// Intentionally left blank, can be hooked via EventBus.
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
private static resolveOwnerName( owner: any ): string {
|
|
192
|
-
if ( typeof owner === "string" ) {
|
|
193
|
-
return owner;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if ( owner instanceof ObjectBase ) {
|
|
197
|
-
return owner.getName();
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if ( owner && typeof owner.getName === "function" ) {
|
|
201
|
-
return owner.getName();
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return "UnknownOwner";
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
private disableLoggers() {
|
|
208
|
-
this.error = () => {};
|
|
209
|
-
this.warn = () => {};
|
|
210
|
-
this.admin = () => {};
|
|
211
|
-
this.info = () => {};
|
|
212
|
-
this.log = () => {};
|
|
213
|
-
this.debug = () => {};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
private getStackTrace(): any[] {
|
|
217
|
-
const stackTrace = ( new Error().stack || "" ).split( "\n" );
|
|
218
|
-
const stackLines = stackTrace.slice( 1 );
|
|
219
|
-
|
|
220
|
-
const stackRegex = / at (.+?) \((.+?)\)/;
|
|
221
|
-
const result = [];
|
|
222
|
-
|
|
223
|
-
for ( const line of stackLines ) {
|
|
224
|
-
const match = line.match( stackRegex );
|
|
225
|
-
|
|
226
|
-
if ( match ) {
|
|
227
|
-
const [ , context, file ] = match;
|
|
228
|
-
const parsedLine: any = { context, file };
|
|
229
|
-
|
|
230
|
-
if ( line.startsWith( "new" ) ) {
|
|
231
|
-
parsedLine.isNew = true;
|
|
232
|
-
parsedLine.object = context.split( " " )[ 1 ];
|
|
233
|
-
} else if ( context !== "Object.<anonymous>" ) {
|
|
234
|
-
parsedLine.object = context;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
result.push( parsedLine );
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return result;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
public getPreviousSource(): string {
|
|
245
|
-
const stack = this.getStackTrace()
|
|
246
|
-
.filter( ( line: any ) => line.file.includes( "/src/" ) )
|
|
247
|
-
.filter( ( line: any ) => !line.file.includes( "logger.ts" ) )
|
|
248
|
-
.filter( ( line: any ) => !line.file.includes( "debugger.ts" ) )
|
|
249
|
-
.filter( ( line: any ) => !line.file.includes( "/node_modules/" ) );
|
|
250
|
-
|
|
251
|
-
let previousSource = "";
|
|
252
|
-
|
|
253
|
-
const previousCaller = stack[ 1 ]?.object?.split( "." );
|
|
254
|
-
|
|
255
|
-
if ( previousCaller?.length > 1 ) {
|
|
256
|
-
const previousCallerName = previousCaller[ 0 ],
|
|
257
|
-
previousCallerMethod = previousCaller[ 1 ];
|
|
258
|
-
|
|
259
|
-
previousSource = `${ previousCallerName }::${ previousCallerMethod }]` + "[";
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return previousSource;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
private writeLog( prefix: string, caller: ICaller, message: string, ...params: any[] ): void {
|
|
266
|
-
const source = this.getPreviousSource() + pc.white( `${ this.ownerName }::${ this.getCallerName( caller ) }` );
|
|
267
|
-
|
|
268
|
-
let messagePrefix = "";
|
|
269
|
-
|
|
270
|
-
if ( this.messagePrefixes.length ) {
|
|
271
|
-
messagePrefix = `[${ this.messagePrefixes.join( "][" ) }]`;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const timeDiff = ( Date.now() - Logger.lastLogTime ).toString().padStart( 4, "0" );
|
|
275
|
-
|
|
276
|
-
this.outputEvent( prefix, timeDiff, source, messagePrefix, message, params );
|
|
277
|
-
|
|
278
|
-
const output = `${ prefix }[+${ timeDiff }ms][${ source }]${ messagePrefix }: ${ message }`;
|
|
279
|
-
|
|
280
|
-
console.log( output, ...params );
|
|
281
|
-
|
|
282
|
-
Logger.lastLogTime = Date.now();
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
export default Logger;
|
|
287
|
-
|
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
* @description a logger for web browser.
|
|
4
|
-
*
|
|
5
|
-
* TODO:
|
|
6
|
-
* - Add dark/light switch for chrome devtools.
|
|
7
|
-
* - https://stackoverflow.com/questions/41961037/is-there-a-way-to-detect-if-chromes-devtools-are-using-dark-mode
|
|
8
|
-
* - Maybe 'zenflux-logging` should be chrome extension.
|
|
9
|
-
*/
|
|
10
|
-
import { ObjectBase } from "../../bases/object-base";
|
|
11
|
-
|
|
12
|
-
import { getHexColorDelta, reduceCircularReferences } from "./utils";
|
|
13
|
-
|
|
14
|
-
import type * as interfaces from "../../interfaces";
|
|
15
|
-
|
|
16
|
-
// TODO: Should by dynamic/configure-able.
|
|
17
|
-
const MAX_MAPPING_RECURSIVE_DEPTH = 4,
|
|
18
|
-
UNKNOWN_CALLER_NAME = "anonymous function",
|
|
19
|
-
MAX_COLOR_ATTEMPTS = 25, // Stop color generation from looping forever when palette is saturated.
|
|
20
|
-
FALLBACK_PALETTE = [
|
|
21
|
-
"#EF4444", // red-500
|
|
22
|
-
"#F59E0B", // amber-500
|
|
23
|
-
"#10B981", // emerald-500
|
|
24
|
-
"#3B82F6", // blue-500
|
|
25
|
-
"#6366F1", // indigo-500
|
|
26
|
-
"#EC4899", // pink-500
|
|
27
|
-
"#8B5CF6", // violet-500
|
|
28
|
-
"#14B8A6", // teal-500
|
|
29
|
-
"#F97316", // orange-500
|
|
30
|
-
"#A855F7", // purple-500
|
|
31
|
-
"#22D3EE", // cyan-400
|
|
32
|
-
"#84CC16", // lime-500
|
|
33
|
-
]; // Predefined distinct colors for guaranteed fallback.
|
|
34
|
-
|
|
35
|
-
// TODO: Should by dynamic/configure-able.
|
|
36
|
-
Error.stackTraceLimit = 50;
|
|
37
|
-
|
|
38
|
-
export abstract class LoggerBrowserInfra extends ObjectBase {
|
|
39
|
-
public static mappers: Function[] = [];
|
|
40
|
-
public static mapperDepth = 0;
|
|
41
|
-
|
|
42
|
-
public static colorsOwners: { [ key: string ]: string } = {};
|
|
43
|
-
public static colorsUsed: string[] = [];
|
|
44
|
-
|
|
45
|
-
public defaultStyle: string[];
|
|
46
|
-
|
|
47
|
-
protected readonly owner: ObjectBase | typeof ObjectBase | string;
|
|
48
|
-
|
|
49
|
-
protected args: {
|
|
50
|
-
repeatedly: boolean;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
protected color: string;
|
|
54
|
-
|
|
55
|
-
private outputHandler: Function = console.log;
|
|
56
|
-
|
|
57
|
-
public static getName() {
|
|
58
|
-
return "ZenFlux/Logging/Modules/LoggerBrowserInfra";
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Reset logger globals.
|
|
63
|
-
*/
|
|
64
|
-
public static reset() {
|
|
65
|
-
LoggerBrowserInfra.mappers = [];
|
|
66
|
-
LoggerBrowserInfra.mapperDepth = 0;
|
|
67
|
-
LoggerBrowserInfra.colorsUsed = [];
|
|
68
|
-
LoggerBrowserInfra.colorsOwners = {};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Creates a custom mapper for a specific class type.
|
|
73
|
-
*
|
|
74
|
-
* The mapper is a callback function that can modify or enhance objects mapping.
|
|
75
|
-
*/
|
|
76
|
-
public static createObjectMapper( callback: Function ) {
|
|
77
|
-
LoggerBrowserInfra.mappers.push( callback );
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Gets mapped version of the object by using custom created object mapper.
|
|
82
|
-
*/
|
|
83
|
-
public static useObjectMapper( obj: object, shouldHandleChildren = true ) {
|
|
84
|
-
if ( ! obj || Array.isArray( obj ) || "object" !== typeof obj ) {
|
|
85
|
-
return obj;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
LoggerBrowserInfra.mapperDepth++;
|
|
89
|
-
|
|
90
|
-
// Result will lose typeof(obj), instanceOf will not work, now based on callback.
|
|
91
|
-
let result = { ... obj } as { [ key: string ]: any };
|
|
92
|
-
|
|
93
|
-
// Run all mappers.
|
|
94
|
-
LoggerBrowserInfra.mappers.forEach( ( mapper ) => {
|
|
95
|
-
result = mapper( obj ) || result;
|
|
96
|
-
} );
|
|
97
|
-
|
|
98
|
-
// Prevent infinite recursive.
|
|
99
|
-
if ( LoggerBrowserInfra.mapperDepth >= MAX_MAPPING_RECURSIVE_DEPTH ) {
|
|
100
|
-
LoggerBrowserInfra.mapperDepth--;
|
|
101
|
-
return result;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Children handling.
|
|
105
|
-
if ( shouldHandleChildren && result ) {
|
|
106
|
-
Object.entries( result ).forEach( ( [ key, value ] ) => {
|
|
107
|
-
result[ key ] = this.useObjectMapper( value, true );
|
|
108
|
-
} );
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
LoggerBrowserInfra.mapperDepth--;
|
|
112
|
-
|
|
113
|
-
return result;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public constructor( owner: ObjectBase | typeof ObjectBase | string, args = {} ) {
|
|
117
|
-
super();
|
|
118
|
-
|
|
119
|
-
this.args = {
|
|
120
|
-
repeatedly: false,
|
|
121
|
-
|
|
122
|
-
... args,
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
this.owner = owner;
|
|
126
|
-
|
|
127
|
-
this.initialize();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
protected getOwnerName() {
|
|
131
|
-
if ( typeof this.owner === "string" ) {
|
|
132
|
-
return this.owner;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if ( this.owner instanceof ObjectBase ) {
|
|
136
|
-
return this.owner.getName();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if ( this.owner && typeof ( this.owner as typeof ObjectBase ).getName === "function" ) {
|
|
140
|
-
return ( this.owner as typeof ObjectBase ).getName();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return "UnknownOwner";
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
protected initialize() {
|
|
147
|
-
const ownerName = this.getOwnerName();
|
|
148
|
-
|
|
149
|
-
if ( this.args.repeatedly && LoggerBrowserInfra.colorsOwners[ ownerName ] ) {
|
|
150
|
-
this.color = LoggerBrowserInfra.colorsOwners[ ownerName ];
|
|
151
|
-
} else {
|
|
152
|
-
this.color = this.getRandomColor();
|
|
153
|
-
|
|
154
|
-
LoggerBrowserInfra.colorsUsed.push( this.color );
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
LoggerBrowserInfra.colorsOwners[ ownerName ] = this.color;
|
|
158
|
-
|
|
159
|
-
this.defaultStyle = [
|
|
160
|
-
"color: grey;font-size:7px",
|
|
161
|
-
"display: block",
|
|
162
|
-
`color: ${ this.color }`,
|
|
163
|
-
"color: grey",
|
|
164
|
-
"font-weight: bold",
|
|
165
|
-
"color: #607D8B",
|
|
166
|
-
"font-size: 16px;color: red;font-weight:800",
|
|
167
|
-
];
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
public output( ... args: any ) {
|
|
171
|
-
this.outputHandler.apply( this, args );
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
protected setOutputHandler( outputHandler: Function ) {
|
|
175
|
-
this.outputHandler = outputHandler;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
protected printFunctionNotify( prefix: string, caller: interfaces.TCaller, output: any ) {
|
|
179
|
-
const callerName = this.getCallerName( caller );
|
|
180
|
-
|
|
181
|
-
this.output.apply(
|
|
182
|
-
this,
|
|
183
|
-
[ `%c(${ prefix })-> %c%c${ this.getOwnerName() }%c::%c${ callerName }%c() ${ output }%c` ].concat(
|
|
184
|
-
this.defaultStyle
|
|
185
|
-
)
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
protected printObjectEfficient( prefix: string, caller: interfaces.TCaller, params: string|object ) {
|
|
190
|
-
params = Object.assign( {}, params );
|
|
191
|
-
|
|
192
|
-
if ( typeof params === "string" ) {
|
|
193
|
-
this.printInLineString( prefix, caller, params );
|
|
194
|
-
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if ( Object.keys( params ).length === 1 ) {
|
|
199
|
-
const key = Object.keys( params )[ 0 ];
|
|
200
|
-
let value = Object.values( params )[ 0 ];
|
|
201
|
-
|
|
202
|
-
// TODO: Check is repeated logic, handle it.
|
|
203
|
-
if ( "object" === typeof value ) {
|
|
204
|
-
this.printInNextLineObject( prefix, caller, key, value || {} );
|
|
205
|
-
} else if ( "function" === typeof value ) {
|
|
206
|
-
this.printInLineFunction( prefix, caller, key, value );
|
|
207
|
-
} else {
|
|
208
|
-
this.printInLineElement( prefix, caller, key, value );
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
this.printMultiLineObject( prefix, caller, params as any );
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
protected printInLineElement( prefix: string, caller: interfaces.TCaller, key: string, value: any, notice = "" ) {
|
|
218
|
-
const callerName = this.getCallerName( caller ),
|
|
219
|
-
ownerName = this.getOwnerName(),
|
|
220
|
-
format = notice.length ?
|
|
221
|
-
`%c(${ prefix })-> %c%c${ ownerName }%c::%c${ callerName }%c() ->> [${ notice }] ->> ${ key }: '${ value }'%c` :
|
|
222
|
-
`%c(${ prefix })-> %c%c${ ownerName }%c::%c${ callerName }%c() ->> ${ key }: '${ value }'%c`;
|
|
223
|
-
|
|
224
|
-
this.output.apply( this, [ format ].concat( this.defaultStyle ) );
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
protected printInLineFunction( prefix: string, caller: interfaces.TCaller, key: string, fn: string | Function ) {
|
|
228
|
-
fn = this.getFunctionView( fn );
|
|
229
|
-
|
|
230
|
-
this.printInLineElement( prefix, caller, key, fn );
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
protected printInLineString( prefix: string, caller: interfaces.TCaller, string: string ) {
|
|
234
|
-
this.printInLineElement( prefix, caller, "(string)", string );
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
protected printInNextLineObject( prefix: string, caller: interfaces.TCaller, key: string, obj: object ) {
|
|
238
|
-
const mapped = LoggerBrowserInfra.useObjectMapper( obj ),
|
|
239
|
-
callerName = this.getCallerName( caller );
|
|
240
|
-
|
|
241
|
-
this.output.apply(
|
|
242
|
-
this,
|
|
243
|
-
[
|
|
244
|
-
`%c(${ prefix })-> %c%c${ this.getOwnerName() }%c::%c${ callerName }%c() ->> ${ key } %cā`,
|
|
245
|
-
].concat( this.defaultStyle )
|
|
246
|
-
);
|
|
247
|
-
|
|
248
|
-
// print in next line
|
|
249
|
-
this.output( mapped );
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
protected printMultiLineObject( prefix: string, caller: interfaces.TCaller, obj: { [ key: string ]: string | Function } ) {
|
|
253
|
-
const callerName = this.getCallerName( caller );
|
|
254
|
-
|
|
255
|
-
this.output.apply(
|
|
256
|
-
this,
|
|
257
|
-
[
|
|
258
|
-
`%c(${ prefix })-> %c%c${ this.getOwnerName() }%c::%c${ callerName }%c(${ Object.keys( obj )
|
|
259
|
-
.join( ", " ) }) %cā`,
|
|
260
|
-
].concat( this.defaultStyle )
|
|
261
|
-
);
|
|
262
|
-
|
|
263
|
-
for ( let key in obj ) {
|
|
264
|
-
let value = obj[ key ];
|
|
265
|
-
|
|
266
|
-
if ( typeof value === "object" ) {
|
|
267
|
-
const mapped = LoggerBrowserInfra.useObjectMapper( value );
|
|
268
|
-
|
|
269
|
-
value = JSON.stringify( mapped, reduceCircularReferences() );
|
|
270
|
-
} else if ( typeof obj[ key ] == "function" ) {
|
|
271
|
-
value = this.getFunctionView( value );
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
this.output.apply( this, [
|
|
275
|
-
"%c" + key + ": `" + value + "`",
|
|
276
|
-
"color: #a3a3a3",
|
|
277
|
-
] );
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
private getRandomColor(): string {
|
|
282
|
-
const hex = "0123456789ABCDEF";
|
|
283
|
-
let attempt = 0;
|
|
284
|
-
let similarityThreshold = 0.8;
|
|
285
|
-
let lastTried = "#000000";
|
|
286
|
-
|
|
287
|
-
while ( attempt < MAX_COLOR_ATTEMPTS ) {
|
|
288
|
-
let color = "#";
|
|
289
|
-
|
|
290
|
-
for ( let i = 0 ; i < 6 ; i++ ) {
|
|
291
|
-
color += hex[ Math.floor( Math.random() * 16 ) ];
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
lastTried = color;
|
|
295
|
-
|
|
296
|
-
const similar = LoggerBrowserInfra.colorsUsed.some( ( value ) => {
|
|
297
|
-
// it returns the ratio of difference... closer to 1.0 is less difference.
|
|
298
|
-
return getHexColorDelta( color, value ) >= similarityThreshold;
|
|
299
|
-
} );
|
|
300
|
-
|
|
301
|
-
if ( ! similar ) {
|
|
302
|
-
return color;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
attempt++;
|
|
306
|
-
|
|
307
|
-
// Gradually relax the threshold to guarantee termination if palette is crowded.
|
|
308
|
-
if ( attempt % 5 === 0 && similarityThreshold > 0.6 ) {
|
|
309
|
-
similarityThreshold -= 0.05;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// Fallback: pick a color from a predefined palette to guarantee termination.
|
|
314
|
-
// Prefer an unused palette color; otherwise reuse by index to preserve determinism.
|
|
315
|
-
const unusedPaletteColor = FALLBACK_PALETTE.find( ( paletteColor ) => {
|
|
316
|
-
return ! LoggerBrowserInfra.colorsUsed.includes( paletteColor );
|
|
317
|
-
} );
|
|
318
|
-
|
|
319
|
-
if ( unusedPaletteColor ) {
|
|
320
|
-
return unusedPaletteColor;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
return FALLBACK_PALETTE[ LoggerBrowserInfra.colorsUsed.length % FALLBACK_PALETTE.length ] || lastTried;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
protected getCallerName( caller: interfaces.TCaller ) {
|
|
327
|
-
if ( "function" === typeof caller ) {
|
|
328
|
-
return caller.prototype instanceof ObjectBase ? "constructor" : caller.name;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
throw new Error( "Invalid caller" );
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
private getFunctionView( fn: string | Function ): ( string | Function ) {
|
|
335
|
-
let fReturn = UNKNOWN_CALLER_NAME;
|
|
336
|
-
|
|
337
|
-
// TODO: Check if needed.
|
|
338
|
-
if ( typeof fn !== "string" && fn.name.length !== 0 ) {
|
|
339
|
-
fReturn = fn.name.split( " " )[ 1 ] + "()";
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return fReturn;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
declare module "picocolors" {
|
|
2
|
-
const pc: {
|
|
3
|
-
( input: string ): string;
|
|
4
|
-
red( input: string ): string;
|
|
5
|
-
blue( input: string ): string;
|
|
6
|
-
green( input: string ): string;
|
|
7
|
-
yellow( input: string ): string;
|
|
8
|
-
gray( input: string ): string;
|
|
9
|
-
white( input: string ): string;
|
|
10
|
-
bold( input: string ): string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default pc;
|
|
14
|
-
}
|
|
15
|
-
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reduce circular references.
|
|
3
|
-
*
|
|
4
|
-
* @copyright https://stackoverflow.com/a/53731154
|
|
5
|
-
*/
|
|
6
|
-
export const reduceCircularReferences = () => {
|
|
7
|
-
const seen = new WeakSet();
|
|
8
|
-
|
|
9
|
-
return ( key: any, value: any ) => {
|
|
10
|
-
if ( typeof value === "object" && value !== null ) {
|
|
11
|
-
if ( seen.has( value ) ) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
seen.add( value );
|
|
15
|
-
}
|
|
16
|
-
return value;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
|