Skip to content

Commit 79cd4e9

Browse files
authored
Adds Logging level control to UICatalog (#3938)
* Tons of API doc updates * Added logging control to UICatalog * Added logging control to UICatalog - more * fixed minor issues * removed logs from .gitignore * Fixed log file path * Fixed app desc
1 parent c88c772 commit 79cd4e9

20 files changed

+398
-196
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ demo.*
5858
*.tui/
5959

6060
*.dotCover
61+
62+
logs/

Terminal.Gui/Application/ApplicationImpl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public virtual void Shutdown ()
215215

216216
bool wasInitialized = Application.Initialized;
217217
Application.ResetState ();
218+
LogJsonErrors ();
218219
PrintJsonErrors ();
219220

220221
if (wasInitialized)

Terminal.Gui/Configuration/AttributeJsonConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public override Attribute Read (ref Utf8JsonReader reader, Type typeToConvert, J
5757
switch (propertyName?.ToLower ())
5858
{
5959
case "foreground":
60-
foreground = JsonSerializer.Deserialize (color, _serializerContext.Color);
60+
foreground = JsonSerializer.Deserialize (color, SerializerContext.Color);
6161

6262
break;
6363
case "background":
64-
background = JsonSerializer.Deserialize (color, _serializerContext.Color);
64+
background = JsonSerializer.Deserialize (color, SerializerContext.Color);
6565

6666
break;
6767

Terminal.Gui/Configuration/ColorSchemeJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override ColorScheme Read (ref Utf8JsonReader reader, Type typeToConvert,
5959

6060
string propertyName = reader.GetString ();
6161
reader.Read ();
62-
var attribute = JsonSerializer.Deserialize (ref reader, _serializerContext.Attribute);
62+
var attribute = JsonSerializer.Deserialize (ref reader, SerializerContext.Attribute);
6363

6464
switch (propertyName.ToLower ())
6565
{

Terminal.Gui/Configuration/ConfigurationManager.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text.Encodings.Web;
99
using System.Text.Json;
1010
using System.Text.Json.Serialization;
11+
using Microsoft.Extensions.Logging;
1112

1213
#nullable enable
1314

@@ -65,7 +66,7 @@ public static class ConfigurationManager
6566
internal static Dictionary<string, ConfigProperty>? _allConfigProperties;
6667

6768
[SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
68-
internal static readonly JsonSerializerOptions _serializerOptions = new ()
69+
internal static readonly JsonSerializerOptions SerializerOptions = new ()
6970
{
7071
ReadCommentHandling = JsonCommentHandling.Skip,
7172
PropertyNameCaseInsensitive = true,
@@ -87,7 +88,7 @@ public static class ConfigurationManager
8788
};
8889

8990
[SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
90-
internal static readonly SourceGenerationContext _serializerContext = new (_serializerOptions);
91+
internal static readonly SourceGenerationContext SerializerContext = new (SerializerOptions);
9192

9293
[SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
9394
internal static StringBuilder _jsonErrors = new ();
@@ -209,7 +210,7 @@ public static string GetEmptyJson ()
209210
var emptyScope = new SettingsScope ();
210211
emptyScope.Clear ();
211212

212-
return JsonSerializer.Serialize (emptyScope, typeof (SettingsScope), _serializerContext);
213+
return JsonSerializer.Serialize (emptyScope, typeof (SettingsScope), SerializerContext);
213214
}
214215

215216
/// <summary>
@@ -235,7 +236,7 @@ public static string GetEmptyJson ()
235236
[RequiresDynamicCode ("AOT")]
236237
public static void Load (bool reset = false)
237238
{
238-
Debug.WriteLine ("ConfigurationManager.Load()");
239+
Logging.Trace ($"reset = {reset}");
239240

240241
if (reset)
241242
{
@@ -292,7 +293,7 @@ public static void Load (bool reset = false)
292293
/// </summary>
293294
public static void OnApplied ()
294295
{
295-
Debug.WriteLine ("ConfigurationManager.OnApplied()");
296+
//Logging.Trace ("");
296297

297298
Applied?.Invoke (null, new ());
298299

@@ -308,7 +309,7 @@ public static void OnApplied ()
308309
/// </summary>
309310
public static void OnUpdated ()
310311
{
311-
Debug.WriteLine (@"ConfigurationManager.OnUpdated()");
312+
//Logging.Trace (@"");
312313
Updated?.Invoke (null, new ());
313314
}
314315

@@ -324,6 +325,18 @@ public static void PrintJsonErrors ()
324325
}
325326
}
326327

328+
329+
public static void LogJsonErrors ()
330+
{
331+
if (_jsonErrors.Length > 0)
332+
{
333+
Logging.Warning (
334+
@"Encountered the following errors while deserializing configuration files:"
335+
);
336+
Logging.Warning (_jsonErrors.ToString ());
337+
}
338+
}
339+
327340
/// <summary>
328341
/// Resets the state of <see cref="ConfigurationManager"/>. Should be called whenever a new app session (e.g. in
329342
/// <see cref="Application.Init"/> starts. Called by <see cref="Load"/> if the <c>reset</c> parameter is
@@ -334,7 +347,7 @@ public static void PrintJsonErrors ()
334347
[RequiresDynamicCode ("AOT")]
335348
public static void Reset ()
336349
{
337-
Debug.WriteLine (@"ConfigurationManager.Reset()");
350+
Logging.Trace ($"_allConfigProperties = {_allConfigProperties}");
338351

339352
if (_allConfigProperties is null)
340353
{
@@ -369,7 +382,7 @@ public static void Reset ()
369382

370383
internal static void AddJsonError (string error)
371384
{
372-
Debug.WriteLine ($"ConfigurationManager: {error}");
385+
Logging.Trace ($"error = {error}");
373386
_jsonErrors.AppendLine (error);
374387
}
375388

@@ -541,8 +554,8 @@ where type.GetProperties ()
541554
classesWithConfigProps.Add (classWithConfig.Name, classWithConfig);
542555
}
543556

544-
//Debug.WriteLine ($"ConfigManager.getConfigProperties found {classesWithConfigProps.Count} classes:");
545-
classesWithConfigProps.ToList ().ForEach (x => Debug.WriteLine ($" Class: {x.Key}"));
557+
//Logging.Trace ($"ConfigManager.getConfigProperties found {classesWithConfigProps.Count} classes:");
558+
classesWithConfigProps.ToList ().ForEach (x => Logging.Trace ($" Class: {x.Key}"));
546559

547560
foreach (PropertyInfo? p in from c in classesWithConfigProps
548561
let props = c.Value
@@ -595,9 +608,9 @@ from p in enumerable
595608
StringComparer.InvariantCultureIgnoreCase
596609
);
597610

598-
//Debug.WriteLine ($"ConfigManager.Initialize found {_allConfigProperties.Count} properties:");
611+
//Logging.Trace ($"Found {_allConfigProperties.Count} properties:");
599612

600-
//_allConfigProperties.ToList ().ForEach (x => Debug.WriteLine ($" Property: {x.Key}"));
613+
//_allConfigProperties.ToList ().ForEach (x => Logging.Trace ($" Property: {x.Key}"));
601614

602615
AppSettings = new ();
603616
}
@@ -608,16 +621,16 @@ from p in enumerable
608621
[RequiresDynamicCode ("AOT")]
609622
internal static string ToJson ()
610623
{
611-
//Debug.WriteLine ("ConfigurationManager.ToJson()");
624+
//Logging.Trace ("ConfigurationManager.ToJson()");
612625

613-
return JsonSerializer.Serialize (Settings!, typeof (SettingsScope), _serializerContext);
626+
return JsonSerializer.Serialize (Settings!, typeof (SettingsScope), SerializerContext);
614627
}
615628

616629
[RequiresUnreferencedCode ("AOT")]
617630
[RequiresDynamicCode ("AOT")]
618631
internal static Stream ToStream ()
619632
{
620-
string json = JsonSerializer.Serialize (Settings!, typeof (SettingsScope), _serializerContext);
633+
string json = JsonSerializer.Serialize (Settings!, typeof (SettingsScope), SerializerContext);
621634

622635
// turn it into a stream
623636
var stream = new MemoryStream ();

Terminal.Gui/Configuration/DictionaryJsonConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ JsonSerializerOptions options
2828
{
2929
string key = reader.GetString ();
3030
reader.Read ();
31-
var value = JsonSerializer.Deserialize (ref reader, typeof (T), _serializerContext);
31+
var value = JsonSerializer.Deserialize (ref reader, typeof (T), SerializerContext);
3232
dictionary.Add (key, (T)value);
3333
}
3434
}
@@ -51,7 +51,7 @@ public override void Write (Utf8JsonWriter writer, Dictionary<string, T> value,
5151

5252
//writer.WriteString (item.Key, item.Key);
5353
writer.WritePropertyName (item.Key);
54-
JsonSerializer.Serialize (writer, item.Value, typeof (T), _serializerContext);
54+
JsonSerializer.Serialize (writer, item.Value, typeof (T), SerializerContext);
5555
writer.WriteEndObject ();
5656
}
5757

Terminal.Gui/Configuration/ScopeJsonConverter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public override scopeT Read (ref Utf8JsonReader reader, Type typeToConvert, Json
8989
try
9090
{
9191
scope! [propertyName].PropertyValue =
92-
JsonSerializer.Deserialize (ref reader, propertyType!, _serializerContext);
92+
JsonSerializer.Deserialize (ref reader, propertyType!, SerializerContext);
9393
}
9494
catch (Exception ex)
9595
{
96-
Debug.WriteLine ($"scopeT Read: {ex}");
96+
// Logging.Trace ($"scopeT Read: {ex}");
9797
}
9898
}
9999
}
@@ -137,7 +137,7 @@ public override scopeT Read (ref Utf8JsonReader reader, Type typeToConvert, Json
137137
if (property is { })
138138
{
139139
PropertyInfo prop = scope.GetType ().GetProperty (propertyName!)!;
140-
prop.SetValue (scope, JsonSerializer.Deserialize (ref reader, prop.PropertyType, _serializerContext));
140+
prop.SetValue (scope, JsonSerializer.Deserialize (ref reader, prop.PropertyType, SerializerContext));
141141
}
142142
else
143143
{
@@ -165,7 +165,7 @@ public override void Write (Utf8JsonWriter writer, scopeT scope, JsonSerializerO
165165
{
166166
writer.WritePropertyName (ConfigProperty.GetJsonPropertyName (p));
167167
object? prop = scope.GetType ().GetProperty (p.Name)?.GetValue (scope);
168-
JsonSerializer.Serialize (writer, prop, prop!.GetType (), _serializerContext);
168+
JsonSerializer.Serialize (writer, prop, prop!.GetType (), SerializerContext);
169169
}
170170

171171
foreach (KeyValuePair<string, ConfigProperty> p in from p in scope
@@ -211,7 +211,7 @@ SerializableConfigurationProperty scp
211211
else
212212
{
213213
object? prop = p.Value.PropertyValue;
214-
JsonSerializer.Serialize (writer, prop, prop!.GetType (), _serializerContext);
214+
JsonSerializer.Serialize (writer, prop, prop!.GetType (), SerializerContext);
215215
}
216216
}
217217

Terminal.Gui/Configuration/SettingsScope.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class SettingsScope : Scope<SettingsScope>
4545
// Update the existing settings with the new settings.
4646
try
4747
{
48-
Update ((SettingsScope)JsonSerializer.Deserialize (stream, typeof (SettingsScope), _serializerOptions)!);
48+
Update ((SettingsScope)JsonSerializer.Deserialize (stream, typeof (SettingsScope), SerializerOptions)!);
4949
OnUpdated ();
50-
Debug.WriteLine ($"ConfigurationManager: Read configuration from \"{source}\"");
50+
Logging.Trace ($"Read from \"{source}\"");
5151
if (!Sources.ContainsValue (source))
5252
{
5353
Sources.Add (location, source);
@@ -79,7 +79,7 @@ public class SettingsScope : Scope<SettingsScope>
7979

8080
if (!File.Exists (realPath))
8181
{
82-
Debug.WriteLine ($"ConfigurationManager: Configuration file \"{realPath}\" does not exist.");
82+
Logging.Warning ($"\"{realPath}\" does not exist.");
8383
if (!Sources.ContainsValue (filePath))
8484
{
8585
Sources.Add (location, filePath);
@@ -105,7 +105,7 @@ public class SettingsScope : Scope<SettingsScope>
105105
}
106106
catch (IOException ioe)
107107
{
108-
Debug.WriteLine ($"Couldn't open {filePath}. Retrying...: {ioe}");
108+
Logging.Warning ($"Couldn't open {filePath}. Retrying...: {ioe}");
109109
Task.Delay (100);
110110
retryCount++;
111111
}

Terminal.Gui/Configuration/ThemeManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ internal static string SelectedTheme
127127
[RequiresDynamicCode ("Calls Terminal.Gui.ThemeManager.Themes")]
128128
internal static void GetHardCodedDefaults ()
129129
{
130-
//Debug.WriteLine ("Themes.GetHardCodedDefaults()");
130+
//Logging.Trace ("Themes.GetHardCodedDefaults()");
131131
var theme = new ThemeScope ();
132132
theme.RetrieveValues ();
133133

@@ -141,15 +141,15 @@ internal static void GetHardCodedDefaults ()
141141
/// <summary>Called when the selected theme has changed. Fires the <see cref="ThemeChanged"/> event.</summary>
142142
internal void OnThemeChanged (string theme)
143143
{
144-
//Debug.WriteLine ($"Themes.OnThemeChanged({theme}) -> {Theme}");
144+
//Logging.Trace ($"Themes.OnThemeChanged({theme}) -> {Theme}");
145145
ThemeChanged?.Invoke (this, new ThemeManagerEventArgs (theme));
146146
}
147147

148148
[RequiresUnreferencedCode ("Calls Terminal.Gui.ThemeManager.Themes")]
149149
[RequiresDynamicCode ("Calls Terminal.Gui.ThemeManager.Themes")]
150150
internal static void Reset ()
151151
{
152-
Debug.WriteLine ("Themes.Reset()");
152+
//Logging.Trace ("Themes.Reset()");
153153
Colors.Reset ();
154154
Themes?.Clear ();
155155
SelectedTheme = string.Empty;

Terminal.Gui/ConsoleDrivers/V2/Logging.cs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Terminal.Gui;
1313
/// <remarks>
1414
/// Also contains the
1515
/// <see cref="Meter"/> instance that should be used for internal metrics
16-
/// (iteration timing etc).
16+
/// (iteration timing etc.).
1717
/// </remarks>
1818
public static class Logging
1919
{
@@ -51,7 +51,71 @@ public static class Logging
5151
public static readonly Histogram<int> DrainInputStream = Meter.CreateHistogram<int> ("Drain Input (ms)");
5252

5353
/// <summary>
54-
/// Logs a trace message including the
54+
/// Logs an error message including the class and method name.
55+
/// </summary>
56+
/// <param name="message"></param>
57+
/// <param name="caller"></param>
58+
/// <param name="filePath"></param>
59+
public static void Error (
60+
string message,
61+
[CallerMemberName] string caller = "",
62+
[CallerFilePath] string filePath = ""
63+
)
64+
{
65+
string className = Path.GetFileNameWithoutExtension (filePath);
66+
Logger.LogError ($"[{className}] [{caller}] {message}");
67+
}
68+
69+
/// <summary>
70+
/// Logs a critical message including the class and method name.
71+
/// </summary>
72+
/// <param name="message"></param>
73+
/// <param name="caller"></param>
74+
/// <param name="filePath"></param>
75+
public static void Critical (
76+
string message,
77+
[CallerMemberName] string caller = "",
78+
[CallerFilePath] string filePath = ""
79+
)
80+
{
81+
string className = Path.GetFileNameWithoutExtension (filePath);
82+
Logger.LogCritical ($"[{className}] [{caller}] {message}");
83+
}
84+
85+
/// <summary>
86+
/// Logs a debug message including the class and method name.
87+
/// </summary>
88+
/// <param name="message"></param>
89+
/// <param name="caller"></param>
90+
/// <param name="filePath"></param>
91+
public static void Debug (
92+
string message,
93+
[CallerMemberName] string caller = "",
94+
[CallerFilePath] string filePath = ""
95+
)
96+
{
97+
string className = Path.GetFileNameWithoutExtension (filePath);
98+
Logger.LogDebug ($"[{className}] [{caller}] {message}");
99+
}
100+
101+
/// <summary>
102+
/// Logs an informational message including the class and method name.
103+
/// </summary>
104+
/// <param name="message"></param>
105+
/// <param name="caller"></param>
106+
/// <param name="filePath"></param>
107+
public static void Information (
108+
string message,
109+
[CallerMemberName] string caller = "",
110+
[CallerFilePath] string filePath = ""
111+
)
112+
{
113+
string className = Path.GetFileNameWithoutExtension (filePath);
114+
Logger.LogInformation ($"[{className}] [{caller}] {message}");
115+
}
116+
117+
/// <summary>
118+
/// Logs a trace message including the class and method name.
55119
/// </summary>
56120
/// <param name="message"></param>
57121
/// <param name="caller"></param>
@@ -65,4 +129,20 @@ public static void Trace (
65129
string className = Path.GetFileNameWithoutExtension (filePath);
66130
Logger.LogTrace ($"[{className}] [{caller}] {message}");
67131
}
132+
133+
/// <summary>
134+
/// Logs a warning message including the class and method name.
135+
/// </summary>
136+
/// <param name="message"></param>
137+
/// <param name="caller"></param>
138+
/// <param name="filePath"></param>
139+
public static void Warning (
140+
string message,
141+
[CallerMemberName] string caller = "",
142+
[CallerFilePath] string filePath = ""
143+
)
144+
{
145+
string className = Path.GetFileNameWithoutExtension (filePath);
146+
Logger.LogWarning ($"[{className}] [{caller}] {message}");
147+
}
68148
}

0 commit comments

Comments
 (0)