FrameworkZ 10.8.3
Provides a framework for Project Zomboid with various systems.
Loading...
Searching...
No Matches
Logs.lua
Go to the documentation of this file.
1local Events = Events
3
4--! \brief Logs module for FrameworkZ. Logs player actions, system events, errors, warnings, and informational messages.
5--! \class FrameworkZ.Logs
6FrameworkZ.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
12-- Define log types
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",
32 ERROR = "Error",
33 WARNING = "Warning",
34 INFO = "Info"
36
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)
42 local logEntry = {
43 timestamp = os.time(),
44 logType = logType,
45 message = message,
46 player = player and player:getUsername() or nil
47 }
48 table.insert(self.LogEntries, logEntry)
50 -- Save the log entry to a file
51 self:SaveLogToFile(logEntry)
52
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)
56 end
58 print(string.format("[%s] %s: %s", os.date("%Y-%m-%d %H:%M:%S", logEntry.timestamp), logType, message))
59end
60
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)
66 if fileWriter then
67 fileWriter:write(string.format("[%s] %s: %s\n", os.date("%Y-%m-%d %H:%M:%S", logEntry.timestamp), logEntry.logType, logEntry.message))
68 fileWriter:close()
69 else
70 print("Error opening log file:", filename)
71 end
72end
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)
83 end
84 end
85 return filteredLogs
86end
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)
93 if fileReader then
94 while true do
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)}),
101 logType = logType,
102 message = message,
103 player = player
104 })
105 end
106 end
107 fileReader:close()
108 else
109 print("Error opening log file:", filename)
110 end
111end
112
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)
121 end
122 end
123 return searchResults
124end
125
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
131end
132
133-- Example usage of logging player actions
134function FrameworkZ.Logs:LogPlayerAction(player, action)
135 self:AddLog(self.LogTypes.PLAYER_ACTION, action, player)
136end
137
138-- Example usage of logging system events
139function FrameworkZ.Logs:LogSystemEvent(message)
140 self:AddLog(self.LogTypes.SYSTEM_EVENT, message)
141end
142
143-- Example usage of logging errors
144function FrameworkZ.Logs:LogError(message)
145 self:AddLog(self.LogTypes.ERROR, message)
146end
147
148-- Example usage of logging warnings
149function FrameworkZ.Logs:LogWarning(message)
150 self:AddLog(self.LogTypes.WARNING, message)
151end
152
153-- Example usage of logging informational messages
154function FrameworkZ.Logs:LogInfo(message)
155 self:AddLog(self.LogTypes.INFO, message)
156end
157
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)
168
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)
174 end
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)
178 end
179end
180Events.OnWeaponHitCharacter.Add(FrameworkZ.Logs.OnWeaponHitCharacter)
void local message
void local Events()
void local instanceof()
void local message()
void local player()
Logs module for FrameworkZ. Logs player actions, system events, errors, warnings, and informational m...
Definition Logs.lua:5
FrameworkZ Logs __index
Definition Logs.lua:7
void LogPlayerAction(player, action)
void LogWarning(message)
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
Definition Logs.lua:11
FrameworkZ Logs LogDirectory
Definition Logs.lua:13
void OnWeaponHitCharacter(characterGivingDamage, characterTakingDamage, handWeapon, damage)
Log damage dealt to players from players and zombies.
FrameworkZ Logs LogTypes
Definition Logs.lua:15
void LogInfo(message)
void LogError(message)
FrameworkZ Logs LogEntries
Definition Logs.lua:9
table SearchLogs(keyword)
Search log entries by keyword.
table GetLogs(logType, player)
Retrieve log entries.