-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
tail_logs.lua
77 lines (72 loc) · 1.64 KB
/
tail_logs.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
#!/usr/bin/env lua
--[[
This script will tail your journal.
Output will be in json format.
Uses the cqueues library (see http://25thandclement.com/~william/projects/cqueues.html)
]]
local cqueues = require "cqueues"
local bit = require "bit32"
local json = require "dkjson"
local sj = require "systemd.journal"
local j = assert(sj.open())
assert(j:seek_tail())
assert(j:previous())
-- Wrap the journal object with an object that implements the cqueues.poll interface
local wrap_journal do
local methods = {}
function methods:pollfd()
return self.j:get_fd()
end
function methods:events()
local mask = self.j:get_events()
local events = ""
if bit.band(mask, 1) ~= 0 then
events = events .. "r"
end
if bit.band(mask, 4) ~= 0 then
events = events .. "w"
end
return events
end
function methods:timeout()
local m = self.j:get_timeout()
if type(m) == "number" then
local now = cqueues.monotime()
return m - now;
end
end
local mt = {
__index = function(t,k)
local f = methods[k]
if f ~= nil then return f end
-- Delegate to original journal methods
local f = t.j[k]
if type(f) == "function" then
-- Need to make sure the correct 'self' is passed through
return function(o, ...)
if o == t then
o = t.j
end
return f(o, ...)
end
end
end;
}
function wrap_journal(j)
return setmetatable({j = j}, mt)
end
end
local w = wrap_journal(j)
local q = cqueues.new()
q:wrap(function()
while true do
local a = cqueues.poll(w)
if a and a:process() ~= sj.WAKEUP.NOP then
while a:next() do
local t = assert(a:to_table())
print(json.encode(t))
end
end
end
end)
assert(q:loop())