Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4 KiB

  1. Attribute VB_Name = "Logging"
  2. Option Explicit
  3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  4. Private Const WM_VSCROLL = &H115
  5. Private Const SB_LINEDOWN = &H1
  6. Private Const EM_SETSEL = &HB1
  7. Private Const EM_REPLACESEL = &HC2
  8. Private p_strLogFile As String
  9. Private p_strTID As String
  10. Public Enum LOGGING_TYPE_E
  11. LOGGING_TYPE_NORMAL_E = 0
  12. LOGGING_TYPE_ERROR_E = 1
  13. LOGGING_TYPE_WARNING_E = 2
  14. End Enum
  15. Public Sub SetLogFile( _
  16. Optional ByVal i_strLogFile As String = "" _
  17. )
  18. If (Trim$(i_strLogFile) = "") Then
  19. p_strLogFile = Environ$("TEMP") & "\" & App.Title & ".log"
  20. Else
  21. p_strLogFile = i_strLogFile
  22. End If
  23. p_strTID = App.ThreadID
  24. End Sub
  25. Public Sub WriteLog( _
  26. ByVal i_str As String, _
  27. Optional ByVal i_blnPlain As Boolean = False, _
  28. Optional ByVal i_blnUnicode As Boolean = False _
  29. )
  30. If (i_blnPlain) Then
  31. FileWrite p_strLogFile, i_str & vbCrLf, True, i_blnUnicode
  32. Else
  33. FileWrite p_strLogFile, "[" & p_strTID & "] " & Date & " " & Time & " > " & _
  34. i_str & vbCrLf, True, i_blnUnicode
  35. End If
  36. End Sub
  37. Public Function GetLogFileName() As String
  38. GetLogFileName = p_strLogFile
  39. End Function
  40. Public Sub OutputToTextBox( _
  41. ByVal i_txt As TextBox, _
  42. ByVal i_str As String _
  43. )
  44. SendMessage i_txt.hwnd, EM_SETSEL, &H7FFFFFFF, &H7FFFFFFF
  45. DoEvents
  46. SendMessage i_txt.hwnd, EM_REPLACESEL, 0, i_str & vbCrLf
  47. DoEvents
  48. SendMessage i_txt.hwnd, WM_VSCROLL, SB_LINEDOWN, ""
  49. DoEvents
  50. End Sub
  51. ' We have i_enumLoggingType so that someone who calls this program from the command line can
  52. ' search for errors and warnings in the log file once the program terminates. Otherwise, how
  53. ' will they know that it failed?
  54. Public Sub OutputToTextBoxAndWriteLog( _
  55. ByVal i_txt As TextBox, _
  56. ByVal i_str As String, _
  57. ByVal i_enumLoggingType As LOGGING_TYPE_E _
  58. )
  59. Dim str As String
  60. Select Case (i_enumLoggingType)
  61. Case LOGGING_TYPE_NORMAL_E
  62. str = i_str
  63. Case LOGGING_TYPE_ERROR_E
  64. str = "Error: " & i_str
  65. Case LOGGING_TYPE_WARNING_E
  66. str = "Warning: " & i_str
  67. End Select
  68. OutputToTextBox i_txt, str
  69. WriteLog str, True, True
  70. End Sub