MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
Log.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Diagnostics;
6
7namespace MyCaffe.basecode
8{
12 public class Log
13 {
14 string m_strPreText = "";
15 string m_strSource;
16 double m_dfProgress = 0;
17 bool m_bEnable = true;
18 bool m_bEnableTrace = false;
19
23 public event EventHandler<LogArg> OnWriteLine;
27 public event EventHandler<LogProgressArg> OnProgress;
28
33 public Log(string strSrc)
34 {
35 m_strSource = strSrc;
36 }
37
41 public bool Enable
42 {
43 set { m_bEnable = value; }
44 }
45
49 public bool IsEnabled
50 {
51 get { return m_bEnable; }
52 }
53
57 public string PreText
58 {
59 get { return m_strPreText; }
60 set { m_strPreText = value; }
61 }
62
66 public bool EnableTrace
67 {
68 get { return m_bEnableTrace; }
69 set { m_bEnableTrace = value; }
70 }
71
80 public void WriteLine(string str, bool bOverrideEnabled = false, bool bHeader = false, bool bError = false, bool bDisable = false)
81 {
82 // Check for enabled and not overridden
83 if (!m_bEnable && !bOverrideEnabled)
84 return;
85
86 string strLine;
87
88 if (!bHeader && m_strPreText != null && m_strPreText.Length > 0)
89 strLine = m_strPreText + str;
90 else
91 strLine = str;
92
93 if (OnWriteLine != null)
94 OnWriteLine(this, new LogArg(m_strSource, strLine, m_dfProgress, bError, bOverrideEnabled, bDisable));
95
96 if (m_bEnableTrace)
97 {
98 if (bHeader)
99 Trace.WriteLine(strLine);
100 else
101 Trace.WriteLine(m_dfProgress.ToString("P") + " " + strLine);
102 }
103 }
104
109 public void WriteHeader(string str)
110 {
111 if (!m_bEnable)
112 return;
113
114 string strLine = "";
115
116 strLine += "=============================================";
117 strLine += Environment.NewLine;
118 strLine += str;
119 strLine += Environment.NewLine;
120 strLine += "=============================================";
121 strLine += Environment.NewLine;
122
123 WriteLine(strLine, false, true);
124 }
125
130 public void WriteError(Exception e)
131 {
132 string strErr = e.Message;
133
134 if (e.InnerException != null)
135 strErr += " " + e.InnerException.Message;
136
137 if (strErr.Trim().Length == 0)
138 strErr = "No error message!";
139
140 WriteLine("ERROR! " + strErr, false, false, true);
141 }
142
146 public double Progress
147 {
148 get { return m_dfProgress; }
149 set
150 {
151 if (!m_bEnable)
152 return;
153
154 m_dfProgress = value;
155
156 if (OnProgress != null)
157 OnProgress(this, new LogProgressArg(m_strSource, m_dfProgress));
158 }
159 }
160
168 public void EXPECT_EQUAL<T>(double df1, double df2, string str = null)
169 {
170 if (typeof(T) == typeof(float))
171 {
172 if (str == null)
173 str = "Float Values " + df1.ToString() + " and " + df2.ToString() + " are NOT FLOAT equal!";
174
175 EXPECT_NEAR(df1, df2, 1e-2, str);
176 }
177 else
178 {
179 if (df1 != df2)
180 {
181 if (str == null)
182 str = "Values " + df1.ToString() + " and " + df2.ToString() + " are NOT equal!";
183
184 throw new Exception(str);
185 }
186 }
187 }
188
196 public void EXPECT_NEAR_FLOAT(double df1, double df2, double dfErr, string str = "")
197 {
198 float f1 = (float)df1;
199 float f2 = (float)df2;
200 float fErr = (float)dfErr;
201 float fDiff = Math.Abs(f1 - f2);
202
203 if (fDiff > fErr)
204 throw new Exception("Values " + f1.ToString() + " and " + f2.ToString() + " are NOT within the range " + fErr.ToString() + " of one another. " + str);
205 }
206
214 public void EXPECT_NEAR(double df1, double df2, double dfErr, string str = "")
215 {
216 double dfDiff = Math.Abs(df1 - df2);
217
218 if (dfDiff > dfErr)
219 throw new Exception("Values " + df1.ToString() + " and " + df2.ToString() + " are NOT within the range " + dfErr.ToString() + " of one another. " + str);
220 }
221
227 public void CHECK(bool b, string str)
228 {
229 if (!b)
230 throw new Exception(str);
231 }
232
239 public void CHECK_EQ(double df1, double df2, string str)
240 {
241 if (df1 != df2)
242 throw new Exception(str);
243 }
244
251 public void CHECK_NE(double df1, double df2, string str)
252 {
253 if (df1 == df2)
254 throw new Exception(str);
255 }
256
263 public void CHECK_LE(double df1, double df2, string str)
264 {
265 if (df1 > df2)
266 throw new Exception(str);
267 }
268
275 public void CHECK_LT(double df1, double df2, string str)
276 {
277 if (df1 >= df2)
278 throw new Exception(str);
279 }
280
287 public void CHECK_GE(double df1, double df2, string str)
288 {
289 if (df1 < df2)
290 throw new Exception(str);
291 }
292
299 public void CHECK_GT(double df1, double df2, string str)
300 {
301 if (df1 <= df2)
302 throw new Exception(str);
303 }
304
311 public void CHECK_EQ(float f1, float f2, string str)
312 {
313 if (f1 != f2)
314 throw new Exception(str);
315 }
316
323 public void CHECK_NE(float f1, float f2, string str)
324 {
325 if (f1 == f2)
326 throw new Exception(str);
327 }
328
335 public void CHECK_LE(float f1, float f2, string str)
336 {
337 if (f1 > f2)
338 throw new Exception(str);
339 }
340
347 public void CHECK_LT(float f1, float f2, string str)
348 {
349 if (f1 >= f2)
350 throw new Exception(str);
351 }
352
359 public void CHECK_GE(float f1, float f2, string str)
360 {
361 if (f1 < f2)
362 throw new Exception(str);
363 }
364
371 public void CHECK_GT(float f1, float f2, string str)
372 {
373 if (f1 <= f2)
374 throw new Exception(str);
375 }
376
384 public void CHECK_BOUNDS(float f1, float fmin, float fmax, string str)
385 {
386 CHECK_GE(f1, fmin, str);
387 CHECK_LE(f1, fmax, str);
388 }
389
394 public void FAIL(string str)
395 {
396 throw new Exception(str);
397 }
398 }
399}
The LogArg is passed as an argument to the Log::OnWriteLine event.
Definition: EventArgs.cs:53
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK(bool b, string str)
Test a flag for true.
Definition: Log.cs:227
void CHECK_LT(float f1, float f2, string str)
Test whether one number is less-than another.
Definition: Log.cs:347
bool IsEnabled
Returns whether or not the Log is enabled.
Definition: Log.cs:50
void CHECK_LE(float f1, float f2, string str)
Test whether one number is less-than or equal to another.
Definition: Log.cs:335
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
void EXPECT_EQUAL< T >(double df1, double df2, string str=null)
Test whether two values are equal using a given type 'T'.
Definition: Log.cs:168
bool Enable
Enables/disables the Log. When disabled, the Log does not output any data.
Definition: Log.cs:42
void CHECK_GE(float f1, float f2, string str)
Test whether one number is greater-than or equal to another.
Definition: Log.cs:359
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
Definition: Log.cs:394
double Progress
Get/set the progress associated with the Log.
Definition: Log.cs:147
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
void EXPECT_NEAR_FLOAT(double df1, double df2, double dfErr, string str="")
Test whether two numbers are within a range (dfErr) of one another using the float resolution.
Definition: Log.cs:196
EventHandler< LogProgressArg > OnProgress
The OnProgress event fires each time the Progress value is set.
Definition: Log.cs:27
void WriteHeader(string str)
Write a header as output.
Definition: Log.cs:109
string PreText
Get/set the pre-text prepended to each output line when set.
Definition: Log.cs:58
void WriteError(Exception e)
Write an error as output.
Definition: Log.cs:130
Log(string strSrc)
The Log constructor.
Definition: Log.cs:33
void CHECK_NE(double df1, double df2, string str)
Test whether one number is not-equal to another.
Definition: Log.cs:251
void CHECK_GT(float f1, float f2, string str)
Test whether one number is greater-than another.
Definition: Log.cs:371
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
Definition: Log.cs:299
void CHECK_NE(float f1, float f2, string str)
Test whether one number is not equal to another.
Definition: Log.cs:323
void CHECK_LE(double df1, double df2, string str)
Test whether one number is less than or equal to another.
Definition: Log.cs:263
bool EnableTrace
Enables/disables the Trace. When enabled, the .Net Trace.WriteLine is called in addition to the norma...
Definition: Log.cs:67
void CHECK_BOUNDS(float f1, float fmin, float fmax, string str)
Test whether a number falls within a set bound or not.
Definition: Log.cs:384
void CHECK_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
Definition: Log.cs:287
void CHECK_LT(double df1, double df2, string str)
Test whether one number is less than another.
Definition: Log.cs:275
EventHandler< LogArg > OnWriteLine
The OnWriteLine event fires each time the WriteLine, WriteHeader or WriteError functions are called.
Definition: Log.cs:23
void EXPECT_NEAR(double df1, double df2, double dfErr, string str="")
Test whether two numbers are within a range (dfErr) of one another using the double resolution.
Definition: Log.cs:214
void CHECK_EQ(float f1, float f2, string str)
Test whether one number is equal to another.
Definition: Log.cs:311
The LogProgressArg is passed as an argument to the Log::OnProgress event.
Definition: EventArgs.cs:17
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12