Skip to content
Snippets Groups Projects
Verified Commit 54ab3f78 authored by Vladimír Čunát's avatar Vladimír Čunát Committed by Tomas Krizek
Browse files

policy trace-logging improvements

The logs can be triggered from policy actions, in per-request fashion:
- they're on LOG_DEBUG level but always sent, regardless of log config
- those messages will show double group tags: "[reqdbg][foo   ]"
  (but they lack proper meta-data - about location of the log's origin)
- reqdbg is *in addition* to normal logs, so the lines may be duplicated
  if that's how the logging was configured
parent 0fdc1940
Branches
Tags
No related merge requests found
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
local kres = require('kres') local kres = require('kres')
local ffi = require('ffi') local ffi = require('ffi')
local LOG_GRP_POLICY_TAG = ffi.string(ffi.C.kr_log_grp2name(ffi.C.LOG_GRP_POLICY))
local LOG_GRP_REQDBG_TAG = ffi.string(ffi.C.kr_log_grp2name(ffi.C.LOG_GRP_REQDBG))
local todname = kres.str2dname -- not available during module load otherwise local todname = kres.str2dname -- not available during module load otherwise
-- Counter of unique rules -- Counter of unique rules
...@@ -643,31 +646,39 @@ end ...@@ -643,31 +646,39 @@ end
local debug_logline_cb = ffi.cast('trace_log_f', function (_, msg) local debug_logline_cb = ffi.cast('trace_log_f', function (_, msg)
jit.off(true, true) -- JIT for (C -> lua)^2 nesting isn't allowed jit.off(true, true) -- JIT for (C -> lua)^2 nesting isn't allowed
-- msg typically ends with newline ffi.C.kr_log_fmt(
log_debug(ffi.C.LOG_GRP_POLICY, "%s", msg) ffi.C.LOG_GRP_REQDBG, -- but the original [group] tag also remains in the string
LOG_DEBUG,
'CODE_FILE=policy.lua', 'CODE_LINE=', 'CODE_FUNC=policy.DEBUG_ALWAYS', -- no meaningful locations
'[%-6s]%s', LOG_GRP_REQDBG_TAG, msg) -- msg should end with newline already
end) end)
ffi.gc(debug_logline_cb, free_cb) ffi.gc(debug_logline_cb, free_cb)
-- LOG_DEBUG without log_trace and without code locations
local function log_notrace(req, fmt, ...)
ffi.C.kr_log_fmt(ffi.C.LOG_GRP_REQDBG, LOG_DEBUG,
'CODE_FILE=policy.lua', 'CODE_LINE=', 'CODE_FUNC=', -- no meaningful locations
'%s', string.format( -- convert in lua, as integers in C varargs would pass as double
'[%-6s][%-6s][%05u.00] ' .. fmt,
LOG_GRP_REQDBG_TAG, LOG_GRP_POLICY_TAG, req.uid, ...)
)
end
local debug_logfinish_cb = ffi.cast('trace_callback_f', function (req) local debug_logfinish_cb = ffi.cast('trace_callback_f', function (req)
jit.off(true, true) -- JIT for (C -> lua)^2 nesting isn't allowed jit.off(true, true) -- JIT for (C -> lua)^2 nesting isn't allowed
log_req(req, 0, 0, ffi.C.LOG_GRP_POLICY, log_notrace(req, 'following rrsets were marked as interesting:\n%s\n',
'following rrsets were marked as interesting:\n' ..
req:selected_tostring()) req:selected_tostring())
if req.answer ~= nil then if req.answer ~= nil then
log_req(req, 0, 0, ffi.C.LOG_GRP_POLICY, log_notrace(req, 'answer packet:\n%s\n', req.answer)
'answer packet:\n' .. tostring(req.answer))
else else
log_req(req, 0, 0, ffi.C.LOG_GRP_POLICY, log_notrace(req, 'answer packet DROPPED\n')
'answer packet DROPPED\n')
end end
end) end)
ffi.gc(debug_logfinish_cb, free_cb) ffi.gc(debug_logfinish_cb, free_cb)
-- log request packet -- log request packet
function policy.REQTRACE(_, req) function policy.REQTRACE(_, req)
log_req(req, 0, 0, ffi.C.LOG_GRP_POLICY, log_notrace(req, 'request packet:\n%s', req.qsource.packet)
'request packet:\n%s',
tostring(req.qsource.packet))
end end
function policy.DEBUG_ALWAYS(state, req) function policy.DEBUG_ALWAYS(state, req)
...@@ -691,8 +702,13 @@ function policy.DEBUG_IF(test) ...@@ -691,8 +702,13 @@ function policy.DEBUG_IF(test)
jit.off(true, true) -- JIT for (C -> lua)^2 nesting isn't allowed jit.off(true, true) -- JIT for (C -> lua)^2 nesting isn't allowed
if test(cbreq) then if test(cbreq) then
debug_logfinish_cb(cbreq) -- unconditional version debug_logfinish_cb(cbreq) -- unconditional version
local stash = cbreq:vars()['policy_debug_stash'] local stash = cbreq:vars()['policy_debug_stash']
io.write(table.concat(stash, '')) for _, line in ipairs(stash) do -- don't want one huge entry
ffi.C.kr_log_fmt(ffi.C.LOG_GRP_REQDBG, LOG_DEBUG,
'CODE_FILE=policy.lua', 'CODE_LINE=', 'CODE_FUNC=', -- no meaningful locations
'[%-6s]%s', LOG_GRP_REQDBG_TAG, line)
end
end end
end) end)
ffi.gc(debug_finish_cb, function (func) func:free() end) ffi.gc(debug_finish_cb, function (func) func:free() end)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment