Skip to content

Commit

Permalink
cleaned up all warnings
Browse files Browse the repository at this point in the history
closes #49
closes #47
  • Loading branch information
ewilliams0305 committed Jul 14, 2024
1 parent 8687704 commit 67e535a
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 164 deletions.
1 change: 1 addition & 0 deletions source/WebLogger.Crestron/WebLogger.Crestron.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<LangVersion>9</LangVersion>
<Title>WebLogger Crestron Console</Title>
<Description>
Provides a web socket servers used to send/receive console communications from an embedded HTML user interface to a application.
Expand Down
8 changes: 6 additions & 2 deletions source/WebLogger.Crestron/WebLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Reflection;
#if NET6_0_OR_GREATER
#pragma warning disable CA1806
#endif

using System.Reflection;
using WebLogger.Utilities;

namespace WebLogger.Crestron
Expand All @@ -17,7 +21,7 @@ public static class WebLoggerExtensions
/// <returns></returns>
public static IWebLogger ServeWebLoggerHtml(this IWebLogger logger, int httpSeverPort)
{
var webPageServer = new WebLoggerHttpServer(
new WebLoggerHttpServer(
port: httpSeverPort,
directory: logger.HtmlDirectory);

Expand Down
12 changes: 8 additions & 4 deletions source/WebLogger.Crestron/WebLoggerHttpServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Crestron.SimplSharp;

#pragma warning disable IDE1006

using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO;
using Crestron.SimplSharp.Net.Http;
using System;
Expand All @@ -17,15 +20,16 @@ public sealed class WebLoggerHttpServer : IDisposable
/// <summary>
/// The extension content types
/// </summary>
public static Dictionary<string, string> ExtensionContentTypes;
private static Dictionary<string, string> _extensionContentTypes;

/// <summary>
/// Gets the type of the content.
/// </summary>
/// <param name="extension">The extension.</param>
/// <returns>System.String.</returns>
public static string GetContentType(string extension)
{
var type = ExtensionContentTypes.TryGetValue(extension, out var contentType) ? contentType : "text/plain";
var type = _extensionContentTypes.TryGetValue(extension, out var contentType) ? contentType : "text/plain";
return type;
}

Expand All @@ -40,7 +44,7 @@ public static string GetContentType(string extension)
/// <param name="directory">File Directory to Serve</param>
public WebLoggerHttpServer(int port, string directory)
{
ExtensionContentTypes = new Dictionary<string, string>
_extensionContentTypes = new Dictionary<string, string>
{
{ ".html", "text/html" },
{ ".json", "application/json" },
Expand Down
43 changes: 18 additions & 25 deletions source/WebLogger.Generators/IncrementalCommandStoreGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
using Microsoft.CodeAnalysis;
#pragma warning disable RS1024
#pragma warning disable IDE0079

#if NET6_0_OR_GREATER
#pragma warning disable CA1067
#else
#pragma warning disable IDE1041
#endif

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
Expand All @@ -16,8 +25,6 @@ public class IncrementalCommandStoreGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
//context.RegisterPostInitializationOutput(PostInitializationCallback);

IncrementalValuesProvider<TargetClassCaptureContext> provider = context.SyntaxProvider
.CreateSyntaxProvider(SyntacticPredicate, SemanticTransform)
.Where(static ((INamedTypeSymbol, Dictionary<IMethodSymbol, List<string>>)? type) => type.HasValue)
Expand All @@ -27,23 +34,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(provider, Execute);
}

private static void PostInitializationCallback(IncrementalGeneratorPostInitializationContext context)
{

//context.AddSource(Constants.CommandStoreAttributeFile, Constants.CommandStoreAttributeValue);
//context.AddSource(Constants.TargetCommandAttributeFile, Constants.TargetCommandAttributeValue);
//context.AddSource(Constants.StoredCommandsInterfaceExtensionsFile, Constants.StoredCommandsInterfaceExtensionsValue);
//context.AddSource(Constants.StoredCommandsInterfaceFile, Constants.StoredCommandsInterfaceValue);
}

private static bool SyntacticPredicate(SyntaxNode node, CancellationToken cancellation)
{
if (node is not AttributeSyntax { Name: IdentifierNameSyntax { Identifier: { Text: "CommandStore" } } } attribute)
{
return false;
}

return true;
return node is AttributeSyntax { Name: IdentifierNameSyntax { Identifier: { Text: "CommandStore" } } };
}

private static (INamedTypeSymbol, Dictionary<IMethodSymbol, List<string>>)? SemanticTransform(GeneratorSyntaxContext context, CancellationToken cancellation)
Expand All @@ -58,7 +51,7 @@ private static (INamedTypeSymbol, Dictionary<IMethodSymbol, List<string>>)? Sema
return null;

// 2. Ensure the class does not already implement the command store and ensure the attribute is applied
ISymbol typeSymbol = context.SemanticModel.GetDeclaredSymbol(candidate);
ISymbol typeSymbol = context.SemanticModel.GetDeclaredSymbol(candidate, cancellation);

if (typeSymbol is not INamedTypeSymbol type)
return null;
Expand Down Expand Up @@ -279,14 +272,14 @@ public bool Equals(TargetClassCaptureContext other)
/// </summary>
internal class TargetClassCaptureContextComparer : IEqualityComparer<TargetClassCaptureContext>
{
private static readonly Lazy<TargetClassCaptureContextComparer> _lazy = new(() => new TargetClassCaptureContextComparer());
public static TargetClassCaptureContextComparer Instance => _lazy.Value;
private static readonly Lazy<TargetClassCaptureContextComparer> Lazy = new(() => new TargetClassCaptureContextComparer());
public static TargetClassCaptureContextComparer Instance => Lazy.Value;

public bool Equals(TargetClassCaptureContext x, TargetClassCaptureContext y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x is null) return false;
if (y is null) return false;
if (x.GetType() != y.GetType()) return false;

return x.Equals(y);
Expand Down Expand Up @@ -343,11 +336,11 @@ private static void Execute(SourceProductionContext context, TargetClassCaptureC
int i = 1;
foreach (var property in method.PropertyValues)
{
methods.Append("\"").Append(i == 1? property.RemoveWhiteSpace().ToUpper() : property).Append("\"");
methods.Append('\\').Append(i == 1? property.RemoveWhiteSpace().ToUpper() : property).Append('\\');

if (i != 3)
{
methods.Append(",");
methods.Append(',');
}
i++;
}
Expand Down
13 changes: 2 additions & 11 deletions source/WebLogger.Generators/WebLogger.Generators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>WebLogger Command Source Generators</Title>
<Description>Source generators used to created custom CLI commands used by the WebLogger console. Simply tag your handler methods with a specific attribute and your command will be generated.</Description>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\log.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>


<ItemGroup>
Expand Down
21 changes: 21 additions & 0 deletions source/WebLogger.Serilog/Commands/LoggerLevelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,41 @@

namespace WebLogger
{
/// <summary>
/// Sets the serilog log level
/// </summary>
public class LoggerLevelCommand : IWebLoggerCommand
{

/// <summary>
/// Log level switcher
/// </summary>
public LoggingLevelSwitch LoggingLevelSwitch;

/// <summary>
/// Creates the new command
/// </summary>
/// <param name="level">with a provided level</param>
public LoggerLevelCommand(LogEventLevel level)
{
LoggingLevelSwitch = new LoggingLevelSwitch(level);
}

/// <inheritdoc />
public string Command => "SERILOGLEVEL";
/// <inheritdoc />
public string Description => "Changes the Verbosity";
/// <inheritdoc />
public string Help => "SerilogLevel [LoggingLevel] (Values: Debug, Verbose, Information, Warning, Error, Fatal)";
/// <inheritdoc />
public Func<string, List<string>, ICommandResponse> CommandHandler => Handler;

/// <summary>
/// Handles the data parameters passed from the command.
/// </summary>
/// <param name="command">The command</param>
/// <param name="args">The argument list</param>
/// <returns></returns>
public ICommandResponse Handler(string command, List<string> args)
{
if (args == null || args.Count == 0)
Expand Down
2 changes: 1 addition & 1 deletion source/WebLogger.Serilog/Formatting/CustomDateFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public object GetFormat(Type formatType)
if (formatType != typeof(DateTimeFormatInfo))
return this._basedOn.GetFormat(formatType);

if (!(_basedOn.GetFormat(formatType) is DateTimeFormatInfo basedOnFormatInfo))
if (_basedOn.GetFormat(formatType) is not DateTimeFormatInfo basedOnFormatInfo)
return null;

var dateFormatInfo = (DateTimeFormatInfo)basedOnFormatInfo.Clone();
Expand Down
27 changes: 27 additions & 0 deletions source/WebLogger.Serilog/Formatting/IRenderMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,38 @@

namespace WebLogger
{
/// <summary>
/// Renders a message to the weblogger
/// </summary>
public interface IRenderMessages
{
/// <summary>
/// renders verbose messages
/// </summary>
/// <param name="logEvent"></param>
/// <param name="formatProvider"></param>
/// <returns></returns>
string RenderVerbose(LogEvent logEvent, IFormatProvider formatProvider);
/// <summary>
/// renders an Information messages
/// </summary>
/// <param name="logEvent"></param>
/// <param name="formatProvider"></param>
/// <returns></returns>
string RenderInformation(LogEvent logEvent, IFormatProvider formatProvider);
/// <summary>
/// renders warnings
/// </summary>
/// <param name="logEvent"></param>
/// <param name="formatProvider"></param>
/// <returns></returns>
string RenderWarning(LogEvent logEvent, IFormatProvider formatProvider);
/// <summary>
/// renders error messages
/// </summary>
/// <param name="logEvent"></param>
/// <param name="formatProvider"></param>
/// <returns></returns>
string RenderError(LogEvent logEvent, IFormatProvider formatProvider);
}
}
38 changes: 22 additions & 16 deletions source/WebLogger.Serilog/Formatting/RenderHtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,49 @@

namespace WebLogger
{
/// <summary>
/// Renders the output as an HTML element
/// </summary>
public sealed class RenderSinkHtml : IRenderMessages
{
private const string _styleyleHeader = "padding:10px;margin:10px;font-weight:bold;background-color:";
private const string _styleyleSuffix = ";";

private const string StyleHeader = "padding:10px;margin:10px;font-weight:bold;background-color:";
private const char StyleSuffix = ';';

private static HtmlElement CreatePrefix(LogEvent logEvent, Color color)
{
var styles = new StringBuilder(_styleyleHeader)
var styles = new StringBuilder(StyleHeader)
.RenderColor(color)
.Append(_styleyleSuffix);
.Append(StyleSuffix);

var builder = new StringBuilder(logEvent.Timestamp.ToString("HH:mm:ss"))
.Append(" ")
.Append(' ')
.Append(logEvent.Level.GetLevel());

return HtmlElement.Span(builder.ToString(), new HtmlElementOptions(additionalStyles: styles.ToString()));
}

private string RenderMessage(LogEvent logEvent, IFormatProvider formatProvider, Color color)
{
return HtmlElement.Div(CreatePrefix(logEvent, color)
.Append(HtmlElement.Span(logEvent.RenderMessage(formatProvider))))
.Render();
}

/// <inheritdoc />
public string RenderVerbose(LogEvent logEvent, IFormatProvider formatProvider)
{
var color = ColorFactory.Instance.GetColor(Severity.Verbose);
return RenderMessage(logEvent, formatProvider, color);
}

/// <inheritdoc />
public string RenderInformation(LogEvent logEvent, IFormatProvider formatProvider)
{
var color = ColorFactory.Instance.GetColor(Severity.Verbose);

return RenderMessage(logEvent, formatProvider, color);
}
/// <inheritdoc />
public string RenderWarning(LogEvent logEvent, IFormatProvider formatProvider)
{
var color = ColorFactory.Instance.GetColor(Severity.Warning);

return RenderMessage(logEvent, formatProvider, color);
}

/// <inheritdoc />
public string RenderError(LogEvent logEvent, IFormatProvider formatProvider)
{
var color = ColorFactory.Instance.GetColor(Severity.Error);
Expand All @@ -59,14 +58,14 @@ public string RenderError(LogEvent logEvent, IFormatProvider formatProvider)
return CreatePrefix(logEvent, color)
.Append(logEvent.RenderMessage(formatProvider))
.Append(HtmlElement.Span(", Exception: "))
.Append(RenderExceptions(logEvent.Exception, color))
.Append(RenderExceptions(logEvent.Exception))
.Render();
}

return RenderMessage(logEvent, formatProvider, color);
}

private static HtmlElement RenderExceptions(Exception exception, Color color)
private static HtmlElement RenderExceptions(Exception exception)
{
var builder = new StringBuilder("background-color:")
.RenderColor(Color.DarkRed)
Expand All @@ -77,5 +76,12 @@ private static HtmlElement RenderExceptions(Exception exception, Color color)
HtmlElement.TableData(exception.ToString(), new HtmlElementOptions(additionalStyles: builder.ToString()))
));
}

private static string RenderMessage(LogEvent logEvent, IFormatProvider formatProvider, Color color)
{
return HtmlElement.Div(CreatePrefix(logEvent, color)
.Append(HtmlElement.Span(logEvent.RenderMessage(formatProvider))))
.Render();
}
}
}
Loading

0 comments on commit 67e535a

Please sign in to comment.