The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
ErrorManager.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: ErrorManager.cs //
6 // Author: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.orgin@gmail.com //
8 // Description: //
9 // Class to allow the management of all different error types happened. //
10 // Implements Singleton pattern. //
11 // -------------------------------------------------------------------------- //
12 // Create date: 24-10-2006 //
13 // Modification date: 21-05-2007 //
15 
16 using System;
17 using System.Collections.Generic;
18 using System.IO;
19 using System.Text;
20 using System.Windows.Forms;
21 
22 namespace ErrorManagement {
29  public sealed class ErrorManager {
30  #region Fields
31 
35  private static readonly ErrorManager instance = new ErrorManager();
36 
40  private string logFileName;
41 
45  private bool showInConsole;
46 
50  private bool errorFound;
51 
55  private bool showMessages;
56 
60  private IList<IError> errorList = new List<IError>();
61 
62  #endregion
63 
64  #region Properties
65 
69  public static ErrorManager Instance {
70  get { return instance; }
71  }
72 
73 
77  public bool ShowInConsole {
78  get { return showInConsole; }
79  set { showInConsole = value; }
80  }
81 
82 
86  public bool ErrorFound {
87  get { return this.errorFound; }
88  }
89 
93  public bool ShowMessages {
94  get { return this.showMessages; }
95  set { this.showMessages = value; }
96  }
97 
101  public string LogFileName {
102  get { return logFileName; }
103  set { logFileName = value; }
104  }
105 
109  public int ErrorCount {
110  get { return this.errorList.Count; }
111  }
112  #endregion
113 
114  #region Constructor
115 
119  private ErrorManager() {
120  this.logFileName = Application.StartupPath + @"\error.log";
121  this.errorFound = false;
122  this.showMessages = true;
123  }
124 
128  static ErrorManager() {
129  }
130 
131  #endregion
132 
133  #region writeErrorLogHeader()
134 
139  private static void writeErrorLogHeader(TextWriter tw) {
140  try {
141  tw.WriteLine();
142  tw.WriteLine("-------------------------------------------------------------------------------------------");
143 
144  tw.Write("Date: ");
145  tw.WriteLine(System.DateTime.Now.ToLongDateString());
146  tw.Write("Time: ");
147  tw.WriteLine(System.DateTime.Now.ToLongTimeString());
148 
149  tw.Flush();
150  } catch (IOException e) {
151  Console.Error.WriteLine("[ErrorManager]: It hasn't been possible to write in error log.");
152  Console.Error.WriteLine(e.Message);
153  Environment.Exit(-1);
154  }
155  }
156 
157  #endregion
158 
159  #region writeError
160  private void writeError(string s, TextWriter tw) {
165  if (!this.showMessages)
166  return;
167  tw.Write(s);
168  if (ShowInConsole)
169  Console.Error.Write(s);
170  }
171  #endregion
172 
173  #region writeErrorLogEntry()
174 
180  private void writeErrorLogEntry(TextWriter tw, IError error) {
181  if (!this.showMessages)
182  return;
183  try {
184  ErrorAdapter errorAdapter = error as ErrorAdapter;
185  if (errorAdapter != null) {
186  writeError(errorAdapter.Location.ToString(), tw);
187  writeError(": Error " + errorAdapter.GetType().FullName + " (" + errorAdapter.ErrorType+ "). ", tw);
188  writeError(errorAdapter.Description + "\n", tw);
189  }
190  else {
191  writeError("Error: ", tw);
192  writeError(error.ErrorType + "\r\n", tw);
193  writeError("Description: ", tw);
194  writeError(error.Description + "\r\n", tw);
195  }
196  tw.Flush();
197  } catch (IOException e) {
198  Console.Error.WriteLine("[ErrorManager]: It has not been possible to write in error log.");
199  Console.Error.WriteLine(e.Message);
200  Environment.Exit(-1);
201  }
202  }
203 
204  #endregion
205 
206  #region NotifyError()
207 
212  public void NotifyError(IError error) {
213  if (!this.showMessages)
214  return;
215 
216  // * Checks if the error was previously shown
217  if (this.errorList.Contains(error))
218  return;
219  this.errorList.Add(error);
220 
221 
222  errorFound = true;
223  try {
224  StreamWriter writer = new StreamWriter(this.logFileName, true);
225 
226  //#if DEBUG
227  // writeErrorLogEntry(Console.Error, error);
228  //#endif
229 
230  // Writes the header
231  writeErrorLogHeader(writer);
232  // Writes error information
233  writeErrorLogEntry(writer, error);
234 
235  writer.Close();
236  } catch (Exception e) {
237  Console.Error.WriteLine("[ErrorManager]: It has not been possible to write in error log.");
238  Console.Error.WriteLine(e.Message);
239  Environment.Exit(-1);
240  }
241  }
242 
243  #endregion
244 
245  #region GetError()
246  public IError GetError(int errorNumber) {
252  return this.errorList[errorNumber];
253  }
254  #endregion
255 
256  #region Clear()
257  public void Clear() {
261  errorList.Clear();
262  errorFound = false;
263  try {
264  FileInfo logFile = new FileInfo(LogFileName);
265  if (logFile.Exists)
266  logFile.Delete();
267  } catch (IOException ex) {
268  System.Diagnostics.Trace.WriteLine(ex);
269  }
270 
271  }
272  #endregion
273 
274  }
275 }
Class to allow the management of all different error types happened.
Definition: ErrorManager.cs:29
void NotifyError(IError error)
Notify the error
bool ShowInConsole
To show the error mesagges in console
Definition: ErrorManager.cs:77
System.IO.FileInfo FileInfo
Definition: CSharpParser.cs:5
Interfaz for the different error types.
Definition: IError.cs:23
bool ErrorFound
Returns TRUE if an error occurred, FALSE otherwise.
Definition: ErrorManager.cs:86
static ErrorManager Instance
Gets the unique instance of ErrorManager
Definition: ErrorManager.cs:69
string LogFileName
Filename of the log file.
IError GetError(int errorNumber)
Returns an error object
void Clear()
Clears error list, sets errorFound to false, and deletes log file.
int ErrorCount
Returns the number of errors
bool ShowMessages
True if messages are shown when the method notify error is called.
Definition: ErrorManager.cs:93