Skip to content

Commit d638bd8

Browse files
committed
Better formatting for logging.
1 parent 0f625fa commit d638bd8

File tree

4 files changed

+133
-119
lines changed

4 files changed

+133
-119
lines changed

CSharpToJavaScript/CSTOJS.cs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,31 @@
1010
using Microsoft.CodeAnalysis.Text;
1111
using System.Text;
1212
using System;
13+
using CSharpToJavaScript.Utils;
1314

1415
namespace CSharpToJavaScript
1516
{
1617
/// <summary>
1718
/// Main type for CSharpToJavaScript.
1819
/// </summary>
1920
public class CSTOJS : ILog
20-
{
21-
private CSTOJSOptions _Options = new();
22-
private Stopwatch _Stopwatch = new();
21+
{
22+
private readonly CSTOJSOptions _Options = new();
23+
private readonly Stopwatch _Stopwatch = new();
24+
private readonly ILog? _Log = null;
25+
2326
private Walker? _Walker = null;
24-
private readonly ILog? _Log = null;
2527

2628
/// <summary>
2729
/// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
2830
/// </summary>
2931
public CSTOJS()
3032
{
31-
_Log = this;
33+
_Log = this;
3234

3335
Assembly assembly = Assembly.GetExecutingAssembly();
34-
//https://stackoverflow.com/a/73474279
35-
_Log.SuccessLine($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
36+
//https://stackoverflow.com/a/73474279
37+
_Log.SuccessLine($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
3638
}
3739

3840
/// <summary>
@@ -41,16 +43,16 @@ public CSTOJS()
4143
/// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
4244
public CSTOJS(CSTOJSOptions options)
4345
{
44-
_Options = options;
46+
_Options = options;
4547

46-
_Log = ILog.GetILog(this, _Options);
48+
_Log = ILog.GetILog(this, _Options);
4749

48-
if (_Options.DisableConsoleOutput == false)
50+
if (_Options.DisableConsoleOutput == false)
4951
{
5052
Assembly assembly = Assembly.GetExecutingAssembly();
51-
//https://stackoverflow.com/a/73474279
52-
_Log.SuccessLine($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
53-
}
53+
//https://stackoverflow.com/a/73474279
54+
_Log.SuccessLine($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
55+
}
5456
}
5557

5658
/// <summary>
@@ -61,7 +63,8 @@ public CSTOJS(CSTOJSOptions options)
6163
/// <returns>empty Task</returns>
6264
public async Task GenerateOneAsync(string path, string? filename = null)
6365
{
64-
Assembly assembly = Assembly.GetEntryAssembly();
66+
67+
Assembly? assembly = Assembly.GetEntryAssembly();
6568
List<FileInfo> files = new();
6669

6770
if (File.Exists(path))
@@ -100,7 +103,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
100103
else
101104
pathCombined = Path.Combine(_Options.OutPutPath, file.Name.Replace(".cs", ".js"));
102105

103-
await File.WriteAllTextAsync(pathCombined, _Walker.JSSB.ToString());
106+
await File.WriteAllTextAsync(pathCombined, _Walker?.JSSB.ToString());
104107

105108
_Log.SuccessLine($"--- Done!");
106109
_Log.SuccessLine($"--- Path: {pathCombined}");
@@ -115,7 +118,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
115118
/// <returns>List of StringBuilder</returns>
116119
public List<StringBuilder> GenerateOne(string path)
117120
{
118-
Assembly assembly = Assembly.GetEntryAssembly();
121+
Assembly? assembly = Assembly.GetEntryAssembly();
119122
List<FileInfo> files = new();
120123
List<StringBuilder> jsStringBuilders = new();
121124

@@ -161,10 +164,9 @@ public List<StringBuilder> GenerateOne(string path)
161164
/// <exception cref="ArgumentNullException"></exception>
162165
public StringBuilder GenerateOneFromString(string csstring, List<MetadataReference>? references = null)
163166
{
164-
if (csstring == null)
165-
throw new ArgumentNullException(nameof(csstring));
167+
ArgumentNullException.ThrowIfNull(csstring);
166168

167-
Assembly assembly = Assembly.GetEntryAssembly();
169+
Assembly? assembly = Assembly.GetEntryAssembly();
168170

169171
SyntaxTree? _tree = CSharpSyntaxTree.ParseText(csstring);
170172

@@ -189,10 +191,9 @@ public StringBuilder GenerateOneFromString(string csstring, List<MetadataReferen
189191
/// <exception cref="ArgumentNullException"></exception>
190192
public async Task GenerateOneFromStringAsync(string csstring, string? filename = "main.js", List<MetadataReference>? references = null)
191193
{
192-
if (csstring == null)
193-
throw new ArgumentNullException(nameof(csstring));
194+
ArgumentNullException.ThrowIfNull(csstring);
194195

195-
Assembly assembly = Assembly.GetEntryAssembly();
196+
Assembly? assembly = Assembly.GetEntryAssembly();
196197

197198
SyntaxTree? _tree = CSharpSyntaxTree.ParseText(csstring);
198199

@@ -217,21 +218,21 @@ public async Task GenerateOneFromStringAsync(string csstring, string? filename =
217218
}
218219

219220

220-
private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReference>? refs = null)
221+
private void Generate(SyntaxTree? tree, Assembly? assembly, List<MetadataReference>? refs = null)
221222
{
222-
if(_Options.Debug)
223-
{
224-
_Stopwatch.Restart();
223+
if(_Options.Debug)
224+
{
225+
_Stopwatch.Restart();
225226
_Log.WriteLine("Start stopwatch");
226-
}
227+
}
227228

228229
CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
229230

230231

231-
string assemblyPath = Path.GetDirectoryName(assembly.Location);
232+
string? assemblyPath = Path.GetDirectoryName(assembly?.Location);
232233
List<MetadataReference> references = new() { };
233234

234-
string rtPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
235+
string? rtPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
235236

236237
if (refs == null)
237238
{
@@ -485,12 +486,12 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
485486

486487
_Walker.JSSB.Append(_Options.AddSBInEnd);
487488

488-
if (_Options.Debug)
489-
{
490-
_Stopwatch.Stop();
489+
if (_Options.Debug)
490+
{
491+
_Stopwatch.Stop();
491492
_Log.WriteLine($"Stop stopwatch: {_Stopwatch.Elapsed}");
492-
}
493-
}
493+
}
494+
}
494495

495496
}
496497
}

CSharpToJavaScript/ILog.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.

CSharpToJavaScript/Utils/ILog.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.CompilerServices;
4+
5+
namespace CSharpToJavaScript.Utils
6+
{
7+
internal interface ILog
8+
{
9+
private static CSTOJSOptions _Options = new();
10+
public static ILog GetILog(CSTOJS cstojs, CSTOJSOptions options)
11+
{
12+
_Options = options;
13+
return cstojs;
14+
}
15+
public void WriteLine(string message)
16+
{
17+
if (_Options.DisableConsoleOutput == true)
18+
return;
19+
20+
Console.Write($"{DateTime.Now.ToLongTimeString()}: ");
21+
Console.Write($"{message}");
22+
23+
Console.WriteLine();
24+
}
25+
26+
public virtual void SuccessLine(string message)
27+
{
28+
if (_Options.DisableConsoleOutput == true)
29+
return;
30+
31+
Console.Write($"{DateTime.Now.ToLongTimeString()}: ");
32+
33+
if (_Options.DisableConsoleColors == false)
34+
Console.ForegroundColor = ConsoleColor.Green;
35+
36+
Console.Write($"\t{message}");
37+
38+
if (_Options.DisableConsoleColors == false)
39+
Console.ResetColor();
40+
41+
Console.WriteLine();
42+
}
43+
44+
public void WarningLine(string message, [CallerFilePath] string? file = null, [CallerMemberName] string? member = null, [CallerLineNumber] int line = 0)
45+
{
46+
if (_Options.DisableConsoleOutput == true)
47+
return;
48+
49+
Console.Write($"{DateTime.Now.ToLongTimeString()}: ");
50+
51+
if (_Options.DisableConsoleColors == false)
52+
Console.ForegroundColor = ConsoleColor.Cyan;
53+
54+
Console.Write($"({line}){Path.GetFileName(file?.Replace("\\", "/"))}.{member}:");
55+
Console.WriteLine();
56+
Console.Write("\tMessage: ");
57+
58+
if (_Options.DisableConsoleColors == false)
59+
Console.ForegroundColor = ConsoleColor.Yellow;
60+
61+
Console.Write($"{message}");
62+
63+
if (_Options.DisableConsoleColors == false)
64+
Console.ResetColor();
65+
66+
Console.WriteLine();
67+
}
68+
69+
public void ErrorLine(string message, [CallerFilePath] string? file = null, [CallerMemberName] string? member = null, [CallerLineNumber] int line = 0)
70+
{
71+
if (_Options.DisableConsoleOutput == true)
72+
return;
73+
74+
Console.Write($"{DateTime.Now.ToLongTimeString()}: ");
75+
76+
if (_Options.DisableConsoleColors == false)
77+
Console.ForegroundColor = ConsoleColor.Cyan;
78+
79+
Console.Write($"({line}){Path.GetFileName(file?.Replace("\\", "/"))}.{member}:");
80+
Console.WriteLine();
81+
Console.Write("\tMessage: ");
82+
83+
if (_Options.DisableConsoleColors == false)
84+
Console.ForegroundColor = ConsoleColor.Red;
85+
86+
Console.Write($"{message}");
87+
88+
if (_Options.DisableConsoleColors == false)
89+
Console.ResetColor();
90+
91+
Console.WriteLine();
92+
}
93+
}
94+
}

CSharpToJavaScript/Walker.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
namespace CSharpToJavaScript
1313
{
14-
//TODO maybe...
15-
//https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/get-started/syntax-transformation
16-
//
14+
//TODO maybe...
15+
//https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/get-started/syntax-transformation
16+
//
1717

18-
internal class Walker : CSharpSyntaxWalker, ILog
18+
internal class Walker : CSharpSyntaxWalker, ILog
1919
{
2020
public StringBuilder JSSB { get; set; } = new();
2121

0 commit comments

Comments
 (0)