diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-11-20 14:23:35 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-11-20 14:24:42 +0000 |
commit | 8f80f9fdae13a320da978dffa2c9d94e7b68a810 (patch) | |
tree | d9f0b181405607a1f42e85cc4aabf8236f703bb4 | |
parent | efb2d07ad642bd227e40ed917332f64f82b2553d (diff) | |
download | libguestfs-8f80f9fdae13a320da978dffa2c9d94e7b68a810.tar.gz libguestfs-8f80f9fdae13a320da978dffa2c9d94e7b68a810.tar.xz libguestfs-8f80f9fdae13a320da978dffa2c9d94e7b68a810.zip |
lua: Print the error thrown by callbacks.
-rw-r--r-- | generator/lua.ml | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/generator/lua.ml b/generator/lua.ml index 3e2e04fb..fc4cb0fa 100644 --- a/generator/lua.ml +++ b/generator/lua.ml @@ -84,6 +84,8 @@ static int64_t get_int64 (lua_State *L, int index); static void push_int64 (lua_State *L, int64_t i64); static void push_int64_array (lua_State *L, const int64_t *array, size_t len); +static void print_any (lua_State *L, int index, FILE *out); + static void event_callback_wrapper (guestfs_h *g, void *esvp, uint64_t event, int eh, int flags, const char *buf, size_t buf_len, const uint64_t *array, size_t array_len); static uint64_t get_event (lua_State *L, int index); static uint64_t get_event_bitmask (lua_State *L, int index); @@ -344,10 +346,11 @@ event_callback_wrapper (guestfs_h *g, case 0: /* call ok - do nothing */ break; case LUA_ERRRUN: - fprintf (stderr, \"lua-guestfs: %%s: unexpected error in event handler\\n\", + fprintf (stderr, \"lua-guestfs: %%s: unexpected error in event handler: \", __func__); - /* XXX could print the error instead of throwing it away */ + print_any (L, -1, stderr); lua_pop (L, 1); + fprintf (stderr, \"\\n\"); break; case LUA_ERRERR: /* can probably never happen */ fprintf (stderr, \"lua-guestfs: %%s: error calling error handler\\n\", @@ -683,6 +686,19 @@ push_int64_array (lua_State *L, const int64_t *array, size_t len) } } +/* Use Lua tostring method to print out any. Useful for debugging + * these bindings, but also used if a callback throws an exception. + */ +static void +print_any (lua_State *L, int index, FILE *out) +{ + lua_getfield (L, LUA_GLOBALSINDEX, \"tostring\"); + lua_pushvalue (L, index >= 0 ? index : index-1); + lua_call (L, 1, 1); + fprintf (out, \"%%s\", luaL_checkstring (L, -1)); + lua_pop (L, 1); +} + "; (* Code to handle events. *) |