Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 54 additions & 18 deletions WinNUT_V2/WinNUT-Client/ApplicationEvents.vb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY

Imports System.IO
Imports Microsoft.VisualBasic.ApplicationServices
Imports WinNUT_Client_Common

Expand Down Expand Up @@ -48,14 +49,19 @@ Namespace My
.Size = New Point(470, 100)
End With

Dim Exception_data As String = String.Format("Exception type: {0}" & vbNewLine & "Exception message: {1}" & vbNewLine & "Exception stack trace: " & vbNewLine & "{2}",
e.Exception.GetType.ToString, e.Exception.Message, e.Exception.StackTrace)
Dim Exception_data = BuildExceptionString(e.Exception)

If e.Exception.InnerException IsNot Nothing Then
Exception_data &= vbNewLine & "InnerException present:" & vbNewLine
Exception_data &= BuildExceptionString(e.Exception.InnerException)
End If

With Msg_Error
.Location = New Point(6, 110)
.Multiline = True
.ScrollBars = ScrollBars.Vertical
.ReadOnly = True
.Text = Exception_data
.Text = Exception_data.ToString()
.Size = New Point(470, 300)
End With

Expand Down Expand Up @@ -96,6 +102,22 @@ Namespace My
WinNUT.HasCrashed = True
End Sub

''' <summary>
''' Generate a friendly message describing an exception.
''' </summary>
''' <param name="ex">The exception that will be read for the message.</param>
''' <returns>The final string representation of the exception.</returns>
Private Function BuildExceptionString(ex As Exception) As String
Dim retStr = String.Empty

retStr &= String.Format("Exception type: {0}" & vbNewLine, ex.GetType.ToString)
retStr &= String.Format("Exception message: {0}" & vbNewLine, ex.Message)
retStr &= "Exception stack trace:" & vbNewLine
retStr &= ex.StackTrace & vbNewLine

Return retStr
End Function

Private Sub CrashBug_FormClosing(sender As Object, e As FormClosingEventArgs)
End
End Sub
Expand All @@ -106,25 +128,43 @@ Namespace My
Private Sub Generate_Button_Click(sender As Object, e As EventArgs)
'Generate a bug report with all essential datas
Dim Crash_Report As String = "WinNUT Bug Report" & vbNewLine
Dim WinNUT_Config As New Dictionary(Of String, Object)(Arr_Reg_Key)
Dim WinNUT_UserData_Dir = ApplicationData ' Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client"
Dim CrashLog_Dir = WinNUT_UserData_Dir & "\CrashLog"
Dim WinNUT_Config As New Dictionary(Of String, Object)
Try
WinNUT_Config = Arr_Reg_Key
Catch ex As Exception
Crash_Report &= "ALERT: Encountered exception while trying to access Arr_Reg_Key:" & vbNewLine
Crash_Report &= BuildExceptionString(ex)
End Try

' Initialize directory for data
Dim CrashLog_Dir = ApplicationData & "\CrashLog"
If Not Computer.FileSystem.DirectoryExists(CrashLog_Dir) Then
Computer.FileSystem.CreateDirectory(CrashLog_Dir)
End If

Dim CrashLog_Filename As String = "Crash_Report_" & Format(Now, "dd-MM-yyyy") & "_" &
String.Format("{0}-{1}-{2}.txt", Now.Hour.ToString("00"), Now.Minute.ToString("00"), Now.Second.ToString("00"))

For Each kvp As KeyValuePair(Of String, Object) In Arr_Reg_Key
Select Case kvp.Key
Case "ServerAddress", "Port", "UPSName", "NutLogin", "NutPassword"
WinNUT_Config.Remove(kvp.Key)
End Select
Next


Crash_Report &= "Os Version : " & Computer.Info.OSVersion & vbNewLine
Crash_Report &= "WinNUT Version : " & Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString & vbNewLine

Crash_Report &= vbNewLine & "WinNUT Parameters : " & vbNewLine
If WinNUT_Config.Count > 0 Then
' Prepare config values by removing sensitive information.
For Each kvp As KeyValuePair(Of String, Object) In Arr_Reg_Key
Select Case kvp.Key
Case "ServerAddress", "Port", "UPSName", "NutLogin", "NutPassword"
WinNUT_Config.Remove(kvp.Key)
End Select
Next
Crash_Report &= Newtonsoft.Json.JsonConvert.SerializeObject(WinNUT_Config, Newtonsoft.Json.Formatting.Indented) & vbNewLine

Else
Crash_Report &= "[EMPTY]" & vbNewLine
End If

Crash_Report &= Newtonsoft.Json.JsonConvert.SerializeObject(WinNUT_Config, Newtonsoft.Json.Formatting.Indented) & vbNewLine
Crash_Report &= vbNewLine & "Error Message : " & vbNewLine
Crash_Report &= Msg_Error.Text & vbNewLine & vbNewLine
Crash_Report &= "Last Events :" & vbNewLine
Expand All @@ -135,11 +175,7 @@ Namespace My

Computer.Clipboard.SetText(Crash_Report)

If Not Computer.FileSystem.DirectoryExists(CrashLog_Dir) Then
Computer.FileSystem.CreateDirectory(CrashLog_Dir)
End If

Dim CrashLog_Report As IO.StreamWriter
Dim CrashLog_Report As StreamWriter
CrashLog_Report = Computer.FileSystem.OpenTextFileWriter(CrashLog_Dir & "\" & CrashLog_Filename, True)
CrashLog_Report.WriteLine(Crash_Report)
CrashLog_Report.Close()
Expand Down
2 changes: 1 addition & 1 deletion WinNUT_V2/WinNUT-Client/WinNUT.vb
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ Public Class WinNUT
' Setup logging preferences
If Arr_Reg_Key.Item("UseLogFile") Then
LogFile.LogLevelValue = Arr_Reg_Key.Item("Log Level")
LogFile.InitializeLogFile()
LogFile.InitializeLogFile(ApplicationData)
ElseIf LogFile.IsWritingToFile Then
LogFile.DeleteLogFile()
End If
Expand Down
120 changes: 0 additions & 120 deletions WinNUT_V2/WinNUT-Client_Common/CryptData.vb

This file was deleted.

59 changes: 15 additions & 44 deletions WinNUT_V2/WinNUT-Client_Common/Logger.vb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Public Class Logger
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"
' Logs will be stored in the program's appdata folder, in a Logs subdirectory.
Public Shared ReadOnly LogFolder = Path.Combine(ApplicationData, "Logs")
' The subfolder that will contain logs.
Public Const LOG_SUBFOLDER = "\Logs\"
#End Region

Private LogFile As Logging.FileLogTraceListener
Expand Down Expand Up @@ -65,67 +65,31 @@ Public Class Logger
Get
Return LogFile IsNot Nothing
End Get

'Get
' Return Not (LogFile Is Nothing)
'End Get

'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

''' <summary>
''' Either retrieve the log file location from the <see cref="LogFile"/> object, or give an estimate of what it
''' would be.
''' Get the log file location from the <see cref="LogFile"/> object.
''' </summary>
''' <returns>The possible path to the log file. Note that this does not gaurantee it exists.</returns>
Public ReadOnly Property LogFileLocation() As String
Get
If IsWritingToFile Then
Return LogFile.FullLogFileName
Else
Return Path.Combine(LogFolder, BASE_FILE_NAME & Date.Now.ToString(LOG_FILE_DATESTRING))
End If
Return LogFile.FullLogFileName
End Get
End Property

' Log all events - this object will keep all logs and allow accessors to decide which ones they want.
'Public Property LogLevel() As LogLvl
' Get
' Return Me.LogLevelValue
' End Get
' Set(ByVal Value As LogLvl)
' Me.LogLevelValue = Value
' End Set
'End Property

#End Region

Public Sub New(writeLog As Boolean, LogLevel As LogLvl)
Public Sub New(LogLevel As LogLvl)
LogLevelValue = LogLevel
' LastEventsList.Capacity = 50

' IsWritingToFile = writeLog
If writeLog = True Then
InitializeLogFile()
End If
End Sub

Public Sub InitializeLogFile()
Public Sub InitializeLogFile(baseDataFolder As String)
LogFile = New Logging.FileLogTraceListener(BASE_FILE_NAME) With {
.TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId,
.Append = True,
.AutoFlush = True,
.LogFileCreationSchedule = LOG_FILE_CREATION_SCHEDULE,
.CustomLocation = LogFolder,
.CustomLocation = baseDataFolder & LOG_SUBFOLDER,
.Location = Logging.LogFileLocation.Custom
}

Expand Down Expand Up @@ -169,7 +133,14 @@ Public Class Logger
''' <param name="LogToDisplay">A user-friendly, translated string to be shown.</param>
Public Sub LogTracing(message As String, LvlError As LogLvl, sender As Object, Optional LogToDisplay As String = Nothing)
Dim Pid = TEventCache.ProcessId
Dim SenderName = sender.GetType.Name
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

Expand Down
2 changes: 1 addition & 1 deletion WinNUT_V2/WinNUT-Client_Common/WinNUT-Client_Common.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
Expand All @@ -74,7 +75,6 @@
<Compile Include="Common_Classes.vb" />
<Compile Include="StringEnum.vb" />
<Compile Include="Common_Enums.vb" />
<Compile Include="CryptData.vb" />
<Compile Include="IniFileVb.vb" />
<Compile Include="Logger.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
Expand Down
Loading