using System; using System.Diagnostics; using System.Threading; using System.IO; namespace Holo { /// /// Provides interface output related functions, such as logging activities. /// public static class Out { /// /// Enum with flags for log importancies. If 'minimumImportance' flag is higher than the action to be logged, then the action won't be logged. /// public enum logFlags { ImportantAction = 3, StandardAction = 2, BelowStandardAction = 1, MehAction = 0 } /// /// Flag for minimum importance in logs. Adjust this to don't print less important logs. /// public static logFlags minimumImportance; /// /// Prints a green line of log, together with timestamp and method name. /// /// The log line to be printed. public static void WriteLine(string logText) { DateTime _DTN = DateTime.Now; StackFrame _SF = new StackTrace().GetFrame(1); Console.Write("[" + _DTN.ToLongTimeString() + ":" + _DTN.Millisecond.ToString() + "] ["); Console.ForegroundColor = ConsoleColor.Green; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine(logText); Console.ForegroundColor = ConsoleColor.Gray; } /// /// Prints a green line of log, together with timestamp and method name. /// /// The log line to be printed. /// The importance flag of this log line. public static void WriteLine(string logText, logFlags logFlag) { if ((int)logFlag < (int)minimumImportance) return; DateTime _DTN = DateTime.Now; StackFrame _SF = new StackTrace().GetFrame(1); Console.Write("[" + _DTN.ToLongTimeString() + ":" + _DTN.Millisecond.ToString() + "] ["); Console.ForegroundColor = ConsoleColor.Green; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine(logText); Console.ForegroundColor = ConsoleColor.Gray; } /// /// Prints a customizeable line of log, together with timestamp and method name. /// /// The log line to be printed. /// The importance flag of this log line. /// The color to use on the left. /// The color to use on the right. public static void WriteLine(string logText, logFlags logFlag, ConsoleColor colorOne, ConsoleColor colorTwo) { if ((int)logFlag < (int)minimumImportance) return; DateTime _DTN = DateTime.Now; StackFrame _SF = new StackTrace().GetFrame(1); Console.Write("[" + _DTN.ToLongTimeString() + ":" + _DTN.Millisecond.ToString() + "] ["); Console.ForegroundColor = colorOne; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = colorTwo; Console.WriteLine(logText); Console.ForegroundColor = ConsoleColor.Gray; } /// /// Prints a red, error line of log, together with timestamp and method name. /// /// The log line to be printed. public static void WriteError(string logText) { DateTime _DTN = DateTime.Now; StackFrame _SF = new StackTrace().GetFrame(1); Console.Write("[" + _DTN.ToLongTimeString() + ":" + _DTN.Millisecond.ToString() + "] ["); Console.ForegroundColor = ConsoleColor.Red; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine(logText); Console.ForegroundColor = ConsoleColor.Gray; } /// /// Prints a red,error line of log, together with timestamp and method name. /// /// The log line to be printed. /// The importance flag of this error. public static void WriteError(string logText, logFlags logFlag) { if ((int)logFlag < (int)minimumImportance) return; DateTime _DTN = DateTime.Now; StackFrame _SF = new StackTrace().GetFrame(1); Console.Write("[" + _DTN.ToLongTimeString() + ":" + _DTN.Millisecond.ToString() + "] ["); Console.ForegroundColor = ConsoleColor.Red; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine(logText); Console.ForegroundColor = ConsoleColor.Gray; } /// /// Writes a plain text line. /// /// The log line to be printed. public static void WritePlain(string logText) { Wait(); Console.WriteLine(logText.Replace(Convert.ToChar(13).ToString(), "{13}")); wait = false; } private static bool wait; private static void Wait() { while (wait) Thread.Sleep(1); wait = true; } /// /// Writes a blank line. /// public static void WriteBlank() { Console.WriteLine(); } /// /// Writes a special line of log, with customizeable colors and header coloring of logText. /// /// The log line to be printed. /// The importance flag of this log line. /// The color to use on the left. /// The color to use on the right. /// The string to use infront of logText. /// The length of the header to color. /// The color for the header in the logText. public static void WriteSpecialLine(string logText, logFlags logFlag, ConsoleColor colorOne, ConsoleColor colorTwo, string headerHead, int headerLength, ConsoleColor headerColor) { Console.ForegroundColor = headerColor; try { Console.Write(logText.Substring(0, headerLength)); } catch { Console.Write(logText); } Console.ForegroundColor = colorTwo; Console.WriteLine(logText.Substring(headerLength)); Console.ForegroundColor = ConsoleColor.Gray; } public static void WriteDCError(string logText) { try { System.IO.FileStream errWriter = new System.IO.FileStream("DC.err", System.IO.FileMode.Append, System.IO.FileAccess.Write); byte[] Msg = System.Text.ASCIIEncoding.ASCII.GetBytes(logText + "\r\n\r\n"); errWriter.Write(Msg, 0, Msg.Length); } catch (Exception eX) { Out.WritePlain(eX.Message); } DateTime _DTN = DateTime.Now; StackFrame _SF = new StackTrace().GetFrame(1); Console.Write("[" + _DTN.ToLongTimeString() + ":" + _DTN.Millisecond.ToString() + "] ["); Console.ForegroundColor = ConsoleColor.Red; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Disconnection has been saved"); Console.ForegroundColor = ConsoleColor.Gray; } public static void WriteCycleStatusError(string logText) { try { System.IO.FileStream Writer = new System.IO.FileStream("Cycle.err", System.IO.FileMode.Append, System.IO.FileAccess.Write); byte[] Msg = System.Text.ASCIIEncoding.ASCII.GetBytes(logText + "\r\n\r\n"); Writer.Write(Msg, 0, Msg.Length); } catch (Exception eX) { Out.WritePlain(eX.Message); } } public static void writeSeriousError(string logText) { try { System.IO.FileStream Writer = new System.IO.FileStream("serious.err", System.IO.FileMode.Append, System.IO.FileAccess.Write); byte[] Msg = System.Text.ASCIIEncoding.ASCII.GetBytes(logText + "\r\n\r\n"); Writer.Write(Msg, 0, Msg.Length); } catch { Out.WriteError("Serious error occured! Sunnieday Shut down.. \r\n Zeg met maar tegen mark hij weet nu wat hij moet maken!"); } } /// /// /// /// internal static void WriteRedText(string logText) { DateTime _DTN = DateTime.Now; StackFrame _SF = new StackTrace().GetFrame(1); Console.Write("[" + _DTN.ToLongTimeString() + ":" + _DTN.Millisecond.ToString() + "] ["); Console.ForegroundColor = ConsoleColor.Green; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(logText); Console.ForegroundColor = ConsoleColor.Gray; } } }