Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bindings/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(LUA_BINDINGS_SOURCE
"src/cubos.cpp"
"src/plugin.cpp"
"src/systems.cpp"
"src/logging.cpp"
)

# ------------------------ Configure lua bindings target -------------------------
Expand Down
4 changes: 4 additions & 0 deletions bindings/lua/samples/hello_world/scripts/hello.lua
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
print("Hello world!")

cubos.info("hello world", 123, 123.3)
cubos.warn("this is a warning", {}, function() cubos.info("ok") end)
cubos.error("this is an error")
6 changes: 6 additions & 0 deletions bindings/lua/src/cubos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "cubos.hpp"
#include "systems.hpp"
#include "logging.hpp"

using cubos::engine::Cubos;

Expand All @@ -25,6 +26,11 @@ void cubos::bindings::lua::injectCubos(lua_State* state, Cubos* cubos)
lua_newtable(state);
pushFunction(state, "startupSystem", startupSystem);
pushFunction(state, "system", system);

pushFunction(state, "info", logInfo);
pushFunction(state, "warn", logWarn);
pushFunction(state, "error", logError);

lua_setglobal(state, "cubos");
}

Expand Down
105 changes: 105 additions & 0 deletions bindings/lua/src/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "systems.hpp"

#include <string>
#include <cmath>

#include <cubos/engine/prelude.hpp>

using namespace cubos::core::tel;

static std::string formatArgs(lua_State *state)
{
std::string str;
int top = lua_gettop(state);

for (int i = 1; i <= top; i++)
{
int type = lua_type(state, i);

switch (type)
{
case LUA_TSTRING:
str.append(lua_tostring(state, i));
break;
case LUA_TNUMBER: {
lua_Number n = lua_tonumber(state, i);

if (std::floor(n) == n)
Comment thread
mcanais marked this conversation as resolved.
{
str.append(std::to_string(static_cast<int>(n)));
}
else
Comment thread
mcanais marked this conversation as resolved.
Comment thread
mcanais marked this conversation as resolved.
{
str.append(std::to_string(n));
}
break;
}
case LUA_TBOOLEAN:
str.append(lua_toboolean(state, i) ? "true" : "false");
Comment thread
mcanais marked this conversation as resolved.
break;
case LUA_TNIL:
str.append("nil");
break;
case LUA_TTABLE:
str.append("<table>");
break;
case LUA_TFUNCTION:
str.append("<function>");
break;
case LUA_TUSERDATA:
str.append("<userdata>");
break;
case LUA_TLIGHTUSERDATA:
str.append("<lightuserdata>");
break;
case LUA_TTHREAD:
str.append("<thread>");
break;
}

if (i != top)
Comment thread
mcanais marked this conversation as resolved.
str.append(" ");
}
Comment thread
mcanais marked this conversation as resolved.

return str;
}

static void log(lua_State* state, Level logLevel)
{
std::string formattedArgs = formatArgs(state);
Logger::Location location;
lua_Debug debugInfo;

if (lua_getstack(state, 1, &debugInfo))
Comment thread
mcanais marked this conversation as resolved.
{
if (lua_getinfo(state, "nSl", &debugInfo))
Comment thread
mcanais marked this conversation as resolved.
{
location.function = debugInfo.name ? debugInfo.name : "undefined";
Comment thread
mcanais marked this conversation as resolved.
location.file = debugInfo.short_src;
location.line = debugInfo.currentline;

Logger::write(logLevel, location, formattedArgs);
}
}
}

namespace cubos::bindings::lua
{
int logInfo(lua_State* state)
{
log(state, Level::Info);
return 0;
};

int logWarn(lua_State* state)
{
log(state, Level::Warn);
return 0;
};

int logError(lua_State* state)
{
log(state, Level::Error);
return 0;
};
} // namespace cubos::bindings::lua
29 changes: 29 additions & 0 deletions bindings/lua/src/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// @dir
/// @brief @ref lua-bindings-plugin plugin directory.

/// @file
/// @brief Logging lua api.
/// @ingroup lua-bindings-plugin
#pragma once

#include <cstdint>

#include <lua.hpp>

namespace cubos::bindings::lua
{
/// @brief Lua api function to log information.
/// @param state The current lua state.
/// @ingroup lua-bindings-plugin
int logInfo(lua_State* state);

/// @brief Lua api function to log a warning.
/// @param state The current lua state.
/// @ingroup lua-bindings-plugin
int logWarn(lua_State* state);

/// @brief Lua api function log an error.
/// @param state The current lua state.
/// @ingroup lua-bindings-plugin
int logError(lua_State* state);
} // namespace cubos::bindings::lua
Loading