PublicLog

This commit is contained in:
Timerix22 2022-07-01 19:38:15 +03:00
parent 72142634a9
commit d99177ae63
12 changed files with 92 additions and 89 deletions

View File

@ -4,7 +4,7 @@ global using System.Collections.Generic;
global using System.Linq; global using System.Linq;
global using System.Text; global using System.Text;
global using DTLib.Extensions; global using DTLib.Extensions;
global using static DTLib.PublicLog; global using static DTLib.Logging.PublicLog;
namespace DTLib.Dtsod; namespace DTLib.Dtsod;

View File

@ -151,16 +151,15 @@ public class FSP
Debug("b", "file <", "c", fileOnClient, "b", ">... "); Debug("b", "file <", "c", fileOnClient, "b", ">... ");
if (!File.Exists(fileOnClient)) if (!File.Exists(fileOnClient))
{ {
DebugNoTime("y", "doesn't exist"); Debug("y", "doesn't exist");
DownloadFile(dirOnServer + fileOnServer, fileOnClient); DownloadFile(dirOnServer + fileOnServer, fileOnClient);
} }
else if (overwrite && hasher.HashFile(fileOnClient).HashToString() != manifest[fileOnServer]) else if (overwrite && hasher.HashFile(fileOnClient).HashToString() != manifest[fileOnServer])
{ {
DebugNoTime("y", "outdated"); Debug("y", "outdated");
DownloadFile(dirOnServer + fileOnServer, fileOnClient); DownloadFile(dirOnServer + fileOnServer, fileOnClient);
} }
else else Debug("g", "without changes");
DebugNoTime("g", "without changes");
} }
// удаление лишних файлов // удаление лишних файлов
if (delete_excess) if (delete_excess)
@ -202,8 +201,4 @@ public class FSP
{ {
if (debug) Log(msg); if (debug) Log(msg);
} }
static void DebugNoTime(params string[] msg)
{
if (debug) LogNoTime(msg);
}
} }

View File

@ -6,7 +6,7 @@ global using System.Linq;
global using System.Text; global using System.Text;
global using DTLib.Extensions; global using DTLib.Extensions;
global using DTLib.Filesystem; global using DTLib.Filesystem;
global using static DTLib.PublicLog; global using static DTLib.Logging.PublicLog;
using System.Diagnostics; using System.Diagnostics;
using System.Net.Http; using System.Net.Http;

View File

@ -19,7 +19,7 @@ public static class TestDtsodV23
Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]-----"); Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]-----");
DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}base_types.dtsod")); DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}base_types.dtsod"));
foreach (var pair in 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"); Info.Log("g", "test completed");
} }
public static void TestLists() public static void TestLists()
@ -28,10 +28,10 @@ public static class TestDtsodV23
DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}lists.dtsod")); DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}lists.dtsod"));
foreach (var pair in 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}"); $" count: {pair.Value.Count}");
foreach (var el in pair.Value) 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"); Info.Log("g", "test completed");
} }
@ -40,11 +40,13 @@ public static class TestDtsodV23
{ {
Info.Log("c", "-----[TestDtsodV23/TestComplexes]-----"); Info.Log("c", "-----[TestDtsodV23/TestComplexes]-----");
DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}complexes.dtsod")); 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, Info.Log("b", complex.Value.GetType().Name + ' ', "w", complex.Key,
"b", " length: ", "c", pair.Value.Keys.Count.ToString() + "\n\t", "b", " size: ", "c", complex.Value.Keys.Count.ToString());
"y", pair.Value.ToString().Replace("\n","\n\t")); 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"); Info.Log("g", "test completed");
} }

View File

@ -20,7 +20,7 @@ public static class TestDtsodV24
Info.Log("c", "-----[TestDtsodV24/TestBaseTypes]-----"); Info.Log("c", "-----[TestDtsodV24/TestBaseTypes]-----");
DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}base_types.dtsod")); DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}base_types.dtsod"));
foreach (var pair in dtsod) foreach (var pair in dtsod)
Info.LogNoTime("b", pair.ToString()); Info.Log("b", pair.ToString());
Info.Log("g", "test completed"); Info.Log("g", "test completed");
} }
@ -39,16 +39,16 @@ public static class TestDtsodV24
foreach (KVPair pair in dtsod) foreach (KVPair pair in dtsod)
{ {
var list = new Autoarr<Unitype>(pair.value.VoidPtr, false); 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) foreach (var el in list)
{ {
Info.LogNoTime("h", '\t' + el.ToString()); Info.Log("h", '\t' + el.ToString());
if (el.TypeCode == KerepTypeCode.AutoarrUnitypePtr) if (el.TypeCode == KerepTypeCode.AutoarrUnitypePtr)
{ {
var ar = new Autoarr<Unitype>(el.VoidPtr, false); var ar = new Autoarr<Unitype>(el.VoidPtr, false);
foreach (var k in ar) foreach (var k in ar)
{ {
Info.LogNoTime($"\t\t{k.ToString()}"); Info.Log($"\t\t{k.ToString()}");
} }
} }
} }

View File

@ -8,9 +8,9 @@ global using DTLib;
global using DTLib.Extensions; global using DTLib.Extensions;
global using DTLib.Filesystem; global using DTLib.Filesystem;
global using DTLib.Dtsod; global using DTLib.Dtsod;
global using static DTLib.Loggers.LogFunctions; global using static DTLib.Logging.Tester;
global using static DTLib.Tests.Program; global using static DTLib.Tests.Program;
using DTLib.Loggers; using DTLib.Logging;
namespace DTLib.Tests; namespace DTLib.Tests;
@ -20,9 +20,7 @@ public static class Program
public static readonly ConsoleLogger Info = new(); public static readonly ConsoleLogger Info = new();
public static void Main() public static void Main()
{ {
Info.Enable();
PublicLog.LogEvent += Info.Log; PublicLog.LogEvent += Info.Log;
PublicLog.LogNoTimeEvent += Info.LogNoTime;
Console.OutputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8;
Console.InputEncoding = Encoding.UTF8; Console.InputEncoding = Encoding.UTF8;
Console.Title="tester"; Console.Title="tester";

View File

@ -6,7 +6,7 @@ global using System.Text;
global using System.Threading.Tasks; global using System.Threading.Tasks;
global using DTLib.Extensions; global using DTLib.Extensions;
global using DTLib.Filesystem; global using DTLib.Filesystem;
global using static DTLib.PublicLog; global using static DTLib.Logging.PublicLog;
namespace DTLib.Extensions; namespace DTLib.Extensions;

View File

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

View File

@ -1,7 +1,7 @@
using System.IO; using System.IO;
using File = DTLib.Filesystem.File; using File = DTLib.Filesystem.File;
namespace DTLib.Loggers; namespace DTLib.Logging;
public abstract class BaseLogger : IDisposable public abstract class BaseLogger : IDisposable
{ {
@ -16,14 +16,24 @@ public abstract class BaseLogger : IDisposable
public BaseLogger(string dir, string programName) public BaseLogger(string dir, string programName)
: this($"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_')) { } : this($"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_')) { }
public string LogfileName { get; protected set; }
public FileStream LogfileStream { get; protected set; }
public string LogfileName; protected readonly object _statelocker = new();
public FileStream LogfileStream { get; init; }
public bool IsEnabled { get; private set; } = false; private bool _isEnabled=true;
public bool WriteToFile { get; private set; } = false; public bool IsEnabled
protected readonly object statelocker = new(); {
public void Disable() { lock (statelocker) IsEnabled = false; } get { lock (_statelocker) return _isEnabled; }
public void Enable() { lock (statelocker) IsEnabled = true; } set { lock (_statelocker) _isEnabled = value; }
}
private bool _writeToFile;
public bool WriteToFile
{
get { lock (_statelocker) return _writeToFile; }
set { lock (_statelocker) _writeToFile = value; }
}
public abstract void Log(params string[] msg); public abstract void Log(params string[] msg);

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

View File

@ -1,15 +1,12 @@
namespace DTLib; namespace DTLib.Logging;
// /// <summary>
// вывод логов со всех классов в библиотеке /// вывод логов со всех классов в библиотеке
// /// </summary>
public static class PublicLog public static class PublicLog
{ {
public delegate void LogDelegate(params string[] msg); public delegate void LogDelegate(params string[] msg);
// вот к этому объекту подключайте методы для вывода логов // вот к этому объекту подключайте методы для вывода логов
public static event LogDelegate LogEvent; public static event LogDelegate LogEvent;
public static void Log(params string[] msg) => LogEvent?.Invoke(msg); 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);
} }

View File

@ -1,9 +1,9 @@
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; 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) public static void LogOperationTime(string op_name, int repeats, Action operation)
{ {
@ -13,6 +13,6 @@ public static class LogFunctions
operation(); operation();
clock.Stop(); clock.Stop();
double time=(double)(clock.ElapsedTicks)/Stopwatch.Frequency/repeats; 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");
} }
} }