Skip to content

Commit 0c0ad4a

Browse files
committed
2.0.0
1 parent e9f8f19 commit 0c0ad4a

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

Terminal/Logging.cs

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ public class Logger : IDisposable {
5858
/// <summary>
5959
/// All the targets, key MUST BE nameof(...Target).
6060
/// </summary>
61-
public Dictionary<string, (ITarget target, bool enabled)> Targets;
61+
public Dictionary<Type, (ITarget target, bool enabled)> Targets;
6262
public Severity logLevel = Severity.Info;
6363
/// <summary>
6464
/// Creates a logger.
6565
/// </summary>
6666
/// <param name="id">The ID to identify this logger, like 'me.0xDED.MyProject' (if this ID is already registered it will throw an <see cref="ArgumentException"/> error).</param>
6767
/// <param name="name">The name of the logger, used in the log files and terminal.</param>
6868
/// <param name="severity">The log level of this logger.</param>
69+
/// <param name="targets">The targets to add and enable (default: <see cref="TerminalTarget"/>, <see cref="FileTarget"/> with path ").</param>
6970
/// <exception cref="ArgumentException"/>
70-
public Logger(string id, string name, Severity severity = Severity.Info, Dictionary<string, ITarget>? targets = null) {
71+
public Logger(string id, string name, Severity severity = Severity.Info, Dictionary<Type, ITarget>? targets = null) {
7172
ID = id;
7273
Name = name;
7374
logLevel = severity;
74-
Targets = (targets ?? new Dictionary<string, ITarget>{{nameof(TerminalTarget), new TerminalTarget()}, {nameof(FileTarget),new FileTarget()}}).Select(target => new KeyValuePair<string, (ITarget, bool)>(target.Key, (target.Value, true))).ToDictionary();
75+
Targets = targets != null ? targets.Select(target => new KeyValuePair<Type, (ITarget, bool)>(target.Key, (target.Value, true))).ToDictionary() : new Dictionary<Type, (ITarget, bool enabled)>{{typeof(FileTarget), (new TerminalTarget(), true)}, {typeof(FileTarget), (new FileTarget("./latest.log"), true)}};
7576
if (!Loggers.Register(this)) {
7677
throw new ArgumentException("A logger with this ID has already been registered.", nameof(id));
7778
}
@@ -83,8 +84,38 @@ public Logger(string id, string name, Severity severity = Severity.Info, Diction
8384
public void SetLevel(Severity maxSeverity) {
8485
logLevel = maxSeverity;
8586
}
86-
public bool SetTarget(string nameOf, bool enabled) {
87-
Targets.Set(nameOf, Targets.Get(nameOf).enabled =
87+
88+
/// <summary>
89+
/// Sets if a target is enabled.
90+
/// </summary>
91+
/// <param name="type">The type of the Target.</param>
92+
/// <param name="enabled">True if enabled.</param>
93+
/// <returns>True if there is a Target with that type.</returns>
94+
public bool SetTarget(Type type, bool enabled) {
95+
if (Targets.TryGetValue(type, out (ITarget target, bool enabled) value)) {
96+
value.enabled = enabled;
97+
Targets[type] = value;
98+
return true;
99+
}
100+
return false;
101+
}
102+
103+
/// <summary>
104+
/// Adds a target.
105+
/// </summary>
106+
/// <param name="target">The target to add.</param>
107+
/// <param name="enabled">If it is enabled.</param>
108+
public void AddTarget(ITarget target, bool enabled = true) {
109+
Targets.Add(target.GetType(), (target, enabled));
110+
}
111+
/// <summary>
112+
/// Removes a Target.
113+
/// </summary>
114+
/// <param name="type">The type of the target</param>
115+
/// <returns>True if there was a target with that type.</returns>
116+
public bool RemoveTarget(Type type) {
117+
return Targets.Remove(type);
118+
}
88119
/// <summary>
89120
/// Returns the ANSI color corresponding to the severity.
90121
/// </summary>
@@ -115,11 +146,11 @@ public static string GetColor(Severity severity) {
115146
/// <param name="severity">The severity of the text.</param>
116147
/// <param name="text">The text to write (<see cref="object.ToString"/>).</param>
117148
public void Log<T>(Severity severity, T? text) {
118-
if (((byte)severity) > ((byte)logLevel)) { return;
149+
if (((byte)severity) > ((byte)logLevel)) { return; }
119150
DateTime time = DateTime.Now;
120-
foreach ((ITarget target, bool enabled) target in Targets) {
121-
if (target.enabled) {
122-
target.target.Write(severity, time, this, text);
151+
foreach (KeyValuePair<Type, (ITarget target, bool enabled)> target in Targets) {
152+
if (target.Value.enabled) {
153+
target.Value.target.Write(severity, time, this, text);
123154
}
124155
}
125156
}
@@ -184,8 +215,8 @@ public void LogTrace<T>(T? text) {
184215
/// </summary>
185216
public void Dispose() {
186217
Loggers.UnRegister(this);
187-
foreach ((ITarget target, bool enabled) target in Targets) {
188-
target.target.Dispose();
218+
foreach (KeyValuePair<Type, (ITarget target, bool enabled)> target in Targets) {
219+
target.Value.target.Dispose();
189220
}
190221
GC.SuppressFinalize(this);
191222
}
@@ -196,7 +227,7 @@ public void Dispose() {
196227
/// </summary>
197228
public interface ITarget : IDisposable {
198229
/// <summary>
199-
/// The method to write to output
230+
/// The method to write to output.
200231
/// </summary>
201232
/// <typeparam name="T">The type of the text.</typeparam>
202233
/// <param name="severity">The severity of the message.</param>
@@ -205,31 +236,37 @@ public interface ITarget : IDisposable {
205236
/// <param name="text">The text to write (<see cref="object.ToString"/>).</param>
206237
public void Write<T>(Severity severity, DateTime time, Logger logger, T? text);
207238
}
239+
/// <summary>
240+
/// A Target for the terminal.
241+
/// </summary>
208242
public class TerminalTarget : ITarget {
243+
/// <summary>
244+
/// The out stream to the terminal.
245+
/// </summary>
209246
public TextWriter Out;
210247
public TerminalTarget(TextWriter? terminalOut = null) {
211-
Out = (terminalOut ?? Terminal.Out);
248+
Out = terminalOut ?? Terminal.Out;
212249
}
213250
public void Dispose() {
214251
GC.SuppressFinalize(this);
215252
}
216253

217-
public void Write<T>(Severity severity, DateTime time, string name, string ID, T? text) {
218-
Out.WriteLine(Logger.GetColor(severity)+"["+Name+"]["+time.ToString()+"]["+ANSI.Styles.Bold+severity.ToString()+ANSI.Styles.ResetBold+"]: "+text?.ToString()+ANSI.Styles.ResetAll);
254+
public void Write<T>(Severity severity, DateTime time, Logger logger, T? text) {
255+
Out.WriteLine(Logger.GetColor(severity)+"["+logger.Name+"]["+time.ToString()+"]["+ANSI.Styles.Bold+severity.ToString()+ANSI.Styles.ResetBold+"]: "+text?.ToString()+ANSI.Styles.ResetAll);
219256
}
220257
}
221258
public class FileTarget : ITarget
222259
{
223-
public FileStream FileOut;
260+
public TextWriter FileOut;
224261
public FileTarget(string path) {
225-
FileOut = File.OpenWrite(path).;
262+
FileOut = new StreamWriter(File.OpenWrite(path));
226263
}
227264
public void Dispose() {
228265
FileOut.Close();
229266
GC.SuppressFinalize(this);
230267
}
231268

232-
public void Write<T>(Severity severity, DateTime time, string name, string ID, T? text) {
233-
FileOut.WriteLine("["+Name+"]["+time+"]["+severity.ToString()+"]: "+text?.ToString());
269+
public void Write<T>(Severity severity, DateTime time, Logger logger, T? text) {
270+
FileOut.WriteLine("["+logger.Name+"]["+time+"]["+severity.ToString()+"]: "+text?.ToString());
234271
}
235272
}

Terminal/Terminal.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
<Authors>0xDED</Authors>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
12+
<PackageTags>Terminal;library;console;logging;keyboard;ansi</PackageTags>
1213
<PackageDescription>Terminal is a C# library for managing everything in the terminal, it's an alternative of Console, and it can be used for tui applications. It also supports logging.</PackageDescription>
14+
<RepositoryUrl>https://github.com/dedouwe26/Terminal.git</RepositoryUrl>
15+
<RepositoryType>git</RepositoryType>
1316
</PropertyGroup>
1417
<ItemGroup>
1518
<None Include="../README.md" Pack="true" PackagePath="\"/>

examples/Logging/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Program {
44
public const string LoggerID = "me.0xDED.Terminal.examples";
5-
public static Logger logger = new(LoggerID, "Logging Example", "./latest.log", Severity.Trace);
5+
public static Logger logger = new(LoggerID, "Logging Example", Severity.Trace);
66
public static void Main() {
77
logger.LogMessage("Hello, world!");
88
logger.LogInfo("This is the start of the program!");

0 commit comments

Comments
 (0)