using System; using System.IO; using System.Text; namespace HalconTools { public sealed class LogManager : IDisposable { private Type _componentType; private FileStream _fs; private string _logfile; private static object mutext = new object(); public Log WriteLog; private LogManager() { this.WriteLog = new Log(this.PrepareLogFile); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.OpenStream)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendLocalTime)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendComponentType)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendType)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendContent)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendNewLine)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.CloseStream)); } public LogManager(Type type, string file) : this() { this._logfile = file; this._componentType = type; } private void AppendComponentType(string content, LogType type) { byte[] bytes = Encoding.Default.GetBytes((this._componentType == null) ? "" : this._componentType.ToString()); this._fs.Write(bytes, 0, bytes.Length); } private void AppendContent(string content, LogType type) { byte[] bytes = Encoding.Default.GetBytes(content); this._fs.Write(bytes, 0, bytes.Length); } private void AppendLocalTime(string content, LogType type) { string s = DateTime.Now.ToLocalTime().ToString(); byte[] bytes = Encoding.Default.GetBytes(s); this._fs.Write(bytes, 0, bytes.Length); } private void AppendNewLine(string content, LogType type) { byte[] bytes = Encoding.Default.GetBytes("\r\n"); this._fs.Write(bytes, 0, bytes.Length); } private void AppendSeperator(string content, LogType type) { byte[] bytes = Encoding.Default.GetBytes(" | "); this._fs.Write(bytes, 0, bytes.Length); } private void AppendType(string content, LogType type) { string s = string.Empty; switch (type) { case LogType.Debug: s = "Debug"; break; case LogType.Trace: s = "Trace"; break; case LogType.Info: s = "Info"; break; case LogType.Warn: s = "Warn"; break; case LogType.Error: s = "Error"; break; default: s = ""; break; } byte[] bytes = Encoding.Default.GetBytes(s); this._fs.Write(bytes, 0, bytes.Length); } private void AppendUTCTime(string content, LogType type) { string s = DateTime.Now.ToUniversalTime().ToString(); byte[] bytes = Encoding.Default.GetBytes(s); this._fs.Write(bytes, 0, bytes.Length); } private void CloseStream(string content, LogType type) { this._fs.Close(); this._fs.Dispose(); } public void Dispose() { if (this._fs != null) { this._fs.Dispose(); } GC.SuppressFinalize(this); } ~LogManager() { if (this._fs != null) { this._fs.Dispose(); } } private void OpenStream(string content, LogType type) { this._fs = File.Open(this._logfile, FileMode.Append); } private void PrepareLogFile(string content, LogType type) { lock (mutext) { if (!File.Exists(this._logfile)) { using (File.Create(this._logfile)) { } } } } public void UseLocalTime() { this.WriteLog = new Log(this.PrepareLogFile); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.OpenStream)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendLocalTime)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendComponentType)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendType)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendContent)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendNewLine)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.CloseStream)); } public void UseUTCTime() { this.WriteLog = new Log(this.PrepareLogFile); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.OpenStream)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendUTCTime)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendComponentType)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendType)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendSeperator)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendContent)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.AppendNewLine)); this.WriteLog = (Log) Delegate.Combine(this.WriteLog, new Log(this.CloseStream)); } } }