MINOR: lua: Slightly improve function dumping the lua traceback
The separator string is now configurable, passing it as parameter when the
function is called. In addition, the message have been slightly changed to
be a bit more readable.
(cherry picked from commit d09cc519bd0c04a0d1ec5bf23ed053c1e596851a)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 5326d6a152e7001c325ce3b00636132f0fb419eb)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit c4e150a99d5eb31ab7fd6355b9f21060b9433ccb)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/include/proto/hlua.h b/include/proto/hlua.h
index 32468b7..9552bd4 100644
--- a/include/proto/hlua.h
+++ b/include/proto/hlua.h
@@ -24,6 +24,7 @@
#define HLUA_INIT(__hlua) do { (__hlua)->T = 0; } while(0)
/* Lua HAProxy integration functions. */
+const char *hlua_traceback(lua_State *L, const char* sep);
void hlua_ctx_destroy(struct hlua *lua);
void hlua_init();
int hlua_post_init();
diff --git a/src/hlua.c b/src/hlua.c
index de03ca3..59717b2 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -295,19 +295,17 @@
return lua_tostring(L, -1);
}
-__LJMP static const char *hlua_traceback(lua_State *L)
+__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
{
lua_Debug ar;
int level = 0;
struct buffer *msg = get_trash_chunk();
- int filled = 0;
while (lua_getstack(L, level++, &ar)) {
/* Add separator */
- if (filled)
- chunk_appendf(msg, ", ");
- filled = 1;
+ if (b_data(msg))
+ chunk_appendf(msg, "%s", sep);
/* Fill fields:
* 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
@@ -319,9 +317,9 @@
/* Append code localisation */
if (ar.currentline > 0)
- chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
+ chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
else
- chunk_appendf(msg, "%s ", ar.short_src);
+ chunk_appendf(msg, "%s: ", ar.short_src);
/*
* Get function name
@@ -331,13 +329,13 @@
* or "main" for main code.
*/
if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
- chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
+ chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
else if (*ar.what == 'm') /* "main", the code is not executed in a function */
- chunk_appendf(msg, "main chunk");
+ chunk_appendf(msg, "in main chunk");
else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
- chunk_appendf(msg, "C function line %d", ar.linedefined);
+ chunk_appendf(msg, "in function line %d", ar.linedefined);
else /* nothing left... */
chunk_appendf(msg, "?");
@@ -1195,7 +1193,7 @@
msg = lua_tostring(lua->T, -1);
lua_settop(lua->T, 0); /* Empty the stack. */
lua_pop(lua->T, 1);
- trace = hlua_traceback(lua->T);
+ trace = hlua_traceback(lua->T, ", ");
if (msg)
lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
else