]> git.lizzy.rs Git - minetest.git/commitdiff
Refactor Script API's log_deprecated
authorsfan5 <sfan5@live.de>
Wed, 12 Feb 2020 18:17:04 +0000 (19:17 +0100)
committersfan5 <sfan5@live.de>
Sun, 23 Feb 2020 21:24:12 +0000 (22:24 +0100)
src/gui/guiFormSpecMenu.cpp
src/script/common/c_internal.cpp
src/script/common/c_internal.h
src/script/lua_api/l_util.cpp
src/script/scripting_server.cpp
src/script/scripting_server.h

index 3d473550ce8fd351c3cd2f2611f3fc1e290ed330..40c3bbdaec1144a1ee6b75b5050dce8b6712791e 100644 (file)
@@ -2364,7 +2364,7 @@ bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &eleme
                return false;
 
        if (type == "invsize")
-               log_deprecated("Deprecated formspec element \"invsize\" is used");
+               warningstream << "Deprecated formspec element \"invsize\" is used" << std::endl;
 
        parseSize(data, description);
 
index f792b6218126201781a6a234aeda4b55e6f409fc..a7dcf9b5f11cf28a737052770217078c1b126c99 100644 (file)
@@ -135,7 +135,27 @@ void script_run_callbacks_f(lua_State *L, int nargs,
        lua_remove(L, error_handler);
 }
 
-void log_deprecated(lua_State *L, const std::string &message)
+static void script_log(lua_State *L, const std::string &message,
+       std::ostream &log_to, bool do_error, int stack_depth)
+{
+       lua_Debug ar;
+
+       log_to << message << " ";
+       if (lua_getstack(L, stack_depth, &ar)) {
+               FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
+               log_to << "(at " << ar.short_src << ":" << ar.currentline << ")";
+       } else {
+               log_to << "(at ?:?)";
+       }
+       log_to << std::endl;
+
+       if (do_error)
+               script_error(L, LUA_ERRRUN, NULL, NULL);
+       else
+               infostream << script_get_backtrace(L) << std::endl;
+}
+
+void log_deprecated(lua_State *L, const std::string &message, int stack_depth)
 {
        static bool configured = false;
        static bool do_log     = false;
@@ -152,24 +172,6 @@ void log_deprecated(lua_State *L, const std::string &message)
                }
        }
 
-       if (do_log) {
-               warningstream << message;
-               if (L) { // L can be NULL if we get called from scripting_game.cpp
-                       lua_Debug ar;
-
-                       if (!lua_getstack(L, 2, &ar))
-                               FATAL_ERROR_IF(!lua_getstack(L, 1, &ar), "lua_getstack() failed");
-                       FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
-                       warningstream << " (at " << ar.short_src << ":" << ar.currentline << ")";
-               }
-               warningstream << std::endl;
-
-               if (L) {
-                       if (do_error)
-                               script_error(L, LUA_ERRRUN, NULL, NULL);
-                       else
-                               infostream << script_get_backtrace(L) << std::endl;
-               }
-       }
+       if (do_log)
+               script_log(L, message, warningstream, do_error, stack_depth);
 }
-
index d2131d1ad3ee4d9b9ae005fc4eefe1d5bd5bff94..69b8a7fdc2768d28d4f90da1d5660f70b79d3259 100644 (file)
@@ -103,4 +103,5 @@ int script_exception_wrapper(lua_State *L, lua_CFunction f);
 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
 void script_run_callbacks_f(lua_State *L, int nargs,
        RunCallbacksMode mode, const char *fxn);
-void log_deprecated(lua_State *L, const std::string &message);
+void log_deprecated(lua_State *L, const std::string &message,
+       int stack_depth=1);
index a58c3a196a36bb5de4786be5506b4bf1fe9754ec..ae3e5df3d2ae70d64b447f0f9c474bf5211c4fdb 100644 (file)
@@ -59,7 +59,7 @@ int ModApiUtil::l_log(lua_State *L)
                std::string name = luaL_checkstring(L, 1);
                text = luaL_checkstring(L, 2);
                if (name == "deprecated") {
-                       log_deprecated(L, text);
+                       log_deprecated(L, text, 2);
                        return 0;
                }
                level = Logger::stringToLevel(name);
index 2204c6884358216576a80866f6cb570132e2d0db..cbf22964015b2b55b9503aabe443c75a9ab90ae3 100644 (file)
@@ -121,8 +121,3 @@ void ServerScripting::InitializeModApi(lua_State *L, int top)
        ModApiStorage::Initialize(L, top);
        ModApiChannels::Initialize(L, top);
 }
-
-void log_deprecated(const std::string &message)
-{
-       log_deprecated(NULL, message);
-}
index 88cea143ca227d1692a1cfffc02fbed55636409e..bf06ab197fc49fe8e7ac088d24b2c4d84b1102f5 100644 (file)
@@ -51,5 +51,3 @@ class ServerScripting:
 private:
        void InitializeModApi(lua_State *L, int top);
 };
-
-void log_deprecated(const std::string &message);