1- // COPYRIGHT 2009, 2010, 2011, 2012, 2013 by the Open Rails project.
2- //
1+ // COPYRIGHT 2009 - 2024 by the Open Rails project.
2+ //
33// This file is part of Open Rails.
4- //
4+ //
55// Open Rails is free software: you can redistribute it and/or modify
66// it under the terms of the GNU General Public License as published by
77// the Free Software Foundation, either version 3 of the License, or
88// (at your option) any later version.
9- //
9+ //
1010// Open Rails is distributed in the hope that it will be useful,
1111// but WITHOUT ANY WARRANTY; without even the implied warranty of
1212// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313// GNU General Public License for more details.
14- //
14+ //
1515// You should have received a copy of the GNU General Public License
1616// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717
2020using System . IO ;
2121using System . Text ;
2222
23- namespace Orts . Common
23+ namespace ORTS . Common
2424{
25- public class NullLogger : TextWriter
26- {
27- public NullLogger ( )
28- {
29- }
30-
31- public override Encoding Encoding
32- {
33- get
34- {
35- return Encoding . UTF8 ;
36- }
37- }
38-
39- public override void Write ( char value )
40- {
41- }
42- }
43-
44- public class FileTeeLogger : TextWriter
45- {
46- public readonly string FileName ;
47- public readonly TextWriter Console ;
48-
49- public FileTeeLogger ( string fileName , TextWriter console )
50- {
51- FileName = fileName ;
52- Console = console ;
53- }
54-
55- public override Encoding Encoding
56- {
57- get
58- {
59- return Encoding . UTF8 ;
60- }
61- }
62-
63- public override void Write ( char value )
64- {
65- // Everything in TextWriter boils down to Write(char), but
66- // actually implementing just this would be horribly inefficient
67- // since we open and close the file every time. Instead, we
68- // implement Write(string) and Write(char[], int, int) which
69- // should mean we only end up here if called directly by user
70- // code. Which we won't support unless necessary.
71- throw new NotImplementedException ( ) ;
72- }
73-
74- public override void Write ( string value )
75- {
76- Console . Write ( value ) ;
77- using ( var writer = new StreamWriter ( FileName , true , Encoding ) )
78- {
79- writer . Write ( value ) ;
80- }
81- }
82-
83- public override void Write ( char [ ] buffer , int index , int count )
84- {
85- Write ( new String ( buffer , index , count ) ) ;
86- }
87- }
88-
8925 public class ORTraceListener : TraceListener
9026 {
9127 public readonly TextWriter Writer ;
9228 public readonly bool OnlyErrors ;
9329 public readonly int [ ] Counts = new int [ 5 ] ;
94- bool LastWrittenFormatted ;
30+ int LinesNeeded ;
9531
9632 public ORTraceListener ( TextWriter writer )
9733 : this ( writer , false )
@@ -128,17 +64,13 @@ void TraceEventInternal(TraceEventCache eventCache, string source, TraceEventTyp
12864 var errorLevel = ( int ) Math . Round ( Math . Log ( ( int ) eventType ) / Math . Log ( 2 ) ) ;
12965 if ( errorLevel < Counts . Length )
13066 Counts [ errorLevel ] ++ ;
131-
67+
13268 // Event is less important than error (and critical) and we're logging only errors... bail.
13369 if ( eventType > TraceEventType . Error && OnlyErrors )
13470 return ;
13571
13672 var output = new StringBuilder ( ) ;
137- if ( ! LastWrittenFormatted )
138- {
139- output . AppendLine ( ) ;
140- output . AppendLine ( ) ;
141- }
73+ while ( LinesNeeded -- > 0 ) output . AppendLine ( ) ;
14274 output . Append ( eventType ) ;
14375 output . Append ( ": " ) ;
14476 if ( args . Length == 0 )
@@ -171,15 +103,15 @@ void TraceEventInternal(TraceEventCache eventCache, string source, TraceEventTyp
171103
172104 output . AppendLine ( ) ;
173105 Writer . Write ( output ) ;
174- LastWrittenFormatted = true ;
106+ LinesNeeded = 0 ;
175107 }
176108
177109 public override void Write ( string message )
178110 {
179111 if ( ! OnlyErrors )
180112 {
181113 Writer . Write ( message ) ;
182- LastWrittenFormatted = false ;
114+ LinesNeeded = 2 ;
183115 }
184116 }
185117
@@ -188,7 +120,7 @@ public override void WriteLine(string message)
188120 if ( ! OnlyErrors )
189121 {
190122 Writer . WriteLine ( message ) ;
191- LastWrittenFormatted = false ;
123+ LinesNeeded = 1 ;
192124 }
193125 }
194126
@@ -206,7 +138,6 @@ public override void WriteLine(object o)
206138 else if ( ! OnlyErrors )
207139 {
208140 base . WriteLine ( o ) ;
209- LastWrittenFormatted = false ;
210141 }
211142 }
212143
@@ -216,13 +147,4 @@ class LogicalOperation
216147 {
217148 }
218149 }
219-
220- public sealed class FatalException : Exception
221- {
222- public FatalException ( Exception innerException )
223- : base ( "A fatal error has occurred" , innerException )
224- {
225- Debug . Assert ( innerException != null , "The inner exception of a FatalException must not be null." ) ;
226- }
227- }
228150}
0 commit comments