Skip to content

Commit

Permalink
chore: Log basic informations
Browse files Browse the repository at this point in the history
  • Loading branch information
densogiaichned committed Feb 11, 2024
1 parent c93af54 commit e80e27d
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
6 changes: 6 additions & 0 deletions TcHaxx.Snappy.CLI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
.github\workflows\cicd.yml = .github\workflows\cicd.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".Zeugwerk", ".Zeugwerk", "{13BFAF66-25FF-4F77-B648-CAB9A16951C8}"
ProjectSection(SolutionItems) = preProject
.Zeugwerk\config.json = .Zeugwerk\config.json
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -71,6 +76,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{C51D268A-94C5-4264-9D30-9A2753B9C872} = {9A6AE3E0-A679-453E-A11A-9DEC17B6B948}
{8F2C7052-0F34-4F1F-B9A5-50D5DC2AD989} = {C51D268A-94C5-4264-9D30-9A2753B9C872}
{13BFAF66-25FF-4F77-B648-CAB9A16951C8} = {9A6AE3E0-A679-453E-A11A-9DEC17B6B948}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5ABE814F-A307-4743-959F-809CDC419600}
Expand Down
11 changes: 11 additions & 0 deletions src/TcHaxx.Snappy.CLI/CLI/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace TcHaxx.Snappy.CLI;

internal static class Constants
{
/// <summary>
/// Default delimiter for CLI info.
/// </summary>
internal const char CLI_DELIMITER = '*';

internal const string DEFAULT_COPYRIGHT = "Copyright (c) 2024 densogiaichned";
}
58 changes: 58 additions & 0 deletions src/TcHaxx.Snappy.CLI/CLI/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Reflection;
using System.Text;

namespace TcHaxx.Snappy.CLI;

/// <summary>
/// Some helper methods/functions and extension methods.
/// </summary>
internal static class Helper
{
/// <summary>
/// Centers a <see cref="string"/>.
/// </summary>
/// <param name="this"></param>
/// <returns>centered string</returns>
internal static string Center(this string @this)
{

if (string.IsNullOrEmpty(@this))
{
return string.Empty;
}
var width = Console.BufferWidth;
return @this.PadLeft(((width - @this.Length) / 2)
+ @this.Length)
.PadRight(width);
}

/// <summary>
/// Prints the application header based on <see cref="Assembly"/>-attributes as <see cref="string"/>.
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
internal static string GetApplicationHeader(Assembly assembly)
{
var sb = new StringBuilder(1000);

var version = assembly.GetName().Version;
var copyright = assembly.GetCustomAttribute<AssemblyCopyrightAttribute>()?.Copyright ?? Constants.DEFAULT_COPYRIGHT;

var repoUrl = assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.Where(x => x.Key.Equals("RepositoryUrl"))
.FirstOrDefault()?.Value ?? string.Empty;

var delimiter = new string(Constants.CLI_DELIMITER, Console.BufferWidth);
sb.Append($"{delimiter}\n");
sb.Append($"{assembly.GetName().Name} V{version}".Center() + "\n");
sb.Append($"{copyright.Center()}\n");
if (!string.IsNullOrWhiteSpace(repoUrl))
{
sb.Append($"{repoUrl.Center()}\n");
}

sb.Append($"{delimiter}\n");

return sb.ToString();
}
}
3 changes: 2 additions & 1 deletion src/TcHaxx.Snappy.CLI/Commands/CommandVerify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public async Task<int> RunAndReturnExitCode(VerifyOptions options)
service.Options = options;
}

_Logger?.Information("Creating AdsSymbolicServer...");
using var symbolicServer = _SymbolicServerFactory.CreateSymbolicServer(options.AdsPort, nameof(CommandVerify));

_Logger?.Information("Starting AdsSymbolicServer...");
var adsRetVal = await symbolicServer.ConnectServerAndWaitAsync(new CancellationToken());

return 0;
Expand Down
6 changes: 4 additions & 2 deletions src/TcHaxx.Snappy.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommandLine;
using System.Reflection;
using CommandLine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
Expand All @@ -7,12 +8,13 @@
using TcHaxx.Snappy.CLI.Commands;
using TcHaxx.Snappy.CLI.Logging;
using TcHaxx.Snappy.Common.RPC;
using TcHaxx.Snappy.Common.Verify;
using TcHaxx.Snappy.TcADS;
using TcHaxx.Snappy.Verifier;

try
{
Console.WriteLine(Helper.GetApplicationHeader(Assembly.GetExecutingAssembly()));

using var host = BuildHost(args);

var logger = host.Services.GetService<ILogger>();
Expand Down
2 changes: 1 addition & 1 deletion src/TcHaxx.Snappy.CLI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"TcHaxx.Snappy.CLI": {
"commandName": "Project",
"commandLineArgs": "verify -l Debug -d D:\\densogiaichned\\snappy\\examples\\examples\\.snappy-verified"
"commandLineArgs": "verify -d D:\\densogiaichned\\snappy\\examples\\examples\\.snappy-verified"
}
}
}
7 changes: 7 additions & 0 deletions src/TcHaxx.Snappy.TcADS/SymbolicServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ namespace TcHaxx.Snappy.TcADS;
internal class SymbolicServer : AdsSymbolicServer, ISymbolicServer
{
private readonly ISymbolFactory _SymbolFactory;
private readonly ILogger? _Logger;

internal SymbolicServer(ushort port, string portName, ISymbolFactory symbolFactory, ILogger? logger)
: base(port, portName, logger)
{
_SymbolFactory = symbolFactory;
_Logger = logger;
}

/// <inheritdoc cref="AdsServer.ConnectServerAndWaitAsync(CancellationToken)"/>
Expand Down Expand Up @@ -50,8 +52,11 @@ protected override AdsErrorCode OnWriteRawValue(ISymbol symbol, ReadOnlySpan<byt
/// </summary>
protected override void OnConnected()
{
_Logger?.LogInformation("Adding symbols ...");
_SymbolFactory.AddSymbols(base.symbolFactory);
_Logger?.LogInformation("done.");
base.OnConnected();
_Logger?.LogInformation("Waiting for RPC requests...");
}

protected override AdsErrorCode OnRpcInvoke(IInterfaceInstance structInstance, IRpcMethod method, object[] values, out object? returnValue)
Expand All @@ -60,8 +65,10 @@ protected override AdsErrorCode OnRpcInvoke(IInterfaceInstance structInstance, I
if (iDataType is null)
{
returnValue = null;
_Logger?.LogError("{OnRpcInvoke}: {IDataType} is null", nameof(OnRpcInvoke), nameof(IDataType));
return AdsErrorCode.DeviceInvalidContext;
}
_Logger?.LogInformation("{OnRpcInvoke}: Invoking method {IRpcMethod} of {IDataTypeFullName}", nameof(OnRpcInvoke), method, iDataType.FullName);
return _SymbolFactory.InvokeRpcMethod(iDataType, values, out returnValue);
}
}

0 comments on commit e80e27d

Please sign in to comment.