Add lua Util.LogInfo(), Util.LogError(), etc. functions (#309)

```lua
Util.LogInfo("Hello, World!")
Util.LogWarn("Cool warning")
Util.LogError("Oh no!")
Util.LogDebug("hi")
```
now produces

```
[19/04/24 11:06:50.142] [Test] [INFO] Hello, World!    
[19/04/24 11:06:50.142] [Test] [WARN] Cool warning    
[19/04/24 11:06:50.142] [Test] [ERROR] Oh no!
[19/04/24 11:06:50.142] [Test] [DEBUG] hi
```

where `Test` is the lua state id.
This commit is contained in:
Lion 2024-05-06 11:39:12 +02:00 committed by GitHub
commit c3b4528c89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 1 deletions

View File

@ -214,6 +214,10 @@ void RegisterThread(const std::string& str);
do { \
Application::Console().Write(_this_location + std::string("[LUA ERROR] ") + (x)); \
} while (false)
#define beammp_lua_log(level, plugin, x) \
do { \
Application::Console().Write(_this_location + fmt::format("[{}] [{}] ", plugin, level) + (x)); \
} while (false)
#define beammp_lua_warn(x) \
do { \
Application::Console().Write(_this_location + std::string("[LUA WARN] ") + (x)); \
@ -269,6 +273,7 @@ void RegisterThread(const std::string& str);
#define beammp_tracef(...) beammp_trace(fmt::format(__VA_ARGS__))
#define beammp_lua_errorf(...) beammp_lua_error(fmt::format(__VA_ARGS__))
#define beammp_lua_warnf(...) beammp_lua_warn(fmt::format(__VA_ARGS__))
#define beammp_lua_log(level, plugin, x) /* x */
#endif // DOCTEST_CONFIG_DISABLE

View File

@ -18,6 +18,7 @@
#include "TLuaEngine.h"
#include "Client.h"
#include "Common.h"
#include "CustomAssert.h"
#include "Http.h"
#include "LuaAPI.h"
@ -829,6 +830,40 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
MPTable.set_function("Set", &LuaAPI::MP::Set);
auto UtilTable = StateView.create_named_table("Util");
UtilTable.set_function("LogDebug", [this](sol::variadic_args args) {
std::string ToPrint = "";
for (const auto& arg : args) {
ToPrint += LuaAPI::LuaToString(static_cast<const sol::object>(arg));
ToPrint += "\t";
}
if (Application::Settings.DebugModeEnabled) {
beammp_lua_log("DEBUG", mStateId, ToPrint);
}
});
UtilTable.set_function("LogInfo", [this](sol::variadic_args args) {
std::string ToPrint = "";
for (const auto& arg : args) {
ToPrint += LuaAPI::LuaToString(static_cast<const sol::object>(arg));
ToPrint += "\t";
}
beammp_lua_log("INFO", mStateId, ToPrint);
});
UtilTable.set_function("LogWarn", [this](sol::variadic_args args) {
std::string ToPrint = "";
for (const auto& arg : args) {
ToPrint += LuaAPI::LuaToString(static_cast<const sol::object>(arg));
ToPrint += "\t";
}
beammp_lua_log("WARN", mStateId, ToPrint);
});
UtilTable.set_function("LogError", [this](sol::variadic_args args) {
std::string ToPrint = "";
for (const auto& arg : args) {
ToPrint += LuaAPI::LuaToString(static_cast<const sol::object>(arg));
ToPrint += "\t";
}
beammp_lua_log("ERROR", mStateId, ToPrint);
});
UtilTable.set_function("JsonEncode", &LuaAPI::MP::JsonEncode);
UtilTable.set_function("JsonDecode", [this](const std::string& str) {
return Lua_JsonDecode(str);
@ -1101,7 +1136,7 @@ void TLuaResult::MarkAsReady() {
void TLuaResult::WaitUntilReady() {
std::unique_lock readyLock(*this->ReadyMutex);
// wait if not ready yet
if(!this->Ready)
if (!this->Ready)
this->ReadyCondition->wait(readyLock);
}