diff --git a/WinNUT_V2/WinNUT-Client_Common/Common_Enums.vb b/WinNUT_V2/WinNUT-Client_Common/Common_Enums.vb index fb2be31..183e49a 100644 --- a/WinNUT_V2/WinNUT-Client_Common/Common_Enums.vb +++ b/WinNUT_V2/WinNUT-Client_Common/Common_Enums.vb @@ -100,7 +100,7 @@ Public Enum NUTResponse End Enum Public Enum Nut_Exception_Value - + CONNECT_ERROR INVALID_USERNAME diff --git a/WinNUT_V2/WinNUT-Client_Common/Logger.vb b/WinNUT_V2/WinNUT-Client_Common/Logger.vb index 92a95fc..a1f2f28 100644 --- a/WinNUT_V2/WinNUT-Client_Common/Logger.vb +++ b/WinNUT_V2/WinNUT-Client_Common/Logger.vb @@ -7,16 +7,22 @@ ' ' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY +Imports System.IO + Public Class Logger - Private ReadOnly LogFile As New Microsoft.VisualBasic.Logging.FileLogTraceListener() + Private Const BaseFileName = "WinNUT-CLient" + ' Logs will be stored in the program's appdata folder, in a Logs subdirectory. + Public Shared ReadOnly LogFolder = Path.Combine(ApplicationData, "Logs") + + Private LogFile As Logging.FileLogTraceListener Private ReadOnly TEventCache As New TraceEventCache() - ' Enable writing to a log file. - Public WriteLogValue As Boolean Public LogLevelValue As LogLvl Private L_CurrentLogData As String Private LastEventsList As New List(Of Object) Public Event NewData(ByVal sender As Object) +#Region "Properties" + Public Property CurrentLogData() As String Get Dim Tmp_Data = Me.L_CurrentLogData @@ -32,32 +38,38 @@ Public Class Logger Return Me.LastEventsList End Get End Property - Public Sub New(ByVal WriteLog As Boolean, ByVal LogLevel As LogLvl) - Me.WriteLogValue = WriteLog - Me.LogLevelValue = LogLevel - Me.LogFile.TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId - Me.LogFile.Append = True - Me.LogFile.AutoFlush = True - Me.LogFile.BaseFileName = "WinNUT-CLient" - Me.LogFile.LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily - Me.LogFile.Location = Microsoft.VisualBasic.Logging.LogFileLocation.Custom - Me.LogFile.CustomLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client" - Me.LastEventsList.Capacity = 50 - WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName - End Sub - Public Property WriteLog() As Boolean + ''' + ''' Returns if data is being written to a file. Also allows for file logging to be setup or stopped. + ''' + ''' True when the object is instantiated, false if not. + Public Property IsWritingToFile() As Boolean Get - Return Me.WriteLogValue + Return Not (LogFile Is Nothing) End Get - Set(ByVal Value As Boolean) - Me.WriteLogValue = Value - If Not Me.WriteLogValue Then + + Set(Value As Boolean) + If Value = False And LogFile IsNot Nothing Then + LogFile.Close() LogFile.Dispose() + LogFile = Nothing + LogTracing("Logging to file has been disabled.", LogLvl.LOG_NOTICE, Me) + ElseIf Value Then + SetupLogfile() End If End Set End Property + Public ReadOnly Property LogFileLocation() As String + Get + If IsWritingToFile Then + Return LogFile.FullLogFileName + Else + Return String.Empty + End If + End Get + End Property + Public Property LogLevel() As LogLvl Get Return Me.LogLevelValue @@ -67,6 +79,47 @@ Public Class Logger End Set End Property +#End Region + + Public Sub New(WriteLog As Boolean, LogLevel As LogLvl) + IsWritingToFile = WriteLog + LogLevelValue = LogLevel + LastEventsList.Capacity = 50 + End Sub + + Public Sub SetupLogfile() + LogFile = New Logging.FileLogTraceListener(BaseFileName) With { + .TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId, + .Append = True, + .AutoFlush = True, + .LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily, + .CustomLocation = LogFolder, + .Location = Logging.LogFileLocation.Custom + } + + LogTracing("Log file is initialized at " & LogFile.FullLogFileName, LogLvl.LOG_NOTICE, Me) + End Sub + + Public Function DeleteLogFile() As Boolean + Try + Dim fileLocation = LogFile.FullLogFileName + IsWritingToFile = False + File.Delete(fileLocation) + Return True + Catch ex As Exception + Return False + End Try + End Function + + ''' + ''' Write the to the Debug tracer is debugging, into the + ''' for report generating, to the if appropriate, and notify any listeners if + ''' is specified. + ''' + ''' The raw information that needs to be recorded. + ''' How important the information is. + ''' + ''' A user-friendly, translated string to be shown. Public Sub LogTracing(ByVal message As String, ByVal LvlError As Int16, sender As Object, Optional ByVal LogToDisplay As String = Nothing) Dim Pid = TEventCache.ProcessId Dim SenderName = sender.GetType.Name @@ -74,11 +127,14 @@ Public Class Logger Dim FinalMsg = EventTime & " Pid: " & Pid & " " & SenderName & " : " & message 'Update LogFilePath to make sure it's still the correct path - WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName + ' gbakeman 31/7/2022: Disabling since the LogFilePath should never change throughout the lifetime of this + ' object, unless proper initialization has occured. + + ' WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName ' Always write log messages to the attached debug messages window. #If DEBUG Then - Debug.WriteLine(FinalMsg) + Debug.WriteLine(FinalMsg) #End If 'Create Event in EventList in case of crash for generate Report @@ -88,7 +144,7 @@ Public Class Logger Me.LastEventsList.Add(FinalMsg) - If Me.WriteLogValue AndAlso Me.LogLevel >= LvlError Then + If IsWritingToFile AndAlso Me.LogLevel >= LvlError Then LogFile.WriteLine(FinalMsg) End If 'If LvlError = LogLvl.LOG_NOTICE Then diff --git a/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb b/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb index 8a0b3d4..c9c3f11 100644 --- a/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb +++ b/WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb @@ -38,6 +38,7 @@ Public Class Nut_Socket Public Event OnNotice(Message As String, NoticeLvl As LogLvl, sender As Object) 'Public Event OnError(Excep As Exception, NoticeLvl As LogLvl, sender As Object, ReportToGui As Boolean) Public Event OnError(Excep As Exception, NoticeLvl As LogLvl, sender As Object) + Public Event OnNUTException(ex As Nut_Exception, NoticeLvl As LogLvl, sender As Object) Public Event Unknown_UPS() Public Event Socket_Broken() @@ -94,10 +95,11 @@ Public Class Nut_Socket Dim Port = Me.Nut_Config.Port Dim Login = Me.Nut_Config.Login Dim Password = Me.Nut_Config.Password - Dim Response As Boolean = False + If Not String.IsNullOrEmpty(Host) And Not IsNothing(Port) Then If Not Create_Socket(Host, Port) Then - Throw New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR) + ' Don't duplicate/override Create_Socket's error throwing functionality. + ' Throw New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR) Disconnect() Else If Not AuthLogin(Login, Password) Then @@ -113,15 +115,22 @@ Public Class Nut_Socket If Nut_Query.Response = NUTResponse.OK Then Me.Net_Ver = Nut_Query.Data End If - Response = True + Return True End If End If - Return Response + + Catch nutEx As Nut_Exception + ' Handle NUT exceptions specifically, without variable boxing/unboxing + RaiseEvent OnNUTException(nutEx, LogLvl.LOG_ERROR, Me) + Return False Catch Excep As Exception RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me) Return False + Finally + End Try End Function + Private Function Create_Socket(ByVal Host As String, ByVal Port As Integer) As Boolean Try Me.NutSocket = New Socket(AddressFamily.InterNetwork, ProtocolType.IP) @@ -131,7 +140,7 @@ Public Class Nut_Socket Me.WriterStream = New StreamWriter(NutStream) Me.ConnectionStatus = True Catch Excep As Exception - RaiseEvent OnError(New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR, Excep.Message), LogLvl.LOG_ERROR, Me) + RaiseEvent OnNUTException(New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR, Excep.Message), LogLvl.LOG_ERROR, Me) Me.ConnectionStatus = False End Try Return Me.ConnectionStatus diff --git a/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb b/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb index 02c305a..9277c6a 100644 --- a/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb +++ b/WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb @@ -10,7 +10,7 @@ Imports System.Globalization Public Class UPS_Device 'Private Nut_Conn As Nut_Comm - Private LogFile As Logger + ' Private LogFile As Logger Private Freq_Fallback As Double Private ciClone As System.Globalization.CultureInfo Private Const CosPhi As Double = 0.6 @@ -107,7 +107,7 @@ Public Class UPS_Device End Sub Public Sub New(ByVal Nut_Config As Nut_Parameter, ByRef LogFile As Logger) - Me.LogFile = LogFile + ' Me.LogFile = LogFile Me.Nut_Config = Nut_Config Me.ciClone = CType(CultureInfo.InvariantCulture.Clone(), CultureInfo) Me.ciClone.NumberFormat.NumberDecimalSeparator = "." @@ -410,4 +410,8 @@ Public Class UPS_Device RaiseEvent Deconnected() End If End Sub + + Private Sub NUTSocketError(nutEx As Nut_Exception, NoticeLvl As LogLvl, sender As Object) Handles Nut_Socket.OnNUTException + LogFile.LogTracing("[" & Nut_Config.UPSName & "] " & nutEx.ToString(), LogLvl.LOG_WARNING, Nut_Socket) + End Sub End Class diff --git a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb index 66ed6e3..246d749 100644 --- a/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb +++ b/WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb @@ -7,19 +7,36 @@ ' ' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY +Imports System.IO + Public Module WinNUT_Globals +#Region "Properties" + ' The directory where volatile appdata is stored. + ReadOnly Property ApplicationData() As String + Get +#If DEBUG Then + ' If debugging, keep any generated data next to the debug executable. + Return Path.Combine(Environment.CurrentDirectory, "Data") +#End If + Return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\WinNUT-Client") + End Get + End Property +#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 Directory_AppData As String Public IsConnected As Boolean - Public LogFile As String + ' Public LogFile As String + ' Handle application messages and debug events. + Public WithEvents LogFile As Logger ' As New Logger(False, 0) Public AppIcon As Dictionary(Of Integer, System.Drawing.Icon) Public StrLog As New List(Of String) - Public LogFilePath As String + ' Public LogFilePath As String + Public Sub Init_Globals() LongProgramName = My.Application.Info.Description ProgramName = My.Application.Info.ProductName @@ -27,11 +44,6 @@ Public Module WinNUT_Globals ShortProgramVersion = ProgramVersion.Substring(0, ProgramVersion.IndexOf(".", ProgramVersion.IndexOf(".") + 1)) GitHubURL = My.Application.Info.Trademark Copyright = My.Application.Info.Copyright - Directory_AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client" - - If Not System.IO.Directory.Exists(Directory_AppData) Then - My.Computer.FileSystem.CreateDirectory(Directory_AppData) - End If IsConnected = False 'Add Main Gui's Strings @@ -79,4 +91,14 @@ Public Module WinNUT_Globals 'StrLog.Insert(AppResxStr.STR_LOG_NUT_FSD, Resources.Log_Str_12) End Sub + 'Sub SetupAppDirectory() + ' If Not Directory.Exists(ApplicationData) Then + ' Try + ' Directory.CreateDirectory(ApplicationData) + ' Catch ex As Exception + ' Logger.LogTracing("Could not create application directory! Operating with reduced functionality.\n\n" & ex.ToString(), + ' LogLvl.LOG_ERROR, Nothing) + ' End Try + ' End If + 'End Sub End Module diff --git a/WinNUT_V2/WinNUT_GUI/About_Gui.vb b/WinNUT_V2/WinNUT_GUI/About_Gui.vb index f8e6c2f..c9d53f0 100644 --- a/WinNUT_V2/WinNUT_GUI/About_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/About_Gui.vb @@ -19,7 +19,7 @@ Public Class About_Gui Lbl_Copyright_2019.Text = Strings.Replace(WinNUT_Globals.Copyright, "©", vbNewLine & "©") LkLbl_Github.Text = WinNUT_Globals.GitHubURL Me.Icon = WinNUT.Icon - Me.LogFile = WinNUT.LogFile + ' Me.LogFile = WinNUT.LogFile LogFile.LogTracing("Load About Gui", LogLvl.LOG_DEBUG, Me) End Sub diff --git a/WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb b/WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb index 21facbe..be2b1cb 100644 --- a/WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb +++ b/WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb @@ -7,6 +7,7 @@ ' ' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY +Imports WinNUT_Client_Common Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params Namespace My @@ -121,7 +122,7 @@ Namespace My Crash_Report &= Msg_Error.Text & vbNewLine & vbNewLine Crash_Report &= "Last Events :" & vbNewLine - For Each WinNUT_Event In WinNUT.LogFile.LastEvents + For Each WinNUT_Event In LogFile.LastEvents Crash_Report &= WinNUT_Event & vbNewLine Next My.Computer.Clipboard.SetText(Crash_Report) diff --git a/WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb b/WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb index 4ea03d4..79b3f6c 100644 --- a/WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb @@ -16,7 +16,7 @@ Public Class List_Var_Gui Private LogFile As Logger Private UPS_Name = WinNUT.Nut_Config.UPSName Private Sub List_Var_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load - Me.LogFile = WinNUT.LogFile + ' Me.LogFile = WinNUT.LogFile LogFile.LogTracing("Load List Var Gui", LogLvl.LOG_DEBUG, Me) Me.Icon = WinNUT.Icon Me.Visible = False diff --git a/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb b/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb index 0fc9d51..92df120 100644 --- a/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/Pref_Gui.vb @@ -10,11 +10,13 @@ Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params Imports Logger = WinNUT_Client_Common.Logger Imports LogLvl = WinNUT_Client_Common.LogLvl -Imports WinNUT_Globals = WinNUT_Client_Common.WinNUT_Globals +Imports WinNUT_Client_Common.WinNUT_Globals +Imports System.IO + Public Class Pref_Gui Private IsShowed As Boolean = False Private IsSaved As Boolean = False - Private LogFile As Logger + Private Sub Btn_Cancel_Click(sender As Object, e As EventArgs) Handles Btn_Cancel.Click LogFile.LogTracing("Close Pref Gui from Button Cancel", LogLvl.LOG_DEBUG, Me) Me.Close() @@ -72,23 +74,13 @@ Public Class Pref_Gui LogFile.LogTracing("WinNUT Removed From Startup.", LogLvl.LOG_DEBUG, Me) End If End If - If CB_Use_Logfile.Checked Then - WinNUT.LogFile.WriteLog = True - LogFile.LogTracing("LogFile Enabled.", LogLvl.LOG_DEBUG, Me) - Else - WinNUT.LogFile.WriteLog = False - LogFile.LogTracing("LogFile Disabled.", LogLvl.LOG_DEBUG, Me) - End If - WinNUT.LogFile.LogLevel = Cbx_LogLevel.SelectedIndex - WinNUT.LogFile.LogTracing("Pref_Gui Params Saved", 1, Me) - If My.Computer.FileSystem.FileExists(WinNUT_Globals.LogFilePath) Then - Btn_ViewLog.Enabled = True - Btn_DeleteLog.Enabled = True - Else - Btn_ViewLog.Enabled = False - Btn_DeleteLog.Enabled = False - End If + LogFile.LogLevel = Cbx_LogLevel.SelectedIndex + LogFile.IsWritingToFile = CB_Use_Logfile.Checked + + LogFile.LogTracing("Pref_Gui Params Saved", 1, Me) + + SetLogControlsStatus() WinNUT.WinNUT_PrefsChanged() Me.IsSaved = True Catch e As Exception @@ -326,35 +318,28 @@ Public Class Pref_Gui End Sub Private Sub TabControl_Options_Selecting(sender As Object, e As TabControlCancelEventArgs) Handles TabControl_Options.Selecting - If My.Computer.FileSystem.FileExists(WinNUT_Globals.LogFilePath) Then - Btn_ViewLog.Enabled = True - Btn_DeleteLog.Enabled = True - Else - Btn_ViewLog.Enabled = False - Btn_DeleteLog.Enabled = False + If TabControl_Options.SelectedTab Is Tab_Miscellanous Then + SetLogControlsStatus() End If End Sub Private Sub Btn_DeleteLog_Click(sender As Object, e As EventArgs) Handles Btn_DeleteLog.Click LogFile.LogTracing("Delete LogFile", LogLvl.LOG_DEBUG, Me) - If My.Computer.FileSystem.FileExists(WinNUT_Globals.LogFilePath) Then - WinNUT.LogFile.WriteLog = False - My.Computer.FileSystem.DeleteFile(WinNUT_Globals.LogFilePath) - WinNUT.LogFile.WriteLog = WinNUT_Params.Arr_Reg_Key.Item("UseLogFile") - Btn_ViewLog.Enabled = True - Btn_DeleteLog.Enabled = True + + If LogFile.DeleteLogFile() Then LogFile.LogTracing("LogFile Deleted", LogLvl.LOG_DEBUG, Me) Else - LogFile.LogTracing("LogFile does not exists", LogLvl.LOG_WARNING, Me) - Btn_ViewLog.Enabled = False - Btn_DeleteLog.Enabled = False + LogFile.LogTracing("Error deleting log file.", LogLvl.LOG_WARNING, Me) End If + + LogFile.IsWritingToFile = WinNUT_Params.Arr_Reg_Key.Item("UseLogFile") + SetLogControlsStatus() End Sub Private Sub Btn_ViewLog_Click(sender As Object, e As EventArgs) Handles Btn_ViewLog.Click LogFile.LogTracing("Show LogFile", LogLvl.LOG_DEBUG, Me) - If My.Computer.FileSystem.FileExists(WinNUT_Globals.LogFilePath) Then - Process.Start(WinNUT_Globals.LogFilePath) + If File.Exists(LogFile.LogFileLocation) Then + Process.Start(LogFile.LogFileLocation) Else LogFile.LogTracing("LogFile does not exists", LogLvl.LOG_WARNING, Me) Btn_ViewLog.Enabled = False @@ -364,7 +349,6 @@ Public Class Pref_Gui Private Sub Pref_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Icon = WinNUT.Icon - Me.LogFile = WinNUT.LogFile LogFile.LogTracing("Load Pref Gui", LogLvl.LOG_DEBUG, Me) End Sub @@ -374,4 +358,19 @@ Public Class Pref_Gui Me.Btn_Apply.Enabled = True End If End Sub + + ''' + ''' Enable or disable controls to view and delete log data if it's available. + ''' + Private Sub SetLogControlsStatus() + If WinNUT_Params.Arr_Reg_Key.Item("UseLogFile") Then ' Directory.Exists(Logger.LogFolder) + Btn_ViewLog.Enabled = True + Btn_DeleteLog.Enabled = True + Else + Btn_ViewLog.Enabled = False + Btn_DeleteLog.Enabled = False + End If + + LogFile.LogTracing("Setting LogControl statuses.", LogLvl.LOG_DEBUG, Me) + End Sub End Class diff --git a/WinNUT_V2/WinNUT_GUI/Shutdown_Gui.vb b/WinNUT_V2/WinNUT_GUI/Shutdown_Gui.vb index e0ca3b7..372238d 100644 --- a/WinNUT_V2/WinNUT_GUI/Shutdown_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/Shutdown_Gui.vb @@ -34,7 +34,7 @@ Public Class Shutdown_Gui Private Sub Shutdown_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Icon = WinNUT.Icon - Me.LogFile = WinNUT.LogFile + ' Me.LogFile = WinNUT.LogFile LogFile.LogTracing("Load ShutDown Gui", LogLvl.LOG_DEBUG, Me) Me.Grace_Timer.Enabled = False Me.Grace_Timer.Stop() diff --git a/WinNUT_V2/WinNUT_GUI/Update_Gui.vb b/WinNUT_V2/WinNUT_GUI/Update_Gui.vb index 1ae7c04..991dcc9 100644 --- a/WinNUT_V2/WinNUT_GUI/Update_Gui.vb +++ b/WinNUT_V2/WinNUT_GUI/Update_Gui.vb @@ -46,7 +46,7 @@ Public Class Update_Gui MyBase.SetVisibleCore(False) WinNUT.NotifyIcon.Visible = False Me.Icon = WinNUT.Icon - LogFile = WinNUT.LogFile + ' LogFile = WinNUT.LogFile VerifyUpdate() End If End Sub @@ -136,7 +136,7 @@ Public Class Update_Gui End If End If Catch excep As Exception - WinNUT.LogFile.LogTracing(excep.Message, LogLvl.LOG_ERROR, Me) + LogFile.LogTracing(excep.Message, LogLvl.LOG_ERROR, Me) End Try WinNUT_Params.Arr_Reg_Key.Item("LastDateVerification") = Now.ToString WinNUT_Params.Save_Params() diff --git a/WinNUT_V2/WinNUT_GUI/WinNUT.vb b/WinNUT_V2/WinNUT_GUI/WinNUT.vb index ed61c4f..7b3f3ea 100644 --- a/WinNUT_V2/WinNUT_GUI/WinNUT.vb +++ b/WinNUT_V2/WinNUT_GUI/WinNUT.vb @@ -10,8 +10,11 @@ Imports WinNUT_Client_Common Public Class WinNUT - 'Logger Class Object - Public Shared WithEvents LogFile As New Logger(False, 0) +#Region "Properties" + +#End Region + ' Logging + Private WithEvents ClientLogger As Logger 'Object for UPS management Public WithEvents UPS_Device As UPS_Device @@ -66,7 +69,7 @@ Public Class WinNUT Private Event UpdateBatteryState(ByVal Reason As String) 'Handle sleep/hibernate mode from windows API - Declare Function SetSuspendState Lib "PowrProf" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer + Declare Function SetSuspendState Lib "PowrProf" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer Public Property UpdateMethod() As String Get @@ -89,13 +92,16 @@ Public Class WinNUT End Property Private Sub WinNUT_Load(sender As Object, e As EventArgs) Handles MyBase.Load - LogFile.WriteLog = True - LogFile.LogLevel = 3 + ' Make sure we have an app directory to write to. + ' SetupAppDirectory() + + LogFile = New Logger(False, LogLvl.LOG_DEBUG) AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf SystemEvents_PowerModeChanged 'Init WinNUT Variables - WinNUT_Globals.Init_Globals() + Init_Globals() + LogFile.LogTracing("Initialisation Globals Variables Complete", LogLvl.LOG_DEBUG, Me) 'Add Main Gui's Strings WinNUT_Globals.StrLog.Insert(AppResxStr.STR_MAIN_OLDINI_RENAMED, My.Resources.Frm_Main_Str_01) @@ -143,17 +149,18 @@ Public Class WinNUT 'Init WinNUT Parameters WinNUT_Params.Init_Params() + LogFile.LogTracing("Initialisation Params Complete", LogLvl.LOG_DEBUG, Me) 'Load WinNUT Parameters WinNUT_Params.Load_Params() - - 'Init Log File - LogFile.WriteLog = WinNUT_Params.Arr_Reg_Key.Item("UseLogFile") - LogFile.LogLevel = WinNUT_Params.Arr_Reg_Key.Item("Log Level") - LogFile.LogTracing("Initialisation Globals Variables Complete", LogLvl.LOG_DEBUG, Me) - LogFile.LogTracing("Initialisation Params Complete", LogLvl.LOG_DEBUG, Me) LogFile.LogTracing("Loaded Params Complete", LogLvl.LOG_DEBUG, Me) + ' Setup logging preferences + LogFile.LogLevel = Arr_Reg_Key.Item("Log Level") + LogFile.IsWritingToFile = Arr_Reg_Key.Item("UseLogFile") + ClientLogger = LogFile + LogFile.LogTracing("Logging is configured.", LogLvl.LOG_DEBUG, Me) + 'Init Systray Me.NotifyIcon.Text = WinNUT_Globals.LongProgramName & " - " & WinNUT_Globals.ShortProgramVersion Me.NotifyIcon.Visible = False @@ -187,7 +194,7 @@ Public Class WinNUT 'Nut_Socket = New Nut_Comm(Me.Nut_Parameter) 'UPS_Device = New UPS_Device(Nut_Socket, WinNUT_Params.Arr_Reg_Key.Item("UPSName"), WinNUT.LogFile) - UPS_Device = New UPS_Device(Me.Nut_Config, WinNUT.LogFile) + UPS_Device = New UPS_Device(Me.Nut_Config, LogFile) Nut_Socket = UPS_Device.Nut_Socket Me.Polling_Interval = WinNUT_Params.Arr_Reg_Key.Item("Delay") With Me.Update_Data @@ -661,33 +668,33 @@ Public Class WinNUT ' End If Select Case Me.UPS_BattCh - Case 76 To 100 - Lbl_VBL.BackColor = Color.White - ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_100 - LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me) - Case 51 To 75 - Lbl_VBL.BackColor = Color.White - ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_75 - LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me) - Case 40 To 50 - Lbl_VBL.BackColor = Color.White - ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_50 - LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me) - Case 26 To 39 - Lbl_VBL.BackColor = Color.Red - ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_50 - LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me) - Case 11 To 25 - Lbl_VBL.BackColor = Color.Red - ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_25 - LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me) - Case 0 To 10 - Lbl_VBL.BackColor = Color.Red - ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_0 - LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me) - End Select - - Dim iSpan As TimeSpan = TimeSpan.FromSeconds(Me.UPS_BattRuntime) + Case 76 To 100 + Lbl_VBL.BackColor = Color.White + ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_100 + LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me) + Case 51 To 75 + Lbl_VBL.BackColor = Color.White + ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_75 + LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me) + Case 40 To 50 + Lbl_VBL.BackColor = Color.White + ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_50 + LogFile.LogTracing("Battery Charged", LogLvl.LOG_DEBUG, Me) + Case 26 To 39 + Lbl_VBL.BackColor = Color.Red + ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_50 + LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me) + Case 11 To 25 + Lbl_VBL.BackColor = Color.Red + ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_25 + LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me) + Case 0 To 10 + Lbl_VBL.BackColor = Color.Red + ActualAppIconIdx = ActualAppIconIdx Or AppIconIdx.IDX_BATT_0 + LogFile.LogTracing("Low Battery", LogLvl.LOG_DEBUG, Me) + End Select + + Dim iSpan As TimeSpan = TimeSpan.FromSeconds(Me.UPS_BattRuntime) 'Lbl_VRTime.Text = iSpan.Hours.ToString.PadLeft(2, "0"c) & ":" & 'iSpan.Minutes.ToString.PadLeft(2, "0"c) & ":" & @@ -997,7 +1004,7 @@ Public Class WinNUT HasFocus = False End Sub - Public Shared Sub Update_InstantLog(ByVal sender As System.Object) Handles LogFile.NewData + Public Shared Sub Update_InstantLog(ByVal sender As System.Object) Handles ClientLogger.NewData Dim Message As String = LogFile.CurrentLogData Static Dim Event_Id = 1 LogFile.LogTracing("New Log to CB_Current Log : " & Message, LogLvl.LOG_DEBUG, sender.ToString)