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 15, 2023
1 parent f00c3a1 commit c173456
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 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 @@ -73,6 +75,48 @@ void LuaPanic(sol::optional<std::string> maybe_msg)
maybe_msg.value_or("unknown error"));
}

void LuaLog(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 (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::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(static_cast<int>(LogCategory::Application), static_cast<SDL_LogPriority>(priority),
"%s", formatted.c_str());
}

} // namespace

void Sol2DebugPrintStack(lua_State *state)
Expand Down Expand Up @@ -110,10 +154,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", [](std::string_view fmt, sol::variadic_args args) { LuaLog(LogPriority::Info, fmt, std::move(args)); },
"logVerbose", [](std::string_view fmt, sol::variadic_args args) { LuaLog(LogPriority::Verbose, fmt, std::move(args)); },
"logDebug", [](std::string_view fmt, sol::variadic_args args) { LuaLog(LogPriority::Debug, fmt, std::move(args)); },
"logWarning", [](std::string_view fmt, sol::variadic_args args) { LuaLog(LogPriority::Warn, fmt, std::move(args)); },
"logError", [](std::string_view fmt, sol::variadic_args args) { LuaLog(LogPriority::Error, fmt, std::move(args)); });

RunScript("lua/init.lua");
RunScript("lua/user.lua");
Expand Down

0 comments on commit c173456

Please sign in to comment.