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
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);
}
}
}
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;
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;
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;
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) {
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;
}
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;
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 {
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);
}
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) {