This is now the best Lua logging library ever made.
This commit is contained in:
@@ -1,11 +1,8 @@
|
|||||||
local stored_messages = {}
|
|
||||||
local display_levels = {}
|
|
||||||
local enable_logging = true
|
|
||||||
local message_limit = math.huge
|
|
||||||
|
|
||||||
-- USAGE:
|
-- USAGE:
|
||||||
-- -- set arbitrary log levels to be printed immediately
|
-- -- set arbitrary log levels to be printed immediately
|
||||||
-- log{ info = true, warning = true, }
|
-- log{ info = true, warning = true, ducksauce = true, }
|
||||||
|
-- -- disable storage of messages depending on log level
|
||||||
|
-- log{ error = false, verbose = false, }
|
||||||
-- -- send anything (except nil) to a log level
|
-- -- send anything (except nil) to a log level
|
||||||
-- log("bacom", true, "text", 5, function() end, {})
|
-- log("bacom", true, "text", 5, function() end, {})
|
||||||
-- -- print all messages saved at a particular log level
|
-- -- print all messages saved at a particular log level
|
||||||
@@ -16,60 +13,70 @@ local message_limit = math.huge
|
|||||||
-- log(50) -- warning: reaching large limits makes logging expensive
|
-- log(50) -- warning: reaching large limits makes logging expensive
|
||||||
-- -- do anything to the stored messages (this example deletes them all)
|
-- -- do anything to the stored messages (this example deletes them all)
|
||||||
-- log(function(messages) return {} end)
|
-- log(function(messages) return {} end)
|
||||||
|
-- -- check if a log level is being displayed
|
||||||
|
-- if log.arbitrary_exit then log("arbitrary_exit") os.exit(1) end
|
||||||
|
|
||||||
return function(options, ...)
|
local microlog = {} -- display_levels are stored directly here
|
||||||
local options_type = type(options)
|
local stored_messages = {}
|
||||||
|
local enable_logging = true
|
||||||
|
local message_limit = math.huge
|
||||||
|
|
||||||
if (options_type == "string") and enable_logging then
|
return setmetatable(microlog, {
|
||||||
if not stored_messages[options] then stored_messages[options] = {} end
|
__call = function(options, ...)
|
||||||
local message_table = stored_messages[options]
|
local options_type = type(options)
|
||||||
|
|
||||||
-- turn log message into text
|
if (options_type == "string") and enable_logging then
|
||||||
local tab = {...}
|
if microlog[options] == false then return end
|
||||||
for i = 1, #tab do
|
if not stored_messages[options] then stored_messages[options] = {} end
|
||||||
tab[i] = tostring(tab[i])
|
local message_table = stored_messages[options]
|
||||||
end
|
|
||||||
local current_message = table.concat(tab, "\t")
|
|
||||||
|
|
||||||
if #current_message == 0 then
|
-- turn log message into text
|
||||||
-- print all stored_messages of specified level
|
local tab = {...}
|
||||||
for i = 1, #message_table do
|
for i = 1, #tab do
|
||||||
print(message_table[i])
|
tab[i] = tostring(tab[i])
|
||||||
|
end
|
||||||
|
local current_message = table.concat(tab, "\t")
|
||||||
|
|
||||||
|
if #current_message == 0 then
|
||||||
|
-- print all stored_messages of specified level
|
||||||
|
for i = 1, #message_table do
|
||||||
|
print(message_table[i])
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
-- store message (and print if in display_levels)
|
||||||
|
message_table[#message_table + 1] = current_message
|
||||||
|
if #message_table > message_limit then table.remove(message_table, 1) end
|
||||||
|
if microlog[options] then print(current_message) end
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
-- set display_levels
|
||||||
-- store message (and print if in display_levels)
|
elseif options_type == "table" then
|
||||||
message_table[#message_table + 1] = current_message
|
for k,v in pairs(options) do
|
||||||
if #message_table > message_limit then table.remove(message_table, 1) end
|
microlog[k] = v
|
||||||
if display_levels[options] then print(current_message) end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- set display_levels
|
|
||||||
elseif options_type == "table" then
|
|
||||||
for k,v in pairs(options) do
|
|
||||||
display_levels[k] = v
|
|
||||||
end
|
|
||||||
|
|
||||||
elseif options_type == "number" then
|
|
||||||
message_limit = options
|
|
||||||
for k,v in pairs(stored_messages) do
|
|
||||||
while #v > message_limit do
|
|
||||||
table.remove(v, 1)
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
-- turn all logging off and delete stored messages; or turn it on
|
elseif options_type == "number" then
|
||||||
elseif options_type == "boolean" then
|
message_limit = options
|
||||||
if options then
|
for k,v in pairs(stored_messages) do
|
||||||
enable_logging = true
|
while #v > message_limit do
|
||||||
else
|
table.remove(v, 1)
|
||||||
enable_logging = false
|
end
|
||||||
stored_messages = {}
|
end
|
||||||
end
|
|
||||||
|
|
||||||
-- arbitrary access :D
|
-- turn all logging off and delete stored messages; or turn it on
|
||||||
elseif options_type == "function" then
|
elseif options_type == "boolean" then
|
||||||
local result = options(stored_messages)
|
if options then
|
||||||
if type(result) == "table" then stored_messages = result end
|
enable_logging = true
|
||||||
|
else
|
||||||
|
enable_logging = false
|
||||||
|
stored_messages = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
-- arbitrary access :D
|
||||||
|
elseif options_type == "function" then
|
||||||
|
local result = options(stored_messages)
|
||||||
|
if type(result) == "table" then stored_messages = result end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user