4--! \brief Logs
module for FrameworkZ. Logs player actions, system events, errors, warnings, and informational messages.
5--! \class FrameworkZ.Logs
7FrameworkZ.Logs.__index = FrameworkZ.Logs
8FrameworkZ.Logs.LogEntries = {}
9FrameworkZ.Logs.MaxEntries = 1000 -- Maximum number of log entries to load
10FrameworkZ.Logs.LogDirectory = "FrameworkZ_Logs/" -- Directory to store log files
13FrameworkZ.Logs.LogTypes = {
14 PLAYER_ACCEPT_TRADE = "Player Accept Trade",
15 PLAYER_CANCEL_TRADE = "Player Cancel Trade",
16 PLAYER_CREATE_CHARACTER = "Player Create Character",
17 PLAYER_DEATH = "Player Death",
18 PLAYER_DECLINE_TRADE = "Player Decline Trade",
19 PLAYER_ENTER_CAR = "Player Enter Car",
20 PLAYER_EXIT_CAR = "Player Exit Car",
21 PLAYER_FINALIZE_TRADE = "Player Finalize Trade",
22 PLAYER_GIVE_DAMAGE = "Player Give Damage",
23 PLAYER_GRAB_ITEM = "Player Grab Item",
24 PLAYER_KILL = "Player Kill",
25 PLAYER_LOAD_CHARACTER = "Player Load Character",
26 PLAYER_PLACE_ITEM = "Player Place Item",
27 PLAYER_RECEIVE_ITEM = "Player Receive Item",
28 PLAYER_TAKE_DAMAGE = "Player Take Damage",
29 ZOMBIE_GIVE_DAMAGE = "Zombie Give Damage",
30 ZOMBIE_TAKE_DAMAGE = "Zombie Take Damage",
31 SYSTEM_EVENT = "System Event",
37--! \brief Add a log entry.
38--! \param logType \string The type of log (e.g., "PlayerAction", "SystemEvent").
39--! \param message \string The log message.
40--! \param player \table Optional player table associated with the log.
41function FrameworkZ.Logs:AddLog(logType, message, player)
43 timestamp = os.time(),
46 player = player and player:getUsername() or nil
48 table.insert(self.LogEntries, logEntry)
50 -- Save the log entry to a file
51 self:SaveLogToFile(logEntry)
53 -- Remove oldest entries if we exceed the maximum number of entries
54 if #self.LogEntries > self.MaxEntries then
55 table.remove(self.LogEntries, 1)
58 print(string.format("[%s] %s: %s", os.date("%Y-%m-%d %H:%M:%S", logEntry.timestamp), logType, message))
61--! \brief Save a log entry to a file.
62--! \param logEntry \table The log entry to save.
63function FrameworkZ.Logs:SaveLogToFile(logEntry)
64 local filename = self.LogDirectory .. (logEntry.player or "system") .. ".log"
65 local fileWriter = getFileWriter(filename, true, false)
67 fileWriter:write(string.format("[%s] %s: %s\n", os.date("%Y-%m-%d %H:%M:%S", logEntry.timestamp), logEntry.logType, logEntry.message))
70 print("Error opening log file:", filename)
74--! \brief Retrieve log entries.
75--! \param logType \string Optional log type to filter by.
76--! \param player \string Optional player username to filter by.
77--! \return \table A table of log entries.
78function FrameworkZ.Logs:GetLogs(logType, player)
79 local filteredLogs = {}
80 for _, logEntry in ipairs(self.LogEntries) do
81 if (not logType or logEntry.logType == FrameworkZ.Logs.LogTypes[logType]) and (not player or logEntry.player == player) then
82 table.insert(filteredLogs, logEntry)
88--! \brief Load log entries from a file.
89--! \param player \string The player username to load logs for.
90function FrameworkZ.Logs:LoadLogsFromFile(player)
91 local filename = self.LogDirectory .. (player or "system") .. ".log"
92 local fileReader = getFileReader(filename, true)
95 local line = fileReader:readLine()
96 if not line then break end
97 local timestamp, logType, message = line:match("%[(.-)%] (%w+): (.+)")
98 if timestamp and logType and message then
99 table.insert(self.LogEntries, {
100 timestamp = os.time({year=timestamp:sub(1,4), month=timestamp:sub(6,7), day=timestamp:sub(9,10), hour=timestamp:sub(12,13), min=timestamp:sub(15,16), sec=timestamp:sub(18,19)}),
109 print("Error opening log file:", filename)
113--! \brief Search log entries by keyword.
114--! \param keyword \string The keyword to search for.
115--! \return \table A table of log entries that contain the keyword.
116function FrameworkZ.Logs:SearchLogs(keyword)
117 local searchResults = {}
118 for _, logEntry in ipairs(self.LogEntries) do
119 if string.find(logEntry.message, keyword) then
120 table.insert(searchResults, logEntry)
126--! \brief Display logs in a menu for admins.
127function FrameworkZ.Logs:OpenLogMenu()
128 -- Placeholder for menu implementation
129 print("Opening log menu for admins...")
130 -- In a real implementation, you would create a UI menu here
133-- Example usage of logging player actions
134function FrameworkZ.Logs:LogPlayerAction(player, action)
135 self:AddLog(self.LogTypes.PLAYER_ACTION, action, player)
138-- Example usage of logging system events
139function FrameworkZ.Logs:LogSystemEvent(message)
140 self:AddLog(self.LogTypes.SYSTEM_EVENT, message)
143-- Example usage of logging errors
144function FrameworkZ.Logs:LogError(message)
145 self:AddLog(self.LogTypes.ERROR, message)
148-- Example usage of logging warnings
149function FrameworkZ.Logs:LogWarning(message)
150 self:AddLog(self.LogTypes.WARNING, message)
153-- Example usage of logging informational messages
154function FrameworkZ.Logs:LogInfo(message)
155 self:AddLog(self.LogTypes.INFO, message)
158--! \brief Log damage dealt to players from players and zombies.
159--! \param characterGivingDamage \table The character dealing the damage.
160--! \param characterTakingDamage \table The character taking the damage.
161--! \param handWeapon \table The weapon used to deal the damage.
162--! \param damage \integer The amount of damage dealt.
163function FrameworkZ.Logs.OnWeaponHitCharacter(characterGivingDamage, characterTakingDamage, handWeapon, damage)
164 if instanceof(characterTakingDamage, "IsoPlayer") then
165 if instanceof(characterGivingDamage, "IsoPlayer") then
166 local message = string.format("%s hit %s with %s for %d damage (X: %d, Y: %d, Z: %d)", characterGivingDamage:getUsername(), characterTakingDamage:getUsername(), handWeapon:getDisplayName(), damage, characterGivingDamage:getX(), characterGivingDamage:getY(), characterGivingDamage:getZ())
167 FrameworkZ.Logs:AddLog(FrameworkZ.Logs.LogTypes.PLAYER_GIVE_DAMAGE, message, characterGivingDamage)
169 message = string.format("%s took %d damage from %s using %s (X: %d, Y: %d, Z: %d)", characterTakingDamage:getUsername(), damage, characterGivingDamage:getUsername(), handWeapon:getDisplayName(), characterTakingDamage:getX(), characterTakingDamage:getY(), characterTakingDamage:getZ())
170 FrameworkZ.Logs:AddLog(FrameworkZ.Logs.LogTypes.PLAYER_TAKE_DAMAGE, message, characterTakingDamage)
171 elseif instanceof(characterGivingDamage, "IsoZombie") then
172 local message = string.format("%s took %d damage from a zombie (X: %d, Y: %d, Z: %d)", characterTakingDamage:getUsername(), damage, characterTakingDamage:getX(), characterTakingDamage:getY(), characterTakingDamage:getZ())
173 FrameworkZ.Logs:AddLog(FrameworkZ.Logs.LogTypes.ZOMBIE_TAKE_DAMAGE, message, characterTakingDamage)
175 elseif instanceof(characterTakingDamage, "IsoZombie") and instanceof(characterGivingDamage, "IsoPlayer") then
176 local message = string.format("%s hit a zombie with %s for %d damage (X: %d, Y: %d, Z: %d)", characterGivingDamage:getUsername(), handWeapon:getDisplayName(), damage, characterTakingDamage:getX(), characterTakingDamage:getY(), characterTakingDamage:getZ())
177 FrameworkZ.Logs:AddLog(FrameworkZ.Logs.LogTypes.ZOMBIE_GIVE_DAMAGE, message, characterGivingDamage)
180Events.OnWeaponHitCharacter.Add(FrameworkZ.Logs.OnWeaponHitCharacter)
Logs module for FrameworkZ. Logs player actions, system events, errors, warnings, and informational m...
void LogPlayerAction(player, action)
void LoadLogsFromFile(player)
Load log entries from a file.
void OpenLogMenu()
Display logs in a menu for admins.
void AddLog(logType, message, player)
Add a log entry.
void LogSystemEvent(message)
void SaveLogToFile(logEntry)
Save a log entry to a file.
FrameworkZ Logs MaxEntries
FrameworkZ Logs LogDirectory
void OnWeaponHitCharacter(characterGivingDamage, characterTakingDamage, handWeapon, damage)
Log damage dealt to players from players and zombies.
FrameworkZ Logs LogEntries
table SearchLogs(keyword)
Search log entries by keyword.
table GetLogs(logType, player)
Retrieve log entries.