Skip to content

Commit 4e34165

Browse files
committed
feat(scripting): added logging functions
1 parent b711df0 commit 4e34165

5 files changed

Lines changed: 145 additions & 0 deletions

File tree

bindings/lua/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(LUA_BINDINGS_SOURCE
1313
"src/cubos.cpp"
1414
"src/plugin.cpp"
1515
"src/systems.cpp"
16+
"src/logging.cpp"
1617
)
1718

1819
# ------------------------ Configure lua bindings target -------------------------
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
print("Hello world!")
2+
3+
cubos.info("hello world", 123, 123.3)
4+
cubos.warn("this is a warning", {}, function() cubos.info("ok") end)
5+
cubos.error("this is an error")

bindings/lua/src/cubos.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "cubos.hpp"
99
#include "systems.hpp"
10+
#include "logging.hpp"
1011

1112
using cubos::engine::Cubos;
1213

@@ -25,6 +26,11 @@ void cubos::bindings::lua::injectCubos(lua_State* state, Cubos* cubos)
2526
lua_newtable(state);
2627
pushFunction(state, "startupSystem", startupSystem);
2728
pushFunction(state, "system", system);
29+
30+
pushFunction(state, "info", logInfo);
31+
pushFunction(state, "warn", logWarn);
32+
pushFunction(state, "error", logError);
33+
2834
lua_setglobal(state, "cubos");
2935
}
3036

bindings/lua/src/logging.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "systems.hpp"
2+
3+
#include <string>
4+
#include <cmath>
5+
6+
#include <cubos/engine/prelude.hpp>
7+
8+
using namespace cubos::core::tel;
9+
10+
static std::string formatArgs(lua_State *state)
11+
{
12+
std::string str;
13+
int top = lua_gettop(state);
14+
15+
for (int i = 1; i <= top; i++)
16+
{
17+
int type = lua_type(state, i);
18+
19+
switch (type)
20+
{
21+
case LUA_TSTRING:
22+
str.append(lua_tostring(state, i));
23+
break;
24+
case LUA_TNUMBER: {
25+
lua_Number n = lua_tonumber(state, i);
26+
27+
if (std::floor(n) == n)
28+
{
29+
str.append(std::to_string(static_cast<int>(n)));
30+
}
31+
else
32+
{
33+
str.append(std::to_string(n));
34+
}
35+
break;
36+
}
37+
case LUA_TBOOLEAN:
38+
str.append(lua_toboolean(state, i) ? "true" : "false");
39+
break;
40+
case LUA_TNIL:
41+
str.append("nil");
42+
break;
43+
case LUA_TTABLE:
44+
str.append("<table>");
45+
break;
46+
case LUA_TFUNCTION:
47+
str.append("<function>");
48+
break;
49+
case LUA_TUSERDATA:
50+
str.append("<userdata>");
51+
break;
52+
case LUA_TLIGHTUSERDATA:
53+
str.append("<lightuserdata>");
54+
break;
55+
case LUA_TTHREAD:
56+
str.append("<thread>");
57+
break;
58+
}
59+
60+
if (i != top)
61+
str.append(" ");
62+
}
63+
64+
return str;
65+
}
66+
67+
static void log(lua_State* state, Level logLevel)
68+
{
69+
std::string formattedArgs = formatArgs(state);
70+
Logger::Location location;
71+
lua_Debug debugInfo;
72+
73+
if (lua_getstack(state, 1, &debugInfo))
74+
{
75+
if (lua_getinfo(state, "nSl", &debugInfo))
76+
{
77+
location.function = debugInfo.name ? debugInfo.name : "undefined";
78+
location.file = debugInfo.short_src;
79+
location.line = debugInfo.currentline;
80+
81+
Logger::write(logLevel, location, formattedArgs);
82+
}
83+
}
84+
}
85+
86+
namespace cubos::bindings::lua
87+
{
88+
int logInfo(lua_State* state)
89+
{
90+
log(state, Level::Info);
91+
return 0;
92+
};
93+
94+
int logWarn(lua_State* state)
95+
{
96+
log(state, Level::Warn);
97+
return 0;
98+
};
99+
100+
int logError(lua_State* state)
101+
{
102+
log(state, Level::Error);
103+
return 0;
104+
};
105+
} // namespace cubos::bindings::lua

bindings/lua/src/logging.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// @dir
2+
/// @brief @ref lua-bindings-plugin plugin directory.
3+
4+
/// @file
5+
/// @brief Logging lua api.
6+
/// @ingroup lua-bindings-plugin
7+
#pragma once
8+
9+
#include <cstdint>
10+
11+
#include <lua.hpp>
12+
13+
namespace cubos::bindings::lua
14+
{
15+
/// @brief Lua api function to log information.
16+
/// @param state The current lua state.
17+
/// @ingroup lua-bindings-plugin
18+
int logInfo(lua_State* state);
19+
20+
/// @brief Lua api function to log a warning.
21+
/// @param state The current lua state.
22+
/// @ingroup lua-bindings-plugin
23+
int logWarn(lua_State* state);
24+
25+
/// @brief Lua api function log an error.
26+
/// @param state The current lua state.
27+
/// @ingroup lua-bindings-plugin
28+
int logError(lua_State* state);
29+
} // namespace cubos::bindings::lua

0 commit comments

Comments
 (0)