Skip to content

Commit bfe8d89

Browse files
committed
Simplified trace logging implementation.
1 parent 765f0fc commit bfe8d89

File tree

2 files changed

+31
-46
lines changed

2 files changed

+31
-46
lines changed

src/Dialogs/MainWindow.xaml.cs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,41 +2158,34 @@ private static async void CheckForUpdates()
21582158
try
21592159
{
21602160
Tuple<Version, Version, DateTime> versionLocal = GetVersionAndBuildDate();
2161-
TraceLogger logger = new TraceLogger("CheckForUpdate");
21622161
HashCode hashCode = HashCode.Compute($"{versionLocal.Item1}\\{versionLocal.Item2}\\{GetUnixTimeSeconds() / 3593}");
21632162
ulong? lastUpdateCheck = ReadRegValue(REGISTRY_VALUE_NAME);
21642163
if ((!lastUpdateCheck.HasValue) || (lastUpdateCheck.Value != hashCode.Value))
21652164
{
2166-
logger.WriteLine($"Update check is starting...");
2165+
TraceLog.WriteLine($"Update check is starting...");
21672166
Version versionRemote = await Task.Run(() => CheckForUpdatesTask(VERSION_URL, SIGNKEY_PUB));
2168-
if (IsNotNull(versionRemote))
2167+
if (versionRemote.CompareTo(versionLocal.Item1) > 0)
21692168
{
2170-
try
2169+
const string message = "A new program version is available!\n\nInstalled version: {0}\nLatest available version: {1}\n\nIt is recommended that you upgrade to the new version. Do you want to download the new version now?";
2170+
TraceLog.WriteLine($"New program version is available: {versionLocal.Item1} -> {versionRemote}");
2171+
if (MessageBox.Show(string.Format(message, versionLocal.Item1, versionRemote), "Update Notification", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
21712172
{
2172-
if (versionRemote.CompareTo(versionLocal.Item1) > 0)
2173-
{
2174-
const string message = "A new program version is available!\n\nInstalled version: {0}\nLatest available version: {1}\n\nIt is recommended that you upgrade to the new version. Do you want to download the new version now?";
2175-
logger.WriteLine($"New program version is available: {versionLocal.Item1} -> {versionRemote}");
2176-
if (MessageBox.Show(string.Format(message, versionLocal.Item1, versionRemote), "Update Notification", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
2177-
{
2178-
Process.Start(new ProcessStartInfo { FileName = WEBSITE_URL, UseShellExecute = true });
2179-
Application.Current.Shutdown();
2180-
}
2181-
}
2182-
else
2183-
{
2184-
logger.WriteLine($"The installed program version is up to date -> nothing to do!");
2185-
}
2173+
Process.Start(new ProcessStartInfo { FileName = WEBSITE_URL, UseShellExecute = true });
2174+
Application.Current.Shutdown();
21862175
}
2187-
finally
2176+
}
2177+
else
2178+
{
2179+
TraceLog.WriteLine($"The installed program version is still up to date.");
2180+
if (versionRemote.Major > 0)
21882181
{
21892182
WriteRegValue(REGISTRY_VALUE_NAME, hashCode.Value);
21902183
}
21912184
}
21922185
}
21932186
else
21942187
{
2195-
logger.WriteLine($"Skipping update check this time.");
2188+
TraceLog.WriteLine($"Skipping update check this time.");
21962189
}
21972190
}
21982191
catch
@@ -2203,47 +2196,46 @@ private static async void CheckForUpdates()
22032196

22042197
private static Version CheckForUpdatesTask(string versionUrl, string verificationKey)
22052198
{
2206-
TraceLogger logger = new TraceLogger("ChckUpdateTask");
22072199
const int MAX_TRIES = 5;
22082200
Tuple<string, string> updateInfo;
22092201
Version version;
22102202
for (int retry = 0; retry < MAX_TRIES; ++retry)
22112203
{
2212-
logger.WriteLine($"Downloading update information (attempt {retry+1}/{MAX_TRIES})");
2204+
TraceLog.WriteLine($"Downloading update information (attempt {retry+1}/{MAX_TRIES})");
22132205
try
22142206
{
2215-
if (IsNotNull(updateInfo = DownloadFileContents(versionUrl)))
2207+
if (IsNotNull(updateInfo = DownloadFile(versionUrl)))
22162208
{
2217-
logger.WriteLine( $"Update information: info=\"{updateInfo.Item1}\", signature=\"{updateInfo.Item2}\"");
2209+
TraceLog.WriteLine( $"Update information: info=\"{updateInfo.Item1}\", signature=\"{updateInfo.Item2}\"");
22182210
if (VerifySignature(updateInfo.Item1, updateInfo.Item2, verificationKey))
22192211
{
2220-
logger.WriteLine($"Signature is valid.");
2212+
TraceLog.WriteLine($"Signature is valid.");
22212213
if (Version.TryParse(updateInfo.Item1, out version))
22222214
{
2223-
logger.WriteLine($"Latest available program version is: {version}");
2215+
TraceLog.WriteLine($"Latest available program version is: {version}");
22242216
return version;
22252217
}
22262218
else
22272219
{
2228-
logger.WriteLine($"Failed to parse version string!");
2220+
TraceLog.WriteLine($"Failed to parse version string!");
22292221
}
22302222
}
22312223
else
22322224
{
2233-
logger.WriteLine($"Signature verification has failed -> discarding update information!");
2225+
TraceLog.WriteLine($"Signature verification has failed -> discarding update information!");
22342226
}
22352227
}
22362228
else
22372229
{
2238-
logger.WriteLine($"Failed to download update information!");
2230+
TraceLog.WriteLine($"Failed to download update information!");
22392231
}
22402232
}
22412233
catch
22422234
{
22432235
if (IS_DEBUG) throw;
22442236
}
22452237
}
2246-
return null;
2238+
return new Version();
22472239
}
22482240

22492241
private static void StartNewInstance()

src/Utilities/Utilities.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -568,21 +568,15 @@ private static void DoEventsHelper() { }
568568
// Trace Log
569569
// ==================================================================
570570

571-
public class TraceLogger
571+
public static class TraceLog
572572
{
573-
private readonly string m_loggerName;
574573
private static readonly Lazy<BooleanSwitch> TRACING = new Lazy<BooleanSwitch>(() => new BooleanSwitch("Tracing", "Enable optional trace outputs", "False"));
575574

576-
public TraceLogger(string name)
577-
{
578-
m_loggerName = string.IsNullOrWhiteSpace(name) ? string.Empty : name;
579-
}
580-
581-
public void WriteLine(FormattableString message)
575+
public static void WriteLine(FormattableString message, [CallerMemberName] string callerName = "UnknownFunction")
582576
{
583577
try
584578
{
585-
Trace.WriteLineIf(TRACING.Value.Enabled, (FormattableString)$"[{m_loggerName}] {message}");
579+
Trace.WriteLineIf(TRACING.Value.Enabled, (FormattableString)$"[{callerName}] {message}");
586580
}
587581
catch { }
588582
}
@@ -599,7 +593,6 @@ public static class SecurityProtocolTypeExt
599593

600594
public static class HttpNetClient
601595
{
602-
private static readonly TraceLogger logger = new TraceLogger("HttpWebRequest");
603596
private const string USER_AGENT_STRING = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0";
604597

605598
static HttpNetClient()
@@ -617,9 +610,9 @@ static HttpNetClient()
617610
}
618611
}
619612

620-
public static Tuple<string, string> DownloadFileContents(string url)
613+
public static Tuple<string, string> DownloadFile(string url)
621614
{
622-
logger.WriteLine($"Request URL: {url}");
615+
TraceLog.WriteLine($"Request URL: {url}");
623616
try
624617
{
625618
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
@@ -630,7 +623,7 @@ public static Tuple<string, string> DownloadFileContents(string url)
630623
request.ReadWriteTimeout = 8000;
631624
using (HttpWebResponse response = GetResponseNoThrow(request))
632625
{
633-
logger.WriteLine($"Response status: {response.StatusCode} ({(int)response.StatusCode})");
626+
TraceLog.WriteLine($"Response status: {response.StatusCode} ({(int)response.StatusCode})");
634627
if (IsSuccess(response.StatusCode))
635628
{
636629
using (Stream responseStream = response.GetResponseStream())
@@ -646,20 +639,20 @@ public static Tuple<string, string> DownloadFileContents(string url)
646639
lines.Add(line);
647640
if (lines.Count >= 2)
648641
{
649-
logger.WriteLine($"Response data received successfully.");
642+
TraceLog.WriteLine($"Response data received successfully.");
650643
return Tuple.Create(lines[0], lines[1]);
651644
}
652645
}
653646
}
654647
}
655648
}
656-
logger.WriteLine($"Response is empty or incomplete.");
649+
TraceLog.WriteLine($"Response is empty or incomplete.");
657650
}
658651
}
659652
}
660653
catch (Exception exception)
661654
{
662-
logger.WriteLine($"Exception: {exception}");
655+
TraceLog.WriteLine($"Exception: {exception}");
663656
}
664657
return null;
665658
}

0 commit comments

Comments
 (0)