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