Skip to content

Commit 3b84ffa

Browse files
committed
Fixing my own attempt at OOP
- I shouldn't have put the Logger in the Globals module, IMO it belongs in the main client and passed as necessary to other objects. - Remove user data from client library project.
1 parent 14fc4f5 commit 3b84ffa

File tree

7 files changed

+40
-41
lines changed

7 files changed

+40
-41
lines changed

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ Public Class Nut_Socket
126126
Catch Excep As Exception
127127
RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me)
128128
Return False
129-
Finally
130-
131129
End Try
130+
131+
Return False
132132
End Function
133133

134134
Private Function Create_Socket(ByVal Host As String, ByVal Port As Integer) As Boolean

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Public Class UPS_Device
2727
Private Socket_Status As Boolean = False
2828

2929

30-
'Private LogFile As Logger
30+
Private LogFile As Logger
3131
'Private ConnectionStatus As Boolean = False
3232
'Private Server As String
3333
'Private Port As Integer
@@ -107,7 +107,7 @@ Public Class UPS_Device
107107
End Sub
108108

109109
Public Sub New(ByVal Nut_Config As Nut_Parameter, ByRef LogFile As Logger)
110-
' Me.LogFile = LogFile
110+
Me.LogFile = LogFile
111111
Me.Nut_Config = Nut_Config
112112
Me.ciClone = CType(CultureInfo.InvariantCulture.Clone(), CultureInfo)
113113
Me.ciClone.NumberFormat.NumberDecimalSeparator = "."

WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Public Module WinNUT_Globals
3232
Public IsConnected As Boolean
3333
' Public LogFile As String
3434
' Handle application messages and debug events.
35-
Public WithEvents LogFile As Logger ' As New Logger(False, 0)
35+
' Public WithEvents LogFile As Logger ' As New Logger(False, 0)
3636
Public AppIcon As Dictionary(Of Integer, System.Drawing.Icon)
3737
Public StrLog As New List(Of String)
3838
' Public LogFilePath As String

WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Namespace My
2323
Private BtnGenerate As New Button
2424
Private Msg_Crash As New Label
2525
Private Msg_Error As New TextBox
26+
2627
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
2728
e.ExitApplication = False
2829

@@ -81,7 +82,7 @@ Namespace My
8182
End With
8283

8384
AddHandler BtnClose.Click, AddressOf My.Application.Close_Button_Click
84-
AddHandler BtnGenerate.Click, AddressOf My.Application.Generate_Button_Click
85+
AddHandler BtnGenerate.Click, AddressOf Application.Generate_Button_Click
8586
AddHandler CrashBug_Form.FormClosing, AddressOf My.Application.CrashBug_FormClosing
8687

8788
CrashBug_Form.Show()
@@ -122,7 +123,7 @@ Namespace My
122123
Crash_Report &= Msg_Error.Text & vbNewLine & vbNewLine
123124
Crash_Report &= "Last Events :" & vbNewLine
124125

125-
For Each WinNUT_Event In LogFile.LastEvents
126+
For Each WinNUT_Event In WinNUT.LogFile.LastEvents
126127
Crash_Report &= WinNUT_Event & vbNewLine
127128
Next
128129
My.Computer.Clipboard.SetText(Crash_Report)

WinNUT_V2/WinNUT_GUI/Pref_Gui.vb

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@
88
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
99

1010
Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params
11-
Imports Logger = WinNUT_Client_Common.Logger
1211
Imports LogLvl = WinNUT_Client_Common.LogLvl
13-
Imports WinNUT_Client_Common.WinNUT_Globals
1412
Imports System.IO
1513

1614
Public Class Pref_Gui
1715
Private IsShowed As Boolean = False
1816
Private IsSaved As Boolean = False
1917

2018
Private Sub Btn_Cancel_Click(sender As Object, e As EventArgs) Handles Btn_Cancel.Click
21-
LogFile.LogTracing("Close Pref Gui from Button Cancel", LogLvl.LOG_DEBUG, Me)
19+
WinNUT.LogFile.LogTracing("Close Pref Gui from Button Cancel", LogLvl.LOG_DEBUG, Me)
2220
Me.Close()
2321
End Sub
2422

2523
Private Sub Save_Params()
2624
Try
2725
Me.IsSaved = False
28-
LogFile.LogTracing("Save Parameters.", LogLvl.LOG_DEBUG, Me)
26+
WinNUT.LogFile.LogTracing("Save Parameters.", LogLvl.LOG_DEBUG, Me)
2927
WinNUT_Params.Arr_Reg_Key.Item("ServerAddress") = Tb_Server_IP.Text
3028
WinNUT_Params.Arr_Reg_Key.Item("Port") = CInt(Tb_Port.Text)
3129
WinNUT_Params.Arr_Reg_Key.Item("UPSName") = Tb_UPS_Name.Text
@@ -66,19 +64,19 @@ Public Class Pref_Gui
6664
If CB_Start_W_Win.Checked Then
6765
If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", Application.ProductName, Nothing) Is Nothing Then
6866
My.Computer.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
69-
LogFile.LogTracing("WinNUT Added to Startup.", LogLvl.LOG_DEBUG, Me)
67+
WinNUT.LogFile.LogTracing("WinNUT Added to Startup.", LogLvl.LOG_DEBUG, Me)
7068
End If
7169
Else
7270
If Not My.Computer.Registry.GetValue("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", Application.ProductName, Nothing) Is Nothing Then
7371
My.Computer.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)
74-
LogFile.LogTracing("WinNUT Removed From Startup.", LogLvl.LOG_DEBUG, Me)
72+
WinNUT.LogFile.LogTracing("WinNUT Removed From Startup.", LogLvl.LOG_DEBUG, Me)
7573
End If
7674
End If
7775

78-
LogFile.LogLevel = Cbx_LogLevel.SelectedIndex
79-
LogFile.IsWritingToFile = CB_Use_Logfile.Checked
76+
WinNUT.LogFile.LogLevel = Cbx_LogLevel.SelectedIndex
77+
WinNUT.LogFile.IsWritingToFile = CB_Use_Logfile.Checked
8078

81-
LogFile.LogTracing("Pref_Gui Params Saved", 1, Me)
79+
WinNUT.LogFile.LogTracing("Pref_Gui Params Saved", 1, Me)
8280

8381
SetLogControlsStatus()
8482
WinNUT.WinNUT_PrefsChanged()
@@ -184,11 +182,11 @@ Public Class Pref_Gui
184182
Next
185183
Me.Btn_Apply.Enabled = False
186184
Me.IsShowed = True
187-
LogFile.LogTracing("Pref Gui Opened.", LogLvl.LOG_DEBUG, Me)
185+
WinNUT.LogFile.LogTracing("Pref Gui Opened.", LogLvl.LOG_DEBUG, Me)
188186
Catch Except As Exception
189187
Me.IsShowed = False
190188
Me.Close()
191-
LogFile.LogTracing("Error on Opening Pref_Gui.", LogLvl.LOG_ERROR, Me)
189+
WinNUT.LogFile.LogTracing("Error on Opening Pref_Gui.", LogLvl.LOG_ERROR, Me)
192190
End Try
193191
End Sub
194192

@@ -238,7 +236,7 @@ Public Class Pref_Gui
238236
Dim Result As Object = 0
239237
Dim MinValue, MaxValue As Integer
240238

241-
LogFile.LogTracing(String.Format("Check that the value of {0} for {1} is correct.", sender.Text, sender.Name), LogLvl.LOG_DEBUG, Me)
239+
WinNUT.LogFile.LogTracing(String.Format("Check that the value of {0} for {1} is correct.", sender.Text, sender.Name), LogLvl.LOG_DEBUG, Me)
242240
Select Case sender.Name
243241
Case "Tb_Delay_Com"
244242
MinValue = 0
@@ -267,10 +265,10 @@ Public Class Pref_Gui
267265

268266
If Integer.TryParse(sender.Text, Result) Then
269267
If (Result >= MinValue And Result <= MaxValue) Then
270-
LogFile.LogTracing(String.Format("Value of {0} for {1} is valid.", Result, sender.Name), LogLvl.LOG_DEBUG, Me)
268+
WinNUT.LogFile.LogTracing(String.Format("Value of {0} for {1} is valid.", Result, sender.Name), LogLvl.LOG_DEBUG, Me)
271269
sender.BackColor = Color.White
272270
Else
273-
LogFile.LogTracing(String.Format("Value of {0} for {1} is invalid.", Result, sender.Name), LogLvl.LOG_ERROR, Me)
271+
WinNUT.LogFile.LogTracing(String.Format("Value of {0} for {1} is invalid.", Result, sender.Name), LogLvl.LOG_ERROR, Me)
274272
e.Cancel = True
275273
sender.BackColor = Color.Red
276274
End If
@@ -281,34 +279,34 @@ Public Class Pref_Gui
281279
End If
282280
End Sub
283281
Private Sub Correct_IP_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Tb_Server_IP.Validating
284-
LogFile.LogTracing("Check that the Nut Host address is valid.", LogLvl.LOG_DEBUG, Me)
282+
WinNUT.LogFile.LogTracing("Check that the Nut Host address is valid.", LogLvl.LOG_DEBUG, Me)
285283
Dim Pattern As String
286284
Dim StrTest As String = sender.Text
287285
Dim Is_Correct As Boolean = False
288286
'Test IPV4
289287
Pattern = "^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$"
290288
If System.Text.RegularExpressions.Regex.IsMatch(sender.Text, Pattern) Then
291289
Is_Correct = True
292-
LogFile.LogTracing("The Nut Host address is a valid IPV4 address.", LogLvl.LOG_WARNING, Me)
290+
WinNUT.LogFile.LogTracing("The Nut Host address is a valid IPV4 address.", LogLvl.LOG_WARNING, Me)
293291
End If
294292
'Test IPV6
295293
Pattern = "^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$"
296294
If (System.Text.RegularExpressions.Regex.IsMatch(sender.Text, Pattern) And Not Is_Correct) Then
297295
Is_Correct = True
298-
LogFile.LogTracing("The Nut Host address is a valid IPV6 address.", LogLvl.LOG_WARNING, Me)
296+
WinNUT.LogFile.LogTracing("The Nut Host address is a valid IPV6 address.", LogLvl.LOG_WARNING, Me)
299297
End If
300298
'Test fqdn
301299
Pattern = "^(?:(?!\d+\.|-)[a-zA-Z0-9_\-]{1,63}(?<!-)\.?)+(?:[a-zA-Z]{2,})$"
302300
If (System.Text.RegularExpressions.Regex.IsMatch(sender.Text, Pattern) And Not Is_Correct) Then
303301
Is_Correct = True
304-
LogFile.LogTracing("The Nut Host address is a valid FQDN address.", LogLvl.LOG_WARNING, Me)
302+
WinNUT.LogFile.LogTracing("The Nut Host address is a valid FQDN address.", LogLvl.LOG_WARNING, Me)
305303
End If
306304

307305
'Result
308306
If Is_Correct Then
309307
sender.BackColor = Color.White
310308
Else
311-
LogFile.LogTracing("The Nut Host address is a invalid", LogLvl.LOG_ERROR, Me)
309+
WinNUT.LogFile.LogTracing("The Nut Host address is a invalid", LogLvl.LOG_ERROR, Me)
312310
e.Cancel = True
313311
sender.BackColor = Color.Red
314312
End If
@@ -324,32 +322,32 @@ Public Class Pref_Gui
324322
End Sub
325323

326324
Private Sub Btn_DeleteLog_Click(sender As Object, e As EventArgs) Handles Btn_DeleteLog.Click
327-
LogFile.LogTracing("Delete LogFile", LogLvl.LOG_DEBUG, Me)
325+
WinNUT.LogFile.LogTracing("Delete LogFile", LogLvl.LOG_DEBUG, Me)
328326

329-
If LogFile.DeleteLogFile() Then
330-
LogFile.LogTracing("LogFile Deleted", LogLvl.LOG_DEBUG, Me)
327+
If WinNUT.LogFile.DeleteLogFile() Then
328+
WinNUT.LogFile.LogTracing("LogFile Deleted", LogLvl.LOG_DEBUG, Me)
331329
Else
332-
LogFile.LogTracing("Error deleting log file.", LogLvl.LOG_WARNING, Me)
330+
WinNUT.LogFile.LogTracing("Error deleting log file.", LogLvl.LOG_WARNING, Me)
333331
End If
334332

335-
LogFile.IsWritingToFile = WinNUT_Params.Arr_Reg_Key.Item("UseLogFile")
333+
WinNUT.LogFile.IsWritingToFile = WinNUT_Params.Arr_Reg_Key.Item("UseLogFile")
336334
SetLogControlsStatus()
337335
End Sub
338336

339337
Private Sub Btn_ViewLog_Click(sender As Object, e As EventArgs) Handles Btn_ViewLog.Click
340-
LogFile.LogTracing("Show LogFile", LogLvl.LOG_DEBUG, Me)
341-
If File.Exists(LogFile.LogFileLocation) Then
342-
Process.Start(LogFile.LogFileLocation)
338+
WinNUT.LogFile.LogTracing("Show LogFile", LogLvl.LOG_DEBUG, Me)
339+
If File.Exists(WinNUT.LogFile.LogFileLocation) Then
340+
Process.Start(WinNUT.LogFile.LogFileLocation)
343341
Else
344-
LogFile.LogTracing("LogFile does not exists", LogLvl.LOG_WARNING, Me)
342+
WinNUT.LogFile.LogTracing("LogFile does not exists", LogLvl.LOG_WARNING, Me)
345343
Btn_ViewLog.Enabled = False
346344
Btn_DeleteLog.Enabled = False
347345
End If
348346
End Sub
349347

350348
Private Sub Pref_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load
351349
Me.Icon = WinNUT.Icon
352-
LogFile.LogTracing("Load Pref Gui", LogLvl.LOG_DEBUG, Me)
350+
WinNUT.LogFile.LogTracing("Load Pref Gui", LogLvl.LOG_DEBUG, Me)
353351
End Sub
354352

355353
Private Sub Event_Ctrl_Value_Changed(sender As Object, e As EventArgs)
@@ -371,6 +369,6 @@ Public Class Pref_Gui
371369
Btn_DeleteLog.Enabled = False
372370
End If
373371

374-
LogFile.LogTracing("Setting LogControl statuses.", LogLvl.LOG_DEBUG, Me)
372+
WinNUT.LogFile.LogTracing("Setting LogControl statuses.", LogLvl.LOG_DEBUG, Me)
375373
End Sub
376374
End Class

WinNUT_V2/WinNUT_GUI/WinNUT-client.vbproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1515
<IsWebBootstrapper>false</IsWebBootstrapper>
1616
<TargetFrameworkProfile />
17-
<PublishUrl>C:\Users\Nicolas\OneDrive\Documents\GitHub\WinNUT-Client\Releases\</PublishUrl>
17+
<PublishUrl></PublishUrl>
1818
<Install>true</Install>
1919
<InstallFrom>Disk</InstallFrom>
2020
<UpdateEnabled>false</UpdateEnabled>

WinNUT_V2/WinNUT_GUI/WinNUT.vb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Public Class WinNUT
1414

1515
#End Region
1616
' Logging
17-
Private WithEvents ClientLogger As Logger
17+
Public Shared WithEvents LogFile As Logger
1818

1919
'Object for UPS management
2020
Public WithEvents UPS_Device As UPS_Device
@@ -158,7 +158,7 @@ Public Class WinNUT
158158
' Setup logging preferences
159159
LogFile.LogLevel = Arr_Reg_Key.Item("Log Level")
160160
LogFile.IsWritingToFile = Arr_Reg_Key.Item("UseLogFile")
161-
ClientLogger = LogFile
161+
162162
LogFile.LogTracing("Logging is configured.", LogLvl.LOG_DEBUG, Me)
163163

164164
'Init Systray
@@ -1011,7 +1011,7 @@ Public Class WinNUT
10111011
HasFocus = False
10121012
End Sub
10131013

1014-
Public Shared Sub Update_InstantLog(ByVal sender As System.Object) Handles ClientLogger.NewData
1014+
Public Shared Sub Update_InstantLog(sender As Object) Handles LogFile.NewData
10151015
Dim Message As String = LogFile.CurrentLogData
10161016
Static Dim Event_Id = 1
10171017
LogFile.LogTracing("New Log to CB_Current Log : " & Message, LogLvl.LOG_DEBUG, sender.ToString)

0 commit comments

Comments
 (0)