diff --git a/DTLib.csproj b/DTLib.csproj index 63e5db3..edc6e01 100644 --- a/DTLib.csproj +++ b/DTLib.csproj @@ -34,6 +34,7 @@ + diff --git a/DefaultLogger.cs b/DefaultLogger.cs new file mode 100644 index 0000000..d0d0423 --- /dev/null +++ b/DefaultLogger.cs @@ -0,0 +1,35 @@ +using System; +using System.Text; +using DTLib.Filesystem; + +namespace DTLib +{ + // вывод лога в консоль и файл + public static class DefaultLogger + { + public static void SetLogfile(string dir, string programName) + => logfile = $"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_'); + + static string logfile; + static readonly SafeMutex LogMutex = new(); + public static void Log(params string[] msg) + { + if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString() + "]: " + msg[0]; + else msg[1] = "[" + DateTime.Now.ToString() + "]: " + msg[1]; + LogNoTime(msg); + } + public static void LogNoTime(params string[] msg) => + LogMutex.Execute(() => + { + ColoredConsole.Write(msg); + if (msg.Length == 1) File.AppendAllText(logfile, msg[0]); + else + { + StringBuilder strB = new(); + for (ushort i = 0; i < msg.Length; i++) + strB.Append(msg[++i]); + File.AppendAllText(logfile, strB.ToString()); + } + }); + } +} diff --git a/EventHandlerAsync.cs b/EventHandlerAsync.cs index e5af840..13361dc 100644 --- a/EventHandlerAsync.cs +++ b/EventHandlerAsync.cs @@ -2,7 +2,5 @@ namespace DTLib { -#pragma warning disable IDE1006 // Стили именования public delegate Task EventHandlerAsync(object sender, TEventArgs e); -#pragma warning restore IDE1006 // Стили именования } diff --git a/FrameworkFix.cs b/FrameworkFix.cs index 8e8c3b8..f82e807 100644 --- a/FrameworkFix.cs +++ b/FrameworkFix.cs @@ -196,5 +196,30 @@ namespace DTLib public static T If(this T input, bool condition, Func if_true, Func if_false) => condition ? if_true(input) : if_false(input); + public static void If(this T input, bool condition, Action if_true, Action if_false) + { + if (condition) if_true(input); + else if_false(input); + } + public static T If(this T input, bool condition, Func if_true) => + condition ? if_true(input) : input; + public static void If(this T input, bool condition, Action if_true) + { + if (condition) if_true(input); + } + + public static T IfIsNull(this T input, Func if_true, Func if_false) => + input is null ? if_true() : if_false(); + public static void IfIsNull(this T input, Action if_true, Action if_false) + { + if (input is null) if_true(); + else if_false(); + } + public static T IfNull(this T input, Func if_true) => + input is null ? if_true() : input; + public static void IfIsNull(this T input, Action if_true) + { + if (input is null) if_true(); + } } } \ No newline at end of file