OA_Log.bas 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. Option Compare Database
  2. Option Explicit
  3. Dim log_file_path As String
  4. Dim debug_level As Boolean
  5. Public Function log_file()
  6. log_file = log_file_path
  7. End Function
  8. Public Sub set_debug_mode()
  9. debug_level = True
  10. End Sub
  11. Public Sub logger(ByVal origin As String, ByVal level As String, ByVal msg As String)
  12. Dim fso As Object
  13. Dim oFile As Object
  14. Dim line As String
  15. Dim new_session As Boolean
  16. new_session = False
  17. If level = "DEBUG" And Not debug_level = True Then Exit Sub
  18. Set fso = CreateObject("Scripting.FileSystemObject")
  19. If Not Len(log_file_path) > 0 Then
  20. 'log_file_path = CurrentProject.path & "\" & "openaccess.log"
  21. Call MkDirIfNotExist(Environ("AppData") & "\OpenAccess\")
  22. log_file_path = Environ("AppData") & "\OpenAccess\oa_" & Format(Now(), "yymmdd_hhMM") & ".log"
  23. Debug.Print log_file_path
  24. new_session = True
  25. If Not fso.FileExists(log_file_path) Then
  26. Set oFile = fso.CreateTextFile(log_file_path)
  27. oFile.Close
  28. End If
  29. End If
  30. Set oFile = fso.OpenTextFile(log_file_path, ForAppending)
  31. If new_session Then oFile.WriteLine ("**********************************")
  32. 'oFile.WriteBlankLines (2)
  33. line = CStr(Now) + " - " + origin + " - " + level + " - " + msg
  34. Debug.Print line
  35. oFile.WriteLine (line)
  36. oFile.Close
  37. Set fso = Nothing
  38. Set oFile = Nothing
  39. If level = "CRITICAL" Then
  40. Call err.Raise(60000, "Critical error", "Critical error occured, see the log file for more informations")
  41. End If
  42. End Sub