0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-11-05 14:13:22 +01:00

loglite enhancements:

- Attempt reading named filter sets from $HOME/loglite-filters.lua
- Slightly improved debug printing
This commit is contained in:
Wouter R 2016-02-25 10:29:02 +01:00
parent 9b88313506
commit fc026f1b38
2 changed files with 47 additions and 7 deletions

View File

@ -1,5 +1,12 @@
local M = {} local M = {}
M.default = { -- TEST set
['options'] = { ['mode'] = 'keep', count = 'all' },
['patterns'] = {
['%(info%)'] = 'magenta'
}
}
M.printstart = { M.printstart = {
['options'] = { ['mode'] = 'delete' }, ['options'] = { ['mode'] = 'delete' },
['patterns'] = { ['patterns'] = {

View File

@ -105,11 +105,22 @@ local function hasValue(t, needle)
return nil return nil
end end
--- Determines if filename exists and can be opened for reading.
-- From http://stackoverflow.com/a/4991602
-- @string filename The file to test.
-- @return True if the file exists and is readable, false otherwise.
function fileExists(filename)
local f = io.open(filename, "r")
if f ~= nil then io.close(f) return true else return false end
end
--[[========================================================================]]-- --[[========================================================================]]--
local function tailStream(stream, patterns, options) local function tailStream(stream, filterSet)
patterns = filterSet.patterns
options = filterSet.options
local c = 0 local c = 0
for line in stream:lines() do for line in stream:lines() do
--c = c + 1 -- Note: this would also count deleted lines --c = c + 1 -- Note: this would also count deleted lines
@ -120,7 +131,7 @@ local function tailStream(stream, patterns, options)
-- look for a pattern matching this line -- look for a pattern matching this line
for p,c in pairs(patterns) do for p,c in pairs(patterns) do
if line:match(p) then if line:match(p) then
-- print("+matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'") -- print("[DEBUG] +matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'")
local kws = c:split(',') local kws = c:split(',')
if hasValue(kws, '_delete') then keepLine = false; keepLineOverridden = true if hasValue(kws, '_delete') then keepLine = false; keepLineOverridden = true
@ -155,13 +166,26 @@ local function tailStream(stream, patterns, options)
if options.count == 'all' then print(c, embellished) if options.count == 'all' then print(c, embellished)
else print(embellished) end else print(embellished) end
else else
-- print("-skipped '"..line.."'") -- print("[DEBUG] -skipped '"..line.."'")
end end
--c = line:match 'truncated' and 0 or c -- from tail on stderr apparently --c = line:match 'truncated' and 0 or c -- from tail on stderr apparently
end end
end end
--TODO: could be extended to look for multiple filenames in multiple paths
local function readConfigFile(filename, searchPath)
fullPath = searchPath .. '/' .. filename
if not fileExists(fullPath) then
-- print("[DEBUG] config file '" .. fullPath .. "' not found")
return nil
end
-- print("[DEBUG] using config file '" .. fullPath .. "'")
-- require does not accept full paths? also, pcall does not help with dofile
return dofile(fullPath)
end
local function main() local function main()
if #arg > 0 and arg[1] == "-h" or arg[1] == "--help" then if #arg > 0 and arg[1] == "-h" or arg[1] == "--help" then
print("Usage: either pass file to tail as argument, or pipe through stdin.") print("Usage: either pass file to tail as argument, or pipe through stdin.")
@ -169,17 +193,26 @@ local function main()
end end
local followFile = #arg > 0 and arg[1] or nil local followFile = #arg > 0 and arg[1] or nil
local filterSetName = 'default' -- TODO: parse from options and leave at 'default' if not specified
--print("following file: '" .. (followFile and followFile or "<stdin>") .. "'.") --print("[DEBUG] following file: '" .. (followFile and followFile or "<stdin>") .. "'.")
--local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r') --local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r')
local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin
local patterns = DEFAULT_FILTERSET.patterns local filterSet = DEFAULT_FILTERSET
local options = { ['mode'] = 'keep', count = 'none' }
pcall(tailStream, tailin, patterns, options) -- Note: protected call to suppress interrupt error thrown by lines iterator configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME'))
for k,_ in pairs(configSets) do
if k == filterSetName then
filterSet = configSets[filterSetName]
-- print("[DEBUG] using filter set '" .. filterSetName .. "' from config")
break
end
end
pcall(tailStream, tailin, filterSet) -- Note: protected call to suppress interrupt error thrown by lines iterator
end end
main() main()