yl_speak_up/fs_log.lua

37 lines
1.1 KiB
Lua

-- handle logging
-- log changes done by players or admins to NPCs
yl_speak_up.log_change = function(pname, n_id, text)
-- make sure all variables are defined
if(not(pname)) then
pname = "- unkown player -"
end
if(not(n_id)) then
n_id = "- unknown NPC -"
end
if(not(text)) then
text = "- no text given -"
end
-- we don't want newlines in the texts
text = string.gsub(text, "\n", "\\n")
-- log in debug.txt
local log_text = "<"..tostring(n_id).."> ["..tostring(pname).."]: "..text
minetest.log("yl_speak_up "..log_text)
-- log in a file for each npc so that it can be shown when needed
-- date needs to be inserted manually (minetest.log does it automaticly);
-- each file logs just one npc, so n_id is not important
log_text = tostring(os.date("%Y-%m-%d %H:%M:%S ")..tostring(pname).." "..text.."\n")
-- actually append to the logfile
local file, err = io.open(yl_speak_up.worldpath..yl_speak_up.log_path..DIR_DELIM..
"log_"..n_id..".txt", "a")
if err then
minetest.log("[yl_speak_up] Error saving NPC logfile: "..minetest.serialize(err))
return
end
file:write(log_text)
file:close()
end