1
0
Fork 0
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:
sfan5 2024-08-31 21:23:16 +02:00
parent ac11a14509
commit b8b99d5cf1
7 changed files with 61 additions and 54 deletions

View file

@ -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;
}

View file

@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <string_view>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
@ -127,7 +129,7 @@ int script_error_handler(lua_State *L);
// Takes an error from lua_pcall and throws it as a LuaError
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
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, std::ostream &log_to,
int stack_depth = 1);
enum DeprecatedHandlingMode {
@ -152,7 +154,8 @@ DeprecatedHandlingMode get_deprecated_handling_mode();
* (ie: not builtin or core). -1 to disabled.
* @param once Log the deprecation warning only once per callsite.
*/
void log_deprecated(lua_State *L, std::string message, int stack_depth = 1, bool once = false);
void log_deprecated(lua_State *L, std::string_view message,
int stack_depth = 1, bool once = false);
// Safely call string.dump on a function value
// (does not pop, leaves one value on stack)

View file

@ -61,13 +61,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
int ModApiUtil::l_log(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
std::string text;
std::string_view text;
LogLevel level = LL_NONE;
if (lua_isnone(L, 2)) {
text = luaL_checkstring(L, 1);
if (lua_isnoneornil(L, 2)) {
text = readParam<std::string_view>(L, 1);
} else {
std::string name = luaL_checkstring(L, 1);
text = luaL_checkstring(L, 2);
auto name = readParam<std::string_view>(L, 1);
text = readParam<std::string_view>(L, 2);
if (name == "deprecated") {
log_deprecated(L, text, 2);
return 0;
@ -75,7 +75,7 @@ int ModApiUtil::l_log(lua_State *L)
level = Logger::stringToLevel(name);
if (level == LL_MAX) {
warningstream << "Tried to log at unknown level '" << name
<< "'. Defaulting to \"none\"." << std::endl;
<< "'. Defaulting to \"none\"." << std::endl;
level = LL_NONE;
}
}