From 8cb2675b8a3cebdb775aac26937b6c205662b3bf Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Mon, 18 Mar 2019 08:12:51 +0100 Subject: [PATCH] WIP --- MemoryLogger.cs | 110 ++++++++++++++++++++++++++++++++++++++++++++++ ln.logging.csproj | 1 + 2 files changed, 111 insertions(+) create mode 100644 MemoryLogger.cs diff --git a/MemoryLogger.cs b/MemoryLogger.cs new file mode 100644 index 0000000..a718e13 --- /dev/null +++ b/MemoryLogger.cs @@ -0,0 +1,110 @@ +// /** +// * File: MemoryLogger.cs +// * Author: haraldwolff +// * +// * This file and it's content is copyrighted by the Author and / or copyright holder. +// * Any use wihtout proper permission is illegal and may lead to legal actions. +// * +// * +// **/ +using System; +using ln.logging; +using System.Linq; +namespace sharp.logging +{ + public class MemoryLogger : ILoggingBackend + { + private LogEntry[] Entries = new LogEntry[256]; + private int CurrentLogPointer = 255; + + public MemoryLogger() + { + } + + public MemoryLogger(int bufferLength) + { + Entries = new LogEntry[bufferLength]; + CurrentLogPointer = Entries.Length - 1; + } + + public LogLevel MinLogLevel { get; set; } = LogLevel.FATAL; + public LogLevel MaxLogLevel { get; set; } = LogLevel.INFO; + + public void Message(LogLevel logLevel, string[] lines) + { + lock (this) + { + Entries[CurrentLogPointer].LogLevel = logLevel; + Entries[CurrentLogPointer].Lines = String.Join("\n", lines); + CurrentLogPointer = NextLogPointer; + } + } + + public LogEntry[] GetLogEntries() + { + lock (this) + { + LogEntry[] prepare = new LogEntry[Entries.Length]; + if (CurrentLogPointer == (Entries.Length - 1)) + { + Array.Copy(Entries, prepare, Entries.Length); + } + else + { + Array.Copy(Entries, PreviousLogPointer, prepare, 0, Entries.Length - PreviousLogPointer); + Array.Copy(Entries, 0, prepare, Entries.Length - PreviousLogPointer, PreviousLogPointer); + } + + return prepare.Where((x) => x.Lines != null).ToArray(); + } + } + public LogEntry[] GetLogEntries(LogLevel maxLogLevel) + { + lock (this) + { + LogEntry[] prepare = new LogEntry[Entries.Length]; + if (CurrentLogPointer == (Entries.Length - 1)) + { + Array.Copy(Entries, prepare, Entries.Length); + } + else + { + Array.Copy(Entries, PreviousLogPointer, prepare, 0, Entries.Length - PreviousLogPointer); + Array.Copy(Entries, 0, prepare, Entries.Length - PreviousLogPointer, PreviousLogPointer); + } + + return prepare.Where((x) => (x.Lines != null) && (maxLogLevel >= x.LogLevel)).ToArray(); + } + } + + private int NextLogPointer + { + get + { + int current = CurrentLogPointer; + current--; + if (current < 0) + current = Entries.Length - 1; + return current; + } + } + private int PreviousLogPointer + { + get + { + int current = CurrentLogPointer; + current++; + if (current >= Entries.Length) + current = 0; + return current; + } + } + + + public struct LogEntry + { + public LogLevel LogLevel; + public string Lines; + } + } +} diff --git a/ln.logging.csproj b/ln.logging.csproj index e922911..576a2d4 100644 --- a/ln.logging.csproj +++ b/ln.logging.csproj @@ -39,6 +39,7 @@ + \ No newline at end of file