Skip to content

Commit

Permalink
加入:Aogger
Browse files Browse the repository at this point in the history
  • Loading branch information
zsh2401 committed Aug 18, 2020
1 parent 319ceb2 commit 5bdde4f
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 38 deletions.
45 changes: 45 additions & 0 deletions src/AutumnBox.Logging.Shared/Aogger.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 自动化获取调用者信息
/// </summary>
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<Import_RootNamespace>AutumnBox.Logging</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Aogger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CallerQuerier.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Internal\LoggerImpl.Generic.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LoggerExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Management\AsyncBufferedRealtimeFileLogger.cs" />
Expand Down
93 changes: 93 additions & 0 deletions src/AutumnBox.Logging.Shared/CallerQuerier.cs
Original file line number Diff line number Diff line change
@@ -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<Result?>
{
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<Result>.Default.Equals(left, right);
}

public static bool operator !=(Result? left, Result? right)
{
return !(left == right);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/AutumnBox.Tests/Basic/Devices/DevicesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Win32AdbManager>();
}
~DevicesTest()
public void Dispose()
{
BasicBooter.Free();
}
Expand All @@ -28,7 +29,6 @@ public void Test()
Debug.WriteLine(dev);
return true;
}); ;
BasicBooter.Free();
}
}
}
8 changes: 4 additions & 4 deletions src/AutumnBox.Tests/Basic/Executor/HestExecutorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@
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<Win32AdbManager>();
}
~HestExecutorTest()
public void Dispose()
{
BasicBooter.Free();
}

[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);
}
}
}
18 changes: 12 additions & 6 deletions src/AutumnBox.Tests/Basic/MADB/BooterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Win32AdbManager>();
}
public void Dispose()
{
BasicBooter.Free();
}

[TestMethod]
public void LoadManager()
{
using var cpm = BasicBooter.CommandProcedureManager;
using var cmd = cpm.OpenCommand("adb", "devices");
cmd.Execute();
Expand All @@ -23,11 +31,9 @@ public void LoadManager()
}
SLogger<BooterTest>.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();
}
}
}
59 changes: 59 additions & 0 deletions src/AutumnBox.Tests/Logging/AoggerTest.cs
Original file line number Diff line number Diff line change
@@ -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<ILog> logs = new List<ILog>();
public void Log(ILog log)
{
logs.Add(log);
Debug.WriteLine(log.ToFormatedString());
}
}
}
Loading

0 comments on commit 5bdde4f

Please sign in to comment.