2026-07-17 18:34:36 +00:00
|
|
|
import { LogColors } from "./LogColors";
|
|
|
|
|
|
|
|
|
|
export class RJLog
|
|
|
|
|
{
|
|
|
|
|
static readonly errorColor = LogColors.red_Background;
|
|
|
|
|
static readonly errorMessageColor = LogColors.red_Foreground;
|
|
|
|
|
static readonly warnColor = LogColors.yellow_Background;
|
|
|
|
|
static readonly logColor = LogColors.gray_Background
|
|
|
|
|
static readonly resetColor = LogColors.reset;
|
|
|
|
|
|
|
|
|
|
static readonly matcherWithFunction = /^\s+at\s(.+)\s\(.+?:(\d+:\d+)\)/;
|
|
|
|
|
static readonly matcherFile = /\(.+?\\(\w+)\.js:(\d+:\d+)\)/;
|
|
|
|
|
static readonly matcherAnonymous = /^\s+at\s(.+)\s\((.+)\)/;
|
2026-07-17 19:12:31 +00:00
|
|
|
static readonly functionMatcher = /^\s+at\s(?:.*?)(\w+\.(?:ts|js)):(\d+:\d+)$/;
|
2026-07-17 18:34:36 +00:00
|
|
|
|
|
|
|
|
static readonly logAlwaysLineInfo = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static _parseLineResult( line:string ):RegExpExecArray|null
|
|
|
|
|
{
|
|
|
|
|
let result = RJLog.matcherWithFunction.exec( line ) ||
|
2026-07-17 19:11:37 +00:00
|
|
|
RJLog.functionMatcher.exec( line ) ||
|
2026-07-17 18:34:36 +00:00
|
|
|
RJLog.matcherFile.exec( line ) ||
|
|
|
|
|
RJLog.matcherAnonymous.exec( line );
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static _parseLine( line:string ):string
|
|
|
|
|
{
|
|
|
|
|
let result = RJLog._parseLineResult( line );
|
|
|
|
|
|
|
|
|
|
if ( result )
|
|
|
|
|
{
|
|
|
|
|
return " " + result[ 1 ] + "(" + result[ 2 ] + ") ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static logError( e:Error )
|
|
|
|
|
{
|
|
|
|
|
console.log( "\n" + RJLog._formatErrorMessage( ( e as any ) .stack ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static splitLines( text:string )
|
|
|
|
|
{
|
|
|
|
|
return text.split( /(?:\r\n)|\n|\r/g );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static _formatErrorMessage( stackTrace:string, color:string = RJLog.errorMessageColor )
|
|
|
|
|
{
|
|
|
|
|
let lines = RJLog.splitLines( stackTrace );
|
|
|
|
|
let output:string[] = [ color ];
|
|
|
|
|
|
|
|
|
|
lines.forEach(
|
|
|
|
|
( line, index ) =>
|
|
|
|
|
{
|
|
|
|
|
let lineInfo = RJLog._parseLine( line );
|
|
|
|
|
|
|
|
|
|
output.push( lineInfo );
|
|
|
|
|
|
|
|
|
|
if ( index !== lines.length - 1 )
|
|
|
|
|
{
|
|
|
|
|
output.push( "\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
output.push( RJLog.resetColor );
|
|
|
|
|
|
|
|
|
|
return output.join( "" );
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 19:21:53 +00:00
|
|
|
static getDateInfo()
|
|
|
|
|
{
|
|
|
|
|
const d = new Date();
|
|
|
|
|
const pad = ( n: number ) => String( n ).padStart( 2, '0' );
|
|
|
|
|
return `${ d.getFullYear() }-${ pad( d.getDate() ) }-${ pad( d.getMonth() + 1 ) } ${ pad( d.getHours() ) }:${ pad( d.getMinutes() ) } +${ d.getSeconds() }s`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 19:11:37 +00:00
|
|
|
static getLineInfo( color:string = RJLog.logColor, stackTrace?:string, lineIndex:number = 3 )
|
2026-07-17 18:34:36 +00:00
|
|
|
{
|
|
|
|
|
stackTrace = stackTrace || ( new Error().stack + "" );
|
|
|
|
|
|
|
|
|
|
let lines = RJLog.splitLines( stackTrace );
|
|
|
|
|
|
|
|
|
|
let result:RegExpExecArray|null = null;
|
|
|
|
|
|
|
|
|
|
while ( ! result && lineIndex < lines.length )
|
|
|
|
|
{
|
|
|
|
|
let line = lines[ lineIndex ];
|
|
|
|
|
|
|
|
|
|
result = RJLog._parseLineResult( line ) ;
|
|
|
|
|
|
|
|
|
|
lineIndex ++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! result )
|
|
|
|
|
{
|
2026-07-17 19:01:56 +00:00
|
|
|
console.log( "Could not parse stack trace:", stackTrace );
|
|
|
|
|
console.log( "StackTrack Lines:", lines );
|
2026-07-17 18:34:36 +00:00
|
|
|
return color + " <Unknown> " + RJLog.resetColor ;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 19:21:53 +00:00
|
|
|
|
|
|
|
|
return color + " " + result[ 1 ] + "(" + result[ 2 ] + ") " + RJLog.getDateInfo() + RJLog.resetColor;
|
2026-07-17 18:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static error( ...params:any[] )
|
|
|
|
|
{
|
|
|
|
|
if ( RJLog.logAlwaysLineInfo || typeof process === "object" )
|
|
|
|
|
{
|
|
|
|
|
let lineInfo = RJLog.getLineInfo( RJLog.errorColor );
|
|
|
|
|
console.log( "\n" + lineInfo );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.error.apply( console, params );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static warn( ...params:any[] )
|
|
|
|
|
{
|
|
|
|
|
if ( RJLog.logAlwaysLineInfo || typeof process === "object" )
|
|
|
|
|
{
|
|
|
|
|
let lineInfo = RJLog.getLineInfo( RJLog.warnColor );
|
|
|
|
|
console.log( "\n" + lineInfo );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.warn.apply( console, params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static log( ...params:any[] )
|
|
|
|
|
{
|
|
|
|
|
if ( RJLog.logAlwaysLineInfo || typeof process === "object" )
|
|
|
|
|
{
|
|
|
|
|
let lineInfo = RJLog.getLineInfo();
|
|
|
|
|
console.log( "\n" + lineInfo );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log.apply( console, params );
|
|
|
|
|
}
|
|
|
|
|
}
|