diff --git a/ConsoleLogger.cs b/ConsoleLogger.cs new file mode 100644 index 0000000..c4d261e --- /dev/null +++ b/ConsoleLogger.cs @@ -0,0 +1,17 @@ +using System; +namespace sharp.logging +{ + public class ConsoleLogger : Logger + { + public ConsoleLogger() + { + } + + public override void WriteEntry(string[] entry) + { + foreach (string line in entry){ + Console.WriteLine(line); + } + } + } +} diff --git a/FileLogger.cs b/FileLogger.cs new file mode 100644 index 0000000..5f80837 --- /dev/null +++ b/FileLogger.cs @@ -0,0 +1,130 @@ +using System; +using System.IO; +using System.Text; +using System.Collections.Generic; + +namespace sharp.logging +{ + public class FileLogger : Logger, IDisposable + { + FileStream fileStream; + TextWriter writer; + + bool SendToConsole; + TextWriter stdout, stderr; + + public FileLogger(string filename) + { + openLogFile(filename); + } + + public FileLogger(string filename,bool sendToConsole,bool captureConsole) + { + openLogFile(filename); + SendToConsole = sendToConsole; + + if (SendToConsole || captureConsole){ + stdout = Console.Out; + } + if (captureConsole) + { + stderr = Console.Error; + + Console.SetOut( new LoggingWriter(this) ); + Console.SetError( new LoggingWriter(this,LogLevel.ERROR)); + } + + } + + private void openLogFile(string filename) + { + fileStream = new FileStream(filename, FileMode.Append); + writer = new StreamWriter(fileStream); + } + + public override void Close() + { + if (writer != null) + { + writer.Close(); + fileStream.Close(); + } + } + + public void Dispose() + { + if (writer != null) + { + writer.Close(); + writer.Dispose(); + writer = null; + } + if (fileStream != null) + { + fileStream.Close(); + fileStream.Dispose(); + fileStream = null; + } + if (stdout != null){ + Console.SetOut(stdout); + Console.SetError(stderr); + } + } + + public override void WriteEntry(string[] entry) + { + if (writer == null) + { + throw new ObjectDisposedException("FileLogger has already been disposed"); + } + foreach (String line in entry) + { + writer.WriteLine(line); + writer.Flush(); + + if (stdout != null){ + stdout.WriteLine(line); + } + } + } + + class LoggingWriter : TextWriter + { + Logger logger; + LogLevel level; + + List linebuffer = new List(); + + public LoggingWriter(Logger logger,LogLevel level = LogLevel.INFO) + { + this.logger = logger; + this.level = level; + } + + public override void Write(char value) + { + if (value == '\n'){ + logger.Log(level,new String(linebuffer.ToArray())); + linebuffer.Clear(); + } else { + linebuffer.Add(value); + } + } + + public override void WriteLine(string format, params object[] args) + { + logger.Log(level,format,args); + } + + public override Encoding Encoding + { + get + { + return Encoding.UTF8; + } + } + } + + + } +} diff --git a/LogLevel.cs b/LogLevel.cs new file mode 100644 index 0000000..e531770 --- /dev/null +++ b/LogLevel.cs @@ -0,0 +1,14 @@ +using System; +namespace sharp.logging +{ + public enum LogLevel + { + FATAL = 0, + ERROR = 5, + WARNING = 10, + INFO = 15, + DEBUG = 30, + DEBUGDETAIL = 40, + DEBUGFULL = 80 + } +} diff --git a/Logger.cs b/Logger.cs new file mode 100644 index 0000000..215e546 --- /dev/null +++ b/Logger.cs @@ -0,0 +1,50 @@ +using System; +using System.Diagnostics; +namespace sharp.logging +{ + public abstract class Logger + { + public static Logger DefaultLogger { get; set; } = new ConsoleLogger(); + + public Logger() + { + } + + public void Log(LogLevel level,string message, params object[] args) + { + Log(level,String.Format(message, args)); + } + + public void Log(string message,params object[] args){ + Log(LogLevel.INFO,String.Format(message, args)); + } + + public void Log(string message) + { + Log(LogLevel.INFO, message); + } + public void Log(LogLevel level,string message) + { + string[] lines = message.Split('\n'); + + lines[0] = String.Format("{0} [{1}] [ {3,-10} ] {2}", + DateTime.Now.ToLocalTime(), + Process.GetCurrentProcess().Id, + lines[0], + level.ToString() + ); + + for (int n = 1; n < lines.Length; n++){ + lines[n] = " " + lines[n]; + } + WriteEntry( lines ); + } + + public abstract void WriteEntry(string[] entry); + + public virtual void Close() + { + } + + } +} diff --git a/Logging.cs b/Logging.cs new file mode 100644 index 0000000..88e1027 --- /dev/null +++ b/Logging.cs @@ -0,0 +1,19 @@ +using System; +namespace sharp.logging +{ + public static class Logging + { + public static void Log(string message, params object[] args) + { + Log(LogLevel.INFO, message, args); + } + public static void Log(LogLevel level,string message, params object[] args) + { + Logger.DefaultLogger.Log(level,message, args); + } + + public static void Log(Exception e){ + Log(LogLevel.ERROR,"Exception: {0} / {1}\n{2}", e.GetType().Name, e.Message, e.StackTrace.ToString()); + } + } +} diff --git a/bin/Debug/sharp.logging.dll b/bin/Debug/sharp.logging.dll new file mode 100644 index 0000000..66d8aba Binary files /dev/null and b/bin/Debug/sharp.logging.dll differ diff --git a/bin/Debug/sharp.logging.pdb b/bin/Debug/sharp.logging.pdb new file mode 100644 index 0000000..4c3aea8 Binary files /dev/null and b/bin/Debug/sharp.logging.pdb differ diff --git a/sharp.logging.csproj b/sharp.logging.csproj index f19b15e..4c2dee0 100644 --- a/sharp.logging.csproj +++ b/sharp.logging.csproj @@ -7,7 +7,7 @@ Library sharp.logging sharp.logging - v4.5 + v4.7 true @@ -31,6 +31,11 @@ + + + + + \ No newline at end of file