Skip to content

Commit 76d6195

Browse files
committed
Better output to the console.
1 parent cb04595 commit 76d6195

File tree

3 files changed

+230
-158
lines changed

3 files changed

+230
-158
lines changed

CSharpToJavaScript/CSTOJS.cs

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,30 @@
99
using System.Linq;
1010
using Microsoft.CodeAnalysis.Text;
1111
using System.Text;
12-
using System.Runtime.CompilerServices;
1312
using System;
1413

1514
namespace CSharpToJavaScript
1615
{
1716
/// <summary>
1817
/// Main type for CSharpToJavaScript.
1918
/// </summary>
20-
public class CSTOJS
21-
{
22-
public CSTOJSOptions Options { get; set; } = new();
23-
19+
public class CSTOJS : ILog
20+
{
21+
private CSTOJSOptions _Options = new();
22+
private Stopwatch _Stopwatch = new();
2423
private Walker? _Walker = null;
25-
26-
static CSTOJS()
27-
{
28-
ConsoleTraceListener consoleTraceListener = new();
29-
Trace.Listeners.Add(consoleTraceListener);
30-
}
24+
private readonly ILog? _Log = null;
3125

3226
/// <summary>
3327
/// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
3428
/// </summary>
3529
public CSTOJS()
3630
{
31+
_Log = this;
32+
3733
Assembly assembly = Assembly.GetExecutingAssembly();
38-
//https://stackoverflow.com/a/73474279
39-
Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
34+
//https://stackoverflow.com/a/73474279
35+
_Log.SuccessLine($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
4036
}
4137

4238
/// <summary>
@@ -45,14 +41,16 @@ public CSTOJS()
4541
/// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
4642
public CSTOJS(CSTOJSOptions options)
4743
{
48-
Options = options;
44+
_Options = options;
4945

50-
if (Options.DisableConsoleOutput == false)
46+
_Log = ILog.GetILog(this, _Options);
47+
48+
if (_Options.DisableConsoleOutput == false)
5149
{
5250
Assembly assembly = Assembly.GetExecutingAssembly();
53-
//https://stackoverflow.com/a/73474279
54-
Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
55-
}
51+
//https://stackoverflow.com/a/73474279
52+
_Log.SuccessLine($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
53+
}
5654
}
5755

5856
/// <summary>
@@ -90,23 +88,23 @@ public async Task GenerateOneAsync(string path, string? filename = null)
9088

9189
Generate(_tree, assembly);
9290

93-
if (!Directory.Exists(Options.OutPutPath))
91+
if (!Directory.Exists(_Options.OutPutPath))
9492
{
95-
Directory.CreateDirectory(Options.OutPutPath);
93+
Directory.CreateDirectory(_Options.OutPutPath);
9694
}
9795

9896
string pathCombined = string.Empty;
9997

10098
if (filename != null)
101-
pathCombined = Path.Combine(Options.OutPutPath, filename);
99+
pathCombined = Path.Combine(_Options.OutPutPath, filename);
102100
else
103-
pathCombined = Path.Combine(Options.OutPutPath, file.Name.Replace(".cs", ".js"));
101+
pathCombined = Path.Combine(_Options.OutPutPath, file.Name.Replace(".cs", ".js"));
104102

105103
await File.WriteAllTextAsync(pathCombined, _Walker.JSSB.ToString());
106104

107-
Log($"--- Done!");
108-
Log($"--- Path: {pathCombined}");
109-
Log($"--- --- ---");
105+
_Log.SuccessLine($"--- Done!");
106+
_Log.SuccessLine($"--- Path: {pathCombined}");
107+
_Log.SuccessLine($"--- --- ---");
110108
}
111109
}
112110

@@ -145,9 +143,9 @@ public List<StringBuilder> GenerateOne(string path)
145143

146144
jsStringBuilders.Add(_Walker.JSSB);
147145

148-
Log($"--- Done!");
149-
Log($"--- File name: {file.Name}");
150-
Log($"--- --- ---");
146+
_Log.SuccessLine($"--- Done!");
147+
_Log.SuccessLine($"--- File name: {file.Name}");
148+
_Log.SuccessLine($"--- --- ---");
151149
}
152150

153151
return jsStringBuilders;
@@ -175,8 +173,8 @@ public StringBuilder GenerateOneFromString(string csstring, List<MetadataReferen
175173
else
176174
Generate(_tree, assembly);
177175

178-
Log($"--- Done!");
179-
Log($"--- --- ---");
176+
_Log.SuccessLine($"--- Done!");
177+
_Log.SuccessLine($"--- --- ---");
180178

181179
return _Walker.JSSB;
182180
}
@@ -204,23 +202,29 @@ public async Task GenerateOneFromStringAsync(string csstring, string? filename =
204202
Generate(_tree, assembly);
205203

206204

207-
if (!Directory.Exists(Options.OutPutPath))
205+
if (!Directory.Exists(_Options.OutPutPath))
208206
{
209-
Directory.CreateDirectory(Options.OutPutPath);
207+
Directory.CreateDirectory(_Options.OutPutPath);
210208
}
211209

212-
string pathCombined = Path.Combine(Options.OutPutPath, filename);
210+
string pathCombined = Path.Combine(_Options.OutPutPath, filename);
213211

214212
await File.WriteAllTextAsync(pathCombined, _Walker.JSSB.ToString());
215213

216-
Log($"--- Done!");
217-
Log($"--- Path: {pathCombined}");
218-
Log($"--- --- ---");
214+
_Log.SuccessLine($"--- Done!");
215+
_Log.SuccessLine($"--- Path: {pathCombined}");
216+
_Log.SuccessLine($"--- --- ---");
219217
}
220218

221219

222220
private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReference>? refs = null)
223221
{
222+
if(_Options.Debug)
223+
{
224+
_Stopwatch.Restart();
225+
_Log.WriteLine("Start stopwatch");
226+
}
227+
224228
CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
225229

226230

@@ -454,17 +458,17 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
454458
//TODO! does not work... sigh
455459
references = references.Distinct().ToList();
456460

457-
if (Options.Debug)
461+
if (_Options.Debug)
458462
{
459-
Log($"+++");
460-
Log($"Path assembly: {assemblyPath}");
461-
Log($"Path rt: {rtPath}");
462-
Log($"List of references:");
463+
_Log.SuccessLine($"+++");
464+
_Log.WriteLine($"Path assembly: {assemblyPath}");
465+
_Log.WriteLine($"Path rt: {rtPath}");
466+
_Log.WriteLine($"List of references:");
463467
foreach (MetadataReference reference in references)
464468
{
465-
Log(reference.Display);
469+
_Log.WriteLine(reference.Display);
466470
}
467-
Log($"+++");
471+
_Log.SuccessLine($"+++");
468472
}
469473

470474
SyntaxTree trueST = trueRoot.SyntaxTree;
@@ -473,37 +477,20 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
473477
.AddReferences(references.ToArray())
474478
.AddSyntaxTrees(trueST);
475479

476-
_Walker = new(this, compilation.GetSemanticModel(trueST));
480+
_Walker = new(_Options, compilation.GetSemanticModel(trueST));
477481

478-
_Walker.JSSB.Append(Options.AddSBInFront);
482+
_Walker.JSSB.Append(_Options.AddSBInFront);
479483

480484
_Walker.Visit(trueRoot);
481485

482-
_Walker.JSSB.Append(Options.AddSBInEnd);
483-
}
486+
_Walker.JSSB.Append(_Options.AddSBInEnd);
484487

485-
public void Log(string message, [CallerFilePath] string? file = null, [CallerMemberName] string? member = null, [CallerLineNumber] int line = 0)
486-
{
487-
if (Options.DisableConsoleOutput == false)
488-
{
489-
if (Options.DisableConsoleColors == false)
490-
{
491-
if (message.StartsWith("---"))
492-
Console.ForegroundColor = ConsoleColor.Green;
493-
494-
if (message.StartsWith("ERROR") || message.StartsWith("as"))
495-
Console.ForegroundColor = ConsoleColor.Red;
496-
497-
if (message.StartsWith("WARNING"))
498-
Console.ForegroundColor = ConsoleColor.Yellow;
499-
}
488+
if (_Options.Debug)
489+
{
490+
_Stopwatch.Stop();
491+
_Log.WriteLine($"Stop stopwatch: {_Stopwatch.Elapsed}");
492+
}
493+
}
500494

501-
502-
Trace.WriteLine($"({line}):{Path.GetFileName(file.Replace("\\", "/"))} {member}: {message}");
503-
504-
if (Options.DisableConsoleColors == false)
505-
Console.ResetColor();
506-
}
507-
}
508495
}
509496
}

CSharpToJavaScript/ILog.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.CompilerServices;
4+
5+
namespace CSharpToJavaScript
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+
16+
public void SuccessLine(string message)
17+
{
18+
if (_Options.DisableConsoleOutput == true)
19+
return;
20+
21+
Console.Write($"\t{DateTime.Now.ToLongTimeString()}: ");
22+
23+
if (_Options.DisableConsoleColors == false)
24+
Console.ForegroundColor = ConsoleColor.Green;
25+
26+
Console.Write($"{message}");
27+
28+
if (_Options.DisableConsoleColors == false)
29+
Console.ResetColor();
30+
31+
Console.WriteLine();
32+
}
33+
34+
public void WriteLine(string message)
35+
{
36+
if (_Options.DisableConsoleOutput == true)
37+
return;
38+
39+
Console.Write($"{DateTime.Now.ToLongTimeString()}: ");
40+
Console.Write($"{message}");
41+
42+
Console.WriteLine();
43+
}
44+
45+
public void WarningLine(string message, [CallerFilePath] string? file = null, [CallerMemberName] string? member = null, [CallerLineNumber] int line = 0)
46+
{
47+
if (_Options.DisableConsoleOutput == true)
48+
return;
49+
50+
Console.Write($"\t{DateTime.Now.ToLongTimeString()}: ");
51+
52+
if (_Options.DisableConsoleColors == false)
53+
Console.ForegroundColor = ConsoleColor.Yellow;
54+
55+
Console.Write($"({line}):{Path.GetFileName(file?.Replace("\\", "/"))} {member}: {message}");
56+
57+
if (_Options.DisableConsoleColors == false)
58+
Console.ResetColor();
59+
60+
Console.WriteLine();
61+
}
62+
63+
public void ErrorLine(string message, [CallerFilePath] string? file = null, [CallerMemberName] string? member = null, [CallerLineNumber] int line = 0)
64+
{
65+
if (_Options.DisableConsoleOutput == true)
66+
return;
67+
68+
Console.Write($"\t{DateTime.Now.ToLongTimeString()}: ");
69+
70+
if (_Options.DisableConsoleColors == false)
71+
Console.ForegroundColor = ConsoleColor.Red;
72+
73+
Console.Write($"({line}):{Path.GetFileName(file?.Replace("\\", "/"))} {member}: {message}");
74+
75+
if (_Options.DisableConsoleColors == false)
76+
Console.ResetColor();
77+
78+
Console.WriteLine();
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)