summaryrefslogtreecommitdiff
path: root/src/script/common
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2020-02-12 19:17:04 +0100
committersfan5 <sfan5@live.de>2020-02-23 22:24:12 +0100
commitc657fb343f120b7462f7ca580852636ad98b8ae0 (patch)
tree6d7b4fbe85a7e56f03e017edff52b0a56aeacfc3 /src/script/common
parent4da057cc49082192bbc45689d80dec3410e41f27 (diff)
downloadminetest-c657fb343f120b7462f7ca580852636ad98b8ae0.tar.gz
minetest-c657fb343f120b7462f7ca580852636ad98b8ae0.tar.bz2
minetest-c657fb343f120b7462f7ca580852636ad98b8ae0.zip
Refactor Script API's log_deprecated
Diffstat (limited to 'src/script/common')
-rw-r--r--src/script/common/c_internal.cpp44
-rw-r--r--src/script/common/c_internal.h3
2 files changed, 25 insertions, 22 deletions
diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp
index f792b6218..a7dcf9b5f 100644
--- a/src/script/common/c_internal.cpp
+++ b/src/script/common/c_internal.cpp
@@ -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);
}
-
diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h
index d2131d1ad..69b8a7fdc 100644
--- a/src/script/common/c_internal.h
+++ b/src/script/common/c_internal.h
@@ -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);