VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "LogFileMaker"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'#####################################################################################
'###
'###    Log file preparation class (LogFile.cls)
'###
'#####################################################################################
'//
'// Function =========================================================================
'//          It is recorded in the log file.
'// ==================================================================================
'//
'// <<Properties>>
'//     Path        : The pass which a log file is kept in (Without designation = c:\PJManLog)
'//     MaxCount    : The held number of log files (Without designation = 32)
'//     MaxSize     : The size of one log file (Without designation = 0[Size is not restricted])
'//
'// <<Method>>
'//     WriteLog(LogMessage, WriteMode) As Boolean
'//     亙堷悢亜
'//         LogMessage  : (String) A log message to record
'//         WriteMode   : (Enum) Record mode
'//                             LFWM_Normal : Normal mode. A log message is recorded as it is.
'//                             LFWM_LFWM_TimeAdd : Time addition mode. (default)
'//                                                 A date and time are added before the log message.
'//     <Return Value>
'//         True/False : It becomes False at the time of the error.
'//
'//丂???????????????????????????????????????????
'// <<Use example>>
'//     <Define>
'//         Public LogFile As LogFileMaker
'//
'//     <Initialize>
'//         Set LogFile = New LogFileMaker
'//         LogFile.Path = "c:\Log\"
'//         LogFile.MaxCount = 100
'//         LogFile.MaxSize = 1400000
'//
'//     <When it is recorded in the log>
'//         LogFile.WriteLog ("Some errors occurred.")
'//
'//丂???????????????????????????????????????????

'The local variable to hold a property value
Private mvarLogFilePath As String 'local copy
'The local variable to hold a property value
Private mvarMaxCount As Integer 'local copy
Public Enum LogFileWriteMode
    LFWM_Normal = 0
    LFWM_TimeAdd
End Enum
'The local variable to hold a property value
Private mvarMaxSize As Long 'local copy

Public Property Let MaxSize(ByVal vData As Long)
'When value is substituted for the properties, it uses in the left side of the assignment expression.
'Syntax: X.MaxSize = 5
    mvarMaxSize = vData
End Property
Public Property Get MaxSize() As Long
'When the value of the properties is acquired, it uses in the right side of the assignment expression.
'Syntax: Debug.Print X.MaxSize
    MaxSize = mvarMaxSize
End Property
Public Property Let MaxCount(ByVal vData As Integer)
'When value is substituted for the properties, it uses in the left side of the assignment expression.
'Syntax: X.MaxCount = 5
    mvarMaxCount = vData
End Property
Public Property Get MaxCount() As Integer
'When the value of the properties is acquired, it uses in the right side of the assignment expression.
'Syntax: Debug.Print X.MaxCount
    MaxCount = mvarMaxCount
End Property
Public Property Let Path(ByVal vData As String)
'When value is substituted for the properties, it uses in the left side of the assignment expression.
'Syntax: X.LogFilePath = 5
    If Right(vData, 1) <> "\" Then
        mvarLogFilePath = vData + "\"
    Else
        mvarLogFilePath = vData
    End If
End Property
Public Property Get Path() As String
'When the value of the properties is acquired, it uses in the right side of the assignment expression.
'Syntax: Debug.Print X.LogFilePath
    Path = mvarLogFilePath
End Property

Private Sub Class_Initialize()
    'Variable initialization
    'set default value
    If Me.Path = "" Then
        Me.Path = "c:\PJManLog\"    'Log file storage directory
    End If
    If Me.MaxCount = 0 Then
        Me.MaxCount = 32            'The number of logs which can be kept
    End If
    Me.MaxSize = 0

End Sub

Public Function WriteLog(ByVal LogMessage As String, Optional ByVal WriteMode As LogFileWriteMode = LFWM_TimeAdd) As Boolean
    Dim LogFilename As String
    Dim LogStr As String
    Dim num As Integer
        
On Error GoTo ErrorOccured
    
    'Without a log directory, preparation
    If (Dir(Me.Path, vbDirectory) = "") Then
        MkDir Me.Path
    End If
    'set log file name
    LogFilename = GetLogFileName(Me.Path)
    
    'write
    num = FreeFile
    Open LogFilename For Append Access Write Lock Write As #num
    'Management by the writing mode
    Select Case WriteMode
        Case LFWM_TimeAdd
            LogStr = "[" & Format(Now, "yyyy/mm/dd hh:nn:ss") & "]," & LogMessage
        Case Else
            LogStr = LogMessage
    End Select
    'write
    Print #num, LogStr
    Close #num
    'Debug.Print LogStr
    
    'delete the past file
    DeleteOldFile
    
    WriteLog = True
    Exit Function
ErrorOccured:
    WriteLog = False
End Function

Private Function GetLogFileName(ByVal Path As String) As String
    'The file name of today's date is made.
    '   A file is divided not to exceed maximum size (Me.MaxSize).
    '   But, the ninth file is returned as it is when the number of division becomes more than 9.
    Dim wkFname As String
    Dim i As Integer
    If Me.MaxSize = 0 Then
        'It is not divided when maximum size is specified in 0.
        'A file name is made "yyyymmdd.log".
        wkFname = Path + Format(Now, "yyyymmdd") + ".log"
    Else
        For i = 1 To 9
            wkFname = Path + Format(Now, "yymmdd") + "_" + Trim(CStr(i)) + ".log"
            If Dir(wkFname) = "" Then Exit For
            If FileLen(wkFname) <= Me.MaxSize Then
                Exit For
            End If
        Next i
    End If
    GetLogFileName = wkFname
End Function

Private Sub DeleteOldFile()
    Dim KillFileName As String
        
    'get delete file name
    KillFileName = GetKillFileName
    'delete the past file
    If (KillFileName <> "") Then
        Kill Me.Path & KillFileName
    End If
End Sub
Private Function GetKillFileName() As String
    Dim FileName As String, OldestFileName As String, KilFileName As String
    Dim FileCount As Integer
    Dim FileDate As Date, OldestDate As Date

    'The number of files is counted and The oldest file name search
    FileCount = 0                       'The number of files is cleared.
    FileName = Dir(Path + "*.log")      'The first file name is acquired.
    OldestFileName = FileName           'The oldest file name is initialized.
    'Is there a file in the specified pass?
    If FileName <> "" Then
        'It is looped about all the files in the specified pass.
        Do While FileName <> ""
            'file num亄侾
                FileCount = FileCount + 1
            'The date of the file is compared.
                FileDate = FileDateTime(Me.Path & FileName)
                OldestDate = FileDateTime(Me.Path & OldestFileName)
                'A file name in the old one is kept.
                If DateDiff("s", OldestDate, FileDate) < 0 Then
                    'DateDiff("s", date1, date2)
                    OldestFileName = FileName
                End If
            'The next file name is acquired.
                FileName = Dir
        Loop
    End If
    
    'When there are more files than regulations, the oldest file name is authorized in the deletion file name.
    If (FileCount < Me.MaxCount) Then
        KillFileName = ""
    Else
        KillFileName = OldestFileName
    End If
    GetKillFileName = KillFileName
End Function

