88namespace ModLoader
99{
1010
11- public class ModConsole
12- {
11+ public class ModConsole : MonoBehaviour
12+ {
13+ public static GameObject root ;
14+
1315 [ DllImport ( "kernel32.dll" ) ]
1416 private static extern IntPtr GetConsoleWindow ( ) ;
1517
@@ -19,24 +21,34 @@ public class ModConsole
1921 [ DllImport ( "Kernel32.dll" ) ]
2022 private static extern bool AllocConsole ( ) ;
2123
22- private static FilePath logFile ;
24+ private FilePath logFile ;
2325
2426 private const int CONSOLE_HIDE = 0 ;
2527
2628 private const int CONSOLE_SHOW = 5 ;
2729
2830 private bool visible = false ;
2931
30- public ModConsole ( )
32+ private void Awake ( )
3133 {
3234 ModConsole . AllocConsole ( ) ;
3335 Console . SetOut ( new StreamWriter ( Console . OpenStandardOutput ( ) )
3436 {
3537 AutoFlush = true
3638 } ) ;
3739 this . visible = true ;
38- DateTime current = new DateTime ( ) ;
39- ModConsole . logFile = FileLocations . BaseFolder . Extend ( "logs" ) . CreateFolder ( ) . ExtendToFile ( current . Year + "-" + current . Month + "-" + current . Day + ".txt" ) ;
40+ string date = string . Format ( "{0:yyyy_MM_dd}" , DateTime . UtcNow ) ;
41+ this . logFile = FileLocations . BaseFolder . Extend ( "logs" ) . CreateFolder ( ) . ExtendToFile ( date + ".txt" ) ;
42+ }
43+
44+ private void OnEnable ( )
45+ {
46+ Application . logMessageReceivedThreaded += this . handleLog ;
47+ }
48+
49+ private void OnDisable ( )
50+ {
51+ Application . logMessageReceivedThreaded -= this . handleLog ;
4052 }
4153
4254 /// <summary>
@@ -55,44 +67,52 @@ public void toggleConsole()
5567 this . visible = ! this . visible ;
5668 }
5769
58- /// <summary>
59- /// if you want to print a error with format
60- /// </summary>
61- /// <param name="e">Exception of your error</param>
62- public void logError ( Exception e )
70+ private void log ( string message )
6371 {
64- StackTrace stackTrace = new StackTrace ( e , true ) ;
65- StackFrame frame = stackTrace . GetFrame ( 0 ) ;
66- int fileColumnNumber = frame . GetFileColumnNumber ( ) ;
67- int fileLineNumber = frame . GetFileLineNumber ( ) ;
68- string fileName = frame . GetFileName ( ) ;
69- this . log ( "##[ERROR]##" , "ErrorReporter" , LogType . Error ) ;
70- this . log ( e . Message , "ErrorReporter" , LogType . Error ) ;
71- this . log ( e . StackTrace , "ErrorReporter" , LogType . Error ) ;
72- this . log ( fileLineNumber + ":" + fileColumnNumber + "@" + fileName , "ErrorReporter" , LogType . Error ) ;
73- this . log ( "##[ERROR]##" , "ErrorReporter" , LogType . Error ) ;
72+ Console . WriteLine ( $ "LOG: { message } ") ;
73+ this . logFile . AppendText ( $ "LOG: { message } ") ;
7474 }
7575
76- /// <summary>
77- /// Print message in console with format
78- /// </summary>
79- /// <param name="msg">Message that you want to ptint</param>
80- /// <param name="tag">tag to identify your log</param>
81- /// <param name="type">what kind of log is it</param>
82- public void log ( string msg , string tag , LogType type = LogType . Log )
76+ private void error ( string message )
8377 {
84- string logMessage = "[" + tag + "]: " + msg ;
85- Console . WriteLine ( logMessage ) ;
86- logFile . AppendText ( logMessage + "\n " ) ;
78+ Console . WriteLine ( $ "ERROR: { message } ") ;
79+ this . logFile . AppendText ( $ "ERROR: { message } ") ;
8780 }
8881
89- /// <summary>
90- /// Print message in console
91- /// </summary>
92- /// <param name="msg">message that you want to print</param>
93- public void log ( string msg )
82+ private void exception ( string message , string stackTrace )
83+ {
84+ Console . WriteLine ( $ "EXCEPTION: { message } ") ;
85+ Console . WriteLine ( stackTrace ) ;
86+ this . logFile . AppendText ( $ "EXCEPTION: { message } ") ;
87+ this . logFile . AppendText ( stackTrace ) ;
88+ }
89+
90+ private void warning ( string message , string stackTrace )
9491 {
95- this . log ( msg , "Unkwn" ) ;
92+ Console . WriteLine ( $ "WARNING: { message } ") ;
93+ Console . WriteLine ( stackTrace ) ;
94+ this . logFile . AppendText ( $ "WARNING: { message } ") ;
95+ this . logFile . AppendText ( stackTrace ) ;
96+ }
97+
98+ private void handleLog ( string message , string stackTrace , LogType type )
99+ {
100+ switch ( type )
101+ {
102+ case LogType . Error :
103+ this . error ( message ) ;
104+ break ;
105+ case LogType . Exception :
106+ this . exception ( message , stackTrace ) ;
107+ break ;
108+ case LogType . Warning :
109+ this . warning ( message , stackTrace ) ;
110+ break ;
111+ default :
112+ this . log ( message ) ;
113+ break ;
114+ }
115+
96116 }
97117
98118 }
0 commit comments