diff options
-rw-r--r-- | lualib/lua_util.lua | 33 | ||||
-rw-r--r-- | src/libutil/logger.c | 2 | ||||
-rw-r--r-- | src/lua/lua_logger.c | 98 |
3 files changed, 89 insertions, 44 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index ba5843ff6..64d949270 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -664,4 +664,37 @@ exports.extract_specific_urls = function(params_or_task, lim, need_emails, filte return res end +-- Debugging support +local unconditional_debug = false +local debug_modules = {} +local log_level = 384 -- debug + forced (1 << 7 | 1 << 8) + +if type(rspamd_config) == 'userdata' then + local logger = require "rspamd_logger" + -- Fill debug modules from the config + local logging = rspamd_config:get_all_opt('logging') + if logging then + local log_level_str = logging.level + if log_level_str then + if log_level_str == 'debug' then + unconditional_debug = true + end + end + + if not unconditional_debug and logging.debug_modules then + for _,m in ipairs(logging.debug_modules) do + debug_modules[m] = true + logger.infox(rspamd_config, 'enable debug for Lua module %s', m) + end + end + end +end + +exports.debugm = function(mod, ...) + local logger = require "rspamd_logger" + if unconditional_debug or debug_modules[mod] then + logger.logx(log_level, ...) + end +end + return exports diff --git a/src/libutil/logger.c b/src/libutil/logger.c index bbdc69e97..c9f14ccb6 100644 --- a/src/libutil/logger.c +++ b/src/libutil/logger.c @@ -545,7 +545,7 @@ rspamd_logger_need_log (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, { g_assert (rspamd_log != NULL); - if (log_level <= rspamd_log->cfg->log_level) { + if ((log_level & RSPAMD_LOG_FORCED) || log_level <= rspamd_log->cfg->log_level) { return TRUE; } diff --git a/src/lua/lua_logger.c b/src/lua/lua_logger.c index 87a45c1d5..fe2fe7d28 100644 --- a/src/lua/lua_logger.c +++ b/src/lua/lua_logger.c @@ -195,44 +195,22 @@ lua_common_log_line (GLogLevelFlags level, lua_State *L, d.currentline); } - if (level == G_LOG_LEVEL_DEBUG) { - rspamd_conditional_debug (NULL, - NULL, - module, - uid, - func_buf, - "%s", - msg); - } - else { - rspamd_common_log_function (NULL, - level, - module, - uid, - func_buf, - "%s", - msg); - } + rspamd_common_log_function (NULL, + level, + module, + uid, + func_buf, + "%s", + msg); } else { - if (level == G_LOG_LEVEL_DEBUG) { - rspamd_conditional_debug (NULL, - NULL, - module, - uid, - G_STRFUNC, - "%s", - msg); - } - else { - rspamd_common_log_function (NULL, - level, - module, - uid, - G_STRFUNC, - "%s", - msg); - } + rspamd_common_log_function (NULL, + level, + module, + uid, + G_STRFUNC, + "%s", + msg); } } @@ -516,7 +494,7 @@ lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf, gsize len) } static const gchar * -lua_logger_get_id (lua_State *L, gint pos) +lua_logger_get_id (lua_State *L, gint pos, GError **err) { const gchar *uid = NULL, *clsname; @@ -536,6 +514,10 @@ lua_logger_get_id (lua_State *L, gint pos) if (task) { uid = task->task_pool->tag.uid; } + else { + g_set_error (err, g_quark_from_static_string ("lua_logger"), + EINVAL, "invalid rspamd{task}"); + } } else if (strcmp (clsname, "rspamd{mempool}") == 0) { rspamd_mempool_t *pool; @@ -545,6 +527,10 @@ lua_logger_get_id (lua_State *L, gint pos) if (pool) { uid = pool->tag.uid; } + else { + g_set_error (err, g_quark_from_static_string ("lua_logger"), + EINVAL, "invalid rspamd{mempool}"); + } } else if (strcmp (clsname, "rspamd{config}") == 0) { struct rspamd_config *cfg; @@ -552,7 +538,13 @@ lua_logger_get_id (lua_State *L, gint pos) cfg = lua_check_config (L, pos); if (cfg) { - uid = cfg->checksum; + if (cfg->checksum) { + uid = cfg->checksum; + } + } + else { + g_set_error (err, g_quark_from_static_string ("lua_logger"), + EINVAL, "invalid rspamd{config}"); } } else if (strcmp (clsname, "rspamd{map}") == 0) { @@ -568,12 +560,24 @@ lua_logger_get_id (lua_State *L, gint pos) uid = "embedded"; } } + else { + g_set_error (err, g_quark_from_static_string ("lua_logger"), + EINVAL, "invalid rspamd{map}"); + } + } + else { + g_set_error (err, g_quark_from_static_string ("lua_logger"), + EINVAL, "unknown class: %s", clsname); } /* Metatable, __index, classname */ lua_pop (L, 3); } + else { + g_set_error (err, g_quark_from_static_string ("lua_logger"), + EINVAL, "no metatable found for userdata"); + } return uid; } @@ -709,7 +713,8 @@ lua_logger_do_log (lua_State *L, gchar logbuf[RSPAMD_LOGBUF_SIZE - 128]; const gchar *uid = NULL; gint fmt_pos = start_pos; - gboolean ret; + gint ret; + GError *err = NULL; if (lua_type (L, start_pos) == LUA_TSTRING) { fmt_pos = start_pos; @@ -717,10 +722,17 @@ lua_logger_do_log (lua_State *L, else if (lua_type (L, start_pos) == LUA_TUSERDATA) { fmt_pos = start_pos + 1; - uid = lua_logger_get_id (L, start_pos); + uid = lua_logger_get_id (L, start_pos, &err); if (uid == NULL) { - return luaL_error (L, "bad userdata for logging"); + ret = luaL_error (L, "bad userdata for logging: %s", + err ? err->message : "unknown error"); + + if (err) { + g_error_free (err); + } + + return ret; } } else { @@ -793,7 +805,7 @@ lua_logger_logx (lua_State *L) LUA_TRACE_POINT; GLogLevelFlags flags = lua_tonumber (L, 1); - return lua_logger_do_log (L, (flags & G_LOG_LEVEL_MASK), FALSE, 2); + return lua_logger_do_log (L, flags, FALSE, 2); } @@ -811,7 +823,7 @@ lua_logger_debugm (lua_State *L) uid = luaL_checkstring (L, 2); } else { - uid = lua_logger_get_id (L, 2); + uid = lua_logger_get_id (L, 2, NULL); } if (uid && module && lua_type (L, 3) == LUA_TSTRING) { |