OA_Log.bas 1.7 KB

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