Attribute VB_Name = "Logging"
Option Explicit


Public LogFile As String
Public LogFileBackup As String
Sub AddLogEntry(strEntry As String, fTimeStamp As Boolean)
    Dim retCode As Integer
    If fTimeStamp = True Then
        retCode = WriteLog(Format$(Now, "mm-dd-yy hh:mm:ss") & " - " & strEntry)
    Else
        retCode = WriteLog(strEntry)
    End If

End Sub

Function FileExists(strFile As String) As Boolean
' Determines if a given file exists
    If Len(Dir(strFile)) > 0 Then
        FileExists = True
    Else
        FileExists = False
    End If
End Function

Sub InitializeLogFiles()
    On Error GoTo errInit

    Dim iMaxSize As Integer
    Dim iCounter As Integer
    Dim iFileNum As Integer
    Dim strfileentry As String

    iMaxSize = 2000     ' Max 2000 Rows

    iFileNum = FreeFile
    
    ' If Fileexists
    If FileExists(LogFile) = True Then
        Open LogFile For Input Access Read As iFileNum
        Do While Not EOF(iFileNum)
            Line Input #iFileNum, strfileentry
            '''Debug.Print strfileentry
            iCounter = iCounter + 1
            If iCounter >= iMaxSize Then Exit Do
        Loop
        Close #iFileNum

        ' Cycle log files
        If iCounter >= iMaxSize Then
            AddLogEntry "** ROLLING OVER LOG FILE **", True
            If FileExists(LogFileBackup) Then
                Kill LogFileBackup
            End If
            Name LogFile As LogFileBackup
        End If
    End If

DoneInit:
    On Error Resume Next
    Screen.MousePointer = vbDefault
    Exit Sub

errInit:
    MsgBox "An error has occurred in the log file initialize routine. " & Err & " - " & Error, vbOKOnly + vbExclamation, "Error"
    GoTo DoneInit

End Sub

Private Function WriteLog(strWriteText As String) As Integer
    On Error GoTo errWriteLog
    
    Dim FILE As String
    Dim iFileNum As Integer
    
    FILE = LogFile
    iFileNum = FreeFile
    
    Open FILE For Append Access Write Shared As iFileNum Len = 512

    Print #iFileNum, strWriteText
    
    ' No Error!
    WriteLog = 0

DoneWriting:
    On Error Resume Next
    Close #iFileNum
    Screen.MousePointer = vbDefault
    Exit Function

errWriteLog:
    MsgBox "An error has occurred in the  logging routine. " & Err & " - " & Error, vbOK + vbExclamation, "Error"
    WriteLog = Err
    GoTo DoneWriting

End Function

