Skip to content

Commit ee3efe7

Browse files
committed
Get do logs check working
1 parent c61558b commit ee3efe7

File tree

5 files changed

+213
-62
lines changed

5 files changed

+213
-62
lines changed

sourcegeneration/MonologueSourceGenerator/LogGenerator.cs

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
using System.Collections.Immutable;
22
using System.Text;
33
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.CSharp;
45
using Microsoft.CodeAnalysis.CSharp.Syntax;
56
using Microsoft.CodeAnalysis.Text;
67

78
namespace Monologue.SourceGenerator;
89

9-
internal record LogData(string? PreComputed, string? GetOperation, string? Path, string? Type);
10+
internal enum DeclarationType
11+
{
12+
Logged,
13+
Struct,
14+
Protobuf,
15+
Other
16+
}
17+
18+
19+
internal record LogAttributeInfo(string Path, string LogLevel, string LogType, bool UseProtobuf);
20+
21+
internal record LogData(string GetOperation, string? Type, DeclarationType DecelType, LogAttributeInfo AttributeInfo);
1022

1123
internal record ClassData(ImmutableArray<LogData> LoggedItems, string Name, string ClassDeclaration, string? Namespace);
1224

@@ -48,20 +60,49 @@ public class LogGenerator : IIncrementalGenerator
4860
if (attributeClass.ToDisplayString() == "Monologue.LogAttribute")
4961
{
5062
token.ThrowIfCancellationRequested();
63+
64+
string path = member.Name;
65+
bool useProtobuf = false;
66+
string logTypeEnum = "Monologue.LogType.Nt | Monologue.LogType.File";
67+
string logLevel = "Monologue.LogLevel.Default";
68+
69+
// Get the log attribute
70+
foreach (var named in attribute.NamedArguments)
71+
{
72+
if (named.Key == "Key")
73+
{
74+
if (!named.Value.IsNull)
75+
{
76+
path = SymbolDisplay.FormatPrimitive(named.Value.Value!, false, false);
77+
}
78+
}
79+
else if (named.Key == "LogLevel")
80+
{
81+
logLevel = named.Value.ToCSharpString();
82+
}
83+
else if (named.Key == "LogType")
84+
{
85+
logTypeEnum = named.Value.ToCSharpString();
86+
}
87+
else if (named.Key == "UseProtobuf")
88+
{
89+
useProtobuf = (bool)named.Value.Value!;
90+
}
91+
}
92+
93+
var attributeInfo = new LogAttributeInfo(path, logLevel, logTypeEnum, useProtobuf);
94+
5195
string getOperation;
52-
string defaultPathName;
5396
ITypeSymbol logType;
5497
// This is ours
5598
if (member is IFieldSymbol field)
5699
{
57100
getOperation = field.Name;
58-
defaultPathName = field.Name;
59101
logType = field.Type;
60102
}
61103
else if (member is IPropertySymbol property)
62104
{
63105
getOperation = property.Name;
64-
defaultPathName = property.Name;
65106
logType = property.Type;
66107
}
67108
else if (member is IMethodSymbol method)
@@ -76,15 +117,14 @@ public class LogGenerator : IIncrementalGenerator
76117
}
77118

78119
getOperation = $"{method.Name}()";
79-
defaultPathName = method.Name;
80120
logType = method.ReturnType;
81121
}
82122
else
83123
{
84124
throw new InvalidOperationException("Field is not loggable");
85125
}
86126

87-
var fullOperation = ComputeOperation(logType, getOperation, defaultPathName);
127+
var fullOperation = ComputeOperation(logType, getOperation, attributeInfo);
88128
token.ThrowIfCancellationRequested();
89129
loggableMembers.Add(fullOperation);
90130
break;
@@ -98,36 +138,41 @@ public class LogGenerator : IIncrementalGenerator
98138
return new ClassData(loggableMembers.ToImmutable(), $"{classSymbol.ContainingNamespace}{classSymbol.ToDisplayString(fmt)}{classSymbol.MetadataName}", typeBuilder.ToString(), ns);
99139
}
100140

101-
private static LogData ComputeOperation(ITypeSymbol logType, string getOp, string path)
141+
private static LogData ComputeOperation(ITypeSymbol logType, string getOp, LogAttributeInfo attributeInfo)
102142
{
103143
if (logType.GetAttributes().Where(x => x.AttributeClass?.ToDisplayString() == "Monologue.GenerateLogAttribute").Any())
104144
{
105-
return new($"{getOp}.UpdateMonologue($\"{{path}}/{path}\", logger);", null, null, null);
145+
return new LogData(getOp, null, DeclarationType.Logged, attributeInfo);
106146
}
107147
if (logType.AllInterfaces.Where(x => x.ToDisplayString() == "Monologue.ILogged").Any())
108148
{
109-
return new($"{getOp}.UpdateMonologue($\"{{path}}/{path}\", logger);", null, null, null);
110-
//return $"{getOp}.UpdateMonologue($\"{{path}}/{path}\", logger);";
149+
return new LogData(getOp, null, DeclarationType.Logged, attributeInfo);
111150
}
112151
var fmt = new SymbolDisplayFormat(typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);
113-
var fullName = logType.ToDisplayString(fmt);
114-
var structName = $"WPIUtil.Serialization.Struct.IStructSerializable<{fullName}>";
115-
var protobufName = $"WPIUtil.Serialization.Protobuf.IProtobufSerializable<{fullName}>";
152+
var fullTypeName = logType.ToDisplayString(fmt);
153+
var structName = $"WPIUtil.Serialization.Struct.IStructSerializable<{fullTypeName}>";
154+
var protobufName = $"WPIUtil.Serialization.Protobuf.IProtobufSerializable<{fullTypeName}>";
155+
116156
foreach (var inf in logType.AllInterfaces)
117157
{
118158
var interfaceName = inf.ToDisplayString();
119-
// For now prefer struct
120159
if (interfaceName == structName)
121160
{
122-
return new($"logger.LogStruct($\"{{path}}/{path}\", LogType.Nt, {getOp});", null, null, null);
161+
if (!attributeInfo.UseProtobuf)
162+
{
163+
return new LogData(getOp, null, DeclarationType.Struct, attributeInfo);
164+
}
123165
}
124166
else if (interfaceName == protobufName)
125167
{
126-
return new($"logger.LogProto($\"{{path}}/{path}\", LogType.Nt, {getOp});", null, null, null);
168+
if (attributeInfo.UseProtobuf)
169+
{
170+
return new LogData(getOp, null, DeclarationType.Protobuf, attributeInfo);
171+
}
127172
}
128173
}
129174

130-
return new(null, getOp, path, fullName);
175+
return new LogData(getOp, fullTypeName, DeclarationType.Other, attributeInfo);
131176
}
132177

133178
public void Initialize(IncrementalGeneratorInitializationContext context)
@@ -145,14 +190,22 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
145190

146191
static void ConstructCall(LogData data, StringBuilder builder)
147192
{
148-
if (data.PreComputed is not null)
149-
{
193+
builder.Append(" ");
150194

151-
builder.AppendLine($" {data.PreComputed}");
152-
return;
195+
switch (data.DecelType)
196+
{
197+
case DeclarationType.Logged:
198+
builder.AppendLine($"{data.GetOperation}?.UpdateMonologue($\"{{path}}/{data.AttributeInfo.Path}\", logger);");
199+
return;
200+
case DeclarationType.Struct:
201+
builder.AppendLine($"logger.LogStruct($\"{{path}}/{data.AttributeInfo.Path}\", {data.AttributeInfo.LogType}, {data.GetOperation});");
202+
return;
203+
case DeclarationType.Protobuf:
204+
builder.AppendLine($"logger.LogProto($\"{{path}}/{data.AttributeInfo.Path}\", {data.AttributeInfo.LogType}, {data.GetOperation});");
205+
return;
153206
}
154207

155-
var ret = data.Type switch
208+
(string? LogMethod, string Cast, string Conversion) ret = data.Type switch
156209
{
157210
"System.Single" => ("LogFloat", "", ""),
158211
"System.Double" => ("LogDouble", "", ""),
@@ -189,7 +242,7 @@ static void ConstructCall(LogData data, StringBuilder builder)
189242
_ => (data.Type, "", "")
190243
};
191244

192-
builder.AppendLine($" logger.{ret.Item1}($\"{{path}}/{data.Path}\", LogType.Nt, {ret.Item2}{data.GetOperation}{ret.Item3});");
245+
builder.AppendLine($"logger.{ret.LogMethod}($\"{{path}}/{data.AttributeInfo.Path}\", {data.AttributeInfo.LogType}, {ret.Cast}{data.GetOperation}{ret.Conversion});");
193246
}
194247

195248
static void Execute(ClassData? classData, SourceProductionContext context)

src/thirdparty/Monologue/Attributes.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ public sealed class LogAttribute : Attribute
77
{
88
public string Key { get; init; } = "";
99
public LogLevel LogLevel { get; init; } = LogLevel.Default;
10-
public LogType LogType { get; init; } = LogType.Nt;
11-
public bool Once { get; init; } = false;
12-
public bool PreferProtobuf { get; init; } = false;
10+
public LogType LogType { get; init; } = LogType.Nt | LogType.File;
11+
public bool UseProtobuf { get; init; } = false;
1312
}
1413

1514
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface)]

src/thirdparty/Monologue/LogType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public enum LogType
77
{
88
None = 0,
99
File = 1,
10-
Nt = 2
10+
Nt = 2,
11+
Once = 4,
1112
}

0 commit comments

Comments
 (0)