0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-12-22 11:03:48 +01:00

Move default filter set to config file.

Show available filter sets in help output.
This commit is contained in:
Wouter R 2016-02-26 00:23:19 +01:00
parent 1db8bf3119
commit c8bea605f7
2 changed files with 46 additions and 48 deletions

View File

@ -1,9 +1,19 @@
local M = {} local M = {}
M.default = { -- TEST set M.default = {
['options'] = { ['mode'] = 'keep', count = 'none' },
['patterns'] = {
['%(error%)'] = 'red',
['%(warning%)'] = 'yellow',
['%(bulk%)'] = 'gray',
['setState%(%)'] = 'bblue'
}
}
M.test = { -- TEST set
['options'] = { ['mode'] = 'keep', count = 'all' }, ['options'] = { ['mode'] = 'keep', count = 'all' },
['patterns'] = { ['patterns'] = {
['%(info%)'] = 'magenta' ['%(info%)'] = 'yellow'
} }
} }
@ -14,14 +24,4 @@ M.printstart = {
} }
} }
M.test = {
['options'] = { 'delete_mode' },
['patterns'] = {
['(verbose)'] = 'underline,cyan,_delete',
['(info)'] = 'magenta',
['ABSD'] = '_nodelete',
['SE?RV?'] = 'bblue'
}
}
return M return M

View File

@ -45,18 +45,11 @@ local ANSI_COLORS = {
local ESCAPE_STR = string.char(27) .. "[" local ESCAPE_STR = string.char(27) .. "["
local RESET_CODE = ESCAPE_STR .. "m" local RESET_CODE = ESCAPE_STR .. "m"
local DFL_FILTERSET_FILE = "loglite-filters.lua" local DFL_FILTERSET_FILE = "loglite-filters.lua"
local DEFAULT_FILTERSET = {
['options'] = { ['mode'] = 'keep', count = 'none' },
['patterns'] = {
['%(error%)'] = 'red',
['%(warning%)'] = 'yellow',
['%(bulk%)'] = 'gray',
['setState%(%)'] = 'bblue'
}
}
--[[========================================================================]]--
--- Stringifies the given object. --- Stringifies the given object.
-- From util/utils.lua -- From util/utils.lua
@ -134,9 +127,10 @@ end
--[[========================================================================]]-- --[[========================================================================]]--
local function tailStream(stream, filterSet) local function tailStream(stream, filterSet)
patterns = filterSet.patterns patterns = filterSet and filterSet.patterns or {}
options = filterSet.options options = filterSet and filterSet.options or { ['mode'] = 'keep' }
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
local embellished = line local embellished = line
@ -146,7 +140,7 @@ local function tailStream(stream, filterSet)
-- 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("[DEBUG] +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
@ -181,7 +175,7 @@ local function tailStream(stream, filterSet)
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("[DEBUG] -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
@ -192,11 +186,11 @@ end
local function readConfigFile(filename, searchPath) local function readConfigFile(filename, searchPath)
fullPath = searchPath .. '/' .. filename fullPath = searchPath .. '/' .. filename
if not fileExists(fullPath) then if not fileExists(fullPath) then
-- print("[DEBUG] config file '" .. fullPath .. "' not found") --print("[DEBUG] config file '" .. fullPath .. "' not found")
return nil return nil
end end
-- print("[DEBUG] using config file '" .. fullPath .. "'") --print("[DEBUG] using config file '" .. fullPath .. "'")
-- require does not accept full paths? also, pcall does not help with dofile -- require does not accept full paths? also, pcall does not help with dofile
return dofile(fullPath) return dofile(fullPath)
end end
@ -204,41 +198,45 @@ end
--NOTE: if command-line options get any more complex, switch to a lightweight --NOTE: if command-line options get any more complex, switch to a lightweight
-- getopt like this one? https://attractivechaos.wordpress.com/2011/04/07/getopt-for-lua/ -- getopt like this one? https://attractivechaos.wordpress.com/2011/04/07/getopt-for-lua/
local function main() local function main()
-- handle command-line arguments
local showHelp, followFile, filterSetName = false, nil, 'default'
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: loglite.lua [file-to-tail] [filter-set]") showHelp = true
print(" If no arguments are supplied, or if the first one is `-', stdin is used as input.") else
print(" If no filter set is supplied, a set named `default' will be looked for.") if #arg > 0 and arg[1] ~= '-' then followFile = arg[1] end
print(" Filter sets can be defined in a file `loglite-filters.lua' in your home directory.") if #arg > 1 then filterSetName = arg[2] end
os.exit(0)
end end
local followFile = #arg > 0 and arg[1] ~= '-' and arg[1] or nil -- read filter set file if available
local filterSetName = #arg > 1 and arg[2] or 'default' local filterSet = nil
configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME')) or {}
--print("[DEBUG] following file: '" .. (followFile and followFile or "<stdin>") .. "', with filter set '" .. filterSetName .. "'.")
--local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r')
local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin
local filterSet = DEFAULT_FILTERSET
configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME'))
for k,_ in pairs(configSets) do for k,_ in pairs(configSets) do
if k == filterSetName then if k == filterSetName then
filterSet = configSets[filterSetName] filterSet = configSets[filterSetName]
-- print("[DEBUG] using filter set '" .. filterSetName .. "' from config") --print("[DEBUG] using filter set '" .. filterSetName .. "' from config")
break break
end end
end end
-- TODO: add commandline option to enable this flag -- if requested, display help and exit
if listAvailableFilterSets == true then if showHelp and showHelp == true then
print(" Available filter sets in " .. DFL_FILTERSET_FILE .. ": " .. keysToString(configSets, ', ')) print("Usage: loglite.lua [file-to-tail] [filter-set]")
print(" If no arguments are supplied, or if the first one is `-', stdin is used as input.")
print(" If no filter set is supplied, a set named `default' will be looked for.")
print(" Filter sets can be defined in a file `loglite-filters.lua' in your home directory.")
print()
print(" Available filter sets in " .. os.getenv('HOME') .. "/" .. DFL_FILTERSET_FILE .. ": " .. keysToString(configSets, ', ', true))
os.exit(0) os.exit(0)
end end
-------------------------
--print("[DEBUG] following file: '" .. (followFile and followFile or "<stdin>") .. "', with filter set '" .. filterSetName .. "'.")
--local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r')
local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin
pcall(tailStream, tailin, filterSet) -- Note: protected call to suppress interrupt error thrown by lines iterator pcall(tailStream, tailin, filterSet) -- Note: protected call to suppress interrupt error thrown by lines iterator
end end