WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
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 using System.Threading;
8 
9 namespace DirectOutput
10 {
14  public class Log
15  {
16  static StreamWriter Logger;
17  static bool IsInitialized = false;
18  static bool IsOk = false;
19 
20  private static object Locker = new object();
21 
22  private static string _Filename = ".\\DirectOutput.log";
23 
30  public static string Filename
31  {
32  get { return _Filename; }
33  set { _Filename = value; }
34  }
35 
36 
40  public static void Init()
41  {
42  lock (Locker)
43  {
44  if (!IsInitialized)
45  {
46  try
47  {
48  Logger = File.AppendText(Filename);
49 
50  Logger.WriteLine("---------------------------------------------------------------------------------");
51  Logger.WriteLine("{0}\t{1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff"), "DirectOutput Logger initialized");
52 
53  Version V = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
54  DateTime BuildDate = new DateTime(2000, 1, 1).AddDays(V.Build).AddSeconds(V.Revision * 2);
55  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")));
56 
57  IsOk = true;
58 
59 
60 
61 
62 
63  }
64  catch
65  {
66  IsOk = false;
67  }
68 
69  IsInitialized = true;
70  }
71  }
72  }
73 
78  public static void Finish()
79  {
80  lock (Locker)
81  {
82  if (Logger != null)
83  {
84  Write("Logging stopped");
85  Logger.Flush();
86  Logger.Close();
87  IsOk = false;
88  IsInitialized = false;
89  Logger = null;
90  }
91  }
92  }
93 
94 
99  public static void Write(string Message)
100  {
101 
102  lock (Locker)
103  {
104  if (IsOk)
105  {
106  try
107  {
108 
109  if (Message.IsNullOrWhiteSpace())
110  {
111  Logger.WriteLine("{0}\t{1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff"), "");
112  }
113  else
114  {
115  foreach (string M in Message.Split(new[] { '\r', '\n' }))
116  {
117  Logger.WriteLine("{0}\t{1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff"), M);
118  }
119  }
120  Logger.Flush();
121  }
122  catch
123  {
124 
125  }
126  }
127  }
128  }
129 
134  public static void Warning(string Message)
135  {
136  Write("Warning: {0}".Build(Message));
137  }
138 
144  public static void Exception(string Message, Exception E = null)
145  {
146  lock (Locker)
147  {
148  if (!Message.IsNullOrWhiteSpace())
149  {
150  Write("EXCEPTION: {0}".Build(Message));
151  }
152  Write("EXCEPTION: Thread: {0}".Build(Thread.CurrentThread.Name));
153  if (E != null)
154  {
155  Write("EXCEPTION: Message: {0} --> {1}".Build(E.GetType().Name, E.Message));
156 
157  foreach (string S in E.StackTrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
158  {
159  Write("EXCEPTION: Stacktrace: {0}".Build(S));
160  }
161 
162  if (E.TargetSite != null)
163  {
164  Write("EXCEPTION: Targetsite: {0}".Build(E.TargetSite.ToString()));
165  }
166 
167 
168 
169  try
170  {
171  // Get stack trace for the exception with source file information
172  StackTrace ST = new StackTrace(E, true);
173  // Get the top stack frame
174  StackFrame Frame = ST.GetFrame(0);
175 
176  int Line = Frame.GetFileLineNumber();
177  string ExceptionFilename = Frame.GetFileName();
178 
179  Write("EXCEPTION: Location: LineNr {0} in {1]".Build(Line, ExceptionFilename));
180  }
181  catch { }
182 
183  int Level = 1;
184  while (E.InnerException != null)
185  {
186  E = E.InnerException;
187  Write("EXCEPTION: InnerException {0}: {1} --> {2}".Build(Level, E.GetType().Name, E.Message));
188  Level++;
189 
190  if (Level > 20)
191  {
192  break;
193  }
194  }
195  }
196  }
197  }
198 
203  public static void Exception(Exception E)
204  {
205  Exception("", E);
206  }
207 
208 
209  //TODO: Make conditional compilation work
215  // [Conditional("DEBUGLOG")]
216  public static void Debug(string Message = "")
217  {
218  Write("Debug: {0}".Build(Message));
219 
220  }
221 
222 
223  }
224 }
static void Finish()
Finishes the logger. Closes the log file.
Definition: Log.cs:78
static void Warning(string Message)
Writes a warning message to the log.
Definition: Log.cs:134
static void Write(string Message)
Writes the specified message to the logfile.
Definition: Log.cs:99
static void Init()
Initializes the log using the file defnied in the Filename property.
Definition: Log.cs:40
A simple logger used to record important events and exceptions.
Definition: Log.cs:14
static void Debug(string Message="")
Writes the specified debug message to the log file.
Definition: Log.cs:216
static void Exception(Exception E)
Writes a exception to the log.
Definition: Log.cs:203
static string Filename
Gets or sets the filename for the log.
Definition: Log.cs:31
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.
Definition: Log.cs:144