OA_Log.bas 2.0 KB

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