From 0df5d34e042b60c7a4d069e492df2ffce0e21b64 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 16 Jul 2024 12:32:00 +0300 Subject: [PATCH] add logging functions to the library 'debug' --- res/modules/internal/stdcomp.lua | 6 ++-- src/logic/scripting/lua/api_lua.hpp | 2 ++ src/logic/scripting/lua/lua_engine.cpp | 2 ++ src/logic/scripting/lua/lua_extensions.cpp | 39 ++++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/logic/scripting/lua/lua_extensions.cpp diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index a5fd21cb..9f9e75d6 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -93,8 +93,7 @@ return { if callback then local result, err = pcall(callback) if err then - --// TODO: replace with error logging - print(err) + debug.error(err) end end end @@ -107,8 +106,7 @@ return { if callback then local result, err = pcall(callback) if err then - --// TODO: replace with error logging - print(err) + debug.error(err) end end end diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index d76a3796..4a92ba22 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -45,4 +45,6 @@ extern const luaL_Reg transformlib []; // Lua Overrides extern int l_print(lua::State* L); +void initialize_libs_extends(lua::State* L); + #endif // LOGIC_SCRIPTING_API_LUA_HPP_ diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 0b2070b2..6d303593 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -97,6 +97,8 @@ void lua::initialize() { createtable(L, 0, 0); setglobal(L, CHUNKS_TABLE); + initialize_libs_extends(L); + newusertype(L, "bytearray"); } diff --git a/src/logic/scripting/lua/lua_extensions.cpp b/src/logic/scripting/lua/lua_extensions.cpp new file mode 100644 index 00000000..6b3a1397 --- /dev/null +++ b/src/logic/scripting/lua/lua_extensions.cpp @@ -0,0 +1,39 @@ +#include "api_lua.hpp" + +#include "../../../debug/Logger.hpp" + +static debug::Logger logger("lua-debug"); + +static int l_debug_error(lua::State* L) { + auto text = lua::require_string(L, 1); + logger.error() << text; + return 0; +} + +static int l_debug_warning(lua::State* L) { + auto text = lua::require_string(L, 1); + logger.warning() << text; + return 0; +} + +static int l_debug_log(lua::State* L) { + auto text = lua::require_string(L, 1); + logger.info() << text; + return 0; +} + +void initialize_libs_extends(lua::State* L) { + if (lua::getglobal(L, "debug")) { + + lua::pushcfunction(L, lua::wrap); + lua::setfield(L, "error"); + + lua::pushcfunction(L, lua::wrap); + lua::setfield(L, "warning"); + + lua::pushcfunction(L, lua::wrap); + lua::setfield(L, "log"); + + lua::pop(L); + } +}