some changes
This commit is contained in:
parent
d99177ae63
commit
7f5d095f0f
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -21,6 +21,7 @@ public struct KVPair
|
|||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
throw new NotImplementedException("Marshal.PtrToStringUTF8 can't get non-ascii chars?");
|
||||||
return $"{{{Marshal.PtrToStringUTF8(key)}, {value}}}";
|
return $"{{{Marshal.PtrToStringUTF8(key)}, {value}}}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -20,7 +20,7 @@
|
|||||||
<Reference Include="System.Dynamic" />
|
<Reference Include="System.Dynamic" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\DTLib.Network\DTLib.Network.csproj" />
|
<ProjectReference Include="..\DTLib.Network\DTLib.Network.csproj" />
|
||||||
<ProjectReference Include="..\DTLib.Dtsod\DTLib.Dtsod.csproj" />
|
<ProjectReference Include="..\DTLib.Dtsod\DTLib.Dtsod.csproj" />
|
||||||
@ -28,8 +28,12 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Update="Dtsod\TestResources\**\*">
|
<None Update="Dtsod\TestResources\**\*" CopyToOutputDirectory="Always" />
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||||
|
<Exec Command="rm -rf ../bin/" />
|
||||||
|
<Exec Command="cp -r bin/ ../bin/" />
|
||||||
|
</Target>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -19,7 +19,7 @@ public static class TestPInvoke
|
|||||||
|
|
||||||
public static void TestPrintf()
|
public static void TestPrintf()
|
||||||
{
|
{
|
||||||
Info.Log("c", "---------[TestPInvoke/Printf]---------");
|
Info.Log("c", "---------[TestPInvoke/Printf]---------", "b", "");
|
||||||
pinvoke_print("ъъ~ 中文");
|
pinvoke_print("ъъ~ 中文");
|
||||||
Info.Log("g", "test completed");
|
Info.Log("g", "test completed");
|
||||||
}
|
}
|
||||||
@ -29,7 +29,7 @@ public static class TestPInvoke
|
|||||||
|
|
||||||
public static unsafe void TestMarshalling()
|
public static unsafe void TestMarshalling()
|
||||||
{
|
{
|
||||||
Info.Log("c", "---------[TestAutoarr/Print]----------");
|
Info.Log("c", "---------[TestAutoarr/TestMarshalling]----------");
|
||||||
string msg = "ъъ~ 中文";
|
string msg = "ъъ~ 中文";
|
||||||
test_marshalling(msg, out KVPair* kptr);
|
test_marshalling(msg, out KVPair* kptr);
|
||||||
KVPair k = *kptr;
|
KVPair k = *kptr;
|
||||||
|
|||||||
@ -17,7 +17,7 @@ namespace DTLib.Tests;
|
|||||||
|
|
||||||
public static class Program
|
public static class Program
|
||||||
{
|
{
|
||||||
public static readonly ConsoleLogger Info = new();
|
public static readonly ConsoleLogger Info = new("logs", "DTLib.Tests");
|
||||||
public static void Main()
|
public static void Main()
|
||||||
{
|
{
|
||||||
PublicLog.LogEvent += Info.Log;
|
PublicLog.LogEvent += Info.Log;
|
||||||
|
|||||||
@ -1,47 +0,0 @@
|
|||||||
using System.IO;
|
|
||||||
using File = DTLib.Filesystem.File;
|
|
||||||
|
|
||||||
namespace DTLib.Logging;
|
|
||||||
|
|
||||||
public abstract class BaseLogger : IDisposable
|
|
||||||
{
|
|
||||||
public BaseLogger() { }
|
|
||||||
public BaseLogger(string logfile)
|
|
||||||
{
|
|
||||||
WriteToFile=true;
|
|
||||||
LogfileName = logfile;
|
|
||||||
LogfileStream = File.OpenWrite(logfile);
|
|
||||||
}
|
|
||||||
|
|
||||||
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; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool _writeToFile;
|
|
||||||
public bool WriteToFile
|
|
||||||
{
|
|
||||||
get { lock (_statelocker) return _writeToFile; }
|
|
||||||
set { lock (_statelocker) _writeToFile = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void Log(params string[] msg);
|
|
||||||
|
|
||||||
public virtual void Dispose()
|
|
||||||
{
|
|
||||||
LogfileStream?.Flush();
|
|
||||||
LogfileStream?.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
~BaseLogger() => Dispose();
|
|
||||||
}
|
|
||||||
@ -1,11 +1,8 @@
|
|||||||
using System.Globalization;
|
namespace DTLib.Logging;
|
||||||
|
|
||||||
namespace DTLib.Logging;
|
|
||||||
|
|
||||||
// вывод лога в консоль и файл
|
// вывод лога в консоль и файл
|
||||||
public class ConsoleLogger : BaseLogger
|
public class ConsoleLogger : FileLogger
|
||||||
{
|
{
|
||||||
public ConsoleLogger() : base() {}
|
|
||||||
public ConsoleLogger(string logfile) : base(logfile){}
|
public ConsoleLogger(string logfile) : base(logfile){}
|
||||||
public ConsoleLogger(string dir, string programName) : base(dir, programName) {}
|
public ConsoleLogger(string dir, string programName) : base(dir, programName) {}
|
||||||
|
|
||||||
@ -14,9 +11,11 @@ public class ConsoleLogger : BaseLogger
|
|||||||
|
|
||||||
public override void Log(params string[] msg)
|
public override void Log(params string[] msg)
|
||||||
{
|
{
|
||||||
if (!IsEnabled) return;
|
// write to file
|
||||||
|
base.Log(msg);
|
||||||
|
// append timestamp
|
||||||
var strb = new StringBuilder();
|
var strb = new StringBuilder();
|
||||||
strb.Append('[').Append(DateTime.Now.ToString(CultureInfo.InvariantCulture)).Append("]: ");
|
strb.Append('[').Append(LastLogMessageTime).Append("]: ");
|
||||||
int index = msg.Length == 1 ? 0 : 1;
|
int index = msg.Length == 1 ? 0 : 1;
|
||||||
strb.Append(msg[index]);
|
strb.Append(msg[index]);
|
||||||
msg[index] = strb.ToString();
|
msg[index] = strb.ToString();
|
||||||
@ -26,21 +25,5 @@ public class ConsoleLogger : BaseLogger
|
|||||||
ColoredConsole.Write(msg);
|
ColoredConsole.Write(msg);
|
||||||
Console.WriteLine();
|
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
49
DTLib/Logging/FileLogger.cs
Normal file
49
DTLib/Logging/FileLogger.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
namespace DTLib.Logging;
|
||||||
|
|
||||||
|
public class FileLogger : IDisposable
|
||||||
|
{
|
||||||
|
public FileLogger(string logfile)
|
||||||
|
{
|
||||||
|
LogfileName = logfile;
|
||||||
|
LogfileStream = File.OpenAppend(logfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileLogger(string dir, string programName)
|
||||||
|
: this($"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_')) { }
|
||||||
|
|
||||||
|
public string LogfileName { get; protected set; }
|
||||||
|
public System.IO.FileStream LogfileStream { get; protected set; }
|
||||||
|
protected string LastLogMessageTime;
|
||||||
|
|
||||||
|
|
||||||
|
public virtual void Log(params string[] msg)
|
||||||
|
{
|
||||||
|
lock (LogfileStream)
|
||||||
|
{
|
||||||
|
LastLogMessageTime = DateTime.Now.ToString(CultureInfo.InvariantCulture);
|
||||||
|
LogfileStream.WriteByte('['.ToByte());
|
||||||
|
LogfileStream.Write(LastLogMessageTime.ToBytes());
|
||||||
|
LogfileStream.Write("]: ".ToBytes());
|
||||||
|
if (msg.Length == 1)
|
||||||
|
LogfileStream.Write(msg[0].ToBytes());
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var strb = new StringBuilder();
|
||||||
|
for (ushort i = 1; i < msg.Length; i += 2)
|
||||||
|
strb.Append(msg[i]);
|
||||||
|
LogfileStream.Write(strb.ToString().ToBytes());
|
||||||
|
}
|
||||||
|
LogfileStream.WriteByte('\n'.ToByte());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void Dispose()
|
||||||
|
{
|
||||||
|
LogfileStream?.Flush();
|
||||||
|
LogfileStream?.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
~FileLogger() => Dispose();
|
||||||
|
}
|
||||||
2
build.sh
2
build.sh
@ -3,4 +3,4 @@ dotnet build -c Release
|
|||||||
dotnet build -c Debug
|
dotnet build -c Debug
|
||||||
rm -rf bin
|
rm -rf bin
|
||||||
mkdir bin
|
mkdir bin
|
||||||
cp -rv DTLib.*/bin/* bin/
|
cp -rv DTLib.Tests/bin/* bin/
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user