' TrsLogClass Class module
' Version 1.0 20FEB17
' Version 1.1 2020-12-15:
'	Public renamed "Constants" as logging type
' 	File name in the format "LogFile_yyyy-MM-dd.log
' 	Method writeLog, which calls the correct submethod per logging type. Parameter is ActivityObject (for entry.source) 	
' 	Method writeLog2 with Parameter "sendingRoutine" as String, if no ActivityObject Is available (e.g. for class modules)

'---------------------------------------------------------------------------------
Sub Class_Globals
	Public ERROR As 	Int = 0
	Public WARNING As 	Int = 1
	Public INFO As 		Int = 2
	Public DEBUG As 	Int = 3
	
	Dim typeStrings() As String = Array As String ("F","W","I","T")

	Dim Const textExtension As String = ".log"
	Private logFileToUse As String
	Private fileRootIs As String

	Private logTimer As Timer

	Type logEntryStruct (logType As Int, timeAndDate As Long, source As String, message As String)
	Public faultReportingList As List
	Private myLogTestResults As Boolean
	
End Sub

Private Sub getDay As String
	DateTime.DateFormat = "_yyyy-MM-dd"
	Return DateTime.Date(DateTime.Now)
End Sub

'---------------------------------------------------------------------------------
'---------------------------------------------------------------------------------
Public Sub Initialize(pathName As String, logFileName As String)
	
	logFileToUse = logFileName&getDay&textExtension
	fileRootIs = pathName
	
	faultReportingList.Initialize
	myLogTestResults = False
	logTimer.Initialize("LogTimer",250)
	logTimer.Enabled = True
	DateTime.TimeFormat = "yyyy-MM-dd HH:mm:ss.SSS"
	
	LogInformation("TrsLogClass", "Log file is "&logFileToUse)
End Sub
'---------------------------------------------------------------------------------
Public Sub Flush
	LogTimer_Tick
End Sub
'---------------------------------------------------------------------------------
'---------------------------------------------------------------------------------
'write to the log file - could include a midnight check if required
Sub LogTimer_Tick()	
	If Not(faultReportingList.IsInitialized) Then 
		Log("faultReportingList not initialized!")
		Return
	End If
	If faultReportingList.Size = 0 Then Return	' nothing to do
	Dim TextWriter1 As TextWriter
	TextWriter1.Initialize(File.OpenOutput(fileRootIs, logFileToUse, True))
	Do While faultReportingList.Size > 0
		Dim entry  As logEntryStruct = faultReportingList.Get(0)
		faultReportingList.RemoveAt(0)
		DateTime.DateFormat= "yyyy-MM-dd"
		DateTime.TimeFormat = "HH:mm:ss.SSS"
		Dim strToLog As String = DateTime.Date(entry.timeAndDate) & " " & DateTime.Time(entry.timeAndDate) &"::" & typeStrings(entry.logType)&":"&entry.source&":"&entry.message
		TextWriter1.WriteLine(strToLog)
		Log("*"&strToLog)
	Loop
	TextWriter1.Close
End Sub
'---------------------------------------------------------------------------------
'---------------------------------------------------------------------------------
Public Sub LogInformation(sendingRoutine As String, theInformationMessage As String)
	Dim entry As logEntryStruct
	entry.Initialize
	entry.logType = INFO 
	entry.timeAndDate = DateTime.Now
	entry.source  = sendingRoutine
	entry.message = theInformationMessage
	faultReportingList.Add(entry)
End Sub
'---------------------------------------------------------------------------------
Public Sub LogFault(sendingRoutine As String, theFaultMessage As String)
	Dim entry As logEntryStruct
	entry.Initialize
	entry.logType = ERROR
	entry.timeAndDate = DateTime.Now
	entry.source  = sendingRoutine
	entry.message = theFaultMessage
	faultReportingList.Add(entry)
End Sub
'---------------------------------------------------------------------------------
Public Sub LogWarning(sendingRoutine As String, theWarningMessage As String)
	Dim entry As logEntryStruct
	entry.Initialize
	entry.logType = WARNING
	entry.timeAndDate = DateTime.Now
	entry.source  = sendingRoutine
	entry.message = theWarningMessage
	faultReportingList.Add(entry)
End Sub

Private Sub getActivityName(ActivityObject As Object) As String
	Dim hlpr As String = ActivityObject
	Return hlpr.Replace("class " & Application.PackageName & ".", "")
End Sub

Public Sub writeLog(ActivityObject As Object, logMessage As String, logType As Int)
	writeLog2(getActivityName(ActivityObject), logMessage, logType)
End Sub

Public Sub writeLog2(sendingRoutine As String, logMessage As String, logType As Int)
	Dim entry As logEntryStruct
	entry.Initialize
	entry.logType = logType
	entry.timeAndDate = DateTime.Now
	entry.source  = sendingRoutine
	entry.message = logMessage
	If faultReportingList.IsInitialized Then 
		faultReportingList.Add(entry)
	Else
		Log("faultReportingList not initialized!")
	End If
End Sub

'---------------------------------------------------------------------------------
' can be turned off to suppress test results
Public Sub LogTest(sendingRoutine As String, theTestMessage As String)
	If Not(myLogTestResults) Then Return
	Dim entry As logEntryStruct
	entry.Initialize
	entry.logType = DEBUG
	entry.timeAndDate = DateTime.Now
	entry.source  = sendingRoutine
	entry.message = theTestMessage
	faultReportingList.Add(entry)
End Sub
'---------------------------------------------------------------------------------
Public Sub LogTestResults(nv As Boolean)
	myLogTestResults = nv
End Sub
'---------------------------------------------------------------------------------
'---------------------------------------------------------------------------------
