Option Compare Database
Option Explicit

Dim log_file_path As String
Dim debug_level As Boolean

Private Sub MkLogDir()
    Call RecursiveMkDir(Environ("AppData") & "\OpenAccess\log\")
End Sub

Public Function log_dir() As String
    log_dir = Environ("AppData") & "\OpenAccess\log\"
End Function

Public Function log_file() As String
    log_file = log_file_path
End Function

Public Sub set_debug_mode()
    debug_level = True
End Sub

Public Sub logger(ByVal origin As String, ByVal level As String, ByVal msg As String)
    Dim FSO As Object
    Dim oFile As Object
    Dim Line As String
    Dim new_session As Boolean
    new_session = False
    
    If level = "DEBUG" And Not debug_level = True Then Exit Sub

    Set FSO = CreateObject("Scripting.FileSystemObject")

    If Not Len(log_file_path) > 0 Then
    
        Call MkLogDir
        
        log_file_path = log_dir() & "oa_" & Format(Now(), "yymmdd_hhMM") & ".log"
        Debug.Print log_file_path
        
        new_session = True
        
        If Not FSO.FileExists(log_file_path) Then
            Set oFile = FSO.CreateTextFile(log_file_path)
            oFile.Close
        End If
        
    End If

    Set oFile = FSO.OpenTextFile(log_file_path, ForAppending)

    If new_session Then oFile.WriteLine ("**********************************")

    'oFile.WriteBlankLines (2)
    Line = CStr(Now) + " - " + origin + " - " + level + " - " + msg
    Debug.Print Line
    oFile.WriteLine (Line)

    oFile.Close
    Set FSO = Nothing
    Set oFile = Nothing
    
    If level = "CRITICAL" Then
        Call err.Raise(60000, "Critical error", "Critical error occured, see the log file for more informations")
    End If
    
End Sub