OA_Log.bas 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 set_log_path(ByVal path As String)
  18. Dim FSO As Object
  19. Dim oFile As Object
  20. Call MkLogDir
  21. If Not FSO.FileExists(path) Then
  22. Set oFile = FSO.CreateTextFile(path)
  23. oFile.Close
  24. End If
  25. End Sub
  26. Public Sub logger(ByVal origin As String, ByVal level As String, ByVal msg As String)
  27. Dim FSO As Object
  28. Dim oFile As Object
  29. Dim Line As String
  30. Dim new_session As Boolean
  31. new_session = False
  32. If level = "DEBUG" And Not debug_level = True Then Exit Sub
  33. Set FSO = CreateObject("Scripting.FileSystemObject")
  34. If Not Len(log_file_path) > 0 Then
  35. log_file_path = log_dir() & "oa_" & Format(Now(), "yymmdd_hhMM") & ".log"
  36. new_session = True
  37. Call set_log_path(log_file_path)
  38. End If
  39. Set oFile = FSO.OpenTextFile(log_file_path, ForAppending)
  40. If new_session Then oFile.WriteLine ("**********************************")
  41. 'oFile.WriteBlankLines (2)
  42. Line = CStr(Now) + " - " + origin + " - " + level + " - " + msg
  43. Debug.Print Line
  44. oFile.WriteLine (Line)
  45. oFile.Close
  46. Set FSO = Nothing
  47. Set oFile = Nothing
  48. If level = "CRITICAL" Then
  49. Call err.Raise(60000, "Critical error", "Critical error occured, see the log file for more informations")
  50. End If
  51. End Sub