DirectOutputR1
DirectOutput framework R1 for virtual pinball cabinets.
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
Log.cs
Go to the documentation of this file.
1 // Use #define DebugLog or #undef DebugLog to turn debug log messages on or off.
2 #define DEBUGLOG
3 
4 using System;
5 using System.IO;
6 using System.Diagnostics;
7 
8 namespace DirectOutput
9 {
13  public class Log
14  {
15  static StreamWriter Logger;
16  static bool IsInitialized = false;
17  static bool IsOk = false;
18 
19  private static object Locker = new object();
20 
21  private static string _Filename = ".\\DirectOutput.log";
22 
29  public static string Filename
30  {
31  get { return _Filename; }
32  set { _Filename = value; }
33  }
34 
35 
39  public static void Init()
40  {
41  lock (Locker)
42  {
43  if (!IsInitialized)
44  {
45  try
46  {
47  Logger = File.AppendText(Filename);
48 
49  Logger.WriteLine("---------------------------------------------------------------------------------");
50  Logger.WriteLine("{0}\t{1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff"), "DirectOutput Logger initialized");
51 
52  Version V = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
53  DateTime BuildDate = new DateTime(2000, 1, 1).AddDays(V.Build).AddSeconds(V.Revision * 2);
54  Logger.WriteLine("{0}\t{1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff"), "DirectOutput Version {0} as of {1}".Build(V.ToString(), BuildDate.ToString("yyyy.MM.dd HH:mm")));
55 
56  IsOk = true;
57 
58  Debug("Writting of debug log messages is enabled");
59 
60 
61 
62  }
63  catch
64  {
65  IsOk = false;
66  }
67 
68  IsInitialized = true;
69  }
70  }
71  }
72 
77  public static void Finish()
78  {
79  lock (Locker)
80  {
81  if (Logger != null)
82  {
83  Write("Logging stopped");
84  Logger.Flush();
85  Logger.Close();
86  IsOk = false;
87  IsInitialized = false;
88  Logger = null;
89  }
90  }
91  }
92 
93 
98  public static void Write(string Message)
99  {
100 
101  lock (Locker)
102  {
103  if (IsOk)
104  {
105  try
106  {
107 
108  if (Message.IsNullOrWhiteSpace())
109  {
110  Logger.WriteLine("{0}\t{1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff"), "");
111  }
112  else
113  {
114  foreach (string M in Message.Split(new[] { '\r', '\n' }))
115  {
116  Logger.WriteLine("{0}\t{1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff"), M);
117  }
118  }
119  Logger.Flush();
120  }
121  catch
122  {
123 
124  }
125  }
126  }
127  }
128 
133  public static void Warning(string Message)
134  {
135  Write("Warning: {0}".Build(Message));
136  }
137 
143  public static void Exception(string Message = "", Exception E = null)
144  {
145  lock (Locker)
146  {
147  if (!Message.IsNullOrWhiteSpace())
148  {
149  Write("EXCEPTION: {0}".Build(Message));
150  }
151  if (E != null)
152  {
153  Write("EXCEPTION: {0}".Build(E.Message));
154 
155  foreach (string S in E.StackTrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
156  {
157  Write("EXCEPTION: Stacktrace: {0}".Build(S));
158  }
159 
160  if (E.TargetSite != null)
161  {
162  Write("EXCEPTION: Targetsite: {0}".Build(E.TargetSite.ToString()));
163  }
164 
165 
166 
167  try
168  {
169  // Get stack trace for the exception with source file information
170  StackTrace ST = new StackTrace(E, true);
171  // Get the top stack frame
172  StackFrame Frame = ST.GetFrame(0);
173 
174  int Line = Frame.GetFileLineNumber();
175  string ExceptionFilename = Frame.GetFileName();
176 
177  Write("EXCEPTION: Location: LineNr {0} in {1]".Build(Line, ExceptionFilename));
178  }
179  catch { }
180 
181  int Level = 1;
182  while (E.InnerException != null)
183  {
184  E = E.InnerException;
185  Write("EXCEPTION: InnerException {0}: {1}".Build(Level, E.Message));
186  Level++;
187 
188  if (Level > 20)
189  {
190  break;
191  }
192  }
193  }
194  }
195  }
196 
201  public static void Exception(Exception E = null)
202  {
203  Exception("", E);
204  }
205 
206 
207  //TODO: Make conditional compilation work
213  [Conditional("DEBUGLOG")]
214  public static void Debug(string Message = "")
215  {
216  Write("Debug: {0}".Build(Message));
217 
218  }
219 
220 
221  }
222 }