DefaultLOgger and IfNull methods

This commit is contained in:
Timerix22 2021-10-26 20:33:33 +03:00
parent 5538178cab
commit 9762d7f528
4 changed files with 61 additions and 2 deletions

View File

@ -34,6 +34,7 @@
<ItemGroup>
<Compile Include="Color.cs" />
<Compile Include="CompressedArray.cs" />
<Compile Include="DefaultLogger.cs" />
<Compile Include="Dtsod\DtsodV21.cs" />
<Compile Include="Dtsod\DtsodV22.cs" />
<Compile Include="Dtsod\V30\DtsodSerializableAttribute.cs" />

35
DefaultLogger.cs Normal file
View File

@ -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());
}
});
}
}

View File

@ -2,7 +2,5 @@
namespace DTLib
{
#pragma warning disable IDE1006 // Стили именования
public delegate Task EventHandlerAsync<TEventArgs>(object sender, TEventArgs e);
#pragma warning restore IDE1006 // Стили именования
}

View File

@ -196,5 +196,30 @@ namespace DTLib
public static T If<T>(this T input, bool condition, Func<T, T> if_true, Func<T, T> if_false) =>
condition ? if_true(input) : if_false(input);
public static void If<T>(this T input, bool condition, Action<T> if_true, Action<T> if_false)
{
if (condition) if_true(input);
else if_false(input);
}
public static T If<T>(this T input, bool condition, Func<T, T> if_true) =>
condition ? if_true(input) : input;
public static void If<T>(this T input, bool condition, Action<T> if_true)
{
if (condition) if_true(input);
}
public static T IfIsNull<T>(this T input, Func<T> if_true, Func<T> if_false) =>
input is null ? if_true() : if_false();
public static void IfIsNull<T>(this T input, Action if_true, Action if_false)
{
if (input is null) if_true();
else if_false();
}
public static T IfNull<T>(this T input, Func<T> if_true) =>
input is null ? if_true() : input;
public static void IfIsNull<T>(this T input, Action if_true)
{
if (input is null) if_true();
}
}
}