Adsense

2019年7月29日月曜日

【ACCESS/EXCEL/VBA】ログ出力を行うモジュールサンプル



ACCESSのみで動作確認をしていますがEXCELでもそのまま使えると思います。
かゆいところに手が届かない部分や雑な箇所があるので、必要に応じてカスタマイズして使用してください。


①ファイル出力処理用に
 事前に[ツール]→[参照設定]から[Microsoft Scripting Runtime]を追加する


②以下ログ出力モジュール
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Type systemTime
  5. Year As Integer
  6. Month As Integer
  7. DayOfeek As Integer
  8. Day As Integer
  9. Hour As Integer
  10. Minute As Integer
  11. Second As Integer
  12. Milliseconds As Integer
  13. End Type
  14.  
  15. Private Declare Sub GetSystemTime Lib "kernel32" (systemTime As systemTime)
  16. Private MeasureStartTime As systemTime
  17. Private MeasureEndTime As systemTime
  18. Private IntermStartTime As systemTime
  19. Private IntermEndTime As systemTime
  20. Private MeasureLog As String
  21.  
  22. Public Sub MeasureStart()
  23. MeasureStartTime = GetDetailSysTime
  24. MeasureLog = "計測開始:" & GetFormatSysTime(MeasureStartTime) & vbCrLf
  25. End Sub
  26.  
  27. Public Sub MeasureEnd()
  28. MeasureEndTime = GetDetailSysTime
  29. MeasureLog = MeasureLog & _
  30. "計測終了:" & GetFormatSysTime(MeasureEndTime) & vbCrLf & _
  31. "計測時間:" & GetMeasureTime(IntermStartTime, IntermEndTime) & vbCrLf
  32. Call OutPutText("Log" & GetUnFormatSysTime(MeasureStartTime), MeasureLog)
  33. End Sub
  34.  
  35. Public Sub IntermStart()
  36. IntermStartTime = GetDetailSysTime
  37. End Sub
  38.  
  39. Public Sub IntermEnd(ByVal text As String)
  40. IntermEndTime = GetDetailSysTime
  41. MeasureLog = MeasureLog & _
  42. text & _
  43. ":" & _
  44. GetFormatSysTime(IntermStartTime) & vbTab & _
  45. GetFormatSysTime(IntermEndTime) & vbTab & _
  46. GetMeasureTime(IntermStartTime, IntermEndTime) & vbCrLf
  47. End Sub
  48.  
  49. Public Sub AddLog(ByVal text As String)
  50. MeasureLog = MeasureLog & _
  51. text & _
  52. ":" & vbTab & _
  53. GetFormatSysTime(GetDetailSysTime) & vbCrLf
  54. End Sub
  55.  
  56. Private Function GetDetailSysTime() As systemTime
  57. Dim sysTime As systemTime
  58. GetSystemTime sysTime
  59. GetDetailSysTime = sysTime
  60. End Function
  61.  
  62. Private Sub OutPutText(ByVal fileName As String, ByVal text As String)
  63. Dim wsh As Object
  64. Set wsh = CreateObject("WScript.Shell")
  65. Dim fso As FileSystemObject
  66. Set fso = New FileSystemObject
  67.  
  68. Dim ts As TextStream
  69. Set ts = fso.CreateTextFile(wsh.SpecialFolders("Desktop") & "\" & fileName & ".txt", Overwrite:=True, Unicode:=True)
  70.  
  71. ts.Write (text)
  72. ts.Close
  73. Set wsh = Nothing
  74. Set ts = Nothing
  75. Set fso = Nothing
  76. End Sub
  77.  
  78. Private Function GetFormatSysTime(ByRef sysTime As systemTime) As String
  79. GetFormatSysTime = CStr(sysTime.Year) & "/" & _
  80. Format(sysTime.Month, "00") & "/" & _
  81. Format(sysTime.Day, "00") & " " & _
  82. Format(sysTime.Hour, "00") & ":" & _
  83. Format(sysTime.Minute, "00") & ":" & _
  84. Format(sysTime.Second, "00") & "." & _
  85. Format(sysTime.Milliseconds, "000")
  86. End Function
  87.  
  88. Private Function GetUnFormatSysTime(ByRef sysTime As systemTime) As String
  89. GetUnFormatSysTime = CStr(sysTime.Year) & _
  90. Format(sysTime.Month, "00") & _
  91. Format(sysTime.Day, "00") & _
  92. Format(sysTime.Hour, "00") & _
  93. Format(sysTime.Minute, "00") & _
  94. Format(sysTime.Second, "00") & _
  95. Format(sysTime.Milliseconds, "000")
  96. End Function
  97.  
  98. Private Function GetMeasureTime(ByRef sysStartTime As systemTime, _
  99. ByRef sysEndTime As systemTime) As String
  100. Dim startTime As Double
  101. Dim endTime As Double
  102. startTime = CDbl(Format(sysStartTime.Hour, "00") & _
  103. Format(sysStartTime.Minute, "00") & _
  104. Format(sysStartTime.Second, "00") & "." & _
  105. Format(sysStartTime.Milliseconds, "000"))
  106. endTime = CDbl(Format(sysEndTime.Hour, "00") & _
  107. Format(sysEndTime.Minute, "00") & _
  108. Format(sysEndTime.Second, "00") & "." & _
  109. Format(sysEndTime.Milliseconds, "000"))
  110. GetMeasureTime = Format(CStr(endTime - startTime), "00:00:00.000")
  111. End Function



③使用例
1.MeasureStartでログを取得開始、
 MeasureEndでログファイルをデスクトップに出力します。
※MeasureStart/MeasureEnd処理は計測時には必須処理となります。
2.MeasureStart~End間の処理で以下の関数を実行すると中間ログの取得が行えます。  AddLog:引数テキスト、AddLog実行時の時刻をログファイルに出力します。  IntermStart/IntermEnd:IntermStartで中間ログ計測開始、
             IntermEndで引数テキスト、Start/End時刻、Start~End間の時間をログファイルに出力します。
  1. Private Sub LogSample()
  2. ' MeasureStart実行でログ計測を開始します
  3. Call MeasureStart
  4. ' 単純なログを残した場合はAddLogを使用します
  5. Call AddLog("AddLogサンプル①")
  6. ' 中間の処理時間を図りたい場合はIntermStart-IntermEndを使用します
  7. ' IntermEndの引数に指定した文言/IntermStartとEnd時刻/IntermEndまでの時間が出力されます。
  8. Call IntermStart
  9. ' --ダミー処理---
  10. Dim i As Long
  11. Dim str As String
  12. For i = 0 To 100000
  13. str = "dammy"
  14. Next i
  15. ' --ダミー処理---
  16. Call IntermEnd("IntermStart/End")
  17. Call AddLog("AddLogサンプル②")
  18. ' MeasureEnd実行でログファイルが出力されます
  19. Call MeasureEnd
  20. End Sub
④実行結果  [③使用例]のコードを実行した結果  以下のファイルがデスクトップに出力される。

0 件のコメント:

コメントを投稿