172 lines
3.6 KiB
C#
172 lines
3.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System;
|
|
using System.Linq;
|
|
using Godot;
|
|
|
|
namespace Rokojori
|
|
{
|
|
public class RJLog
|
|
{
|
|
|
|
public static string Stringify( object obj )
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
Stringify( obj, sb );
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static void Stringify( object obj, StringBuilder output )
|
|
{
|
|
if ( obj == null )
|
|
{
|
|
output.Append( "<null>" );
|
|
return;
|
|
}
|
|
|
|
if ( obj is float )
|
|
{
|
|
var floatValue = (float) obj;
|
|
var doubleValue = (double) floatValue;
|
|
output.Append( RegexUtility.NumberToString( doubleValue ) );
|
|
return;
|
|
}
|
|
|
|
if ( obj is double )
|
|
{
|
|
output.Append( RegexUtility.NumberToString( (double)obj ) );
|
|
return;
|
|
}
|
|
|
|
if ( obj is IList && obj.GetType().IsGenericType )
|
|
{
|
|
var list = (IList)obj;
|
|
|
|
output.Append( "[" );
|
|
|
|
var first = true;
|
|
|
|
foreach ( var it in list )
|
|
{
|
|
if ( first )
|
|
{
|
|
first = false;
|
|
}
|
|
else
|
|
{
|
|
output.Append( ", " );
|
|
}
|
|
|
|
|
|
Stringify( it, output );
|
|
}
|
|
|
|
output.Append( "]" );
|
|
|
|
return;
|
|
}
|
|
|
|
output.Append( obj.ToString() );
|
|
}
|
|
|
|
static void LogMessage( string message )
|
|
{
|
|
var trace = GetTrace();
|
|
GD.PrintRich("\n[b]" + message + "[/b]" );
|
|
GD.PrintRich( trace );
|
|
}
|
|
|
|
static void LogMessageWithFullTrace( string message )
|
|
{
|
|
var trace = GetFullTrace();
|
|
GD.PrintRich("\n[b]" + message + "[/b]" );
|
|
GD.PrintRich( trace );
|
|
}
|
|
|
|
static void LogErrorMessage( string message )
|
|
{
|
|
var trace = GetTrace();
|
|
GD.PrintErr( "\n"+ message );
|
|
GD.PrintRich( trace );
|
|
}
|
|
|
|
static string GetFullTrace()
|
|
{
|
|
return ( new System.Diagnostics.StackTrace( true ) ).ToString();
|
|
}
|
|
|
|
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]" ;
|
|
}
|
|
|
|
public static void LogWithFullTrace( params object[] objects)
|
|
{
|
|
LogMessageWithFullTrace( GetLogString( objects ) );
|
|
}
|
|
|
|
public static void Log( params object[] objects )
|
|
{
|
|
LogMessage( GetLogString( objects ) );
|
|
}
|
|
|
|
public static void Error( params object[] objects)
|
|
{
|
|
LogErrorMessage( GetLogString( objects ) );
|
|
}
|
|
|
|
public static string GetLogString( object[] objects)
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
for ( int i = 0; i < objects.Length; i++ )
|
|
{
|
|
if ( i != 0 )
|
|
{
|
|
sb.Append( " " );
|
|
}
|
|
|
|
Stringify( objects[ i ], sb );
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
|
|
}
|