Skip to content

Commit 103a19c

Browse files
committed
Tweaking how paths are handled
- Correcting PreRelease build config so that projects are building correctly as needed. - Logger class handles its own directory since the Logging API provides default file locations. - Removing global variables and logic relating to data paths since the only code that really used it was the Logger class. - Specified output path for crashbugs. - Simplified Logger initialization to be more predictable and utilize standard values and paths.
1 parent 56a0900 commit 103a19c

File tree

6 files changed

+38
-82
lines changed

6 files changed

+38
-82
lines changed

WinNUT_V2/WinNUT-Client/ApplicationEvents.vb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Namespace My
2323
' NetworkAvailabilityChanged : Déclenché quand la connexion réseau est connectée ou déconnectée.
2424
Partial Friend Class MyApplication
2525
' Default culture for output so logs can be shared with the project.
26-
Private Shared DEF_CULTURE_INFO As CultureInfo = CultureInfo.InvariantCulture
27-
26+
Private Shared ReadOnly DEF_CULTURE_INFO As CultureInfo = CultureInfo.InvariantCulture
27+
Private Shared ReadOnly CRASHBUG_OUTPUT_PATH = System.Windows.Forms.Application.LocalUserAppDataPath
2828

2929
Private CrashBug_Form As New Form
3030
Private BtnClose As New Button
@@ -168,13 +168,13 @@ Namespace My
168168

169169
Computer.Clipboard.SetText(crashReportData)
170170

171-
Directory.CreateDirectory(TEMP_DATA_PATH)
172-
Dim CrashLog_Report = New StreamWriter(Path.Combine(TEMP_DATA_PATH, logFileName))
171+
Directory.CreateDirectory(CRASHBUG_OUTPUT_PATH)
172+
Dim CrashLog_Report = New StreamWriter(Path.Combine(CRASHBUG_OUTPUT_PATH, logFileName))
173173
CrashLog_Report.WriteLine(crashReportData)
174174
CrashLog_Report.Close()
175175

176176
' Open an Explorer window to the crash log.
177-
Process.Start(TEMP_DATA_PATH)
177+
Process.Start(CRASHBUG_OUTPUT_PATH)
178178
End
179179
End Sub
180180

WinNUT_V2/WinNUT-Client/Pref_Gui.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ Public Class Pref_Gui
343343

344344
Private Sub Btn_ViewLog_Click(sender As Object, e As EventArgs) Handles Btn_ViewLog.Click
345345
LogFile.LogTracing("Show LogFile", LogLvl.LOG_DEBUG, Me)
346-
If File.Exists(LogFile.LogFileLocation) Then
347-
Process.Start(LogFile.LogFileLocation)
346+
If File.Exists(LogFile.LogFilePath) Then
347+
Process.Start(LogFile.LogFilePath)
348348
Else
349349
LogFile.LogTracing("LogFile does not exists", LogLvl.LOG_WARNING, Me)
350350
Btn_ViewLog.Enabled = False

WinNUT_V2/WinNUT-Client/WinNUT.vb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,6 @@ Public Class WinNUT
140140
WinNUT_PrefsChanged(True)
141141
LogFile.LogTracing("Loaded Params Complete", LogLvl.LOG_DEBUG, Me)
142142

143-
' Setup logging preferences
144-
' LogFile.LogLevel = Arr_Reg_Key.Item("Log Level")
145-
' LogFile.IsWritingToFile = Arr_Reg_Key.Item("UseLogFile")
146-
'If Arr_Reg_Key.Item("UseLogFile") Then
147-
' LogFile.InitializeLogFile()
148-
'End If
149-
150-
'LogFile.LogTracing("Logging is configured.", LogLvl.LOG_DEBUG, Me)
151-
152143
'Init Systray
153144
NotifyIcon.Text = LongProgramName & " - " & ShortProgramVersion
154145
NotifyIcon.Visible = False
@@ -836,7 +827,7 @@ Public Class WinNUT
836827
' Setup logging preferences
837828
If Arr_Reg_Key.Item("UseLogFile") Then
838829
LogFile.LogLevelValue = Arr_Reg_Key.Item("Log Level")
839-
LogFile.InitializeLogFile(ApplicationDataPath)
830+
LogFile.InitializeLogFile()
840831
ElseIf LogFile.IsWritingToFile Then
841832
LogFile.DeleteLogFile()
842833
End If

WinNUT_V2/WinNUT-Client_Common/Logger.vb

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@
99

1010
Imports System.Globalization
1111
Imports System.IO
12+
Imports Microsoft.VisualBasic.Logging
1213

1314
Public Class Logger
1415
#Region "Constants/Shared"
15-
Private Const LOG_FILE_CREATION_SCHEDULE = Logging.LogFileCreationScheduleOption.Daily
16+
Private Const LOG_FILE_CREATION_SCHEDULE = LogFileCreationScheduleOption.Daily
1617

17-
#If DEBUG Then
18+
' Set TEST_RELEASE_DIRS in the custom compiler constants dialog for file storage to behave like release.
19+
#If DEBUG And Not TEST_RELEASE_DIRS Then
1820
Private Shared ReadOnly DEFAULT_DATETIMEFORMAT = DateTimeFormatInfo.InvariantInfo
21+
Private Shared ReadOnly DEFAULT_LOCATION = LogFileLocation.ExecutableDirectory
1922
#Else
2023
Private Shared ReadOnly DEFAULT_DATETIMEFORMAT = DateTimeFormatInfo.CurrentInfo
24+
' Actually goes to the Roaming folder.
25+
Private Shared ReadOnly DEFAULT_LOCATION = LogFileLocation.LocalUserApplicationDirectory
2126
#End If
2227

23-
24-
' The subfolder that will contain logs.
25-
Public Const LOG_SUBFOLDER = "Logs"
26-
27-
Private Shared ReadOnly BASE_FILE_NAME = ProgramName
2828
Private ReadOnly TEventCache As New TraceEventCache()
2929
#End Region
3030

3131
#Region "Private/backing values"
3232

33-
Private LogFile As Logging.FileLogTraceListener
33+
Private LogFile As FileLogTraceListener
3434
Private L_CurrentLogData As String
3535
Private LastEventsList As New List(Of Object)
3636
Private _DateTimeFormatInfo As DateTimeFormatInfo = DEFAULT_DATETIMEFORMAT
@@ -86,7 +86,7 @@ Public Class Logger
8686
''' Get the log file location from the <see cref="LogFile"/> object.
8787
''' </summary>
8888
''' <returns>The possible path to the log file. Note that this does not gaurantee it exists.</returns>
89-
Public ReadOnly Property LogFileLocation() As String
89+
Public ReadOnly Property LogFilePath() As String
9090
Get
9191
Return LogFile.FullLogFileName
9292
End Get
@@ -108,16 +108,27 @@ Public Class Logger
108108
LogLevelValue = LogLevel
109109
End Sub
110110

111-
Public Sub InitializeLogFile(baseDataFolder As String)
112-
LogFile = New Logging.FileLogTraceListener(BASE_FILE_NAME) With {
111+
''' <summary>
112+
''' Instantiates a new <see cref="FileLogTraceListener"/> at a the desired location, and outputs the
113+
''' <see cref="LastEvents"/> buffer into the file before synchronizing with write calls.
114+
''' </summary>
115+
''' <param name="baseDataFolder">Desired location to initiate the log file. If unspecified,
116+
''' then a default location is used.</param>
117+
Public Sub InitializeLogFile(Optional baseDataFolder As String = Nothing)
118+
LogFile = New FileLogTraceListener() With {
113119
.TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId,
114120
.Append = True,
115121
.AutoFlush = True,
116-
.LogFileCreationSchedule = LOG_FILE_CREATION_SCHEDULE,
117-
.CustomLocation = Path.Combine(baseDataFolder, LOG_SUBFOLDER),
118-
.Location = Logging.LogFileLocation.Custom
122+
.LogFileCreationSchedule = LOG_FILE_CREATION_SCHEDULE
119123
}
120124

125+
If baseDataFolder Is Nothing Then
126+
LogFile.Location = DEFAULT_LOCATION
127+
Else
128+
LogFile.Location = LogFileLocation.Custom
129+
LogFile.CustomLocation = baseDataFolder
130+
End If
131+
121132
LogTracing(String.Format("{0} {1} Log file init", LongProgramName, ProgramVersion), LogLvl.LOG_NOTICE, Me)
122133

123134
If LastEventsList.Count > 0 Then

WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,12 @@ Public Module WinNUT_Globals
2020
Public ReadOnly GitHubURL = My.Application.Info.Trademark
2121
Public ReadOnly Copyright = My.Application.Info.Copyright
2222

23-
#Region "File Directories"
24-
25-
Private ReadOnly DATA_DIRECTORY_NAME = "WinNUT-Client"
26-
Private ReadOnly TEMP_FOLDER = Path.GetTempPath() + DATA_DIRECTORY_NAME
27-
28-
#If DEBUG Then
29-
Public ReadOnly IsDebugBuild = True
30-
' If debugging, keep any generated data next to the debug executable.
31-
Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.CurrentDirectory, DATA_DIRECTORY_NAME)
32-
#Else
33-
Public ReadOnly IsDebugBuild = False
34-
Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.GetFolderPath(
35-
Environment.SpecialFolder.ApplicationData), DATA_DIRECTORY_NAME)
36-
#End If
37-
38-
Public ReadOnly TEMP_DATA_PATH = Path.Combine(Path.GetTempPath(), DATA_DIRECTORY_NAME)
39-
40-
#End Region
41-
42-
Public ApplicationDataPath = TEMP_DATA_PATH
4323
Public WithEvents LogFile As Logger = New Logger(LogLvl.LOG_DEBUG)
4424
Public StrLog As New List(Of String)
4525

4626
#End Region
4727

4828
Public Sub Init_Globals()
49-
ApplicationDataPath = GetAppDirectory(DESIRED_DATA_PATH)
50-
End Sub
51-
52-
''' <summary>
53-
''' Do everything possible to find a safe place to write to, with the <see cref="ProgramName"/> appended to it. If
54-
''' the requested option is unavailable, we fall back to the temporary directory for the current user.
55-
''' </summary>
56-
''' <param name="requestedDir">The requested directory, with <see cref="ProgramName"/> appended to it.</param>
57-
''' <returns>The best possible option available as a writable data directory.</returns>
58-
Private Function GetAppDirectory(requestedDir As String) As String
59-
Dim finalDir As String
60-
61-
Try
62-
Directory.CreateDirectory(requestedDir)
63-
LogFile.LogTracing("Successfully created or opened requested data directory for WinNUT." &
64-
vbNewLine & "requestedDir: " & requestedDir, LogLvl.LOG_DEBUG, Nothing)
65-
finalDir = requestedDir
6629

67-
Catch ex As Exception
68-
LogFile.LogTracing(ex.ToString & " encountered trying to create app data directory. Falling back to temp.",
69-
LogLvl.LOG_ERROR, Nothing)
70-
71-
Directory.CreateDirectory(TEMP_DATA_PATH)
72-
finalDir = TEMP_DATA_PATH
73-
End Try
74-
75-
Return finalDir
76-
End Function
30+
End Sub
7731
End Module

WinNUT_V2/WinNUT_V2.sln

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ Global
9999
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.Debug|x64.Build.0 = Debug|Any CPU
100100
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.Debug|x86.ActiveCfg = Debug|Any CPU
101101
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.Debug|x86.Build.0 = Debug|Any CPU
102-
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.PreRelease|Any CPU.ActiveCfg = Debug|Any CPU
103-
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.PreRelease|Any CPU.Build.0 = Debug|Any CPU
102+
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.PreRelease|Any CPU.ActiveCfg = Release|Any CPU
103+
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.PreRelease|Any CPU.Build.0 = Release|Any CPU
104104
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.PreRelease|ARM.ActiveCfg = Release|Any CPU
105105
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.PreRelease|ARM.Build.0 = Release|Any CPU
106106
{CBDB1D25-5A95-43D4-A958-BF6AE65C6C3F}.PreRelease|ARM64.ActiveCfg = Release|Any CPU
@@ -129,8 +129,8 @@ Global
129129
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.Debug|x64.Build.0 = Debug|Any CPU
130130
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.Debug|x86.ActiveCfg = Debug|Any CPU
131131
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.Debug|x86.Build.0 = Debug|Any CPU
132-
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.PreRelease|Any CPU.ActiveCfg = Debug|Any CPU
133-
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.PreRelease|Any CPU.Build.0 = Debug|Any CPU
132+
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.PreRelease|Any CPU.ActiveCfg = PreRelease|Any CPU
133+
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.PreRelease|Any CPU.Build.0 = PreRelease|Any CPU
134134
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.PreRelease|ARM.ActiveCfg = Release|Any CPU
135135
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.PreRelease|ARM.Build.0 = Release|Any CPU
136136
{9074CE60-DB9A-4BDF-B851-6EAB8A81F366}.PreRelease|ARM64.ActiveCfg = Release|Any CPU

0 commit comments

Comments
 (0)