-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
53 lines (42 loc) · 1.2 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- License: MIT
chatlog = {}
chatlog.modname = core.get_current_modname()
function chatlog.log(lvl, msg)
if not msg then
msg = lvl
lvl = nil
end
msg = "[" .. chatlog.modname .. "] " .. msg
if not lvl then
core.log(msg)
else
core.log(lvl, msg)
end
end
chatlog.format = core.settings:get("chatlog.format") or "%m/%d/%Y %H:%M:%S"
chatlog.single_file = core.settings:get_bool("chatlog.single_file", false)
chatlog.out = minetest.get_worldpath() .. "/chatlog"
if chatlog.single_file then
chatlog.out = chatlog.out .. ".txt"
end
local function write_log(name, msg)
local out_file = chatlog.out
if not chatlog.single_file then
-- make sure directory exists
if not core.mkdir(chatlog.out) then
chatlog.log("error", "could not create directory for writing: " .. chatlog.out)
return
end
out_file = chatlog.out .. "/" .. os.date("%Y_%m_%d") .. ".txt"
end
local f = io.open(out_file, "a")
if f then
f:write("(" .. os.date(chatlog.format) .. ") [" .. name .. "]: " .. msg .. "\n")
f:close()
else
chatlog.log("error", "could not open chatlog file for writing: " .. out_file)
end
end
if not core.settings:get_bool("chatlog.disable", false) then
core.register_on_chat_message(write_log)
end