132 lines
3.1 KiB
TypeScript
132 lines
3.1 KiB
TypeScript
import { LogColors } from "./LogColors";
|
|
import { Texts } from "./Texts";
|
|
|
|
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\((.+)\)/;
|
|
|
|
static readonly logAlwaysLineInfo = true;
|
|
|
|
|
|
static _parseLineResult( line:string ):RegExpExecArray
|
|
{
|
|
let result = RJLog.matcherWithFunction.exec( line ) ||
|
|
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.stack ) );
|
|
}
|
|
|
|
static _formatErrorMessage( stackTrace:string, color:string = RJLog.errorMessageColor )
|
|
{
|
|
let lines = Texts.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( "" );
|
|
}
|
|
|
|
static getLineInfo( color:string = RJLog.logColor, stackTrace?:string, lineIndex:number = 3 )
|
|
{
|
|
stackTrace = stackTrace || ( new Error().stack + "" );
|
|
|
|
let lines = Texts.splitLines( stackTrace );
|
|
|
|
let result:RegExpExecArray = null;
|
|
|
|
while ( ! result && lineIndex < lines.length )
|
|
{
|
|
let line = lines[ lineIndex ];
|
|
|
|
result = RJLog._parseLineResult( line ) ;
|
|
|
|
lineIndex ++;
|
|
}
|
|
|
|
|
|
if ( ! result )
|
|
{
|
|
console.log( stackTrace );
|
|
return color + " <Unknown> " + RJLog.resetColor ;
|
|
}
|
|
|
|
return color + " " + result[ 1 ] + "(" + result[ 2 ] + ") " + RJLog.resetColor;
|
|
}
|
|
|
|
|
|
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 );
|
|
}
|
|
} |