Reorganize Lua error logging for consistency

This commit is contained in:
Paul Kulchenko 2021-08-20 22:34:35 -07:00
parent 813fb8ff39
commit e0c08389b9

View file

@ -1030,6 +1030,10 @@ static int LuaCallWithTrace(lua_State *L, int nargs, int nres) {
return status; return status;
} }
static void LogLuaError(char *hook, char *err) {
ERRORF("(lua) failed to run %s: %s", hook, err);
}
static bool LuaOnClientConnection(void) { static bool LuaOnClientConnection(void) {
bool dropit; bool dropit;
uint32_t ip, serverip; uint32_t ip, serverip;
@ -1044,7 +1048,7 @@ static bool LuaOnClientConnection(void) {
if (LuaCallWithTrace(L, 4, 1) == LUA_OK) { if (LuaCallWithTrace(L, 4, 1) == LUA_OK) {
dropit = lua_toboolean(L, -1); dropit = lua_toboolean(L, -1);
} else { } else {
WARNF("(lua) %s: %s", "OnClientConnection", lua_tostring(L, -1)); LogLuaError("OnClientConnection", lua_tostring(L, -1));
dropit = false; dropit = false;
} }
lua_pop(L, 1); lua_pop(L, 1);
@ -1063,7 +1067,7 @@ static void LuaOnProcessCreate(int pid) {
lua_pushinteger(L, serverip); lua_pushinteger(L, serverip);
lua_pushinteger(L, serverport); lua_pushinteger(L, serverport);
if (LuaCallWithTrace(L, 5, 0) != LUA_OK) { if (LuaCallWithTrace(L, 5, 0) != LUA_OK) {
WARNF("(lua) %s: %s", "OnProcessCreate", lua_tostring(L, -1)); LogLuaError("OnProcessCreate", lua_tostring(L, -1));
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
@ -1072,7 +1076,7 @@ static void LuaOnProcessDestroy(int pid) {
lua_getglobal(L, "OnProcessDestroy"); lua_getglobal(L, "OnProcessDestroy");
lua_pushinteger(L, pid); lua_pushinteger(L, pid);
if (LuaCallWithTrace(L, 1, 0) != LUA_OK) { if (LuaCallWithTrace(L, 1, 0) != LUA_OK) {
WARNF("(lua) %s: %s", "OnProcessDestroy", lua_tostring(L, -1)); LogLuaError("OnProcessDestroy", lua_tostring(L, -1));
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
@ -1090,7 +1094,7 @@ static inline bool IsHookDefined(const char *s) {
static void CallSimpleHook(const char *s) { static void CallSimpleHook(const char *s) {
lua_getglobal(L, s); lua_getglobal(L, s);
if (LuaCallWithTrace(L, 0, 0) != LUA_OK) { if (LuaCallWithTrace(L, 0, 0) != LUA_OK) {
WARNF("(lua) %s: %s", s, lua_tostring(L, -1)); LogLuaError(s, lua_tostring(L, -1));
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
@ -2882,7 +2886,7 @@ static char *LuaOnHttpRequest(void) {
return CommitOutput(GetLuaResponse()); return CommitOutput(GetLuaResponse());
} else { } else {
char *error; char *error;
ERRORF("(lua) failed to run OnHttpRequest: %s", lua_tostring(L, -1)); LogLuaError("OnHttpRequest", lua_tostring(L, -1));
error = error =
ServeErrorWithDetail(500, "Internal Server Error", ServeErrorWithDetail(500, "Internal Server Error",
IsLoopbackClient() ? lua_tostring(L, -1) : NULL); IsLoopbackClient() ? lua_tostring(L, -1) : NULL);
@ -2905,7 +2909,7 @@ static char *ServeLua(struct Asset *a, const char *s, size_t n) {
return CommitOutput(GetLuaResponse()); return CommitOutput(GetLuaResponse());
} else { } else {
char *error; char *error;
ERRORF("(lua) failed to run lua code: %s", lua_tostring(L, -1)); LogLuaError("lua code", lua_tostring(L, -1));
error = error =
ServeErrorWithDetail(500, "Internal Server Error", ServeErrorWithDetail(500, "Internal Server Error",
IsLoopbackClient() ? lua_tostring(L, -1) : NULL); IsLoopbackClient() ? lua_tostring(L, -1) : NULL);
@ -5144,7 +5148,7 @@ static bool LuaRun(const char *path, bool mandatory) {
status = status =
luaL_loadbuffer(L, code, codelen, FreeLater(xasprintf("@%s", path))); luaL_loadbuffer(L, code, codelen, FreeLater(xasprintf("@%s", path)));
if (status != LUA_OK || LuaCallWithTrace(L, 0, 0) != LUA_OK) { if (status != LUA_OK || LuaCallWithTrace(L, 0, 0) != LUA_OK) {
ERRORF("(lua) script failed to run: %s", lua_tostring(L, -1)); LogLuaError("lua code", lua_tostring(L, -1));
lua_pop(L, 1); lua_pop(L, 1);
if (mandatory) exit(1); if (mandatory) exit(1);
} }