-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlog.lua
712 lines (626 loc) · 22 KB
/
wlog.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
-- =====================================================
-- wlog: an example of lua logger
--
-- author: di carlo walter
--
-- date: july/august 2016
--
-- wlog.set_level( <level> ) -- set level of all modules to <level>
-- wlog.<mod>.<level> ( <msg> ) -- log <msg> if <level> is active
-- wlog.<mod>.<level>.writer = wlog.writers.<writer> -- set single writer
-- wlog.<mod>.<level>.writer = { wlog.writers.<writer1>, wlog.writers.<writer2>, ...} -- set group of writers
-- wlog.<mod>.<level> ( ) -- true if <level> is active, otherwise false
-- wlog.<mod> ( <level> ) -- set <mod>'s level to <level>
-- wlog.<mod> ( ) -- get <mod>'s level
-- wlog.<level> () -- is equivalent to wlog.GEN.<level> ()
-- wlog.<mod>.<level> ( wlog.<mod>.<level>() and <msg> ) -- log <msg> if <level> is active
-- wlog.<mod> ( wlog.<mod>.<level>() and <msg> ) -- log <msg> if <level> is active
-- if wlog.<mod>.<level>() then wlog.<mod>(<msg1>); wlog.<mod>(<msg2>); end -- log set of messages using contextual level
-- if wlog.<mod>.<level>{"tag1",...} then wlog.<mod>(<msg1>); wlog.<mod>(<msg2>); end -- log set of messages using contextual level
-- wlog.<mod> ( wlog.<mod>.<level>{"tag1",...} and <msg> ) -- log <msg> if <level> is active
-- wlog.<mod>.<level> ( msg, "tag" ) .. single tag
-- wlog.<mod>.<level> ( msg, {"tag1","tag2", ...} ) -- tags in or expression
-- wlog.<mod>.<level> {"tag1","tag2", ...} -- tags in or expression
-- wlog() -- equivalent to wlog.GEN() which return level of GEN module
-- wlog(<level>) -- equivalent to wlog.GEN(<level>)
-- wlog.tags{<tag1>,...} -- start logging section associated to <tag1>,...
-- wlog.tags() -- get logging section's tags
-- wlog.tags{} -- stop logging section with associated tags <tag1>,...
-- wlog.config{<config>} -- set configuration
-- wlog.config() -- get configuration
-- wlog.plugin(<plugin>) -- set wlog plugin
-- wlog.fromlevel(<internal_level) -- get external representation of level
-- wlog.tolevel (<external_level) -- get internal representation of level
-- wlog.frommodule(<internal_module) -- get external representation of module
-- wlog.tomodule (<external_module) -- get internal representation of module
--
-- TODO:
-- wlog.<mod>.set_level ( level )
-- wlog.<mod>.<level> ( msg, "tag expression" )
-- wlog.<mod>.<level> { tags="tag expression", msg }
-- =====================================================
local ok,dbg = pcall(require,"debugger")
if not ok then
dbg = function() end
end
dbg = function() end -- disable debugger
local debug = false
local _plugin = nil
local function prequire(m)
local ok, res = pcall(require, m)
if not ok then return nil, res end -- return error
return res
end
local core = prequire("wlog.core")
local inspect = prequire("inspect")
local tostring = inspect and function (arg)
if type(arg) == "table" then return inspect(arg) end
return tostring(arg)
end or tostring
if core == nil then
core = {
enums = {
WLOG_LEVELS = {
LOG_ERR = 1,
LOG_WARN = 2,
LOG_INFO = 3,
LOG_TRACE = 4,
LOG_DEBUG = 5,
},
WLOG_FUNCTS = {
LOG_GEN = 1,
LOG_ALM = 2,
LOG_SYS = 3,
LOG_SQL = 4,
LOG_GUI = 5,
LOG_SRV = 6,
LOG_WEB = 7,
}
}
}
end
local defaults = {
module = "GEN",
level = "INFO",
writer = "con",
}
local wlog = {}
local ctags = {} -- contextual tags
local levels = {}
local modules = {}
local writers = {}
local function is_level( level )
for k,v in pairs(levels) do
if v == level then return true end
end
return false
end
local function is_module(module)
return modules[module] ~= nil
end
local function add_module(module)
assert(type(module) == "string")
local max = 0
for k,v in pairs(modules) do
if k == module then
return modules[module]
end
if v.id > max then max = v.id end
end
modules[module] = {
id = max + 1,
name = module
}
if debug then print("Added module: "..tostring(modules[module])) end
return modules[module]
end
local fromlevel = function (level)
assert(type(level)=="string","Wrong level")
return string.sub(level,5)
end
local tolevel = function (level)
assert(type(level)=="string","Wrong level")
return "LOG_"..level
end
local frommodule = function (module)
assert(type(module)=="string","Wrong module")
return string.sub(module,5)
end
local tomodule = function (module)
assert(type(module)=="string","Wrong module")
return "LOG_"..module
end
local function is_in(tags,tag)
assert(type(tags) == "table")
assert(type(tag) == "string")
for _,t in ipairs(tags) do
if t == tag then return true end
end
return false
end
local con = function (txt)
if txt and type(txt) == "string" then
print("con: "..txt)
end
end
local ram = function (txt)
if txt and type(txt) == "string" then
print("ram: "..txt)
end
end
---------------------------------------------------------------------------
-- sto is a wlog writer that rolls over the logfile
-- once it has reached a certain size limit. It also mantains a
-- maximum number of log files.
--
-- sto is based on code present in the module rollfile
--
-- @author Tiago Cesar Katcipis ([email protected])
--
-- @copyright 2004-2013 Kepler Project
---------------------------------------------------------------------------
--
local rollfile = {
config = {
filename = "log.txt",
maxSize = 50000,
maxIndex = 5
}
}
local function openFile(self)
if self.file then
return nil, string.format("file `%s' is already open", self.filename)
end
self.file = io.open(self.filename, "a")
if not self.file then
return nil, string.format("file `%s' could not be opened for writing", self.filename)
end
self.file:setvbuf ("line")
return self.file
end
local rollOver = function (self)
for i = self.maxIndex - 1, 1, -1 do
-- files may not exist yet, lets ignore the possible errors.
os.rename(self.filename.."."..i, self.filename.."."..i+1)
end
self.file:close()
self.file = nil
local _, msg = os.rename(self.filename, self.filename..".".."1")
if msg then
return nil, string.format("error %s on log rollover", msg)
end
return openFile(self)
end
local openRollingFileLogger = function (self)
if not self.file then
return openFile(self)
end
local filesize = self.file:seek("end", 0)
if (filesize < self.maxSize) then
return self.file
end
return rollOver(self)
end
local sto = function (txt)
local f, err = openRollingFileLogger(rollfile.config)
if not f then
print("ERROR: "..err)
return nil
end
if txt and type(txt) == "string" then
f:write("sto: "..txt.."\n")
end
end
---------------------------------------------------------------------------
--
local nul = function (txt)
end
local function is_writer(func)
for name,writer in pairs(writers) do
if func == writer then return true end
end
return false
end
local eval = function (module,level,tags,modules,context)
if module.level.value >= level.value then
local wrt = rawget(context,"_writer")
return true, wrt
end
local res = false
local writer = nil
if tags then
for _,tag in ipairs(tags) do
-- if any tag has level lower than the current one then the text can be logged
--if modules[tag].level.value <= module.level.value then
if modules[tag].level.value >= level.value then
res = true
writer = rawget(context,"_writer")
break
end
end
end
return res, writer
end
local level_mt = {
__call = function(self,...)
rawset(self.module,"_last",self.level)
rawset(self.module,"_last_tags",nil)
local n = select("#",...)
if n == 0 then
local eval_res, eval_writer = eval(self.module,self.level,nil,wlog,self)
return eval_res
end
local tags = nil
local ntags = 0
if n == 1 then
local par = select(1,...)
local tpar = type(par)
if tpar == "table" then
-- add modules
ntags = 0
tags = {}
for _,tag in ipairs(par) do
if tags == nil then tags = {} end
add_module(tag)
if is_in(tags,tag) == false then
tags[#tags + 1] = tag
ntags = ntags + 1
end
end
end
elseif n > 1 then
tags = select(2,...)
local ttags = type(tags)
if ttags == "string" then
-- add module
ntags = 1
add_module(tags)
tags = { tags } -- put in an array
elseif ttags == "table" then
-- add modules
ntags = 0
for _,tag in ipairs(tags) do
add_module(tag)
ntags = ntags + 1
end
end
end
if ctags and #ctags > 0 then
for _,tag in ipairs(ctags) do
add_module(tag)
if tags == nil then tags = {} end
if is_in(tags,tag) == false then
tags[#tags + 1] = tag
ntags = ntags + 1
end
end
end
if tags then
rawset(self.module,"_last_tags",tags)
end
-- evaluate status considering also the tags
local eval_res, eval_writer = eval(self.module,self.level,tags,wlog,self)
if eval_res == false then
return false
end
-- ok, the text can be logged
local par = select(1,...)
local tpar = type(par)
if tpar == "string" then
local msg = self.module.name..": level="..self.level.name.." msg="..par
if ntags > 0 then
for _,tag in ipairs(tags) do
msg = msg .. " #"..tag
end
end
-- use writers of the module
local writer = rawget(self,"_writer")
local writer = eval_writer
if type(writer) == "function" then
writer(msg)
elseif type(writer) == "table" then
for _,out in ipairs(writer) do
if out then out(msg)
end
end
end
return true
elseif tpar == "boolean" then
return par
elseif tpar == "table" and ntags > 0 then
return true
end
return false
end,
__index = function(self,key)
--assert(writers[key] or key == "writer","Wrong writer: "..key)
if key == "writer" and rawget(self,"_writer") == nil then
rawset(self,"_writer",writers[defaults.writer])
end
--return self._writer
local writer = rawget(self,"_writer") -- self._writer
if type(writer) == "table" then
writer = writer[1]
end
return writer
end,
__newindex = function(self,key,val)
assert(type(key)=="string","Wrong key: "..tostring(key))
assert(key=="module" or key=="writer" or key=="level","Wrong key: "..tostring(key))
--assert(val~=nil,"Wrong value: nil")
if key=="writer" then
local wt = type(val)
--assert(wt=="function" or wt=="table")
assert(wt=="function" or wt=="table" or wt=="nil")
-- check it is a valid writer
if wt == "function" then
assert(is_writer(val),"Wrong writer")
elseif wt == "table" then
for _,w in ipairs(val) do
assert(type(w) == "function", "Wrong writer")
assert(is_writer(w), "Wrong writer")
end
--else
-- assert(false,"Wrong writer type")
end
rawset(self,"_writer", val)
return
end
if debug then print("key="..tostring(key).. " val="..tostring(val)) end
rawset(self,key,val)
end
}
local module_mt = {
__call = function(self,...)
local n = select("#",...)
if debug then print("Module: "..self.name.." Level: "..tostring(self.level)) end
if n == 0 then
return self.level
end
local par = select(1,...)
local tpar = type(par)
if tpar == "table" then
assert( is_level(par), "wrong level: "..tostring(par))
self.level = par
elseif tpar == "string" then
-- message to log using the last level/tags
local level = rawget(self,"_last")
local tags = rawget(self,"_last_tags")
if level == nil then return end -- no know message level
local lvl = fromlevel(level.name)
if tags then
self[lvl](par,tags)
else
self[lvl](par)
end
return self.level
end
end,
__index = function(self,key)
assert(type(key)=="string","Wrong key type for: "..tostring(key))
assert(levels[key]~=nil,"Wrong level: "..tostring(key))
self[key] = setmetatable({}, level_mt)
self[key].level = levels[key]
--self[key].writer = writers[defaults.writer]
self[key].writer = nil
self[key].module = self
return self[key]
end,
}
local wlog_mt = {
__call = function(self,...)
local n = select("#",...)
if n == 0 then
return rawget(self[defaults.module],"level")
end
local par = select(1,...)
return self[defaults.module](par)
end,
__index = function(self,key)
assert(type(key)=="string","Wrong key type for: "..tostring(key))
if is_module(key) == false then
add_module(key)
end
self[key] = setmetatable({}, module_mt)
self[key].level = levels[defaults.level]
self[key].name = key
self[key].id = modules[key]["id"]
return self[key]
end,
}
local function is_ctag(tag)
for _,t in ipairs(ctags) do
if t == tag then return true end
end
return false
end
local function tags(list)
assert(list == nil or type(list)=="table","Wrong list of tags")
if list == nil then
return ctags
end
if #list == 0 then
ctags = {}
return ctags
end
-- check if list of modules need to be updated
for _,tag in ipairs(list) do
if is_ctag(tag) == false then
ctags[#ctags+1] = tag
end
end
end
local function set_level(level)
assert(type(level) == "table","Wrong level: "..tostring(level))
assert(level.name,"Missing level's name")
assert(level.value,"Missing level's value")
assert(core.enums.WLOG_LEVELS[level.name],"Unknown level")
assert(core.enums.WLOG_LEVELS[level.name] == level.value,
"Wrong level - expected "..core.enums.WLOG_LEVELS[level.name].." got "..level.value)
rawset(wlog,"level",level)
if debug then print("wlog.level = "..wlog.level.name) end
for k,v in pairs(wlog.modules)
do
--local mod = frommodule(k)
--if wlog[mod] then
-- rawset(wlog[mod],"level",level)
--end
if wlog[k] then
rawset(wlog[k],"level",level)
end
end
end
local _reset = function()
ctags = {} -- contextual tags
levels = {}
modules = {}
writers = {}
for k,v in pairs(wlog) do
if type(v) == "table" then
rawset(wlog,k,nil)
end
end
end
local function _init_enums()
-- levels defaults
for k,v in pairs(core.enums.WLOG_LEVELS)
do
local lvl = fromlevel(k)
dbg("k = "..tostring(k).." lvl = "..tostring(lvl))
levels[lvl] = {
name = k,
value = v,
}
end
if debug then print("levels:\n",tostring(levels)) end
-- modules defaults
for k,v in pairs(core.enums.WLOG_FUNCTS)
do
local mod = frommodule(k)
modules[mod] = {
id = v,
name = k
}
end
if debug then print("modules:\n",tostring(modules)) end
end
local function _init_writers()
writers.con = con
writers.ram = ram
writers.sto = sto
writers.nul = nul
if debug then print("writers:\n",tostring(writers)) end
end
local function plugin(wlog_plugin)
dbg("begin of wlog_plugin")
if wlog_plugin == nil then return _plugin end
assert(wlog_plugin and type(wlog_plugin)=="table","Wrong plugin")
assert(wlog_plugin.is_wlog_plugin,"Wrong plugin")
assert(wlog_plugin.wlog_plugin_name,"Wrong plugin")
_reset()
--if wlog_plugin.setup then wlog_plugin.setup(wlog) end
if wlog_plugin.enums then
assert(wlog_plugin.enums.WLOG_FUNCTS,"Wrong enums")
assert(wlog_plugin.enums.WLOG_LEVELS,"Wrong enums")
core.enums = wlog_plugin.enums
dbg("after enums setup")
end
if wlog_plugin.fromlevel then fromlevel = wlog_plugin.fromlevel end
if wlog_plugin.tolevel then tolevel = wlog_plugin.tolevel end
if wlog_plugin.frommodule then frommodule = wlog_plugin.frommodule end
if wlog_plugin.tomodule then tomodule = wlog_plugin.tomodule end
wlog.fromlevel = fromlevel
wlog.tolevel = tolevel
wlog.frommodule = frommodule
wlog.tomodule = tomodule
dbg("before _init_enums")
_init_enums()
dbg("before _init_writers")
_init_writers()
if wlog_plugin.con then writers.con = wlog_plugin.con end
if wlog_plugin.ram then writers.ram = wlog_plugin.ram end
if wlog_plugin.sto then writers.sto = wlog_plugin.sto end
--if wlog_plugin.setup then wlog_plugin.setup() end
if wlog_plugin.config then config = wlog_plugin.config end
if wlog_plugin.defaults then
local defs = wlog_plugin.defaults
if defs.module then defaults.module = defs.module end
if defs.level then defaults.level = defs.level end
if defs.writer then defaults.writer = defs.writer end
end
if wlog_plugin.eval then eval = wlog_plugin.eval end
_plugin = wlog_plugin
wlog.writers = writers
wlog.levels = levels
wlog.modules = modules
wlog.tags = tags
wlog.config = config
dbg("before set_level")
set_level(levels[defaults.level])
for k,v in pairs(core.enums.WLOG_LEVELS)
do
local lvl = fromlevel(k)
wlog[lvl] = wlog[defaults.module][lvl]
end
if wlog_plugin.setup then wlog_plugin.setup(wlog) end
if debug then print("wlog: pluged-in: "..wlog_plugin.wlog_plugin_name) end
return true
end
local config = function (cfg)
-- {
-- enums = <wlog enums>,
-- defaults = <wlog defaults>,
-- }
if cfg then
assert(type(cfg) == "table", "Wrong config: "..tostring(cfg))
_reset()
if cfg.defaults then defaults = cfg.defaults end -- TODO: assert defaults struct
if cfg.enums then core.enums = cfg.enums end -- TODO: assert enums struct
_init_enums()
_init_writers()
wlog.writers = writers
wlog.levels = levels
wlog.modules = modules
wlog.tags = tags
for k,v in pairs(core.enums.WLOG_LEVELS)
do
local lvl = fromlevel(k)
wlog[lvl] = wlog[defaults.module][lvl]
end
set_level(levels[defaults.level])
else
local cfg = {}
cfg.defaults = defaults
cfg.enums = core.enums
return cfg
end
end
local function _init()
wlog = {}
_init_enums()
_init_writers()
wlog = setmetatable(wlog,wlog_mt)
wlog._VERSION = "1.0"
wlog.set_level = set_level
wlog.writers = writers
wlog.levels = levels
wlog.modules = modules
wlog.tags = tags
wlog.plugin = plugin
wlog.config = config
wlog.fromlevel = fromlevel
wlog.tolevel = tolevel
wlog.frommodule = frommodule
wlog.tomodule = tomodule
wlog.is_module = is_module
wlog.is_writer = is_writer
wlog.add_module = add_module
set_level(levels[defaults.level])
for k,v in pairs(core.enums.WLOG_LEVELS)
do
local lvl = fromlevel(k)
wlog[lvl] = wlog[defaults.module][lvl]
end
end
_init()
return wlog