From d99177ae6303ee80d4716222b475ff92a03a95dd Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 1 Jul 2022 19:38:15 +0300 Subject: [PATCH] PublicLog --- DTLib.Dtsod/IDtsod.cs | 2 +- DTLib.Network/FSP.cs | 11 ++---- DTLib.Network/OldNetwork.cs | 2 +- DTLib.Tests/Dtsod/TestDtsodV23.cs | 16 +++++---- DTLib.Tests/Dtsod/TestDtsodV24.cs | 8 ++--- DTLib.Tests/Program.cs | 6 ++-- DTLib/Extensions/BaseConverter.cs | 2 +- DTLib/Loggers/ConsoleLogger.cs | 45 ----------------------- DTLib/{Loggers => Logging}/BaseLogger.cs | 26 +++++++++----- DTLib/Logging/ConsoleLogger.cs | 46 ++++++++++++++++++++++++ DTLib/{ => Logging}/PublicLog.cs | 11 +++--- DTLib/{Loggers => Logging}/Tester.cs | 6 ++-- 12 files changed, 92 insertions(+), 89 deletions(-) delete mode 100644 DTLib/Loggers/ConsoleLogger.cs rename DTLib/{Loggers => Logging}/BaseLogger.cs (51%) create mode 100644 DTLib/Logging/ConsoleLogger.cs rename DTLib/{ => Logging}/PublicLog.cs (55%) rename DTLib/{Loggers => Logging}/Tester.cs (65%) diff --git a/DTLib.Dtsod/IDtsod.cs b/DTLib.Dtsod/IDtsod.cs index 6a88a50..443ffbf 100644 --- a/DTLib.Dtsod/IDtsod.cs +++ b/DTLib.Dtsod/IDtsod.cs @@ -4,7 +4,7 @@ global using System.Collections.Generic; global using System.Linq; global using System.Text; global using DTLib.Extensions; -global using static DTLib.PublicLog; +global using static DTLib.Logging.PublicLog; namespace DTLib.Dtsod; diff --git a/DTLib.Network/FSP.cs b/DTLib.Network/FSP.cs index a092916..d9703d3 100644 --- a/DTLib.Network/FSP.cs +++ b/DTLib.Network/FSP.cs @@ -151,16 +151,15 @@ public class FSP Debug("b", "file <", "c", fileOnClient, "b", ">... "); if (!File.Exists(fileOnClient)) { - DebugNoTime("y", "doesn't exist"); + Debug("y", "doesn't exist"); DownloadFile(dirOnServer + fileOnServer, fileOnClient); } else if (overwrite && hasher.HashFile(fileOnClient).HashToString() != manifest[fileOnServer]) { - DebugNoTime("y", "outdated"); + Debug("y", "outdated"); DownloadFile(dirOnServer + fileOnServer, fileOnClient); } - else - DebugNoTime("g", "without changes"); + else Debug("g", "without changes"); } // удаление лишних файлов if (delete_excess) @@ -202,8 +201,4 @@ public class FSP { if (debug) Log(msg); } - static void DebugNoTime(params string[] msg) - { - if (debug) LogNoTime(msg); - } } diff --git a/DTLib.Network/OldNetwork.cs b/DTLib.Network/OldNetwork.cs index c853d31..6f96738 100644 --- a/DTLib.Network/OldNetwork.cs +++ b/DTLib.Network/OldNetwork.cs @@ -6,7 +6,7 @@ global using System.Linq; global using System.Text; global using DTLib.Extensions; global using DTLib.Filesystem; -global using static DTLib.PublicLog; +global using static DTLib.Logging.PublicLog; using System.Diagnostics; using System.Net.Http; diff --git a/DTLib.Tests/Dtsod/TestDtsodV23.cs b/DTLib.Tests/Dtsod/TestDtsodV23.cs index b84d46e..c397943 100644 --- a/DTLib.Tests/Dtsod/TestDtsodV23.cs +++ b/DTLib.Tests/Dtsod/TestDtsodV23.cs @@ -19,7 +19,7 @@ public static class TestDtsodV23 Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]-----"); DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}base_types.dtsod")); foreach (var pair in dtsod) - Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); + Info.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); Info.Log("g", "test completed"); } public static void TestLists() @@ -28,10 +28,10 @@ public static class TestDtsodV23 DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}lists.dtsod")); foreach (var pair in dtsod) { - Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", + Info.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", $" count: {pair.Value.Count}"); foreach (var el in pair.Value) - Info.LogNoTime("b", '\t'+el.GetType().Name + ' ', "c", el.ToString()); + Info.Log("b", '\t'+el.GetType().Name + ' ', "c", el.ToString()); } Info.Log("g", "test completed"); } @@ -40,11 +40,13 @@ public static class TestDtsodV23 { Info.Log("c", "-----[TestDtsodV23/TestComplexes]-----"); DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}complexes.dtsod")); - foreach (var pair in dtsod) + foreach (var complex in dtsod) { - Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, - "b", " length: ", "c", pair.Value.Keys.Count.ToString() + "\n\t", - "y", pair.Value.ToString().Replace("\n","\n\t")); + Info.Log("b", complex.Value.GetType().Name + ' ', "w", complex.Key, + "b", " size: ", "c", complex.Value.Keys.Count.ToString()); + foreach (var pair in (DtsodV23) complex.Value) + Info.Log("b", '\t' + pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', + "c", pair.Value.ToString()); } Info.Log("g", "test completed"); } diff --git a/DTLib.Tests/Dtsod/TestDtsodV24.cs b/DTLib.Tests/Dtsod/TestDtsodV24.cs index ea77d63..ce5ba70 100644 --- a/DTLib.Tests/Dtsod/TestDtsodV24.cs +++ b/DTLib.Tests/Dtsod/TestDtsodV24.cs @@ -20,7 +20,7 @@ public static class TestDtsodV24 Info.Log("c", "-----[TestDtsodV24/TestBaseTypes]-----"); DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}base_types.dtsod")); foreach (var pair in dtsod) - Info.LogNoTime("b", pair.ToString()); + Info.Log("b", pair.ToString()); Info.Log("g", "test completed"); } @@ -39,16 +39,16 @@ public static class TestDtsodV24 foreach (KVPair pair in dtsod) { var list = new Autoarr(pair.value.VoidPtr, false); - Info.LogNoTime("b", pair.key.ToStringUTF8(), "w", $" length: {list.Length}"); + Info.Log("b", pair.key.ToStringUTF8(), "w", $" length: {list.Length}"); foreach (var el in list) { - Info.LogNoTime("h", '\t' + el.ToString()); + Info.Log("h", '\t' + el.ToString()); if (el.TypeCode == KerepTypeCode.AutoarrUnitypePtr) { var ar = new Autoarr(el.VoidPtr, false); foreach (var k in ar) { - Info.LogNoTime($"\t\t{k.ToString()}"); + Info.Log($"\t\t{k.ToString()}"); } } } diff --git a/DTLib.Tests/Program.cs b/DTLib.Tests/Program.cs index 94aaa0b..bd1bfd1 100644 --- a/DTLib.Tests/Program.cs +++ b/DTLib.Tests/Program.cs @@ -8,9 +8,9 @@ global using DTLib; global using DTLib.Extensions; global using DTLib.Filesystem; global using DTLib.Dtsod; -global using static DTLib.Loggers.LogFunctions; +global using static DTLib.Logging.Tester; global using static DTLib.Tests.Program; -using DTLib.Loggers; +using DTLib.Logging; namespace DTLib.Tests; @@ -20,9 +20,7 @@ public static class Program public static readonly ConsoleLogger Info = new(); public static void Main() { - Info.Enable(); PublicLog.LogEvent += Info.Log; - PublicLog.LogNoTimeEvent += Info.LogNoTime; Console.OutputEncoding = Encoding.UTF8; Console.InputEncoding = Encoding.UTF8; Console.Title="tester"; diff --git a/DTLib/Extensions/BaseConverter.cs b/DTLib/Extensions/BaseConverter.cs index b0a7a54..5ce7712 100644 --- a/DTLib/Extensions/BaseConverter.cs +++ b/DTLib/Extensions/BaseConverter.cs @@ -6,7 +6,7 @@ global using System.Text; global using System.Threading.Tasks; global using DTLib.Extensions; global using DTLib.Filesystem; -global using static DTLib.PublicLog; +global using static DTLib.Logging.PublicLog; namespace DTLib.Extensions; diff --git a/DTLib/Loggers/ConsoleLogger.cs b/DTLib/Loggers/ConsoleLogger.cs deleted file mode 100644 index f87919e..0000000 --- a/DTLib/Loggers/ConsoleLogger.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.Globalization; - -namespace DTLib.Loggers; - -// вывод лога в консоль и файл -public class ConsoleLogger : BaseLogger -{ - public ConsoleLogger() : base() {} - public ConsoleLogger(string logfile) : base(logfile){} - public ConsoleLogger(string dir, string programName) : base(dir, programName) {} - - - public override void Log(params string[] msg) - { - lock (statelocker) if (!IsEnabled) return; - if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "]: " + msg[0]; - else msg[1] = "[" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "]: " + msg[1]; - LogNoTime(msg); - } - - - readonly object consolelocker = new(); - - public void LogNoTime(params string[] msg) - { - lock (statelocker) if (!IsEnabled) return; - msg[msg.Length - 1] += '\n'; - lock (consolelocker) - ColoredConsole.Write(msg); - if (WriteToFile) - { - if (msg.Length == 1) - lock (LogfileStream) LogfileStream.Write(msg[0].ToBytes()); - else - { - StringBuilder strB = new(); - for (ushort i = 0; i < msg.Length; i++) - strB.Append(msg[++i]); - lock (LogfileStream) LogfileStream.Write(strB.ToString().ToBytes()); - } - } - } - - public void LogAsync(params string[] msg) => Task.Run(() => Log(msg)); -} diff --git a/DTLib/Loggers/BaseLogger.cs b/DTLib/Logging/BaseLogger.cs similarity index 51% rename from DTLib/Loggers/BaseLogger.cs rename to DTLib/Logging/BaseLogger.cs index 717c937..354b2d2 100644 --- a/DTLib/Loggers/BaseLogger.cs +++ b/DTLib/Logging/BaseLogger.cs @@ -1,7 +1,7 @@ using System.IO; using File = DTLib.Filesystem.File; -namespace DTLib.Loggers; +namespace DTLib.Logging; public abstract class BaseLogger : IDisposable { @@ -15,15 +15,25 @@ public abstract class BaseLogger : IDisposable public BaseLogger(string dir, string programName) : this($"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_')) { } + + public string LogfileName { get; protected set; } + public FileStream LogfileStream { get; protected set; } + protected readonly object _statelocker = new(); + + private bool _isEnabled=true; + public bool IsEnabled + { + get { lock (_statelocker) return _isEnabled; } + set { lock (_statelocker) _isEnabled = value; } + } - public string LogfileName; - public FileStream LogfileStream { get; init; } - public bool IsEnabled { get; private set; } = false; - public bool WriteToFile { get; private set; } = false; - protected readonly object statelocker = new(); - public void Disable() { lock (statelocker) IsEnabled = false; } - public void Enable() { lock (statelocker) IsEnabled = true; } + private bool _writeToFile; + public bool WriteToFile + { + get { lock (_statelocker) return _writeToFile; } + set { lock (_statelocker) _writeToFile = value; } + } public abstract void Log(params string[] msg); diff --git a/DTLib/Logging/ConsoleLogger.cs b/DTLib/Logging/ConsoleLogger.cs new file mode 100644 index 0000000..74c8118 --- /dev/null +++ b/DTLib/Logging/ConsoleLogger.cs @@ -0,0 +1,46 @@ +using System.Globalization; + +namespace DTLib.Logging; + +// вывод лога в консоль и файл +public class ConsoleLogger : BaseLogger +{ + public ConsoleLogger() : base() {} + public ConsoleLogger(string logfile) : base(logfile){} + public ConsoleLogger(string dir, string programName) : base(dir, programName) {} + + + readonly object consolelocker = new(); + + public override void Log(params string[] msg) + { + if (!IsEnabled) return; + var strb = new StringBuilder(); + strb.Append('[').Append(DateTime.Now.ToString(CultureInfo.InvariantCulture)).Append("]: "); + int index = msg.Length == 1 ? 0 : 1; + strb.Append(msg[index]); + msg[index] = strb.ToString(); + // write to console + lock (consolelocker) + { + ColoredConsole.Write(msg); + Console.WriteLine(); + } + // write to file + if (!WriteToFile) return; + if (msg.Length == 1) + lock (LogfileStream) + { + LogfileStream.Write(msg[0].ToBytes()); + LogfileStream.WriteByte('\n'.ToByte()); + } + else + { + for (ushort i = 3; i < msg.Length; i+=2) + strb.Append(msg[i]); + strb.Append('\n'); + lock (LogfileStream) + LogfileStream.Write(strb.ToString().ToBytes()); + } + } +} diff --git a/DTLib/PublicLog.cs b/DTLib/Logging/PublicLog.cs similarity index 55% rename from DTLib/PublicLog.cs rename to DTLib/Logging/PublicLog.cs index 90776de..3a0df2d 100644 --- a/DTLib/PublicLog.cs +++ b/DTLib/Logging/PublicLog.cs @@ -1,15 +1,12 @@ -namespace DTLib; +namespace DTLib.Logging; -// -// вывод логов со всех классов в библиотеке -// +/// +/// вывод логов со всех классов в библиотеке +/// public static class PublicLog { public delegate void LogDelegate(params string[] msg); // вот к этому объекту подключайте методы для вывода логов public static event LogDelegate LogEvent; public static void Log(params string[] msg) => LogEvent?.Invoke(msg); - - public static event LogDelegate LogNoTimeEvent; - public static void LogNoTime(params string[] msg) => LogNoTimeEvent?.Invoke(msg); } diff --git a/DTLib/Loggers/Tester.cs b/DTLib/Logging/Tester.cs similarity index 65% rename from DTLib/Loggers/Tester.cs rename to DTLib/Logging/Tester.cs index 9bb2bef..6f12c28 100644 --- a/DTLib/Loggers/Tester.cs +++ b/DTLib/Logging/Tester.cs @@ -1,9 +1,9 @@ using System.Diagnostics; using System.Globalization; -namespace DTLib.Loggers; +namespace DTLib.Logging; -public static class LogFunctions +public static class Tester { public static void LogOperationTime(string op_name, int repeats, Action operation) { @@ -13,6 +13,6 @@ public static class LogFunctions operation(); clock.Stop(); double time=(double)(clock.ElapsedTicks)/Stopwatch.Frequency/repeats; - LogNoTime("y",$"operation ","b",op_name,"y"," lasted ","b",time.ToString(CultureInfo.InvariantCulture),"y"," seconds"); + Log("y",$"operation ","b",op_name,"y"," lasted ","b",time.ToString(CultureInfo.InvariantCulture),"y"," seconds"); } } \ No newline at end of file