-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.lua
50 lines (43 loc) · 1.04 KB
/
log.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
--[[
log.lua - simple pure-lua logging functions
This library aims to provide a simple logging function in pure.
It supports output to multiple streams, making seperate log files easy.
Licensed unter the MIT license.
For deatils see: https://github.com/max1220/lua-log/
]]
local function log(enabled, class, streams, color)
local function _log(stream)
return function(...)
local _s = {}
for _, str in ipairs({...}) do
table.insert(_s, tostring(str))
end
local s = table.concat(_s, "\t")
if color then
stream:write(string.char(27), "[" .. color .. "m")
end
stream:write("["..os.date().."]["..class.."]\t"..s.."\n")
if color then
stream:write(string.char(27), "[0m")
end
end
end
if enabled then
if type(streams) == "table" then
local logs = {}
for _,stream in pairs(streams) do
table.insert(logs, _log(stream))
end
return function(...)
for _,log_f in ipairs(logs) do
log_f(...)
end
end
else
return _log(steams or io.stdout)
end
else
return function() end
end
end
return log