Skip to content

Commit 2da34f3

Browse files
authored
Added DisableLogs attribute (#98)
1 parent 5596397 commit 2da34f3

File tree

5 files changed

+107
-3
lines changed

5 files changed

+107
-3
lines changed

src/App/NetDaemon.App/Common/Attributes.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Generic;
2+
13
namespace JoySoftware.HomeAssistant.NetDaemon.Common
24
{
35
/// <summary>
@@ -15,7 +17,7 @@ public sealed class HomeAssistantStateChangedAttribute : System.Attribute
1517
// See the attribute guidelines at
1618
// http://go.microsoft.com/fwlink/?LinkId=85236
1719
private string _entityId;
18-
20+
1921
private readonly object? _to;
2022
private readonly object? _from;
2123
private readonly bool _allChanges;
@@ -55,4 +57,42 @@ public HomeAssistantStateChangedAttribute(string entityId, object? to = null, ob
5557
/// </summary>
5658
public object? To => _to;
5759
}
60+
61+
/// <summary>
62+
/// Type of log to supress
63+
/// </summary>
64+
public enum SupressLogType
65+
{
66+
/// <summary>
67+
/// Supress Missing Execute Error in a method
68+
/// </summary>
69+
MissingExecute
70+
}
71+
72+
73+
/// <summary>
74+
/// Attribute to mark function as callback for state changes
75+
/// </summary>
76+
[System.AttributeUsage(System.AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
77+
public sealed class DisableLogAttribute : System.Attribute
78+
{
79+
// See the attribute guidelines at
80+
// http://go.microsoft.com/fwlink/?LinkId=85236
81+
private SupressLogType[] _logTypesToSupress;
82+
83+
84+
/// <summary>
85+
/// Default constructor
86+
/// </summary>
87+
/// <param name="logTypes">List of logtypes to supress</param>
88+
public DisableLogAttribute(params SupressLogType[] logTypes)
89+
{
90+
_logTypesToSupress = logTypes;
91+
}
92+
93+
/// <summary>
94+
/// Log types to supress
95+
/// </summary>
96+
public IEnumerable<SupressLogType> LogTypesToSupress => _logTypesToSupress;
97+
}
5898
}

src/DaemonRunner/DaemonRunner/Service/App/CodeManager.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,21 @@ private void WarnIfExecuteIsMissing(SyntaxTree syntaxTree, CSharpCompilation com
593593

594594
if (symbol is object && symbol.ContainingType.Name == "NetDaemonApp")
595595
{
596+
var disableLogging = false;
597+
596598
var symbolName = symbol.Name;
597599

598600
SyntaxNode? parentInvocationExpression = invocationExpression.Parent;
599601

600602
while (parentInvocationExpression is object)
601603
{
604+
if (parentInvocationExpression is MethodDeclarationSyntax)
605+
{
606+
if (ExpressionContainsDisableLogging((MethodDeclarationSyntax)parentInvocationExpression))
607+
{
608+
disableLogging = true;
609+
}
610+
}
602611
if (parentInvocationExpression is InvocationExpressionSyntax)
603612
{
604613
var parentSymbol = (IMethodSymbol?)semModel?.GetSymbolInfo(invocationExpression).Symbol;
@@ -611,7 +620,7 @@ private void WarnIfExecuteIsMissing(SyntaxTree syntaxTree, CSharpCompilation com
611620

612621
// Now when we have the top InvocationExpression,
613622
// lets check for Execute and ExecuteAsync
614-
if (ExpressionContainsExecuteInvocations(topInvocationExpression) == false)
623+
if (ExpressionContainsExecuteInvocations(topInvocationExpression) == false && disableLogging == false)
615624
{
616625
var x = syntaxTree.GetLineSpan(topInvocationExpression.Span);
617626
if (linesReported.Contains(x.StartLinePosition.Line) == false)
@@ -624,7 +633,18 @@ private void WarnIfExecuteIsMissing(SyntaxTree syntaxTree, CSharpCompilation com
624633
}
625634
}
626635

627-
// Todo: Refactor using something smarter than string match. In future use Roslyn
636+
// Todo: Refactor using something smarter than string match. In the future use Roslyn
637+
private bool ExpressionContainsDisableLogging(MethodDeclarationSyntax methodInvocationExpression)
638+
{
639+
var invocationString = methodInvocationExpression.ToFullString();
640+
if (invocationString.Contains("[DisableLog") && invocationString.Contains("SupressLogType.MissingExecute"))
641+
{
642+
return true;
643+
}
644+
return false;
645+
}
646+
647+
// Todo: Refactor using something smarter than string match. In the future use Roslyn
628648
private bool ExpressionContainsExecuteInvocations(InvocationExpressionSyntax invocation)
629649
{
630650
var invocationString = invocation.ToFullString();

tests/NetDaemon.Daemon.Tests/DaemonRunner/App/DaemonAppTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,22 @@ public void InsanceAppsThatHasMissingExecuteShouldLogError()
360360

361361
}
362362

363+
[Fact]
364+
public void InsanceAppsThatHasMissingExecuteAndSupressLogsShouldNotLogError()
365+
{
366+
// ARRANGE
367+
var path = Path.Combine(FaultyAppPath, "SupressLogs");
368+
var moqDaemon = new Mock<INetDaemonHost>();
369+
var moqLogger = new LoggerMock();
370+
371+
// ACT
372+
var codeManager = new CodeManager(path, moqLogger.Logger);
373+
374+
// ASSERT
375+
moqLogger.AssertLogged(LogLevel.Error, Times.Exactly(1));
376+
377+
}
378+
363379
public static CodeManager CM(string path) => new CodeManager(path, new LoggerMock().Logger);
364380
}
365381
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using JoySoftware.HomeAssistant.NetDaemon.Common;
5+
6+
/// <summary>
7+
/// Greets (or insults) people when coming home :)
8+
/// </summary>
9+
public class SupressLogs : NetDaemonApp
10+
{
11+
[DisableLog(SupressLogType.MissingExecute)]
12+
public override async Task InitializeAsync()
13+
{
14+
// All warnings in this is supressed
15+
Entity("Test");
16+
await DoStuff();
17+
}
18+
19+
public async Task DoStuff()
20+
{
21+
// This is not supressed
22+
Entity("Test");
23+
}
24+
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
missing_executes:
3+
class: SupressLogs

0 commit comments

Comments
 (0)