Pages

Friday, 18 September 2009

Write log using VB6

Function to write log file using Visual Basic 6

Private Sub writeLog(strMessage As String)

Dim hFile As Long
Dim sFolder As String
Dim sFile As String
Dim sDestination As String
Dim sLog As String

On Error GoTo errHandler

hFile = FreeFile()
sFolder = App.Path & "\logs"
sFile = sFolder & "\Softphone.log"

sLog = Format(Now(), "yyyy-mm-dd HH:nn:ss") & "." & Right(Format(Timer(), "0.000"), 3)
sLog = sLog & vbTab & strMessage

If lLineNumber > 60000 Then 'Approx 5 MB
    lLineNumber = 1
    sDestination = sFolder & "\Softphone_" & Format(Now(), "yyyymmdd_HHnnss") & ".log"
    FileCopy sFile, sDestination
    Kill sFile
Else
    lLineNumber = lLineNumber + 1
End If

Open sFile For Append As #hFile
    Print #hFile, sLog
Close hFile

Exit Sub

errHandler:

If Err.Number = 76 Then 'If folder not exists, create it
    MkDir sFolder
    Resume
End If
MsgBox Err.Number & ":" & Err.Description

End Sub

No comments:

Post a Comment