From 32a0613780f5ad116ff08072de6f3d0150763fef Mon Sep 17 00:00:00 2001 From: Sokomine Date: Fri, 9 Sep 2022 22:13:42 +0200 Subject: [PATCH] moved logging into fs_log.lua and logged in extra npc log files --- config.lua | 4 +++- fs_log.lua | 36 ++++++++++++++++++++++++++++++++++++ functions.lua | 24 ------------------------ init.lua | 3 +++ 4 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 fs_log.lua diff --git a/config.lua b/config.lua index 9f6e31a..ccfaad1 100644 --- a/config.lua +++ b/config.lua @@ -112,6 +112,9 @@ yl_speak_up.inventory_path = "yl_speak_up_inventories" -- Where shall player-specific varialbes (usually quest states) be stored? yl_speak_up.player_vars_save_file = "yl_speak_up_player_vars" +-- Where to store the logfiles for the individual NPC +yl_speak_up.log_path = "yl_speak_up_log" + -- amount of time in seconds that has to have passed before the above file will be saved again -- (more time can pass if no variable is changed) yl_speak_up.player_vars_min_save_time = 60 @@ -246,4 +249,3 @@ local function read_skins_and_capes_from_folders(races) end end read_skins_and_capes_from_folders({"dwarf","elf","goblin","human","npc","orc"}) - diff --git a/fs_log.lua b/fs_log.lua new file mode 100644 index 0000000..52a33c0 --- /dev/null +++ b/fs_log.lua @@ -0,0 +1,36 @@ +-- 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 diff --git a/functions.lua b/functions.lua index 1a0d40e..b7367ad 100644 --- a/functions.lua +++ b/functions.lua @@ -528,27 +528,3 @@ yl_speak_up.may_edit_npc = function(player, n_id) or (yl_speak_up.speak_to[pname] and yl_speak_up.speak_to[pname].may_edit_this_npc)) end - - --- 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") - - local log_text = "<"..tostring(n_id).."> ["..tostring(pname).."]: "..text - -- log in general logfile - minetest.log("yl_speak_up "..log_text) - -- log with timestamp - local log_text = tostring(os.time())..log_text - -- TODO: log in a file for each npc and show it on demand? -end diff --git a/init.lua b/init.lua index 9332e60..3d663f0 100644 --- a/init.lua +++ b/init.lua @@ -28,6 +28,8 @@ yl_speak_up.custom_server_functions = {} -- the server-specific configuration dofile(modpath .. "config.lua") +-- logging and showing the log +dofile(modpath .. "fs_log.lua") -- players *and* npc need privs for certain things dofile(modpath .. "privs.lua") -- contains the command to hand out privs to NPC @@ -127,6 +129,7 @@ end minetest.mkdir(yl_speak_up.worldpath..yl_speak_up.path) minetest.mkdir(yl_speak_up.worldpath..yl_speak_up.inventory_path) +minetest.mkdir(yl_speak_up.worldpath..yl_speak_up.log_path) yl_speak_up.mob_table = yl_speak_up.init_mob_table() or {}