From 3c3e8868437336bcd6fb12ada40b658a98617cf1 Mon Sep 17 00:00:00 2001 From: gbakeman Date: Wed, 23 Nov 2022 15:25:48 -0800 Subject: [PATCH 1/8] Attempt at fixing weird log file locations Trying to fix log files ending up in the C:\ directory. SetupAppDirectory is now private, and named GetAppDirectory. This is now strictly a function that tries to create the base directory and return it. It will also fallback to the temp directory if necessary, and hopefully log the error. The Globals method has also generally been rearranged and cleaned up with respect to logging and directories. --- .../WinNUT-Client_Common/WinNUT_Globals.vb | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index 6886b81..a962151 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -12,7 +12,17 @@ Imports System.IO Public Module WinNUT_Globals #Region "Constants/Shareds" - Private DEFAULT_DATA_PATH As String + ' What path we'd like to keep our data in. + +#If DEBUG Then + ' If debugging, keep any generated data next to the debug executable. + Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.CurrentDirectory, "Data") +#Else + Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.GetFolderPath( + Environment.SpecialFolder.ApplicationData), "\WinNUT-Client") +#End If + + Private ReadOnly FALLBACK_DATA_PATH = Path.GetTempPath() & ProgramName #End Region Public LongProgramName As String @@ -23,22 +33,13 @@ Public Module WinNUT_Globals Public Copyright As String Public IsConnected As Boolean Public ApplicationData As String - ' Public LogFile As String - ' Handle application messages and debug events. - ' Public WithEvents LogFile As Logger ' As New Logger(False, 0) - ' Logging Public WithEvents LogFile As Logger Public AppIcon As Dictionary(Of Integer, Drawing.Icon) Public StrLog As New List(Of String) - ' Public LogFilePath As String Public Sub Init_Globals() -#If DEBUG Then - ' If debugging, keep any generated data next to the debug executable. - DEFAULT_DATA_PATH = Path.Combine(Environment.CurrentDirectory, "Data") -#Else - DEFAULT_DATA_PATH = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\WinNUT-Client") -#End If + ApplicationData = GetAppDirectory(DESIRED_DATA_PATH) + LogFile = New Logger(LogLvl.LOG_DEBUG) LongProgramName = My.Application.Info.Description ProgramName = My.Application.Info.ProductName @@ -47,22 +48,25 @@ Public Module WinNUT_Globals GitHubURL = My.Application.Info.Trademark Copyright = My.Application.Info.Copyright IsConnected = False - LogFile = New Logger(LogLvl.LOG_DEBUG) - - SetupAppDirectory() End Sub - Sub SetupAppDirectory() - If Not Directory.Exists(DEFAULT_DATA_PATH) Then - Try - Directory.CreateDirectory(DEFAULT_DATA_PATH) - ApplicationData = DEFAULT_DATA_PATH - Catch ex As Exception - LogFile.LogTracing(ex.ToString & " encountered trying to create app data directory. Falling back to temp.", - LogLvl.LOG_ERROR, Nothing) - ApplicationData = Path.GetTempPath() & "\WinNUT_Data\" - Directory.CreateDirectory(ApplicationData) - End Try - End If - End Sub + ''' + ''' Do everything possible to find a safe place to write to. If the requested option is unavailable, we fall back + ''' to the temporary directory for the current user. + ''' + ''' The requested directory. + ''' The best possible option available as a writable data directory. + Private Function GetAppDirectory(requestedDir As String) As String + Try + Directory.CreateDirectory(requestedDir) + Return requestedDir + + Catch ex As Exception + LogFile.LogTracing(ex.ToString & " encountered trying to create app data directory. Falling back to temp.", + LogLvl.LOG_ERROR, Nothing) + + Directory.CreateDirectory(FALLBACK_DATA_PATH) + Return FALLBACK_DATA_PATH + End Try + End Function End Module From da54b63010550d00c0b9a51113d6f2691f995206 Mon Sep 17 00:00:00 2001 From: gbakeman Date: Wed, 23 Nov 2022 15:28:28 -0800 Subject: [PATCH 2/8] More log messages when creating app directory. --- WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index a962151..dd5022d 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -59,6 +59,8 @@ Public Module WinNUT_Globals Private Function GetAppDirectory(requestedDir As String) As String Try Directory.CreateDirectory(requestedDir) + LogFile.LogTracing("Successfully created or opened requested data directory for WinNUT." & + vbNewLine & "requestedDir: " & requestedDir, LogLvl.LOG_DEBUG, Nothing) Return requestedDir Catch ex As Exception From 79cc539bb3c5f57ef958fcc564e5be540a58fc40 Mon Sep 17 00:00:00 2001 From: gbakeman Date: Wed, 23 Nov 2022 15:37:55 -0800 Subject: [PATCH 3/8] Move static var init out of subroutine To vastly simplify the Init_Globals routine, I've moved some shortcut properties/other static information into the variable definitions area of the module. Also instantiate the Logger class right away so it's available to take calls ASAP. --- .../WinNUT-Client_Common/WinNUT_Globals.vb | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index dd5022d..49f1e96 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -12,7 +12,6 @@ Imports System.IO Public Module WinNUT_Globals #Region "Constants/Shareds" - ' What path we'd like to keep our data in. #If DEBUG Then ' If debugging, keep any generated data next to the debug executable. @@ -23,31 +22,26 @@ Public Module WinNUT_Globals #End If Private ReadOnly FALLBACK_DATA_PATH = Path.GetTempPath() & ProgramName -#End Region - Public LongProgramName As String - Public ProgramName As String - Public ProgramVersion As String - Public ShortProgramVersion As String - Public GitHubURL As String - Public Copyright As String - Public IsConnected As Boolean + Public ReadOnly ProgramName = My.Application.Info.ProductName + Public ReadOnly LongProgramName = My.Application.Info.Description + Public ReadOnly ProgramVersion = Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString + Public ReadOnly ShortProgramVersion = ProgramVersion.Substring(0, ProgramVersion.IndexOf(".", ProgramVersion.IndexOf(".") + 1)) + Public ReadOnly GitHubURL = My.Application.Info.Trademark + Public ReadOnly Copyright = My.Application.Info.Copyright + + Public IsConnected = False Public ApplicationData As String - Public WithEvents LogFile As Logger + Public WithEvents LogFile As Logger = New Logger(LogLvl.LOG_DEBUG) Public AppIcon As Dictionary(Of Integer, Drawing.Icon) Public StrLog As New List(Of String) +#End Region + + + Public Sub Init_Globals() ApplicationData = GetAppDirectory(DESIRED_DATA_PATH) - LogFile = New Logger(LogLvl.LOG_DEBUG) - - LongProgramName = My.Application.Info.Description - ProgramName = My.Application.Info.ProductName - ProgramVersion = Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString - ShortProgramVersion = ProgramVersion.Substring(0, ProgramVersion.IndexOf(".", ProgramVersion.IndexOf(".") + 1)) - GitHubURL = My.Application.Info.Trademark - Copyright = My.Application.Info.Copyright - IsConnected = False End Sub ''' From cff8de2718f48225a2870283f8df528f2eebfe2c Mon Sep 17 00:00:00 2001 From: gbakeman Date: Wed, 23 Nov 2022 15:56:26 -0800 Subject: [PATCH 4/8] Path constants only represent base path Any path-related constants only represent the directory that the WinNUT-Client data container directory goes in. The WinNUT directory will be appended later on. --- WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index 49f1e96..9360a60 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -15,13 +15,13 @@ Public Module WinNUT_Globals #If DEBUG Then ' If debugging, keep any generated data next to the debug executable. - Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.CurrentDirectory, "Data") + Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.CurrentDirectory) #Else - Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.GetFolderPath( - Environment.SpecialFolder.ApplicationData), "\WinNUT-Client") + Private ReadOnly DESIRED_DATA_PATH As String = Environment.GetFolderPath( + Environment.SpecialFolder.ApplicationData)) #End If - Private ReadOnly FALLBACK_DATA_PATH = Path.GetTempPath() & ProgramName + Private ReadOnly FALLBACK_DATA_PATH = Path.GetTempPath() Public ReadOnly ProgramName = My.Application.Info.ProductName Public ReadOnly LongProgramName = My.Application.Info.Description @@ -45,12 +45,14 @@ Public Module WinNUT_Globals End Sub ''' - ''' Do everything possible to find a safe place to write to. If the requested option is unavailable, we fall back - ''' to the temporary directory for the current user. + ''' Do everything possible to find a safe place to write to, with the appended to it. If + ''' the requested option is unavailable, we fall back to the temporary directory for the current user. ''' - ''' The requested directory. + ''' The requested directory, with appended to it. ''' The best possible option available as a writable data directory. Private Function GetAppDirectory(requestedDir As String) As String + requestedDir = Path.Combine(requestedDir, ProgramName) + Try Directory.CreateDirectory(requestedDir) LogFile.LogTracing("Successfully created or opened requested data directory for WinNUT." & From dbe0c61fe2c43ef2e354e4abc6d293cdb4bae82e Mon Sep 17 00:00:00 2001 From: gbakeman Date: Wed, 23 Nov 2022 16:05:09 -0800 Subject: [PATCH 5/8] Make global debug build constant --- WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index 9360a60..a05fbb7 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -14,9 +14,11 @@ Public Module WinNUT_Globals #Region "Constants/Shareds" #If DEBUG Then + Public ReadOnly IsDebugBuild = True ' If debugging, keep any generated data next to the debug executable. Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.CurrentDirectory) #Else + Public ReadOnly IsDebugBuild = False Private ReadOnly DESIRED_DATA_PATH As String = Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData)) #End If From 2ea829e63352dce4d0048255d77b11c3abe35b94 Mon Sep 17 00:00:00 2001 From: gbakeman Date: Wed, 23 Nov 2022 17:18:24 -0800 Subject: [PATCH 6/8] Logging updates - Tried to use default date/time formats when possible (not sure it actually affects anything though) - Bolstered initialization routine for logging to provide better info and past events sitting in the buffer. - Broke out log line formatting into its own function - Return the data directory name back to what it was before. --- WinNUT_V2/WinNUT-Client_Common/Logger.vb | 75 ++++++++++++++----- .../WinNUT-Client_Common/WinNUT_Globals.vb | 1 + 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/WinNUT_V2/WinNUT-Client_Common/Logger.vb b/WinNUT_V2/WinNUT-Client_Common/Logger.vb index d04cbc8..231c326 100644 --- a/WinNUT_V2/WinNUT-Client_Common/Logger.vb +++ b/WinNUT_V2/WinNUT-Client_Common/Logger.vb @@ -7,23 +7,38 @@ ' ' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY +Imports System.Globalization Imports System.IO Public Class Logger #Region "Constants/Shared" - Private Shared ReadOnly BASE_FILE_NAME = ProgramName ' "WinNUT-CLient" Private Const LOG_FILE_CREATION_SCHEDULE = Logging.LogFileCreationScheduleOption.Daily - ' The LogFileCreationScheduleOption doesn't present the string format of what it uses - Private Const LOG_FILE_DATESTRING = "yyyy-MM-dd" + +#If DEBUG Then + Private Shared ReadOnly DEFAULT_DATETIMEFORMAT = DateTimeFormatInfo.InvariantInfo +#Else + Private Shared ReadOnly DEFAULT_DATETIMEFORMAT = DateTimeFormatInfo.CurrentInfo +#End If + + ' The subfolder that will contain logs. - Public Const LOG_SUBFOLDER = "\Logs\" + Public Const LOG_SUBFOLDER = "Logs" + + Private Shared ReadOnly BASE_FILE_NAME = ProgramName + Private ReadOnly TEventCache As New TraceEventCache() #End Region +#Region "Private/backing values" + Private LogFile As Logging.FileLogTraceListener - Private ReadOnly TEventCache As New TraceEventCache() - Public LogLevelValue As LogLvl Private L_CurrentLogData As String Private LastEventsList As New List(Of Object) + Private _DateTimeFormatInfo As DateTimeFormatInfo = DEFAULT_DATETIMEFORMAT + +#End Region + + Public LogLevelValue As LogLvl + Public Event NewData(sender As Object) #Region "Properties" @@ -77,6 +92,16 @@ Public Class Logger End Get End Property + Public Property DateTimeFormatInfo As DateTimeFormatInfo + Get + Return _DateTimeFormatInfo + End Get + Set(value As DateTimeFormatInfo) + _DateTimeFormatInfo = value + End Set + End Property + + #End Region Public Sub New(LogLevel As LogLvl) @@ -89,11 +114,22 @@ Public Class Logger .Append = True, .AutoFlush = True, .LogFileCreationSchedule = LOG_FILE_CREATION_SCHEDULE, - .CustomLocation = baseDataFolder & LOG_SUBFOLDER, + .CustomLocation = Path.Combine(baseDataFolder, LOG_SUBFOLDER), .Location = Logging.LogFileLocation.Custom } - LogTracing("Log file is initialized at " & LogFile.FullLogFileName, LogLvl.LOG_NOTICE, Me) + LogTracing(String.Format("{0} {1} Log file init", LongProgramName, ProgramVersion), LogLvl.LOG_NOTICE, Me) + + If LastEventsList.Count > 0 Then + ' Fill new file with the LastEventsList buffer + LogFile.WriteLine("==== History of " & LastEventsList.Count & " previous events ====") + + For index As Integer = 0 To LastEventsList.Count - 1 + LogFile.WriteLine(String.Format("[{0}] {1}", index + 1, LastEventsList(index))) + Next + End If + + LogFile.WriteLine(String.Empty) End Sub ''' @@ -132,17 +168,7 @@ Public Class Logger ''' What generated this message. ''' A user-friendly, translated string to be shown. Public Sub LogTracing(message As String, LvlError As LogLvl, sender As Object, Optional LogToDisplay As String = Nothing) - Dim Pid = TEventCache.ProcessId - Dim SenderName - ' Handle a null sender - If sender Is Nothing Then - SenderName = "Nothing" - Else - SenderName = sender.GetType.Name - End If - - Dim EventTime = Now.ToLocalTime - Dim FinalMsg = EventTime & " Pid: " & Pid & " " & SenderName & " : " & message + Dim FinalMsg = FormatLogLine(message, LvlError, sender) ' Always write log messages to the attached debug messages window. #If DEBUG Then @@ -166,4 +192,15 @@ Public Class Logger RaiseEvent NewData(sender) End If End Sub + + Private Function FormatLogLine(message As String, logLvl As LogLvl, Optional sender As Object = Nothing) + Dim Pid = TEventCache.ProcessId + Dim SenderName = "Nothing" + + If sender IsNot Nothing Then + SenderName = sender.GetType.Name + End If + + Return String.Format("{0} [{1}, {2}]: {3}", Date.Now.ToString(_DateTimeFormatInfo), Pid, SenderName, message) + End Function End Class diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index a05fbb7..72421de 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -24,6 +24,7 @@ Public Module WinNUT_Globals #End If Private ReadOnly FALLBACK_DATA_PATH = Path.GetTempPath() + Private ReadOnly DATA_DIRECTORY_NAME = "WinNut-Client" Public ReadOnly ProgramName = My.Application.Info.ProductName Public ReadOnly LongProgramName = My.Application.Info.Description From c0d36a35978c017fd875cc3a7ef599adc5b5fd15 Mon Sep 17 00:00:00 2001 From: gbakeman Date: Wed, 23 Nov 2022 17:20:01 -0800 Subject: [PATCH 7/8] Use directory name as a constant --- WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index 72421de..5b21384 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -54,7 +54,7 @@ Public Module WinNUT_Globals ''' The requested directory, with appended to it. ''' The best possible option available as a writable data directory. Private Function GetAppDirectory(requestedDir As String) As String - requestedDir = Path.Combine(requestedDir, ProgramName) + requestedDir = Path.Combine(requestedDir, DATA_DIRECTORY_NAME) Try Directory.CreateDirectory(requestedDir) From 7dc0214e06218af83516fbba194deca8fb9da23b Mon Sep 17 00:00:00 2001 From: gbakeman Date: Wed, 23 Nov 2022 17:22:24 -0800 Subject: [PATCH 8/8] Add notification about beginning of current log --- WinNUT_V2/WinNUT-Client_Common/Logger.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WinNUT_V2/WinNUT-Client_Common/Logger.vb b/WinNUT_V2/WinNUT-Client_Common/Logger.vb index 231c326..03059cf 100644 --- a/WinNUT_V2/WinNUT-Client_Common/Logger.vb +++ b/WinNUT_V2/WinNUT-Client_Common/Logger.vb @@ -129,7 +129,7 @@ Public Class Logger Next End If - LogFile.WriteLine(String.Empty) + LogFile.WriteLine("==== Begin Live Log ====") End Sub '''