Move logging to sol

This commit is contained in:
Mm2PL 2024-10-06 17:23:43 +02:00
parent 0d9f5f2576
commit a8438bfa50
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
3 changed files with 31 additions and 38 deletions

View file

@ -17,21 +17,24 @@
# include <QUrl>
# include <sol/forward.hpp>
# include <sol/state_view.hpp>
# include <sol/variadic_args.hpp>
# include <utility>
namespace {
using namespace chatterino;
void logHelper(lua_State *L, Plugin *pl, QDebug stream, int argc)
void logHelper(lua_State *L, Plugin *pl, QDebug stream,
const sol::variadic_args &args)
{
stream.noquote();
stream << "[" + pl->id + ":" + pl->meta.name + "]";
for (int i = 1; i <= argc; i++)
for (const auto &arg : args)
{
stream << lua::toString(L, i);
stream << lua::toString(L, arg.stack_index());
// Remove this from our stack
lua_pop(L, 1);
}
lua_pop(L, argc);
}
QDebug qdebugStreamForLogLevel(lua::api::LogLevel lvl)
@ -86,25 +89,14 @@ void c2_register_callback(Plugin *pl, EventType evtType,
pl->callbacks[evtType] = std::move(callback);
}
int c2_log(lua_State *L)
void c2_log(sol::this_state L, Plugin *pl, LogLevel lvl,
sol::variadic_args args)
{
auto *pl = getApp()->getPlugins()->getPluginByStatePtr(L);
if (pl == nullptr)
lua::StackGuard guard(L);
{
luaL_error(L, "c2_log: internal error: no plugin?");
return 0;
QDebug stream = qdebugStreamForLogLevel(lvl);
logHelper(L, pl, stream, args);
}
auto logc = lua_gettop(L) - 1;
// This is almost the expansion of qCDebug() macro, actual thing is wrapped in a for loop
LogLevel lvl{};
if (!lua::pop(L, &lvl, 1))
{
luaL_error(L, "Invalid log level, use one from c2.LogLevel.");
return 0;
}
QDebug stream = qdebugStreamForLogLevel(lvl);
logHelper(L, pl, stream, logc);
return 0;
}
int c2_later(lua_State *L)
@ -301,22 +293,14 @@ int searcherRelative(lua_State *L)
return loadfile(L, filename);
}
int g_print(lua_State *L)
void g_print(sol::this_state L, Plugin *pl, sol::variadic_args args)
{
auto *pl = getApp()->getPlugins()->getPluginByStatePtr(L);
if (pl == nullptr)
{
luaL_error(L, "c2_print: internal error: no plugin?");
return 0;
}
auto argc = lua_gettop(L);
// This is almost the expansion of qCDebug() macro, actual thing is wrapped in a for loop
auto stream =
(QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE,
QT_MESSAGELOG_FUNC, chatterinoLua().categoryName())
.debug());
logHelper(L, pl, stream, argc);
return 0;
logHelper(L, pl, stream, args);
}
} // namespace chatterino::lua::api

View file

@ -113,7 +113,8 @@ void c2_register_callback(Plugin *pl, EventType evtType,
* @lua@param ... any Values to log. Should be convertible to a string with `tostring()`.
* @exposed c2.log
*/
int c2_log(lua_State *L);
void c2_log(sol::this_state L, Plugin *pl, LogLevel lvl,
sol::variadic_args args);
/**
* Calls callback around msec milliseconds later. Does not freeze Chatterino.
@ -126,7 +127,7 @@ int c2_later(lua_State *L);
// These ones are global
int g_load(lua_State *L);
int g_print(lua_State *L);
void g_print(sol::this_state L, Plugin *pl, sol::variadic_args args);
// NOLINTEND(readability-identifier-naming)
// This is for require() exposed as an element of package.searchers

View file

@ -24,6 +24,7 @@
# include <QJsonDocument>
# include <sol/forward.hpp>
# include <sol/sol.hpp>
# include <sol/variadic_args.hpp>
# include <memory>
# include <utility>
@ -149,7 +150,6 @@ void PluginController::openLibrariesFor(Plugin *plugin, const QDir &pluginDir)
// NOLINTNEXTLINE(*-avoid-c-arrays)
static const luaL_Reg c2Lib[] = {
{"log", lua::api::c2_log},
{"later", lua::api::c2_later},
{nullptr, nullptr},
};
@ -161,9 +161,6 @@ void PluginController::openLibrariesFor(Plugin *plugin, const QDir &pluginDir)
luaL_setfuncs(L, c2Lib, 0);
lua::pushEnumTable<lua::api::LogLevel>(L);
lua_setfield(L, c2libIdx, "LogLevel");
lua_setfield(L, gtable, "c2");
// ban functions
@ -179,7 +176,6 @@ void PluginController::openLibrariesFor(Plugin *plugin, const QDir &pluginDir)
// NOLINTNEXTLINE(*-avoid-c-arrays)
static const luaL_Reg replacementFuncs[] = {
{"load", lua::api::g_load},
{"print", lua::api::g_print},
{nullptr, nullptr},
};
luaL_setfuncs(L, replacementFuncs, 0);
@ -280,7 +276,14 @@ void PluginController::openLibrariesFor(Plugin *plugin, const QDir &pluginDir)
// especially in cases when the plugin is errored.
void PluginController::initSol(sol::state_view &lua, Plugin *plugin)
{
sol::table c2 = lua.globals()["c2"];
auto g = lua.globals();
// Do not capture plugin->state_ in lambdas, this makes the functions unusable in callbacks
g.set_function("print",
[plugin](sol::this_state s, sol::variadic_args args) {
lua::api::g_print(s, plugin, args);
});
sol::table c2 = g["c2"];
c2.set_function("register_command",
[plugin](const QString &name, sol::protected_function cb) {
return plugin->registerCommand(name, std::move(cb));
@ -289,6 +292,10 @@ void PluginController::initSol(sol::state_view &lua, Plugin *plugin)
sol::protected_function cb) {
lua::api::c2_register_callback(plugin, ev, std::move(cb));
});
c2.set_function("log", [plugin](sol::this_state s, lua::api::LogLevel lvl,
sol::variadic_args args) {
lua::api::c2_log(s, plugin, lvl, args);
});
lua::api::ChannelRef::createUserType(c2);
lua::api::HTTPResponse::createUserType(c2);
@ -296,6 +303,7 @@ void PluginController::initSol(sol::state_view &lua, Plugin *plugin)
c2["ChannelType"] = lua::createEnumTable<Channel::Type>(lua);
c2["HTTPMethod"] = lua::createEnumTable<NetworkRequestType>(lua);
c2["EventType"] = lua::createEnumTable<lua::api::EventType>(lua);
c2["LogLevel"] = lua::createEnumTable<lua::api::LogLevel>(lua);
}
void PluginController::load(const QFileInfo &index, const QDir &pluginDir,