rj-action-library/Runtime/Logging/RJLog.cs

111 lines
2.3 KiB
C#
Raw Normal View History

2024-05-05 07:52:06 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
using System.Linq;
using Godot;
namespace Rokojori
{
public class RJLog
{
static void Stringify( object obj, StringBuilder output )
{
2024-05-12 17:03:20 +00:00
if ( obj == null )
{
output.Append( "<null>" );
return;
}
2024-05-05 07:52:06 +00:00
output.Append( obj.ToString() );
}
static void LogMessage( string message )
{
2024-05-12 17:03:20 +00:00
var trace = GetTrace();
GD.PrintRich("[b]" + message + "[/b]");
GD.PrintRich( trace );
}
static void LogErrorMessage( string message )
{
var trace = GetTrace();
GD.PrintErr( message );
GD.PrintRich( trace );
}
static string GetTrace()
{
var stackTrace = new System.Diagnostics.StackTrace( true );
var frameIndex = 3;
var frame = stackTrace.GetFrame( frameIndex );
var className = frame.GetFileName();
if ( className != null )
{
var slashIndex = -1;
for ( int i = className.Length - 1; i >= 0 && slashIndex == -1; i-- )
{
if ( className[ i ] == '\\' || className[ i ] == '/' )
{
slashIndex = i;
}
}
if ( slashIndex != -1 )
{
var end = className.EndsWith( ".cs" ) ? className.Length - 3 : className.Length;
className = className.Substring( slashIndex + 1, end - ( slashIndex + 1 ) );
}
}
if ( className == null )
{
className ="<Unknown>";
}
var trace = className + "." + frame.GetMethod().Name + "() ln."+ frame.GetFileLineNumber();
return "[color=gray] " + trace + "[/color]" ;
2024-05-05 07:52:06 +00:00
}
public static void Log( params object[] objects)
{
var sb = new StringBuilder();
for ( int i = 0; i < objects.Length; i++ )
{
if ( i != 0 )
{
sb.Append( " " );
}
Stringify( objects[ i ], sb );
}
LogMessage( sb.ToString() );
}
2024-05-12 17:03:20 +00:00
public static void Error( params object[] objects)
{
var sb = new StringBuilder();
for ( int i = 0; i < objects.Length; i++ )
{
if ( i != 0 )
{
sb.Append( " " );
}
Stringify( objects[ i ], sb );
}
LogErrorMessage( sb.ToString() );
}
2024-05-05 07:52:06 +00:00
}
}