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()
|
||||
{
|
||||
throw new NotImplementedException("Marshal.PtrToStringUTF8 can't get non-ascii chars?");
|
||||
return $"{{{Marshal.PtrToStringUTF8(key)}, {value}}}";
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,7 @@
|
||||
<Reference Include="System.Dynamic" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DTLib.Network\DTLib.Network.csproj" />
|
||||
<ProjectReference Include="..\DTLib.Dtsod\DTLib.Dtsod.csproj" />
|
||||
@ -28,8 +28,12 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Dtsod\TestResources\**\*">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Dtsod\TestResources\**\*" CopyToOutputDirectory="Always" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||
<Exec Command="rm -rf ../bin/" />
|
||||
<Exec Command="cp -r bin/ ../bin/" />
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -19,7 +19,7 @@ public static class TestPInvoke
|
||||
|
||||
public static void TestPrintf()
|
||||
{
|
||||
Info.Log("c", "---------[TestPInvoke/Printf]---------");
|
||||
Info.Log("c", "---------[TestPInvoke/Printf]---------", "b", "");
|
||||
pinvoke_print("ъъ~ 中文");
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
@ -29,7 +29,7 @@ public static class TestPInvoke
|
||||
|
||||
public static unsafe void TestMarshalling()
|
||||
{
|
||||
Info.Log("c", "---------[TestAutoarr/Print]----------");
|
||||
Info.Log("c", "---------[TestAutoarr/TestMarshalling]----------");
|
||||
string msg = "ъъ~ 中文";
|
||||
test_marshalling(msg, out KVPair* kptr);
|
||||
KVPair k = *kptr;
|
||||
|
||||
@ -17,7 +17,7 @@ namespace DTLib.Tests;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
public static readonly ConsoleLogger Info = new();
|
||||
public static readonly ConsoleLogger Info = new("logs", "DTLib.Tests");
|
||||
public static void Main()
|
||||
{
|
||||
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 dir, string programName) : base(dir, programName) {}
|
||||
|
||||
@ -14,9 +11,11 @@ public class ConsoleLogger : BaseLogger
|
||||
|
||||
public override void Log(params string[] msg)
|
||||
{
|
||||
if (!IsEnabled) return;
|
||||
// write to file
|
||||
base.Log(msg);
|
||||
// append timestamp
|
||||
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;
|
||||
strb.Append(msg[index]);
|
||||
msg[index] = strb.ToString();
|
||||
@ -26,21 +25,5 @@ public class ConsoleLogger : BaseLogger
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
rm -rf bin
|
||||
mkdir bin
|
||||
cp -rv DTLib.*/bin/* bin/
|
||||
cp -rv DTLib.Tests/bin/* bin/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user