From 3c7ecaaa01f7a49f7ffcaa0c533fbc97e9470700 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 13 May 2015 13:44:55 +0100 Subject: [PATCH] Print userdata in format 'class(address)' --- src/lua/lua_logger.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/lua/lua_logger.c b/src/lua/lua_logger.c index dbeea2ea3..7108c94a3 100644 --- a/src/lua/lua_logger.c +++ b/src/lua/lua_logger.c @@ -267,6 +267,38 @@ lua_logger_out_boolean (lua_State *L, gint pos, gchar *outbuf, gsize len) return r; } +static gsize +lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len) +{ + gint r; + + if (!lua_getmetatable (L, pos)) { + return 0; + } + + lua_pushstring (L, "__index"); + lua_gettable (L, -2); + + if (!lua_istable (L, -1)) { + lua_pop (L, 2); + return 0; + } + + lua_pushstring (L, "class"); + lua_gettable (L, -2); + + if (!lua_isstring (L, -1)) { + lua_pop (L, 3); + return 0; + } + + r = rspamd_snprintf (outbuf, len, "%s(%p)", lua_tostring (L, -1), + lua_touserdata (L, pos)); + lua_pop (L, 3); + + return r; +} + #define MOVE_BUF(d, remain, r) if ((remain) - (r) == 0) break; (d) += (r); (remain) -= (r) static gsize @@ -325,7 +357,7 @@ lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len) r = rspamd_snprintf (d, remain, "[%s] = ", lua_tostring (L, -2)); MOVE_BUF(d, remain, r); - r = lua_logger_out_type (L, -1, d, remain); + r = lua_logger_out_type (L, lua_gettop (L), d, remain); MOVE_BUF(d, remain, r); first = FALSE; @@ -358,6 +390,9 @@ lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf, gsize len) case LUA_TTABLE: r = lua_logger_out_table (L, pos, outbuf, len); break; + case LUA_TUSERDATA: + r = lua_logger_out_userdata (L, pos, outbuf, len); + break; default: /* Try to push everything as string using tostring magic */ r = lua_logger_out_str (L, pos, outbuf, len); @@ -370,7 +405,7 @@ lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf, gsize len) static gint lua_logger_logx (lua_State *L, GLogLevelFlags level, gboolean is_string) { - static gchar logbuf[BUFSIZ]; + gchar logbuf[BUFSIZ]; gchar *d; const gchar *s, *c; gsize remain, r; -- 2.39.5