-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
235 lines (201 loc) · 5.16 KB
/
Logger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// CardFileRdr - Copyright © 2016 John Oliver
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Text;
using System.IO;
namespace CardFileRdr {
/// <summary>
/// A simple common logger
/// </summary>
public sealed class Logger : IDisposable {
/// <summary>
/// Write formats
/// </summary>
public enum Wrtformats {
/// <summary>
/// Output number in base 10
/// </summary>
NATURAL,
/// <summary>
/// Output number as Hex
/// </summary>
HEX
};
/// <summary>
/// Newline char
/// </summary>
public static string NL {
get {
return lF;
}
}
private static string lF = "\n";
private static string logSeparator = "________________________________________________________________________________";
private string logfileName = "CardFileRdr.log";
private StreamWriter logFile;
/// <summary>
/// Construct a logger with a given log file name but do not open the file yet.
/// Thus it may be referenced in code, but not used.
/// </summary>
/// <param name="inLogfile">Name and path of the logfile</param>
public Logger( string inLogfile ) {
if (!(inLogfile == null)) {
logfileName = inLogfile;
}
}//Logger
/// <summary>
/// Open the log for writing.
/// </summary>
public void open() {
logFile = new StreamWriter( logfileName, true, Encoding.UTF8 ); // true to append
logFile.WriteLine( logSeparator );
logFile.WriteLine( "Starting at: " + DateTime.Now.ToString() );
logFile.Flush();
}
/// <summary>
/// Explicitly close the log
/// </summary>
public void close() {
if (logFile == null)
return;
logFile.Flush();
logFile.Close();
logFile = null;
}
/// <summary>
/// Dispose of the object (override of IDisposable)
/// </summary>
public void Dispose() {
close();
}
/// <summary>
/// Write a new line to the log
/// </summary>
public void writeLn() {
if (logFile == null)
return;
logFile.WriteLine();
logFile.Flush();
}
/// <summary>
/// Write a text line to the log
/// </summary>
/// <param name="logmsg">The text</param>
public void writeLn( string logmsg ) {
if (logFile == null)
return;
logFile.WriteLine( logmsg );
logFile.Flush();
}
/// <summary>
/// Write a text line to the log
/// </summary>
/// <param name="a">The char array</param>
public void writeLn( char[] a ) {
if (logFile == null)
return;
logFile.Write( a );
writeLn();
logFile.Flush();
}
/// <summary>
/// Write text to the log
/// </summary>
/// <param name="logmsg">The text</param>
public void write( string logmsg ) {
if (logFile == null)
return;
logFile.Write( logmsg );
logFile.Flush();
}
/// <summary>
/// Write a char to the log
/// </summary>
/// <param name="c">The text</param>
public void write( char c ) {
if (logFile == null)
return;
logFile.Write( c );
logFile.Flush();
}
/// <summary>
/// Write a char array to the log
/// </summary>
/// <param name="a">The char array</param>
public void write( char[] a ) {
if (logFile == null)
return;
for (int i = 0; i < a.Length; i++) {
logFile.Write( a[i] );
}
logFile.Flush();
}
/// <summary>
/// Write a byte as hex to the log
/// </summary>
/// <param name="b">The byte</param>
/// <param name="opf">Write format</param>
public void write( byte b, Wrtformats opf ) {
if (logFile == null)
return;
if (opf == Wrtformats.HEX) {
logFile.Write( b.ToString( "X" ) );
} else {
logFile.Write( b );
}
logFile.Flush();
}
/// <summary>
/// write a byte array to the log as hex
/// </summary>
/// <param name="ba">The byte array</param>
/// <param name="opf">Write format</param>
public void write( byte[] ba, Wrtformats opf ) {
if (logFile == null)
return;
if (opf == Wrtformats.HEX) {
string str = BitConverter.ToString( ba ).Replace( "-", " " );
logFile.Write( str );
} else {
logFile.Write( ba );
}
logFile.Flush();
}
/// <summary>
/// Write a UInt32 to log
/// </summary>
/// <param name="ui">The data</param>
/// <param name="opf">Write format</param>
public void write( UInt32 ui, Wrtformats opf ) {
if (logFile == null)
return;
int len = 0;
if (opf == Wrtformats.HEX) {
string s = ui.ToString( "X" );
logFile.Write( s );
len = s.Length;
} else {
logFile.Write( ui );
len = ui.ToString( "X" ).Length;
}
logFile.Flush();
}
/// <summary>
/// Reset the byte count used by write(byte) (and write a newline)
/// </summary>
public void resetByteCount() {
if (logFile == null)
return;
logFile.Write( NL );
logFile.Flush();
}
}//class
}//namespace