mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Use std::string_view in logging code
This commit is contained in:
parent
ac11a14509
commit
b8b99d5cf1
7 changed files with 61 additions and 54 deletions
|
@ -122,27 +122,30 @@ void script_error(lua_State *L, int pcall_result, const char *mod, const char *f
|
|||
throw LuaError(err_msg);
|
||||
}
|
||||
|
||||
static void script_log_add_source(lua_State *L, std::string &message, int stack_depth)
|
||||
[[nodiscard]] static std::string script_log_add_source(lua_State *L,
|
||||
std::string_view message, int stack_depth)
|
||||
{
|
||||
std::string ret(message);
|
||||
if (stack_depth <= 0)
|
||||
return;
|
||||
return ret;
|
||||
|
||||
lua_Debug ar;
|
||||
if (lua_getstack(L, stack_depth, &ar)) {
|
||||
FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
|
||||
message.append(" (at " + std::string(ar.short_src) + ":"
|
||||
ret.append(" (at ").append(ar.short_src).append(":"
|
||||
+ std::to_string(ar.currentline) + ")");
|
||||
} else {
|
||||
message.append(" (at ?:?)");
|
||||
ret.append(" (at ?:?)");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to,
|
||||
bool script_log_unique(lua_State *L, std::string_view message_in, std::ostream &log_to,
|
||||
int stack_depth)
|
||||
{
|
||||
thread_local std::vector<u64> logged_messages;
|
||||
|
||||
script_log_add_source(L, message, stack_depth);
|
||||
auto message = script_log_add_source(L, message_in, stack_depth);
|
||||
u64 hash = murmur_hash_64_ua(message.data(), message.length(), 0xBADBABE);
|
||||
|
||||
if (std::find(logged_messages.begin(), logged_messages.end(), hash)
|
||||
|
@ -174,7 +177,7 @@ DeprecatedHandlingMode get_deprecated_handling_mode()
|
|||
return ret;
|
||||
}
|
||||
|
||||
void log_deprecated(lua_State *L, std::string message, int stack_depth, bool once)
|
||||
void log_deprecated(lua_State *L, std::string_view message, int stack_depth, bool once)
|
||||
{
|
||||
DeprecatedHandlingMode mode = get_deprecated_handling_mode();
|
||||
if (mode == DeprecatedHandlingMode::Ignore)
|
||||
|
@ -184,12 +187,12 @@ void log_deprecated(lua_State *L, std::string message, int stack_depth, bool onc
|
|||
if (once) {
|
||||
log = script_log_unique(L, message, warningstream, stack_depth);
|
||||
} else {
|
||||
script_log_add_source(L, message, stack_depth);
|
||||
warningstream << message << std::endl;
|
||||
auto message2 = script_log_add_source(L, message, stack_depth);
|
||||
warningstream << message2 << std::endl;
|
||||
}
|
||||
|
||||
if (mode == DeprecatedHandlingMode::Error)
|
||||
throw LuaError(message);
|
||||
throw LuaError(std::string(message));
|
||||
else if (log)
|
||||
infostream << script_get_backtrace(L) << std::endl;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue