Source code for Issue Number 8

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into a new class module and change the name to cLogFile.

'----------------------------------------------------------------------
'
'   Class Name:     cLogFile
'   Written By:     C&D Programming Corp.
'   Create Date:    10/28/97
'   Copyright:      Copyright 1997 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'
'   Purpose:        Provide easy methods to output data to a log file.
'
'   Notes:          Filename defaults to App.Path & "\DEBUG.LOG"
'
'
'----------------------------------------------------------------------

Option Explicit

Private msFilename As String
Private mbActive As Boolean
Private miIndent As Integer
Private miIndentAmount As Integer

'----------------------------------------------------------------------
' Public properties
'----------------------------------------------------------------------
Public Property Let Filename(sFilename As String)
    msFilename = sFilename
End Property

Public Property Get Filename() As String
    Filename = msFilename
End Property

Public Property Let IndentAmount(iIndentAmount As Integer)
    miIndentAmount = iIndentAmount
End Property

Public Property Get IndentAmount() As Integer
    IndentAmount = miIndentAmount
End Property

Public Property Let Active(bActive As Boolean)
    If bActive Then
        mbActive = bActive
        Output "Started Log File " & String(40, "-")
    Else
        Output "Ended Log File " & String(40, "-")
        mbActive = bActive
    End If

End Property

'----------------------------------------------------------------------
' Private subroutines
'----------------------------------------------------------------------
Private Sub Class_Initialize()
    If Right(App.Path, 1) = "\" Then
        Filename = App.Path & "DEBUG.LOG"
    Else
        Filename = App.Path & "\DEBUG.LOG"
    End If
    Active = False
    IndentAmount = 3
    miIndent = -1
End Sub

Private Sub Class_Terminate()
    Active = False
End Sub

'----------------------------------------------------------------------
' Public subroutines
'----------------------------------------------------------------------
Public Sub Output(sMsg As String)
    Dim iFile As Integer

    On Error Goto AbortIt
    If mbActive Then
        iFile = FreeFile
        Open Filename For Append As #iFile
        If miIndent = -1 Then
            Print #iFile, Now & " " & sMsg
        Else
            Print #iFile, Now & " " & Space(miIndent * miIndentAmount) & sMsg
        End If
        Close #iFile
    End If
    Exit Sub

AbortIt:
    MsgBox "Can not open " & Filename
    Exit Sub
End Sub

Public Sub Push(sMsg As String)
    miIndent = miIndent + 1
    Output "Enter " & sMsg
End Sub

Public Sub Pop(sMsg As String)
    Output "Exit " & sMsg
    miIndent = miIndent - 1
End Sub

Public Function Remove() As Boolean
    On Error Resume Next
    Kill Filename
    Remove = (Err = 0)
End Function