-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.lua
101 lines (83 loc) · 2.53 KB
/
logger.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
--[[
@script logger
@created May 9, 2022
@github https://github.com/Wizxrd/DCSLogger
@version 0.1.1
@author Wizard
@description
Simple Object Orientated logger module for DCS World Mission Scripting Environment and Hook Environment.
@features
- 6 levels of logging
- custom log files
- optional format of date and time for custom log files
]]
local format = string.format
local osdate
if os then osdate = os.date end
logger = {
["openmode"] = "a",
["datetime"] = "%Y-%m-%d %H:%M:%S",
["level"] = 6,
}
logger.enum = {
["none"] = 0,
["alert"] = 1,
["error"] = 2,
["warning"] = 3,
["info"] = 4,
["debug"] = 5,
["trace"] = 6
}
local callbacks = {
{["method"] = "alert", ["enum"] = "ALERT"},
{["method"] = "error", ["enum"] = "ERROR"},
{["method"] = "warning", ["enum"] = "WARNING"},
{["method"] = "info", ["enum"] = "INFO"},
{["method"] = "debug", ["enum"] = "DEBUG"},
{["method"] = "trace", ["enum"] = "TRACE"},
}
for i, callback in ipairs(callbacks) do
logger[callback.method] = function(self, func, message, ...)
if self.level < i then
return
end
local logMessage = format(message, ...)
if self.file then
local fullMessage = format("%s %s\t%s: %s: %s\n", osdate(self.datetime), callback.enum, self.source, func, logMessage)
self.file:write(fullMessage)
return
end
log.write(self.source, log[callback.enum], format("%s: %s", func, logMessage))
end
end
function logger:new(source, level, file, openmode, datetime)
local self = setmetatable({}, {__index = logger})
self.source = source
if type(level) == "number" then self.level = level end
if type(level) == "string" then self.level = self.enum[level] end
if file then
if not openmode then openmode = logger.openmode end
self.file = assert(io.open(file, openmode))
end
if datetime then self.datetime = datetime end
return self
end
function logger:setSource(source)
self.source = source
return self
end
function logger:setLevel(level)
if type(level) == "number" then self.level = level end
if type(level) == "string" then self.level = self.enum[level] end
return self
end
function logger:setFile(file, openmode)
if self.file then self.file:close() end
if not openmode then openmode = logger.openmode end
self.file = assert(io.open(file, openmode))
return self
end
function logger:setDateTime(datetime)
self.datetime = datetime
return self
end