Skip to content

Commit c657fb3

Browse files
committedFeb 23, 2020
Refactor Script API's log_deprecated
1 parent 4da057c commit c657fb3

File tree

6 files changed

+27
-31
lines changed

6 files changed

+27
-31
lines changed
 

Diff for: ‎src/gui/guiFormSpecMenu.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2364,7 +2364,7 @@ bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &eleme
23642364
return false;
23652365

23662366
if (type == "invsize")
2367-
log_deprecated("Deprecated formspec element \"invsize\" is used");
2367+
warningstream << "Deprecated formspec element \"invsize\" is used" << std::endl;
23682368

23692369
parseSize(data, description);
23702370

Diff for: ‎src/script/common/c_internal.cpp

+23-21
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,27 @@ void script_run_callbacks_f(lua_State *L, int nargs,
135135
lua_remove(L, error_handler);
136136
}
137137

138-
void log_deprecated(lua_State *L, const std::string &message)
138+
static void script_log(lua_State *L, const std::string &message,
139+
std::ostream &log_to, bool do_error, int stack_depth)
140+
{
141+
lua_Debug ar;
142+
143+
log_to << message << " ";
144+
if (lua_getstack(L, stack_depth, &ar)) {
145+
FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
146+
log_to << "(at " << ar.short_src << ":" << ar.currentline << ")";
147+
} else {
148+
log_to << "(at ?:?)";
149+
}
150+
log_to << std::endl;
151+
152+
if (do_error)
153+
script_error(L, LUA_ERRRUN, NULL, NULL);
154+
else
155+
infostream << script_get_backtrace(L) << std::endl;
156+
}
157+
158+
void log_deprecated(lua_State *L, const std::string &message, int stack_depth)
139159
{
140160
static bool configured = false;
141161
static bool do_log = false;
@@ -152,24 +172,6 @@ void log_deprecated(lua_State *L, const std::string &message)
152172
}
153173
}
154174

155-
if (do_log) {
156-
warningstream << message;
157-
if (L) { // L can be NULL if we get called from scripting_game.cpp
158-
lua_Debug ar;
159-
160-
if (!lua_getstack(L, 2, &ar))
161-
FATAL_ERROR_IF(!lua_getstack(L, 1, &ar), "lua_getstack() failed");
162-
FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
163-
warningstream << " (at " << ar.short_src << ":" << ar.currentline << ")";
164-
}
165-
warningstream << std::endl;
166-
167-
if (L) {
168-
if (do_error)
169-
script_error(L, LUA_ERRRUN, NULL, NULL);
170-
else
171-
infostream << script_get_backtrace(L) << std::endl;
172-
}
173-
}
175+
if (do_log)
176+
script_log(L, message, warningstream, do_error, stack_depth);
174177
}
175-

Diff for: ‎src/script/common/c_internal.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,5 @@ int script_exception_wrapper(lua_State *L, lua_CFunction f);
103103
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
104104
void script_run_callbacks_f(lua_State *L, int nargs,
105105
RunCallbacksMode mode, const char *fxn);
106-
void log_deprecated(lua_State *L, const std::string &message);
106+
void log_deprecated(lua_State *L, const std::string &message,
107+
int stack_depth=1);

Diff for: ‎src/script/lua_api/l_util.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int ModApiUtil::l_log(lua_State *L)
5959
std::string name = luaL_checkstring(L, 1);
6060
text = luaL_checkstring(L, 2);
6161
if (name == "deprecated") {
62-
log_deprecated(L, text);
62+
log_deprecated(L, text, 2);
6363
return 0;
6464
}
6565
level = Logger::stringToLevel(name);

Diff for: ‎src/script/scripting_server.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,3 @@ void ServerScripting::InitializeModApi(lua_State *L, int top)
121121
ModApiStorage::Initialize(L, top);
122122
ModApiChannels::Initialize(L, top);
123123
}
124-
125-
void log_deprecated(const std::string &message)
126-
{
127-
log_deprecated(NULL, message);
128-
}

Diff for: ‎src/script/scripting_server.h

-2
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,3 @@ class ServerScripting:
5151
private:
5252
void InitializeModApi(lua_State *L, int top);
5353
};
54-
55-
void log_deprecated(const std::string &message);

0 commit comments

Comments
 (0)