PublicLog
This commit is contained in:
parent
72142634a9
commit
d99177ae63
@ -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;
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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");
|
||||
}
|
||||
|
||||
@ -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<Unitype>(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<Unitype>(el.VoidPtr, false);
|
||||
foreach (var k in ar)
|
||||
{
|
||||
Info.LogNoTime($"\t\t{k.ToString()}");
|
||||
Info.Log($"\t\t{k.ToString()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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));
|
||||
}
|
||||
@ -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);
|
||||
|
||||
46
DTLib/Logging/ConsoleLogger.cs
Normal file
46
DTLib/Logging/ConsoleLogger.cs
Normal file
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,15 +1,12 @@
|
||||
namespace DTLib;
|
||||
namespace DTLib.Logging;
|
||||
|
||||
//
|
||||
// вывод логов со всех классов в библиотеке
|
||||
//
|
||||
/// <summary>
|
||||
/// вывод логов со всех классов в библиотеке
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user