Skip to content

Commit 14fc4f5

Browse files
committed
Merge branch 'Dev-2.1' into #4-suspend-resume-dbg
2 parents 1a94ed2 + fc6fc00 commit 14fc4f5

File tree

13 files changed

+227
-124
lines changed

13 files changed

+227
-124
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ It will probably be necessary to allow the WinNUT-Client IP to communicate with
2020
*See issue 47 for more information, specifically [this commentary](https://github.com/gawindx/WinNUT-Client/issues/47#issuecomment-759180793).*
2121

2222
###
23-
## Translation
23+
## Contributing
24+
### Translation
2425
WinNUT-Client V2 is natively multilingual, so it is no longer necessary to select your language from the software interface.
2526
Currently, WinNUT-Client supports:
2627
- English
@@ -57,6 +58,10 @@ Currently, WinNUT-Client supports:
5758

5859
Your translation / correction will be added on a new version and will thus be available to the entire community.
5960

61+
### Code
62+
#### Development Environment Setup
63+
This project is built for **.NET Framework 4.7.2**, which is supported up to **Visual Studio 2019**. If you want to compile an installer, you will need the [Microsoft Visual Studio Installer Projects](https://marketplace.visualstudio.com/items?itemName=visualstudioclient.MicrosoftVisualStudio2017InstallerProjects) extension installed.
64+
6065
## Update WinNUT-Client
6166

6267
Since version 1.8.0.0, WinNUT-Client includes a process for checking for updates.

WinNUT_V2/WinNUT-Client_Common/Common_Enums.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Public Enum NUTResponse
100100
End Enum
101101

102102
Public Enum Nut_Exception_Value
103-
<StringValue("Unable to create connection :")>
103+
<StringValue("Unable to create connection: ")>
104104
CONNECT_ERROR
105105
<StringValue("Invalid Username.")>
106106
INVALID_USERNAME

WinNUT_V2/WinNUT-Client_Common/Logger.vb

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

10+
Imports System.IO
11+
1012
Public Class Logger
11-
Private ReadOnly LogFile As New Microsoft.VisualBasic.Logging.FileLogTraceListener()
13+
Private Const BaseFileName = "WinNUT-CLient"
14+
' Logs will be stored in the program's appdata folder, in a Logs subdirectory.
15+
Public Shared ReadOnly LogFolder = Path.Combine(ApplicationData, "Logs")
16+
17+
Private LogFile As Logging.FileLogTraceListener
1218
Private ReadOnly TEventCache As New TraceEventCache()
13-
' Enable writing to a log file.
14-
Public WriteLogValue As Boolean
1519
Public LogLevelValue As LogLvl
1620
Private L_CurrentLogData As String
1721
Private LastEventsList As New List(Of Object)
1822
Public Event NewData(ByVal sender As Object)
1923

24+
#Region "Properties"
25+
2026
Public Property CurrentLogData() As String
2127
Get
2228
Dim Tmp_Data = Me.L_CurrentLogData
@@ -32,32 +38,38 @@ Public Class Logger
3238
Return Me.LastEventsList
3339
End Get
3440
End Property
35-
Public Sub New(ByVal WriteLog As Boolean, ByVal LogLevel As LogLvl)
36-
Me.WriteLogValue = WriteLog
37-
Me.LogLevelValue = LogLevel
38-
Me.LogFile.TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId
39-
Me.LogFile.Append = True
40-
Me.LogFile.AutoFlush = True
41-
Me.LogFile.BaseFileName = "WinNUT-CLient"
42-
Me.LogFile.LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily
43-
Me.LogFile.Location = Microsoft.VisualBasic.Logging.LogFileLocation.Custom
44-
Me.LogFile.CustomLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client"
45-
Me.LastEventsList.Capacity = 50
46-
WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName
47-
End Sub
4841

49-
Public Property WriteLog() As Boolean
42+
''' <summary>
43+
''' Returns if data is being written to a file. Also allows for file logging to be setup or stopped.
44+
''' </summary>
45+
''' <returns>True when the <see cref="LogFile"/> object is instantiated, false if not.</returns>
46+
Public Property IsWritingToFile() As Boolean
5047
Get
51-
Return Me.WriteLogValue
48+
Return Not (LogFile Is Nothing)
5249
End Get
53-
Set(ByVal Value As Boolean)
54-
Me.WriteLogValue = Value
55-
If Not Me.WriteLogValue Then
50+
51+
Set(Value As Boolean)
52+
If Value = False And LogFile IsNot Nothing Then
53+
LogFile.Close()
5654
LogFile.Dispose()
55+
LogFile = Nothing
56+
LogTracing("Logging to file has been disabled.", LogLvl.LOG_NOTICE, Me)
57+
ElseIf Value Then
58+
SetupLogfile()
5759
End If
5860
End Set
5961
End Property
6062

63+
Public ReadOnly Property LogFileLocation() As String
64+
Get
65+
If IsWritingToFile Then
66+
Return LogFile.FullLogFileName
67+
Else
68+
Return String.Empty
69+
End If
70+
End Get
71+
End Property
72+
6173
Public Property LogLevel() As LogLvl
6274
Get
6375
Return Me.LogLevelValue
@@ -67,18 +79,62 @@ Public Class Logger
6779
End Set
6880
End Property
6981

82+
#End Region
83+
84+
Public Sub New(WriteLog As Boolean, LogLevel As LogLvl)
85+
IsWritingToFile = WriteLog
86+
LogLevelValue = LogLevel
87+
LastEventsList.Capacity = 50
88+
End Sub
89+
90+
Public Sub SetupLogfile()
91+
LogFile = New Logging.FileLogTraceListener(BaseFileName) With {
92+
.TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId,
93+
.Append = True,
94+
.AutoFlush = True,
95+
.LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily,
96+
.CustomLocation = LogFolder,
97+
.Location = Logging.LogFileLocation.Custom
98+
}
99+
100+
LogTracing("Log file is initialized at " & LogFile.FullLogFileName, LogLvl.LOG_NOTICE, Me)
101+
End Sub
102+
103+
Public Function DeleteLogFile() As Boolean
104+
Try
105+
Dim fileLocation = LogFile.FullLogFileName
106+
IsWritingToFile = False
107+
File.Delete(fileLocation)
108+
Return True
109+
Catch ex As Exception
110+
Return False
111+
End Try
112+
End Function
113+
114+
''' <summary>
115+
''' Write the <paramref name="message"/> to the Debug tracer is debugging, into the <see cref="LastEventsList" />
116+
''' for report generating, to the <see cref="LogFile"/> if appropriate, and notify any listeners if
117+
''' <paramref name="LogToDisplay"/> is specified.
118+
''' </summary>
119+
''' <param name="message">The raw information that needs to be recorded.</param>
120+
''' <param name="LvlError">How important the information is.</param>
121+
''' <param name="sender"></param>
122+
''' <param name="LogToDisplay">A user-friendly, translated string to be shown.</param>
70123
Public Sub LogTracing(ByVal message As String, ByVal LvlError As Int16, sender As Object, Optional ByVal LogToDisplay As String = Nothing)
71124
Dim Pid = TEventCache.ProcessId
72125
Dim SenderName = sender.GetType.Name
73126
Dim EventTime = Now.ToLocalTime
74127
Dim FinalMsg = EventTime & " Pid: " & Pid & " " & SenderName & " : " & message
75128

76129
'Update LogFilePath to make sure it's still the correct path
77-
WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName
130+
' gbakeman 31/7/2022: Disabling since the LogFilePath should never change throughout the lifetime of this
131+
' object, unless proper initialization has occured.
132+
133+
' WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName
78134

79135
' Always write log messages to the attached debug messages window.
80136
#If DEBUG Then
81-
Debug.WriteLine(FinalMsg)
137+
Debug.WriteLine(FinalMsg)
82138
#End If
83139

84140
'Create Event in EventList in case of crash for generate Report
@@ -88,7 +144,7 @@ Public Class Logger
88144

89145
Me.LastEventsList.Add(FinalMsg)
90146

91-
If Me.WriteLogValue AndAlso Me.LogLevel >= LvlError Then
147+
If IsWritingToFile AndAlso Me.LogLevel >= LvlError Then
92148
LogFile.WriteLine(FinalMsg)
93149
End If
94150
'If LvlError = LogLvl.LOG_NOTICE Then

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Public Class Nut_Socket
3838
Public Event OnNotice(Message As String, NoticeLvl As LogLvl, sender As Object)
3939
'Public Event OnError(Excep As Exception, NoticeLvl As LogLvl, sender As Object, ReportToGui As Boolean)
4040
Public Event OnError(Excep As Exception, NoticeLvl As LogLvl, sender As Object)
41+
Public Event OnNUTException(ex As Nut_Exception, NoticeLvl As LogLvl, sender As Object)
4142

4243
Public Event Unknown_UPS()
4344
Public Event Socket_Broken()
@@ -94,10 +95,11 @@ Public Class Nut_Socket
9495
Dim Port = Me.Nut_Config.Port
9596
Dim Login = Me.Nut_Config.Login
9697
Dim Password = Me.Nut_Config.Password
97-
Dim Response As Boolean = False
98+
9899
If Not String.IsNullOrEmpty(Host) And Not IsNothing(Port) Then
99100
If Not Create_Socket(Host, Port) Then
100-
Throw New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR)
101+
' Don't duplicate/override Create_Socket's error throwing functionality.
102+
' Throw New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR)
101103
Disconnect()
102104
Else
103105
If Not AuthLogin(Login, Password) Then
@@ -113,15 +115,22 @@ Public Class Nut_Socket
113115
If Nut_Query.Response = NUTResponse.OK Then
114116
Me.Net_Ver = Nut_Query.Data
115117
End If
116-
Response = True
118+
Return True
117119
End If
118120
End If
119-
Return Response
121+
122+
Catch nutEx As Nut_Exception
123+
' Handle NUT exceptions specifically, without variable boxing/unboxing
124+
RaiseEvent OnNUTException(nutEx, LogLvl.LOG_ERROR, Me)
125+
Return False
120126
Catch Excep As Exception
121127
RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me)
122128
Return False
129+
Finally
130+
123131
End Try
124132
End Function
133+
125134
Private Function Create_Socket(ByVal Host As String, ByVal Port As Integer) As Boolean
126135
Try
127136
Me.NutSocket = New Socket(AddressFamily.InterNetwork, ProtocolType.IP)
@@ -131,7 +140,7 @@ Public Class Nut_Socket
131140
Me.WriterStream = New StreamWriter(NutStream)
132141
Me.ConnectionStatus = True
133142
Catch Excep As Exception
134-
RaiseEvent OnError(New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR, Excep.Message), LogLvl.LOG_ERROR, Me)
143+
RaiseEvent OnNUTException(New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR, Excep.Message), LogLvl.LOG_ERROR, Me)
135144
Me.ConnectionStatus = False
136145
End Try
137146
Return Me.ConnectionStatus

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Imports System.Globalization
1111
Public Class UPS_Device
1212
'Private Nut_Conn As Nut_Comm
13-
Private LogFile As Logger
13+
' Private LogFile As Logger
1414
Private Freq_Fallback As Double
1515
Private ciClone As System.Globalization.CultureInfo
1616
Private Const CosPhi As Double = 0.6
@@ -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 = "."
@@ -410,4 +410,8 @@ Public Class UPS_Device
410410
RaiseEvent Deconnected()
411411
End If
412412
End Sub
413+
414+
Private Sub NUTSocketError(nutEx As Nut_Exception, NoticeLvl As LogLvl, sender As Object) Handles Nut_Socket.OnNUTException
415+
LogFile.LogTracing("[" & Nut_Config.UPSName & "] " & nutEx.ToString(), LogLvl.LOG_WARNING, Nut_Socket)
416+
End Sub
413417
End Class

WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb

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

10+
Imports System.IO
11+
1012
Public Module WinNUT_Globals
13+
#Region "Properties"
14+
' The directory where volatile appdata is stored.
15+
ReadOnly Property ApplicationData() As String
16+
Get
17+
#If DEBUG Then
18+
' If debugging, keep any generated data next to the debug executable.
19+
Return Path.Combine(Environment.CurrentDirectory, "Data")
20+
#End If
21+
Return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\WinNUT-Client")
22+
End Get
23+
End Property
24+
#End Region
25+
1126
Public LongProgramName As String
1227
Public ProgramName As String
1328
Public ProgramVersion As String
1429
Public ShortProgramVersion As String
1530
Public GitHubURL As String
1631
Public Copyright As String
17-
Public Directory_AppData As String
1832
Public IsConnected As Boolean
19-
Public LogFile As String
33+
' Public LogFile As String
34+
' Handle application messages and debug events.
35+
Public WithEvents LogFile As Logger ' As New Logger(False, 0)
2036
Public AppIcon As Dictionary(Of Integer, System.Drawing.Icon)
2137
Public StrLog As New List(Of String)
22-
Public LogFilePath As String
38+
' Public LogFilePath As String
39+
2340
Public Sub Init_Globals()
2441
LongProgramName = My.Application.Info.Description
2542
ProgramName = My.Application.Info.ProductName
2643
ProgramVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString
2744
ShortProgramVersion = ProgramVersion.Substring(0, ProgramVersion.IndexOf(".", ProgramVersion.IndexOf(".") + 1))
2845
GitHubURL = My.Application.Info.Trademark
2946
Copyright = My.Application.Info.Copyright
30-
Directory_AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client"
31-
32-
If Not System.IO.Directory.Exists(Directory_AppData) Then
33-
My.Computer.FileSystem.CreateDirectory(Directory_AppData)
34-
End If
3547
IsConnected = False
3648

3749
'Add Main Gui's Strings
@@ -79,4 +91,14 @@ Public Module WinNUT_Globals
7991
'StrLog.Insert(AppResxStr.STR_LOG_NUT_FSD, Resources.Log_Str_12)
8092
End Sub
8193

94+
'Sub SetupAppDirectory()
95+
' If Not Directory.Exists(ApplicationData) Then
96+
' Try
97+
' Directory.CreateDirectory(ApplicationData)
98+
' Catch ex As Exception
99+
' Logger.LogTracing("Could not create application directory! Operating with reduced functionality.\n\n" & ex.ToString(),
100+
' LogLvl.LOG_ERROR, Nothing)
101+
' End Try
102+
' End If
103+
'End Sub
82104
End Module

WinNUT_V2/WinNUT_GUI/About_Gui.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Public Class About_Gui
1919
Lbl_Copyright_2019.Text = Strings.Replace(WinNUT_Globals.Copyright, "©", vbNewLine & "©")
2020
LkLbl_Github.Text = WinNUT_Globals.GitHubURL
2121
Me.Icon = WinNUT.Icon
22-
Me.LogFile = WinNUT.LogFile
22+
' Me.LogFile = WinNUT.LogFile
2323
LogFile.LogTracing("Load About Gui", LogLvl.LOG_DEBUG, Me)
2424
End Sub
2525

WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'
88
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
99

10+
Imports WinNUT_Client_Common
1011
Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params
1112

1213
Namespace My
@@ -121,7 +122,7 @@ Namespace My
121122
Crash_Report &= Msg_Error.Text & vbNewLine & vbNewLine
122123
Crash_Report &= "Last Events :" & vbNewLine
123124

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

WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Public Class List_Var_Gui
1616
Private LogFile As Logger
1717
Private UPS_Name = WinNUT.Nut_Config.UPSName
1818
Private Sub List_Var_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load
19-
Me.LogFile = WinNUT.LogFile
19+
' Me.LogFile = WinNUT.LogFile
2020
LogFile.LogTracing("Load List Var Gui", LogLvl.LOG_DEBUG, Me)
2121
Me.Icon = WinNUT.Icon
2222
Me.Visible = False

0 commit comments

Comments
 (0)