-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.lua
More file actions
68 lines (47 loc) · 1.73 KB
/
logger.lua
File metadata and controls
68 lines (47 loc) · 1.73 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local Logger = {}
Logger.DEBUG = 4
Logger.INFO = 3
Logger.WARNING = 2
Logger.WARN = 2 --Couldn't decide on naming...
Logger.ERROR = 1
--0 = no messages, higher = more
Logger.level = 0
local ansiFormatting = true
local function aFormat(str, ...)
--Formats str with ANSI escape sequences
if not ansiFormatting or table.getn({...}) == 0 then return str end
local formatting = ""
for i,v in ipairs({...}) do
formatting = formatting .. '[' .. v .. 'm'
end
return '\27' .. formatting .. str .. "\27[0m" --Reset to default
end
local function log(message, level)
if level > Logger.level then return end
local strErrLevel
if level == 4 then strErrLevel = aFormat("DEBUG", 32)
elseif level == 3 then strErrLevel = "INFO"
elseif level == 2 then strErrLevel = aFormat("WARNING", 33)
elseif level == 1 then strErrLevel = aFormat("ERROR", 31)
else
log("Invalid log level (" .. level .. ") of following message, setting to DEBUG level", Logger.WARNING)
level = Logger.DEBUG
strErrLevel = aFormat("DEBUG", 32)
end
local timeStamp = aFormat(os.date("%M:%S", os.time()), 2)
--Get info about the caller
t = debug.getinfo(2, "nSl")
--TODO: add detection for non file sources and format the name better
local fileName = aFormat(t.source, 1)
local prefix = string.format("%s [%s]\t%s:%s", timeStamp, strErrLevel, fileName, aFormat(t.currentline, 1))
--Only add the function name if it was called from inside a function
if t.name then prefix = prefix .. " " .. aFormat(t.name .. "():", 2) end
print(prefix .. " " .. message)
end
if love._os == "Windows" then
ansiFormatting = false
log("Windows detected, disabling ANSI formatting", Logger.INFO)
end
--Assign the function to the module name space
Logger.log = log
return Logger