-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlog_plugin_mock.lua
119 lines (97 loc) · 2.67 KB
/
wlog_plugin_mock.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
local mock_plugin = {
is_wlog_plugin = true,
wlog_plugin_name = "mock plugin",
}
local enums = {
WLOG_LEVELS = {
LOG_FATAL = 1,
LOG_ERROR = 2,
LOG_WARNING = 3,
LOG_INFO = 4,
LOG_TRACE = 5,
LOG_DEBUG = 6,
},
WLOG_FUNCTS = {
LOG_GEN = 1,
LOG_SYS = 2,
LOG_SQL = 3,
LOG_GUI = 4,
}
}
local defaults = {
module = "GEN",
level = "WARNING",
writer = "ram",
}
local fromlevel = function (level)
assert(type(level)=="string","Wrong level")
if level=="LOG_FATAL" then return "CRIT" end
return string.sub(level,5)
end
local tolevel = function (level)
assert(type(level)=="string","Wrong level")
if level == "CRIT" then return "LOG_FATAL" end
return "LOG_"..level
end
local con = function (txt)
if txt and type(txt) == "string" then
print("pcon: "..txt) -- just proof its usage
end
end
local ram = function (txt)
if txt and type(txt) == "string" then
print("pram: "..txt) -- just proof its usage
end
end
-- plugin setup
local _wlog = nil
local setup = function (wlog)
assert(type(wlog)=="table","Wrong wlog reference")
-- TODO: add more assertion to make sure a ref to wlog is provided
-- store a reference to the wlog
_wlog = wlog
end
-- same as wlog.<def_mod>(<level>) and wlog<def_mod>()
local config = function(cfg)
assert(_wlog,"Setup before config plugin")
if cfg then
assert(type(cfg) == "table","Wrong level: "..tostring(cfg))
assert(cfg.name,"Missing level's name")
assert(cfg.value,"Missing level's value")
assert(enums.WLOG_LEVELS[cfg.name],"Unknown level")
local tcfg = type(cfg)
if tcfg == "table" then
_wlog(cfg)
end
else
return _wlog()
end
end
-- compare current level with the level of the default module
local eval = function(module,level,tags,modules)
if level.value > modules[defaults.module].level.value then
local res = false
if tags then
for _,tag in ipairs(tags) do
if modules[tag].level.value <= modules[defaults.module].level.value then
res = true
break
end
end
end
if res == false then
return false
end
end
return true
end
mock_plugin.enums = enums
mock_plugin.defaults = defaults
mock_plugin.con = con
mock_plugin.ram = ram
mock_plugin.setup = setup
mock_plugin.config = config
mock_plugin.eval = eval
mock_plugin.fromlevel = fromlevel
mock_plugin.tolevel = tolevel
return mock_plugin