From 9931f5926705d351638e0a1ee7d4a1dc278f99ec Mon Sep 17 00:00:00 2001
From: dedouwe26 <63008025+dedouwe26@users.noreply.github.com>
Date: Tue, 17 Dec 2024 17:43:54 +0100
Subject: [PATCH] Added FormattedTarget, namespace
---
Terminal/Logging/Targets/FileTarget.cs | 48 ++++-------------
Terminal/Logging/Targets/FormattedTarget.cs | 58 +++++++++++++++++++++
Terminal/Logging/Targets/TerminalTarget.cs | 34 ++++--------
Terminal/Terminal.csproj | 2 +-
examples/Logging/Program.cs | 1 +
5 files changed, 80 insertions(+), 63 deletions(-)
create mode 100644 Terminal/Logging/Targets/FormattedTarget.cs
diff --git a/Terminal/Logging/Targets/FileTarget.cs b/Terminal/Logging/Targets/FileTarget.cs
index c7a139d..4a0f8e9 100644
--- a/Terminal/Logging/Targets/FileTarget.cs
+++ b/Terminal/Logging/Targets/FileTarget.cs
@@ -1,33 +1,14 @@
-namespace OxDED.Terminal.Logging;
+namespace OxDED.Terminal.Logging.Targets;
///
/// A Logger Target for a log file.
///
-public class FileTarget : ITarget {
+public class FileTarget : FormattedTarget {
///
/// The output stream to the file.
///
public readonly TextWriter FileOut;
- ///
- /// The format to use for writing to the terminal (0: logger name, 1: logger ID, 2: time, 3: severity, 4: message).
- ///
- ///
- /// Default:
- /// [{0}][{2}][{3}]: {4}
- ///
- public string Format = "[{0}][{2}][{3}]: {4}";
-
- ///
- /// The format to use for creating names (0: parent name, 1: own name).
- /// Example for default:
- /// {{App}: Sublogger}: sub-sublogger
- ///
- ///
- /// default:
- /// {0}: {1}
- ///
- public string NameFormat = "{0}: {1}";
-
+
///
/// Creates a target that targets a log file.
///
@@ -39,25 +20,16 @@ public FileTarget(string path, string? format = null) {
}
FileOut = new StreamWriter(File.Open(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
}
- ///
- public void Dispose() {
- FileOut.Close();
- GC.SuppressFinalize(this);
- }
-
- private string GetName(Logger logger) {
- if (!logger.IsSubLogger) {
- return logger.Name;
- } else {
- return string.Format(NameFormat, GetName((logger as SubLogger)!.ParentLogger), logger.Name);
- }
- }
-
///
///
/// Writes a line.
///
- public void Write(Severity severity, DateTime time, Logger logger, T? text) {
- FileOut.WriteLine(string.Format(Format, GetName(logger), logger.ID, time.ToString(), severity.ToString(), text?.ToString()??""));
+ public override void Write(Severity severity, DateTime time, Logger logger, T? text) where T : default {
+ FileOut.WriteLine(GetText(logger, time, severity, text?.ToString() ?? "(Null)"));
+ }
+ ///
+ public override void Dispose() {
+ FileOut.Close();
+ GC.SuppressFinalize(this);
}
}
\ No newline at end of file
diff --git a/Terminal/Logging/Targets/FormattedTarget.cs b/Terminal/Logging/Targets/FormattedTarget.cs
new file mode 100644
index 0000000..3e2e106
--- /dev/null
+++ b/Terminal/Logging/Targets/FormattedTarget.cs
@@ -0,0 +1,58 @@
+
+namespace OxDED.Terminal.Logging.Targets;
+
+///
+/// Represents a target with formattable logs.
+///
+public abstract class FormattedTarget : ITarget {
+ ///
+ /// The format to use for writing to the terminal (0: logger name, 1: logger ID, 2: time, 3: severity, 4: message).
+ ///
+ ///
+ /// Default:
+ /// [{0}][{2}][{3}]: {4}
+ ///
+ public string Format = "[{0}][{2}][{3}]: {4}";
+
+ ///
+ /// The format to use for creating names (0: parent name, 1: own name).
+ /// Example for default:
+ /// {{App}: Sublogger}: sub-sublogger
+ ///
+ ///
+ /// default:
+ /// {0}: {1}
+ ///
+ public string NameFormat = "{0}: {1}";
+
+ ///
+ /// Generates the name of a logger with a format.
+ ///
+ /// The logger which name to generate.
+ /// The generated name.
+ protected string GetName(Logger logger) {
+ if (!logger.IsSubLogger) {
+ return logger.Name;
+ } else {
+ return string.Format(NameFormat, GetName((logger as SubLogger)!.ParentLogger), logger.Name);
+ }
+ }
+
+ ///
+ /// Generates the log with a format.
+ ///
+ /// The logger which name and ID to use.
+ /// The time of the log.
+ /// The severity of the log.
+ /// The text of the log.
+ /// The generated log.
+ protected string GetText(Logger logger, DateTime time, Severity severity, string text) {
+ return string.Format(Format, GetName(logger), logger.ID, time.ToString(), severity.ToString(), text);
+ }
+
+ ///
+ public abstract void Write(Severity severity, DateTime time, Logger logger, T? text);
+
+ ///
+ public abstract void Dispose();
+}
\ No newline at end of file
diff --git a/Terminal/Logging/Targets/TerminalTarget.cs b/Terminal/Logging/Targets/TerminalTarget.cs
index 4617abb..2d293cd 100644
--- a/Terminal/Logging/Targets/TerminalTarget.cs
+++ b/Terminal/Logging/Targets/TerminalTarget.cs
@@ -1,9 +1,9 @@
-namespace OxDED.Terminal.Logging;
+namespace OxDED.Terminal.Logging.Targets;
///
/// A Logger Target for the terminal.
///
-public class TerminalTarget : ITarget {
+public class TerminalTarget : FormattedTarget {
///
/// The out stream to the terminal.
///
@@ -13,23 +13,13 @@ public class TerminalTarget : ITarget {
///
public TextWriter Error;
///
- /// The format to use for writing to the terminal (0: name see , 1: logger ID, 2: time, 3: severity, 4: message, 5: color ANSI).
+ /// The format to use for writing to the terminal (0: name see , 1: logger ID, 2: time, 3: severity, 4: message, 5: color ANSI).
///
///
/// Default:
/// {5}[{0}][{2}][BOLD{3}RESETBOLD]: {4}RESETALL
///
- public string Format = "{5}[{0}][{2}]["+ANSI.Styles.Bold+"{3}"+ANSI.Styles.ResetBold+"]: {4}"+ANSI.Styles.ResetAll;
- ///
- /// The format to use for creating names (0: parent name, 1: own name).
- /// Example for default:
- /// {{App}: Sublogger}: sub-sublogger
- ///
- ///
- /// default:
- /// {0}: {1}
- ///
- public string NameFormat = "{0}: {1}";
+ public new string Format = "{5}[{0}][{2}]["+ANSI.Styles.Bold+"{3}"+ANSI.Styles.ResetBold+"]: {4}"+ANSI.Styles.ResetAll;
///
/// The colors of the severities (index: 0: Fatal, 1: Error, 2: Warning, 3: Message, 4: Info, 5: Debug, 6: Trace).
///
@@ -48,27 +38,23 @@ public TerminalTarget(string? format = null, TextWriter? terminalOut = null, Tex
Error = terminalError ?? Terminal.Error;
}
///
- public void Dispose() {
+ public override void Dispose() {
GC.SuppressFinalize(this);
}
- private string GetName(Logger logger) {
- if (!logger.IsSubLogger) {
- return logger.Name;
- } else {
- return string.Format(NameFormat, GetName((logger as SubLogger)!.ParentLogger), logger.Name);
- }
+ private string GetText(Logger logger, DateTime time, Severity severity, string text, string color) {
+ return string.Format(Format, GetName(logger), logger.ID, time.ToString(), severity.ToString(), text, color);
}
///
///
/// Writes a line.
///
- public void Write(Severity severity, DateTime time, Logger logger, T? text) {
+ public override void Write(Severity severity, DateTime time, Logger logger, T? text) where T : default {
if (((byte)severity) < 2) {
- Error.WriteLine(string.Format(Format, GetName(logger), logger.ID, time.ToString(), severity.ToString(), text?.ToString()??"", SeverityColors[(byte)severity].ToForegroundANSI()));
+ Error.WriteLine(GetText(logger, time, severity, text?.ToString() ?? "(Null)", SeverityColors[(byte)severity].ToForegroundANSI()));
} else {
- Out.WriteLine(string.Format(Format, GetName(logger), logger.ID, time.ToString(), severity.ToString(), text?.ToString()??"", SeverityColors[(byte)severity].ToForegroundANSI()));
+ Out.WriteLine(GetText(logger, time, severity, text?.ToString() ?? "(Null)", SeverityColors[(byte)severity].ToForegroundANSI()));
}
}
diff --git a/Terminal/Terminal.csproj b/Terminal/Terminal.csproj
index 3e7f6a4..24350b7 100644
--- a/Terminal/Terminal.csproj
+++ b/Terminal/Terminal.csproj
@@ -7,7 +7,7 @@
true
true
0xDED.Terminal
- 4.1.0
+ 4.1.1
0xDED
MIT
README.md
diff --git a/examples/Logging/Program.cs b/examples/Logging/Program.cs
index fdc46d3..6eb800a 100644
--- a/examples/Logging/Program.cs
+++ b/examples/Logging/Program.cs
@@ -1,6 +1,7 @@
// The namespace of terminal logging.
using OxDED.Terminal;
using OxDED.Terminal.Logging;
+using OxDED.Terminal.Logging.Targets;
class Program {
public const string LoggerID = "me.0xDED.Terminal.examples";