using System; using System.IO; using System.Text; using System.Reflection; using System.Diagnostics; using System.Runtime.InteropServices; namespace Duck { /// /// Provides logging, file, environment and minor string manipulation actions. /// public static class IO { #region DLL Imports [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); #endregion #region Logging /// /// 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 cyan 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.Cyan; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = ConsoleColor.DarkCyan; Console.WriteLine(logText); Console.ForegroundColor = ConsoleColor.Gray; } /// /// Prints a cyan 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.Cyan; Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("] » "); Console.ForegroundColor = ConsoleColor.DarkCyan; 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 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) { //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("] " + headerHead + " "); Console.ForegroundColor = headerColor; Console.Write(logText.Substring(0, headerLength)); Console.ForegroundColor = colorTwo; Console.WriteLine(logText.Substring(headerLength)); Console.ForegroundColor = ConsoleColor.Gray; } /// /// Writes a plain text line. /// /// The log line to be printed. public static void WritePlain(string logText) { Console.WriteLine(logText); } /// /// Writes a blank line. /// public static void WriteBlank() { Console.WriteLine(); } #endregion #region Filesystem /// /// Returns the directory of the executeable (without backslash at end) as a string. /// public static string workingDirectory { get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6); } } /// /// Returns the value of a private profile string in a textfile as a string. /// /// The section where the value is located in. /// The key of the value. /// The location of the textfile. public static string readINI(string iniSection, string iniKey, string iniLocation) { StringBuilder _TMP = new StringBuilder(255); try { int i = GetPrivateProfileString(iniSection, iniKey, "", _TMP, 255, iniLocation); return _TMP.ToString(); } catch { return ""; } } /// /// Updates a value of a key in a textfile using WritePrivateProfileString. /// /// The section where the key to update is located in. /// The key to update. /// The new value for the key. /// The location of the textfile. public static void writeINI(string iniSection, string iniKey, string iniValue, string iniLocation) { WritePrivateProfileString(iniSection, iniKey, iniValue, iniLocation); } /// /// Returns a bool, which indicates if the specified path leads to a file. /// /// The full location of the file. public static bool fileExists(string fileLocation) { return File.Exists(fileLocation); } #endregion } }