using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Woodpecker.Core
{
///
/// 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
///
/// Represents five log importancies for logging actions.
///
public enum logImportancies { Important = 3, Error = 2, Warning = 1, Info = 0, Debug = -1};
///
/// The minimum importancy of actions, in order to be logged.
///
public static logImportancies minimumLogImportancy = logImportancies.Debug;
///
/// The minimum importancy of logged actions, in order to be written to logfile.
///
public static logImportancies minimumWriteLogImportancy = logImportancies.Warning;
///
/// Indicates if logs have to be written to file. Only logs important enough will be logged.
///
public static bool writeLogsToFile = true;
public static void PrintLine(string Text, logImportancies Importancy)
{
if ((int)Importancy >= (int)minimumLogImportancy)
{
string Now = DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond;
StackFrame _SF = new StackTrace().GetFrame(1);
Console.Write("[" + Now + "] [Importancy: " + (int)Importancy + " [" + Importancy.ToString() + "]] [");
if (Importancy == logImportancies.Error)
Console.ForegroundColor = ConsoleColor.Red;
else
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("] » ");
if (Importancy == logImportancies.Error)
Console.ForegroundColor = ConsoleColor.DarkRed;
else
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(Text);
Console.ForegroundColor = ConsoleColor.Gray;
if (writeLogsToFile && (int)Importancy >= (int)minimumWriteLogImportancy)
{
TextWriter Writer = new StreamWriter(workingDirectory + "/" + DateTime.Now.ToShortDateString() + ".txt", true);
Writer.WriteLine("Time: " + DateTime.Now.ToLongTimeString());
Writer.WriteLine("Importancy: " + Importancy.ToString() + " (" + (int)Importancy + ")");
Writer.WriteLine("Assembly: " + _SF.GetMethod().ReflectedType.Name);
Writer.WriteLine("Method: " + _SF.GetMethod().Name);
Writer.WriteLine("Description: " + Text);
Writer.WriteLine("");
Writer.Flush();
Writer.Close();
}
}
}
#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
}
}