Skip to content

Commit

Permalink
Added FormattedTarget, namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dedouwe26 committed Dec 17, 2024
1 parent 7f59bc1 commit 9931f59
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 63 deletions.
48 changes: 10 additions & 38 deletions Terminal/Logging/Targets/FileTarget.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
namespace OxDED.Terminal.Logging;
namespace OxDED.Terminal.Logging.Targets;

/// <summary>
/// A Logger Target for a log file.
/// </summary>
public class FileTarget : ITarget {
public class FileTarget : FormattedTarget {
/// <summary>
/// The output stream to the file.
/// </summary>
public readonly TextWriter FileOut;
/// <summary>
/// The format to use for writing to the terminal (0: logger name, 1: logger ID, 2: time, 3: severity, 4: message).
/// </summary>
/// <remarks>
/// Default:
/// <c>[{0}][{2}][{3}]: {4}</c>
/// </remarks>
public string Format = "[{0}][{2}][{3}]: {4}";

/// <summary>
/// The format to use for creating names (0: parent name, 1: own name). <para/>
/// Example for default:
/// <c>{{App}: Sublogger}: sub-sublogger</c>
/// </summary>
/// <remarks>
/// default:
/// <c>{0}: {1}</c>
/// </remarks>
public string NameFormat = "{0}: {1}";


/// <summary>
/// Creates a target that targets a log file.
/// </summary>
Expand All @@ -39,25 +20,16 @@ public FileTarget(string path, string? format = null) {
}
FileOut = new StreamWriter(File.Open(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
}
/// <inheritdoc/>
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);
}
}

/// <inheritdoc/>
/// <remarks>
/// Writes a line.
/// </remarks>
public void Write<T>(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<T>(Severity severity, DateTime time, Logger logger, T? text) where T : default {
FileOut.WriteLine(GetText(logger, time, severity, text?.ToString() ?? "(Null)"));
}
/// <inheritdoc/>
public override void Dispose() {
FileOut.Close();
GC.SuppressFinalize(this);
}
}
58 changes: 58 additions & 0 deletions Terminal/Logging/Targets/FormattedTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

namespace OxDED.Terminal.Logging.Targets;

/// <summary>
/// Represents a target with formattable logs.
/// </summary>
public abstract class FormattedTarget : ITarget {
/// <summary>
/// The format to use for writing to the terminal (0: logger name, 1: logger ID, 2: time, 3: severity, 4: message).
/// </summary>
/// <remarks>
/// Default:
/// <c>[{0}][{2}][{3}]: {4}</c>
/// </remarks>
public string Format = "[{0}][{2}][{3}]: {4}";

/// <summary>
/// The format to use for creating names (0: parent name, 1: own name). <para/>
/// Example for default:
/// <c>{{App}: Sublogger}: sub-sublogger</c>
/// </summary>
/// <remarks>
/// default:
/// <c>{0}: {1}</c>
/// </remarks>
public string NameFormat = "{0}: {1}";

/// <summary>
/// Generates the name of a logger with a format.
/// </summary>
/// <param name="logger">The logger which name to generate.</param>
/// <returns>The generated name.</returns>
protected string GetName(Logger logger) {
if (!logger.IsSubLogger) {
return logger.Name;
} else {
return string.Format(NameFormat, GetName((logger as SubLogger)!.ParentLogger), logger.Name);
}
}

/// <summary>
/// Generates the log with a format.
/// </summary>
/// <param name="logger">The logger which name and ID to use.</param>
/// <param name="time">The time of the log.</param>
/// <param name="severity">The severity of the log.</param>
/// <param name="text">The text of the log.</param>
/// <returns>The generated log.</returns>
protected string GetText(Logger logger, DateTime time, Severity severity, string text) {
return string.Format(Format, GetName(logger), logger.ID, time.ToString(), severity.ToString(), text);
}

/// <inheritdoc/>
public abstract void Write<T>(Severity severity, DateTime time, Logger logger, T? text);

/// <inheritdoc/>
public abstract void Dispose();
}
34 changes: 10 additions & 24 deletions Terminal/Logging/Targets/TerminalTarget.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace OxDED.Terminal.Logging;
namespace OxDED.Terminal.Logging.Targets;

/// <summary>
/// A Logger Target for the terminal.
/// </summary>
public class TerminalTarget : ITarget {
public class TerminalTarget : FormattedTarget {
/// <summary>
/// The out stream to the terminal.
/// </summary>
Expand All @@ -13,23 +13,13 @@ public class TerminalTarget : ITarget {
/// </summary>
public TextWriter Error;
/// <summary>
/// The format to use for writing to the terminal (0: name see <see cref="NameFormat"/>, 1: logger ID, 2: time, 3: severity, 4: message, 5: color ANSI).
/// The format to use for writing to the terminal (0: name see <see cref="FormattedTarget.NameFormat"/>, 1: logger ID, 2: time, 3: severity, 4: message, 5: color ANSI).
/// </summary>
/// <remarks>
/// Default:
/// <c>{5}[{0}][{2}][BOLD{3}RESETBOLD]: {4}RESETALL</c>
/// </remarks>
public string Format = "{5}[{0}][{2}]["+ANSI.Styles.Bold+"{3}"+ANSI.Styles.ResetBold+"]: {4}"+ANSI.Styles.ResetAll;
/// <summary>
/// The format to use for creating names (0: parent name, 1: own name). <para/>
/// Example for default:
/// <c>{{App}: Sublogger}: sub-sublogger</c>
/// </summary>
/// <remarks>
/// default:
/// <c>{0}: {1}</c>
/// </remarks>
public string NameFormat = "{0}: {1}";
public new string Format = "{5}[{0}][{2}]["+ANSI.Styles.Bold+"{3}"+ANSI.Styles.ResetBold+"]: {4}"+ANSI.Styles.ResetAll;
/// <summary>
/// The colors of the severities (index: 0: Fatal, 1: Error, 2: Warning, 3: Message, 4: Info, 5: Debug, 6: Trace).
/// </summary>
Expand All @@ -48,27 +38,23 @@ public TerminalTarget(string? format = null, TextWriter? terminalOut = null, Tex
Error = terminalError ?? Terminal.Error;
}
/// <inheritdoc/>
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);
}

/// <inheritdoc/>
/// <remarks>
/// Writes a line.
/// </remarks>
public void Write<T>(Severity severity, DateTime time, Logger logger, T? text) {
public override void Write<T>(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()));
}

}
Expand Down
2 changes: 1 addition & 1 deletion Terminal/Terminal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PublishTrimmed>true</PublishTrimmed>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>0xDED.Terminal</PackageId>
<Version>4.1.0</Version>
<Version>4.1.1</Version>
<Authors>0xDED</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
1 change: 1 addition & 0 deletions examples/Logging/Program.cs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit 9931f59

Please sign in to comment.