-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader_test.lua
95 lines (83 loc) · 2.53 KB
/
reader_test.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
require'reader'
__api = {}
function __api.await(delay)
local time = require("time")
time.sleep(delay / 1000.)
end
function __api.is_close()
return false
end
--[[ Use cases:
reader:open("access.log", "tail") -- default values
reader:open("access.log", "tail", true, 15) -- set limit
reader:open("access.log", "tail", true, -1, 3) -- set step
reader:open("access.log", "tail", false, 15) -- set limit without follow
reader:open("access.log", "tail", false, 20, 3) -- set limit and step without follow
reader:open("access.log", "head") -- default values
reader:open("access.log", "head", true, 15) -- set limit
reader:open("access.log", "head", true, -1, 3) -- set step
reader:open("access.log", "head", false, 15) -- set limit without follow
reader:open("access.log", "head", false, 20, 3) -- set limit and step without follow
]]
function reader_init()
local reader = CReader()
if not reader:open("access.log", "tail", true, -1, 1) then
print("Failed to initialize file descriptor")
os.exit(0)
end
return reader
end
function print_lines(lines)
local date_marker = os.date("%Y-%m-%d %H:%M:%S ", os.time())
date_marker = "<" .. date_marker .. "> "
if type(lines) == "table" then
for n = 1, #lines do
local prefix = date_marker .. tostring(n) .. ": "
print(prefix .. lines[n])
end
end
end
--[[ Inline using:
local reader = reader_init()
while not __api.is_close() do
local lines = reader:read_line(__api.await, __api.is_close)
if type(lines) ~= "table" then
break
end
print_lines(lines)
end
]]
--[[ Inline using with callback:
local reader = reader_init()
reader:read_line_cb(print_lines, __api.await, __api.is_close)
]]
--[[ Thread using in box with callback:
local thread = require'thread'
local rth = thread.new(function (reader_init, print_lines, await, is_close)
require'reader'
local reader = reader_init()
reader:read_line_cb(print_lines, await, is_close)
end, reader_init, print_lines, __api.await, __api.is_close)
rth:join()
]]
--[[ Thread using queue out of the box with callback:
local thread = require'thread'
local q = thread.queue(1)
local rth = thread.new(function (reader_init, q, await, is_close)
require'reader'
local reader = reader_init()
reader:read_line_cb(function (lines) q:push(lines) end, await, is_close)
q:push()
end, reader_init, q, __api.await, __api.is_close)
while true do
local _, v = q:shift()
if type(v) ~= "table" then
break
end
print_lines(v)
end
q:free()
print("wait thread")
rth:join()
]]
print("done")