PageRenderTime 1129ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/SongDatabase/Logging/ActivityLog.vb

#
Visual Basic | 139 lines | 78 code | 24 blank | 37 comment | 1 complexity | 970697b8736bafb8c3857386336f625b MD5 | raw file
  1. Imports System.IO
  2. Namespace Logging
  3. ''' <summary>
  4. ''' Provides simple logging functionality for a PowerSong database.
  5. ''' </summary>
  6. Public Class ActivityLog
  7. Private FDatabase As Database = Nothing
  8. Private FFiles As New Dictionary(Of String, FileStream)
  9. ''' <summary>
  10. ''' Adds the specified log entry to the activity log.
  11. ''' The entry is only added if logging is enabled by the database's settings.
  12. ''' </summary>
  13. ''' <param name="itemSort">The type of log entry to record.</param>
  14. ''' <param name="id">The ID of the item in question.</param>
  15. ''' <param name="time">The time of the action.</param>
  16. ''' <param name="action">The action performed.</param>
  17. Public Sub Add(ByVal itemSort As LogEntry.ItemType, _
  18. ByVal id As Guid, _
  19. ByVal time As Date, _
  20. ByVal action As LogEntry.Action)
  21. ' Only do this if logging is enabled
  22. If FDatabase.Settings.EnableLogging Then
  23. ' Ensure the file handle exists
  24. Dim File As FileStream = GetFileHandle(time.Year, time.Month)
  25. ' Add to the file
  26. File.Seek(0, SeekOrigin.End)
  27. File.WriteByte(DirectCast(itemSort, Byte))
  28. File.Write(id.ToByteArray, 0, 16)
  29. File.WriteByte(CByte(time.Day))
  30. File.WriteByte(CByte(time.Hour))
  31. File.WriteByte(CByte(time.Minute))
  32. File.WriteByte(CByte(time.Second))
  33. File.WriteByte(DirectCast(action, Byte))
  34. End If
  35. End Sub
  36. ''' <summary>
  37. ''' Determines the number of records available for the given year and month.
  38. ''' </summary>
  39. ''' <param name="year">The year.</param>
  40. ''' <param name="month">The month.</param>
  41. ''' <returns>The number of records for the given year and month.</returns>
  42. Public Function RecordCount(ByVal year As Integer, ByVal month As Integer) As Integer
  43. If LogFileExists(year, month) Then
  44. Dim File As FileStream = GetFileHandle(year, month)
  45. Return File.Length / 22
  46. Else
  47. Return 0
  48. End If
  49. End Function
  50. ''' <summary>
  51. ''' Gets the requested record, based on its date and record index.
  52. ''' A log file must exist for the given year and month before records can be retrieved.
  53. ''' </summary>
  54. ''' <param name="year">The year.</param>
  55. ''' <param name="month">The month.</param>
  56. ''' <param name="recordIndex">Index of the record to retrieve in the given year and month.</param>
  57. ''' <returns>The requested record.</returns>
  58. Public Function GetRecord(ByVal year As Integer, _
  59. ByVal month As Integer, _
  60. ByVal recordIndex As Integer) As LogEntry
  61. ' Check that the log file exists
  62. If Not LogFileExists(year, month) Then Throw New Exception("A log file for " + month.ToString + "/" + year.ToString + " does not exist.")
  63. ' Get the record data
  64. Dim File As FileStream = FFiles(GetFileName(year, month))
  65. File.Seek(22 * recordIndex, SeekOrigin.Begin)
  66. Dim ItemSort As LogEntry.ItemType = File.ReadByte
  67. Dim ItemID(15) As Byte
  68. File.Read(ItemID, 0, 16)
  69. Dim Day As Byte = CByte(File.ReadByte)
  70. Dim Hour As Byte = CByte(File.ReadByte)
  71. Dim Minute As Byte = CByte(File.ReadByte)
  72. Dim Second As Byte = CByte(File.ReadByte)
  73. Dim Time As New DateTime(year, month, Day, Hour, Minute, Second)
  74. Dim Action As LogEntry.Action = File.ReadByte
  75. ' Return the result
  76. Return New LogEntry(ItemSort, New Guid(ItemID), Time, Action)
  77. End Function
  78. ''' <summary>
  79. ''' Determines whether or not a log file exists for the given year and month.
  80. ''' </summary>
  81. ''' <param name="year">The year.</param>
  82. ''' <param name="month">The month.</param>
  83. ''' <returns>True, if the log file exists. False otherwise.</returns>
  84. Public Function LogFileExists(ByVal year As Integer, ByVal month As Integer) As Boolean
  85. Return File.Exists(FDatabase.Location + "\" + GetFileName(year, month))
  86. End Function
  87. ''' <summary>
  88. ''' Gets the name of the log file for the given date.
  89. ''' </summary>
  90. ''' <param name="year">The year.</param>
  91. ''' <param name="month">The month.</param>
  92. ''' <returns>The file name of the log file that would or does exist for the given date.</returns>
  93. Private Function GetFileName(ByVal year As Integer, ByVal month As Integer) As String
  94. Dim YearString As String = year.ToString
  95. Dim MonthString As String = month.ToString.PadLeft(2, "0")
  96. Return YearString + "_" + MonthString + ".Log"
  97. End Function
  98. Private Function GetFileHandle(ByVal year As Integer, ByVal month As Integer) As FileStream
  99. Dim FileName As String = GetFileName(year, month)
  100. If Not FFiles.ContainsKey(FileName) Then
  101. FFiles.Add(FileName, New FileStream(FDatabase.Location + "\" + FileName, FileMode.OpenOrCreate))
  102. End If
  103. Return FFiles(FileName)
  104. End Function
  105. Friend Sub New(ByVal database As Database)
  106. FDatabase = database
  107. End Sub
  108. End Class
  109. End Namespace