From b664092990bddc95562626847396e067d50fae85 Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Thu, 8 Jul 2021 02:40:18 +0800 Subject: [PATCH] lib: standardize zlog error messages Signed-off-by: Donald Lee --- lib/frrscript.c | 37 +++++++++++++++++++++-------------- lib/frrscript.h | 52 ++++++++++++++++++++++++------------------------- 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/lib/frrscript.c b/lib/frrscript.c index 9b497a431d..8252455fd9 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -137,7 +137,7 @@ static void lua_function_free(struct hash_bucket *b, void *data) XFREE(MTYPE_SCRIPT, lfs); } -/* Generic script APIs */ +/* internal frrscript APIs */ int _frrscript_call_lua(struct lua_function_state *lfs, int nargs) { @@ -214,7 +214,9 @@ void *frrscript_get_result(struct frrscript *fs, const char *function_name, lua_getfield(lfs->L, 1, name); if (lua_isnil(lfs->L, -1)) { lua_pop(lfs->L, 1); - zlog_err("No result in results table with that name %s", name); + zlog_warn( + "frrscript: '%s.lua': '%s': tried to decode '%s' as result but failed", + fs->name, function_name, name); return NULL; } p = lua_to(lfs->L, 2); @@ -259,34 +261,39 @@ int frrscript_load(struct frrscript *fs, const char *function_name, lua_State *L = luaL_newstate(); frrlua_export_logging(L); - char fname[MAXPATHLEN * 2]; + char script_name[MAXPATHLEN * 2]; - snprintf(fname, sizeof(fname), "%s/%s.lua", scriptdir, fs->name); - int ret = luaL_dofile(L, fname); + snprintf(script_name, sizeof(script_name), "%s/%s.lua", scriptdir, + fs->name); + int ret = luaL_dofile(L, script_name); switch (ret) { case LUA_OK: break; case LUA_ERRSYNTAX: - zlog_err("Failed loading script '%s': syntax error: %s", fname, - lua_tostring(L, -1)); + zlog_err( + "frrscript: failed loading script '%s.lua': syntax error: %s", + script_name, lua_tostring(L, -1)); break; case LUA_ERRMEM: - zlog_err("Failed loading script '%s': out-of-memory error: %s", - fname, lua_tostring(L, -1)); + zlog_err( + "frrscript: failed loading script '%s.lua': out-of-memory error: %s", + script_name, lua_tostring(L, -1)); break; case LUA_ERRGCMM: zlog_err( - "Failed loading script '%s': garbage collector error: %s", - fname, lua_tostring(L, -1)); + "frrscript: failed loading script '%s.lua': garbage collector error: %s", + script_name, lua_tostring(L, -1)); break; case LUA_ERRFILE: - zlog_err("Failed loading script '%s': file read error: %s", - fname, lua_tostring(L, -1)); + zlog_err( + "frrscript: failed loading script '%s.lua': file read error: %s", + script_name, lua_tostring(L, -1)); break; default: - zlog_err("Failed loading script '%s': unknown error: %s", fname, - lua_tostring(L, -1)); + zlog_err( + "frrscript: failed loading script '%s.lua': unknown error: %s", + script_name, lua_tostring(L, -1)); break; } diff --git a/lib/frrscript.h b/lib/frrscript.h index 85e7b15cd3..1f234a38a9 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -192,33 +192,31 @@ int _frrscript_call_lua(struct lua_function_state *lfs, int nargs); * 0 if the script ran successfully, nonzero otherwise. */ -#define frrscript_call(fs, f, ...) \ - ({ \ - struct lua_function_state lookup = {.name = f}; \ - struct lua_function_state *lfs; \ - lfs = hash_lookup(fs->lua_function_hash, &lookup); \ - lfs == NULL ? ({ \ - zlog_err( \ - "Lua script call: tried to call '%s' in '%s' which was not loaded", \ - f, fs->name); \ - 1; \ - }) \ - : ({ \ - MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \ - _frrscript_call_lua( \ - lfs, PP_NARG(__VA_ARGS__)); \ - }) != 0 \ - ? ({ \ - zlog_err( \ - "Lua script call: '%s' in '%s' returned non-zero exit code", \ - f, fs->name); \ - 1; \ - }) \ - : ({ \ - MAP_LISTS(DECODE_ARGS, \ - ##__VA_ARGS__); \ - 0; \ - }); \ +#define frrscript_call(fs, f, ...) \ + ({ \ + struct lua_function_state lookup = {.name = f}; \ + struct lua_function_state *lfs; \ + lfs = hash_lookup(fs->lua_function_hash, &lookup); \ + lfs == NULL ? ({ \ + zlog_err( \ + "frrscript: '%s.lua': '%s': tried to call this function but it was not loaded", \ + fs->name, f); \ + 1; \ + }) \ + : ({ \ + MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \ + _frrscript_call_lua(lfs, PP_NARG(__VA_ARGS__)); \ + }) != 0 \ + ? ({ \ + zlog_err( \ + "frrscript: '%s.lua': '%s': this function called but returned non-zero exit code. No variables modified.", \ + fs->name, f); \ + 1; \ + }) \ + : ({ \ + MAP_LISTS(DECODE_ARGS, ##__VA_ARGS__); \ + 0; \ + }); \ }) /* -- 2.39.5