Skip to content

Commit

Permalink
lua: add log{Verbose,Debug,Warn,Error}
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Oct 16, 2023
1 parent f00c3a1 commit af470a0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
78 changes: 73 additions & 5 deletions Source/utils/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <optional>
#include <string_view>

#include <fmt/args.h>
#include <fmt/format.h>
#include <sol/sol.hpp>
#include <sol/utility/to_string.hpp>

Expand Down Expand Up @@ -67,10 +69,72 @@ void RunScript(std::string_view path)
CheckResult(luaState->safe_script(luaScriptStr));
}

void LuaPanic(sol::optional<std::string> maybe_msg)
void LuaPanic(sol::optional<std::string> message)
{
LogError("Lua is in a panic state and will now abort() the application:\n",
maybe_msg.value_or("unknown error"));
message.value_or("unknown error"));
}

void LuaLogMessage(LogPriority priority, std::string_view fmt, sol::variadic_args args)
{
std::string formatted;
FMT_TRY
{
fmt::dynamic_format_arg_store<fmt::format_context> store;
for (const sol::stack_proxy arg : args) {
switch (arg.get_type()) {
case sol::type::boolean:
store.push_back(arg.as<bool>());
break;
case sol::type::number:
if (lua_isinteger(arg.lua_state(), arg.stack_index())) {
store.push_back(arg.as<lua_Integer>());
} else {
store.push_back(arg.as<lua_Number>());
}
break;
case sol::type::string:
store.push_back(arg.as<std::string>());
break;
default:
store.push_back(sol::utility::to_string(sol::stack_object(arg)));
break;
}
}
formatted = fmt::vformat(fmt, store);
}
FMT_CATCH(const fmt::format_error &e)
{
#if FMT_EXCEPTIONS
// e.what() is undefined if exceptions are disabled, so we wrap the whole block
// with an `FMT_EXCEPTIONS` check.
std::string error = StrCat("Format error, fmt: ", fmt, " error: ", e.what());
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", error.c_str());
return;
#endif
}
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, static_cast<SDL_LogPriority>(priority), "%s", formatted.c_str());
}

void LuaLog(std::string_view fmt, sol::variadic_args args)
{
LuaLogMessage(LogPriority::Info, fmt, std::move(args));
}
void LuaLogVerbose(std::string_view fmt, sol::variadic_args args)
{
LuaLogMessage(LogPriority::Verbose, fmt, std::move(args));
}
void LuaLogDebug(std::string_view fmt, sol::variadic_args args)
{
LuaLogMessage(LogPriority::Debug, fmt, std::move(args));
}
void LuaLogWarn(std::string_view fmt, sol::variadic_args args)
{
LuaLogMessage(LogPriority::Warn, fmt, std::move(args));
}
void LuaLogError(std::string_view fmt, sol::variadic_args args)
{
LuaLogMessage(LogPriority::Error, fmt, std::move(args));
}

} // namespace
Expand Down Expand Up @@ -110,10 +174,14 @@ void LuaInitialize()
// Registering devilutionx object table
lua.create_named_table(
"devilutionx",
"message", [](std::string_view text) { EventPlrMsg(text, UiFlags::ColorRed); }
"message", [](std::string_view text) { EventPlrMsg(text, UiFlags::ColorRed); },
// TODO: Re-enable once https://github.com/bebbo/amiga-gcc/issues/363 is fixed.
// "drawString", [](std::string_view text, int x, int y) { DrawString(GlobalBackBuffer(), text, { x, y }); }
);
"log", LuaLog,
"logVerbose", LuaLogVerbose,
"logDebug", LuaLogDebug,
"logWarn", LuaLogWarn,
"logError", LuaLogError);

RunScript("lua/init.lua");
RunScript("lua/user.lua");
Expand Down Expand Up @@ -150,7 +218,7 @@ tl::expected<std::string, std::string> RunLua(std::string_view code)
return tl::make_unexpected("Unknown Lua error");
}

return sol::utility::to_string(sol::object(result));
return sol::utility::to_string(sol::stack_object(result));
}

} // namespace devilution
1 change: 1 addition & 0 deletions Source/utils/sdl2_to_1_2_backports.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void SDL_LogInfo(int category, const char *fmt, ...) DVL_PRINTF_ATTRIBUTE(2, 3);
void SDL_LogWarn(int category, const char *fmt, ...) DVL_PRINTF_ATTRIBUTE(2, 3);
void SDL_LogError(int category, const char *fmt, ...) DVL_PRINTF_ATTRIBUTE(2, 3);
void SDL_LogCritical(int category, const char *fmt, ...) DVL_PRINTF_ATTRIBUTE(2, 3);
void SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ...) DVL_PRINTF_ATTRIBUTE(3, 4);
void SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap) DVL_PRINTF_ATTRIBUTE(3, 0);

void SDL_LogSetAllPriority(SDL_LogPriority priority);
Expand Down

0 comments on commit af470a0

Please sign in to comment.