From bc6b4e5ab02c32160cd9f126eaf4f91d823a12d7 Mon Sep 17 00:00:00 2001 From: Cal <42492716+ly3027929699@users.noreply.github.com> Date: Mon, 9 Dec 2024 10:40:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=A8=A1=E5=BC=8F=EF=BC=8C=E8=A7=84=E8=8C=83=E5=86=99?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Console/Program.cs | 25 ++++++++++---- FileUtilsCs/EntryPoints.cs | 70 +++++++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 30 deletions(-) diff --git a/Console/Program.cs b/Console/Program.cs index c77b3d2..1bcbe03 100644 --- a/Console/Program.cs +++ b/Console/Program.cs @@ -4,11 +4,13 @@ using System.Runtime.InteropServices; unsafe { Console.WriteLine(1); - SetLogImmediately(true); + SetLogOutputMode(LogMode.Manual); SetLogInfo(& LogInfo); SetLogError(& LogError); - TimeProvider.System.CreateTimer(Callback, null, TimeSpan.FromMilliseconds(16), TimeSpan.FromMilliseconds(16)); - + //Manual 才需要这个手动调用 + //TimeProvider.System.CreateTimer(Callback, null, TimeSpan.FromMilliseconds(16), TimeSpan.FromMilliseconds(16)); + + void Callback(object? state) { Tick(); @@ -20,9 +22,9 @@ unsafe (char*)Unsafe.AsPointer(ref Unsafe.AsRef(in "C:\\A".GetPinnableReference())), (char*)Unsafe.AsPointer(ref Unsafe.AsRef(in "C:\\B".GetPinnableReference()))); Console.ReadKey(); - - [DllImport("FileUtilsCs.dll", EntryPoint = "SetLogImmediately")] - static extern void SetLogImmediately(bool value); + + [DllImport("FileUtilsCs.dll", EntryPoint = "SetLogOutputMode")] + static extern void SetLogOutputMode([MarshalAs(UnmanagedType.I4)]LogMode value); [DllImport("FileUtilsCs.dll", EntryPoint = "TestLog")] static extern void TestLog(); @@ -47,4 +49,15 @@ unsafe var addrOfPinnedObject = new string((char*)obj); System.Console.WriteLine(addrOfPinnedObject); } + + +} +enum LogMode +{ + //自动 + Auto, + //立即输出 + Immediate, + //手动Tick + Manual, } \ No newline at end of file diff --git a/FileUtilsCs/EntryPoints.cs b/FileUtilsCs/EntryPoints.cs index 3ebbf86..b35195b 100644 --- a/FileUtilsCs/EntryPoints.cs +++ b/FileUtilsCs/EntryPoints.cs @@ -1,41 +1,64 @@ using System.Collections.Concurrent; +using System.ComponentModel; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace FileUtilsCs; -public class EntryPoints +enum LogMode +{ + //自动 + Auto, + //立即输出 + Immediate, + //手动Tick + Manual, +} + +public static class EntryPoints { internal static Action? logInfoAction; internal static Action? logErrorAction; - internal static bool logImmediately = false; + internal static LogMode mode = LogMode.Auto; - static ConcurrentQueue _logQueue = new ConcurrentQueue(); + static ConcurrentQueue _logInfoQueue = new ConcurrentQueue(); static ConcurrentQueue _logErrorQueue = new ConcurrentQueue(); - [UnmanagedCallersOnly(EntryPoint = "Tick")] - static void Tick() + static EntryPoints() + { + TimeProvider.System.CreateTimer(LogCallBack, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(16)); + + } + private static void LogCallBack(object? state) + { + if (mode == LogMode.Auto) + TickLog(); + } + + private static void TickLog() { - if (logImmediately) - return; if (_logErrorQueue.TryDequeue(out var error)) { logErrorAction?.Invoke(error); } - - _logErrorQueue.Clear(); - if (_logQueue.TryDequeue(out var message)) + if (_logInfoQueue.TryDequeue(out var message)) { logInfoAction?.Invoke(message); } + } - _logQueue.Clear(); + [UnmanagedCallersOnly(EntryPoint = "Tick")] + static void Tick() + { + if (mode != LogMode.Manual) + return; + TickLog(); } public static void LogError(string message) { - if (logImmediately) + if (mode == LogMode.Immediate) logErrorAction?.Invoke(message); else _logErrorQueue.Enqueue(message); @@ -43,22 +66,22 @@ public class EntryPoints public static void Log(string message) { - if (logImmediately) + if (mode == LogMode.Immediate) logInfoAction?.Invoke(message); else - _logQueue.Enqueue(message); + _logInfoQueue.Enqueue(message); } /// - /// 立即输出日志 + /// 设置日志输出类型 /// - /// - [UnmanagedCallersOnly(EntryPoint = "SetLogImmediately")] - public static void SetLogImmediately(bool value) + /// + [UnmanagedCallersOnly(EntryPoint = "SetLogOutputMode")] + public static void SetLogOutputMode(int mode = 0) { - logImmediately = value; + EntryPoints.mode = (LogMode)mode; } - + [UnmanagedCallersOnly(EntryPoint = "SetLogInfo")] public static unsafe void SetLogInfo(delegate* managed logAction) { @@ -106,9 +129,9 @@ public class EntryPoints try { if (isRoot && !Directory.Exists(srcRootPath)) - throw new Exception($"srcRootPath : {srcRootPath} not exists!"); + throw new Win32Exception($"srcRootPath : {srcRootPath} not exists!"); if (isRoot && !Directory.Exists(destRootPath)) - throw new Exception($"srcRootPath : {destRootPath} not exists!"); + throw new Win32Exception($"srcRootPath : {destRootPath} not exists!"); if (Path.GetFullPath(srcRootPath) == Path.GetFullPath(destRootPath)) return; srcRootPath = srcRootPath[srcRootPath.Length - 1] != Path.DirectorySeparatorChar ? srcRootPath + Path.DirectorySeparatorChar : srcRootPath; @@ -128,8 +151,7 @@ public class EntryPoints var destDirectoryInfo = new FileInfo(destFileFullName).Directory; if (destDirectoryInfo == null) { - LogError($"destDirectoryInfo is null where path is :{destFileFullName}"); - return; + throw new IOException($"destDirectoryInfo is null where path is :{destFileFullName}"); } if (!destDirectoryInfo.Exists)