OSDN Git Service

d89ff96c62606f533bdd48a902673117258ae1f8
[dtxmania/dtxmania.git] / FDK / コード / 00.共通 / CTraceLogListener.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Diagnostics;
6
7 namespace FDK
8 {
9         public class CTraceLogListener : TraceListener
10         {
11                 public CTraceLogListener( StreamWriter stream )
12                 {
13                         this.streamWriter = stream;
14                 }
15
16                 public override void Flush()
17                 {
18                         if( this.streamWriter != null )
19                         {
20                                 try
21                                 {
22                                         this.streamWriter.Flush();
23                                 }
24                                 catch( ObjectDisposedException )
25                                 {
26                                 }
27                         }
28                 }
29                 public override void TraceEvent( TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message )
30                 {
31                         if( this.streamWriter != null )
32                         {
33                                 try
34                                 {
35                                         this.tイベント種別を出力する( eventType );
36                                         this.tインデントを出力する();
37                                         this.streamWriter.WriteLine( message );
38                                 }
39                                 catch( ObjectDisposedException )
40                                 {
41                                 }
42                         }
43                 }
44                 public override void TraceEvent( TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args )
45                 {
46                         if( this.streamWriter != null )
47                         {
48                                 try
49                                 {
50                                         this.tイベント種別を出力する( eventType );
51                                         this.tインデントを出力する();
52                                         this.streamWriter.WriteLine( string.Format( format, args ) );
53                                 }
54                                 catch( ObjectDisposedException )
55                                 {
56                                 }
57                         }
58                 }
59                 public override void Write( string message )
60                 {
61                         if( this.streamWriter != null )
62                         {
63                                 try
64                                 {
65                                         this.streamWriter.Write( message );
66                                 }
67                                 catch( ObjectDisposedException )
68                                 {
69                                 }
70                         }
71                 }
72                 public override void WriteLine( string message )
73                 {
74                         if( this.streamWriter != null )
75                         {
76                                 try
77                                 {
78                                         this.streamWriter.WriteLine( message );
79                                 }
80                                 catch( ObjectDisposedException )
81                                 {
82                                 }
83                         }
84                 }
85
86                 protected override void Dispose( bool disposing )
87                 {
88                         if( this.streamWriter != null )
89                         {
90                                 try
91                                 {
92                                         this.streamWriter.Close();
93                                 }
94                                 catch
95                                 {
96                                 }
97                                 this.streamWriter = null;
98                         }
99                         base.Dispose( disposing );
100                 }
101
102                 #region [ private ]
103                 //-----------------
104                 private StreamWriter streamWriter;
105
106                 private void tイベント種別を出力する( TraceEventType eventType )
107                 {
108                         if( this.streamWriter != null )
109                         {
110                                 try
111                                 {
112                                         var now = DateTime.Now;
113                                         this.streamWriter.Write( string.Format( "{0:D4}/{1:D2}/{2:D2} {3:D2}:{4:D2}:{5:D2}.{6:D3} ", new object[] { now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond } ) );
114                                         switch( eventType )
115                                         {
116                                                 case TraceEventType.Error:
117                                                         this.streamWriter.Write( "[ERROR] " );
118                                                         return;
119
120                                                 case ( TraceEventType.Error | TraceEventType.Critical ):
121                                                         return;
122
123                                                 case TraceEventType.Warning:
124                                                         this.streamWriter.Write( "[WARNING] " );
125                                                         return;
126
127                                                 case TraceEventType.Information:
128                                                         break;
129
130                                                 default:
131                                                         return;
132                                         }
133                                         this.streamWriter.Write( "[INFO] " );
134                                 }
135                                 catch( ObjectDisposedException )
136                                 {
137                                 }
138                         }
139                 }
140                 private void tインデントを出力する()
141                 {
142                         if( ( this.streamWriter != null ) && ( base.IndentLevel > 0 ) )
143                         {
144                                 try
145                                 {
146                                         for( int i = 0; i < base.IndentLevel; i++ )
147                                                 this.streamWriter.Write( "    " );
148                                 }
149                                 catch( ObjectDisposedException )
150                                 {
151                                 }
152                         }
153                 }
154                 //-----------------
155                 #endregion
156         }
157 }