From 5bdde4f5637246e5718eea24cd844dfd4eabcc83 Mon Sep 17 00:00:00 2001 From: zsh2401 Date: Tue, 18 Aug 2020 11:10:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5:Aogger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AutumnBox.Logging.Shared/Aogger.cs | 45 ++++++++ .../AutumnBox.Logging.Shared.projitems | 2 + src/AutumnBox.Logging.Shared/CallerQuerier.cs | 93 +++++++++++++++ .../MyCommandProcedureManagerTest.cs | 2 +- .../Basic/Devices/DevicesTest.cs | 6 +- .../Basic/Executor/HestExecutorTest.cs | 8 +- src/AutumnBox.Tests/Basic/MADB/BooterTest.cs | 18 ++- src/AutumnBox.Tests/Logging/AoggerTest.cs | 59 ++++++++++ .../Logging/CallerQuerierTest.cs | 106 ++++++++++++++++++ src/AutumnBox.Tests/Main/UpdateCheckerTest.cs | 22 ---- src/AutumnBox.Tests/TestStartup.cs | 4 +- 11 files changed, 327 insertions(+), 38 deletions(-) create mode 100644 src/AutumnBox.Logging.Shared/Aogger.cs create mode 100644 src/AutumnBox.Logging.Shared/CallerQuerier.cs create mode 100644 src/AutumnBox.Tests/Logging/AoggerTest.cs create mode 100644 src/AutumnBox.Tests/Logging/CallerQuerierTest.cs delete mode 100644 src/AutumnBox.Tests/Main/UpdateCheckerTest.cs diff --git a/src/AutumnBox.Logging.Shared/Aogger.cs b/src/AutumnBox.Logging.Shared/Aogger.cs new file mode 100644 index 000000000..07cd0fd91 --- /dev/null +++ b/src/AutumnBox.Logging.Shared/Aogger.cs @@ -0,0 +1,45 @@ +/* + +* ============================================================================== +* +* Filename: Aogger +* Description:  +* +* Version: 1.0 +* Created: 2020/8/18 10:38:22 +* Compiler: Visual Studio 2019 +* +* Author: zsh2401 +* +* ============================================================================== +*/ +using AutumnBox.Logging.Management; +using System; +using System.Collections.Generic; +using System.Text; + +namespace AutumnBox.Logging +{ + /// + /// 自动化获取调用者信息 + /// + public static class Aogger + { + public static void WriteToLog(this Exception e, object? additionMessage = null) + { + SLogger.Warn(CallerQuerier.Get(2).TypeName, additionMessage ?? String.Empty, e); + } + public static void Info(object message) + { + SLogger.Info(CallerQuerier.Get(2).TypeName, message); + } + public static void Warn(object message) + { + SLogger.Warn(CallerQuerier.Get(2).TypeName, message); + } + public static void Exception(Exception e) + { + SLogger.Exception(CallerQuerier.Get(2).TypeName, e); + } + } +} diff --git a/src/AutumnBox.Logging.Shared/AutumnBox.Logging.Shared.projitems b/src/AutumnBox.Logging.Shared/AutumnBox.Logging.Shared.projitems index 73c6454d0..6e0320e3b 100644 --- a/src/AutumnBox.Logging.Shared/AutumnBox.Logging.Shared.projitems +++ b/src/AutumnBox.Logging.Shared/AutumnBox.Logging.Shared.projitems @@ -9,6 +9,8 @@ AutumnBox.Logging + + diff --git a/src/AutumnBox.Logging.Shared/CallerQuerier.cs b/src/AutumnBox.Logging.Shared/CallerQuerier.cs new file mode 100644 index 000000000..6f843e9c8 --- /dev/null +++ b/src/AutumnBox.Logging.Shared/CallerQuerier.cs @@ -0,0 +1,93 @@ +/* + +* ============================================================================== +* +* Filename: CallerQuerier +* Description:  +* +* Version: 1.0 +* Created: 2020/8/18 10:08:46 +* Compiler: Visual Studio 2019 +* +* Author: zsh2401 +* +* ============================================================================== +*/ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; + +namespace AutumnBox.Logging +{ + public static class CallerQuerier + { + public static Result Default { get; } = new Result("Unknow", String.Empty); + public static Result GetCurrent() + { + return Get(2); + } + public static Result Get(int startAt) + { + Result? result = null; + var frames = new StackTrace().GetFrames(); + for (int i = startAt; i < frames.Length; i++) + { + var current = At(i); + if (current.TypeName.StartsWith("<>c")) + continue; + else + { + result = current; + break; + } + } + return result ?? Default; + Result At(int index) + { + return new Result( + frames[index]?.GetMethod()?.DeclaringType?.Name ?? String.Empty, + frames[index]?.GetMethod()?.Name ?? String.Empty + ); + } + } + public sealed class Result : IEquatable + { + public Result(string typeName, string methodName) + { + TypeName = typeName ?? throw new ArgumentNullException(nameof(typeName)); + MethodName = methodName ?? throw new ArgumentNullException(nameof(methodName)); + } + + public string TypeName { get; } + public string MethodName { get; } + + public override bool Equals(object? obj) + { + return Equals(obj as Result); + } + + public bool Equals(Result? other) + { + return other != null && + TypeName == other.TypeName && + MethodName == other.MethodName; + } + + public override int GetHashCode() + { + return HashCode.Combine(TypeName, MethodName); + } + + public static bool operator ==(Result? left, Result? right) + { + return EqualityComparer.Default.Equals(left, right); + } + + public static bool operator !=(Result? left, Result? right) + { + return !(left == right); + } + } + } +} diff --git a/src/AutumnBox.Tests/Basic/CommandDriven/MyCommandProcedureManagerTest.cs b/src/AutumnBox.Tests/Basic/CommandDriven/MyCommandProcedureManagerTest.cs index 5f6e23202..9d53845a7 100644 --- a/src/AutumnBox.Tests/Basic/CommandDriven/MyCommandProcedureManagerTest.cs +++ b/src/AutumnBox.Tests/Basic/CommandDriven/MyCommandProcedureManagerTest.cs @@ -17,7 +17,7 @@ public class MyCommandProcedureManagerTest [TestMethod] public void ExitCodeTest() { - var cpm = new ProcedureManager(); + using var cpm = new ProcedureManager(); using var command = cpm.OpenCommand("cmd.exe", "/c", "ping 127.0.0.1"); var result = command.Execute(); if (result.ExitCode != 0) diff --git a/src/AutumnBox.Tests/Basic/Devices/DevicesTest.cs b/src/AutumnBox.Tests/Basic/Devices/DevicesTest.cs index df83bbb3d..6af023253 100644 --- a/src/AutumnBox.Tests/Basic/Devices/DevicesTest.cs +++ b/src/AutumnBox.Tests/Basic/Devices/DevicesTest.cs @@ -2,19 +2,20 @@ using AutumnBox.Basic.MultipleDevices; using AutumnBox.Tests.Util; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.Diagnostics; using System.Linq; namespace AutumnBox.Tests.Basic.Devices { [TestClass] - public class DevicesTest + public class DevicesTest : IDisposable { public DevicesTest() { BasicBooter.Use(); } - ~DevicesTest() + public void Dispose() { BasicBooter.Free(); } @@ -28,7 +29,6 @@ public void Test() Debug.WriteLine(dev); return true; }); ; - BasicBooter.Free(); } } } diff --git a/src/AutumnBox.Tests/Basic/Executor/HestExecutorTest.cs b/src/AutumnBox.Tests/Basic/Executor/HestExecutorTest.cs index bb2f102ed..d9b00499c 100644 --- a/src/AutumnBox.Tests/Basic/Executor/HestExecutorTest.cs +++ b/src/AutumnBox.Tests/Basic/Executor/HestExecutorTest.cs @@ -2,18 +2,19 @@ using AutumnBox.Basic.Calling; using AutumnBox.Tests.Util; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.Diagnostics; namespace AutumnBox.Tests.Basic.Executor { [TestClass] - public class HestExecutorTest + public class HestExecutorTest : IDisposable { public HestExecutorTest() { BasicBooter.Use(); } - ~HestExecutorTest() + public void Dispose() { BasicBooter.Free(); } @@ -21,17 +22,16 @@ public HestExecutorTest() [TestMethod] public void PingTest() { - bool outputReceived = false; var executor = new HestExecutor(); executor.OutputReceived += (s, e) => { outputReceived = true; + Debug.WriteLine(e.Text); }; var result = executor.Cmd("ping baidu.com"); Assert.IsTrue(outputReceived); Assert.IsTrue(result.Output.Contains("Pinging")); - Debug.WriteLine(result.Output); } } } diff --git a/src/AutumnBox.Tests/Basic/MADB/BooterTest.cs b/src/AutumnBox.Tests/Basic/MADB/BooterTest.cs index 46500eacc..a29ac6050 100644 --- a/src/AutumnBox.Tests/Basic/MADB/BooterTest.cs +++ b/src/AutumnBox.Tests/Basic/MADB/BooterTest.cs @@ -3,17 +3,25 @@ using System.Diagnostics; using AutumnBox.Logging; using AutumnBox.Tests.Util; +using System; namespace AutumnBox.Tests.Basic.MADB { [TestClass] - public class BooterTest + public class BooterTest : IDisposable { - [TestMethod] - public void LoadManager() + public BooterTest() { BasicBooter.Use(); + } + public void Dispose() + { + BasicBooter.Free(); + } + [TestMethod] + public void LoadManager() + { using var cpm = BasicBooter.CommandProcedureManager; using var cmd = cpm.OpenCommand("adb", "devices"); cmd.Execute(); @@ -23,11 +31,9 @@ public void LoadManager() } SLogger.CDebug(cmd.Result.Output); - Assert.IsTrue(cmd.Result.ExitCode == 0); + Assert.AreEqual(0,cmd.Result.ExitCode); Assert.IsFalse(cmd.Result.Output.Contains("daemon not running")); Assert.IsTrue(cmd.Result.Output.Contains("List of devices attached")); - - BasicBooter.Free(); } } } diff --git a/src/AutumnBox.Tests/Logging/AoggerTest.cs b/src/AutumnBox.Tests/Logging/AoggerTest.cs new file mode 100644 index 000000000..88ceae35c --- /dev/null +++ b/src/AutumnBox.Tests/Logging/AoggerTest.cs @@ -0,0 +1,59 @@ +/* + +* ============================================================================== +* +* Filename: AoggerTest +* Description:  +* +* Version: 1.0 +* Created: 2020/8/18 10:43:57 +* Compiler: Visual Studio 2019 +* +* Author: zsh2401 +* +* ============================================================================== +*/ +using AutumnBox.Logging; +using AutumnBox.Logging.Management; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; + +namespace AutumnBox.Tests.Logging +{ + [TestClass] + public class AoggerTest : ICoreLogger + { + public void Dispose() + { + LoggingManager.Use(_defaultCoreLogger); + } + + private ICoreLogger _defaultCoreLogger; + + public AoggerTest() + { + _defaultCoreLogger = LoggingManager.CoreLogger; + LoggingManager.Use(this); + } + + [TestMethod] + public void Info() + { + Aogger.Info("f"); + var last = logs.Last(); + Assert.AreEqual(nameof(AoggerTest), last.Category); + Assert.AreEqual(nameof(Aogger.Info), last.Level); + } + + private readonly List logs = new List(); + public void Log(ILog log) + { + logs.Add(log); + Debug.WriteLine(log.ToFormatedString()); + } + } +} diff --git a/src/AutumnBox.Tests/Logging/CallerQuerierTest.cs b/src/AutumnBox.Tests/Logging/CallerQuerierTest.cs new file mode 100644 index 000000000..883db8715 --- /dev/null +++ b/src/AutumnBox.Tests/Logging/CallerQuerierTest.cs @@ -0,0 +1,106 @@ +/* + +* ============================================================================== +* +* Filename: CallerQuerierTest +* Description:  +* +* Version: 1.0 +* Created: 2020/8/18 10:14:24 +* Compiler: Visual Studio 2019 +* +* Author: zsh2401 +* +* ============================================================================== +*/ +using AutumnBox.Logging; +using AutumnBox.Logging.Management; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; + +namespace AutumnBox.Tests.Logging +{ + [TestClass] + public class CallerQuerierTest + { + + [TestMethod] + public void NormalMethod() + { + Assert.AreEqual(nameof(CallerQuerierTest), CallerQuerier.GetCurrent().TypeName); + } + + [TestMethod] + public void ActionMethod() + { + Action a = () => + { + Assert.AreEqual(nameof(CallerQuerierTest), CallerQuerier.GetCurrent().TypeName); + }; + a(); + Action b = (x) => + { + a(); + }; + b(1); + } + + [TestMethod] + public void FuncMethod() + { + Func a = () => + { + Assert.AreEqual(nameof(CallerQuerierTest), CallerQuerier.GetCurrent().TypeName); + return 1; + }; + a(); + Func b = (x) => + { + a(); + return x; + }; + b(1); + } + + [TestMethod] + public void Event() + { + TestEvent += (s, e) => Assert.AreEqual(nameof(CallerQuerierTest), CallerQuerier.GetCurrent().TypeName); + TestEvent?.Invoke(this, new EventArgs()); + } + + [TestMethod] + public void OtherClassTest() + { + var clazz = new OtherClass(); + clazz.Called += clazz.OtherClass_Called; + clazz.Raise(); + } + + + public event EventHandler? TestEvent; + + public class OtherClass : ICloneable + { + public event EventHandler? Called; + + public void Raise() + { + Called?.Invoke(this, new EventArgs()); + } + + public object Clone() + { + throw new NotImplementedException(); + } + + public void OtherClass_Called(object? sender, EventArgs e) + { + Assert.AreEqual(nameof(OtherClass), CallerQuerier.GetCurrent().TypeName); + } + } + } +} diff --git a/src/AutumnBox.Tests/Main/UpdateCheckerTest.cs b/src/AutumnBox.Tests/Main/UpdateCheckerTest.cs deleted file mode 100644 index ecce21672..000000000 --- a/src/AutumnBox.Tests/Main/UpdateCheckerTest.cs +++ /dev/null @@ -1,22 +0,0 @@ -/* - -* ============================================================================== -* -* Filename: UpdateCheckerTest -* Description:  -* -* Version: 1.0 -* Created: 2020/8/11 5:28:54 -* Compiler: Visual Studio 2019 -* -* Author: zsh2401 -* -* ============================================================================== -*/ - -namespace AutumnBox.Tests.Main -{ - class UpdateCheckerTest - { - } -} diff --git a/src/AutumnBox.Tests/TestStartup.cs b/src/AutumnBox.Tests/TestStartup.cs index 7d9e94449..a29216ee3 100644 --- a/src/AutumnBox.Tests/TestStartup.cs +++ b/src/AutumnBox.Tests/TestStartup.cs @@ -25,12 +25,12 @@ class TestStartup [AssemblyInitialize] public static void Startup(TestContext context) { - BasicBooter.Use(); + //BasicBooter.Use(); } [AssemblyCleanup] public static void Cleanup() { - BasicBooter.Free(); + //BasicBooter.Free(); } } }