fix: debug.print output is not captured by core.capture_output
This commit is contained in:
@@ -3,6 +3,9 @@
|
|||||||
|
|
||||||
#include "libs/api_lua.hpp"
|
#include "libs/api_lua.hpp"
|
||||||
#include "debug/Logger.hpp"
|
#include "debug/Logger.hpp"
|
||||||
|
#include "logic/scripting/scripting.hpp"
|
||||||
|
|
||||||
|
using namespace scripting;
|
||||||
|
|
||||||
static debug::Logger logger("lua-debug");
|
static debug::Logger logger("lua-debug");
|
||||||
|
|
||||||
@@ -28,13 +31,13 @@ const int MAX_DEPTH = 10;
|
|||||||
|
|
||||||
int l_debug_print(lua::State* L) {
|
int l_debug_print(lua::State* L) {
|
||||||
auto addIndentation = [](int depth) {
|
auto addIndentation = [](int depth) {
|
||||||
for (int i = 0; i < depth; ++i) std::cout << " ";
|
for (int i = 0; i < depth; ++i) *output_stream << " ";
|
||||||
};
|
};
|
||||||
|
|
||||||
auto printHexData = [](const void* ptr, size_t size) {
|
auto printHexData = [](const void* ptr, size_t size) {
|
||||||
const auto* bytePtr = reinterpret_cast<const uint8_t*>(ptr);
|
const auto* bytePtr = reinterpret_cast<const uint8_t*>(ptr);
|
||||||
for (size_t i = 0; i < size; ++i) {
|
for (size_t i = 0; i < size; ++i) {
|
||||||
std::cout << std::hex << std::setw(2) << std::setfill('0')
|
*output_stream << std::hex << std::setw(2) << std::setfill('0')
|
||||||
<< static_cast<int>(bytePtr[i])
|
<< static_cast<int>(bytePtr[i])
|
||||||
<< ((i + 1) % 8 == 0 && i + 1 < size ? "\n" : " ");
|
<< ((i + 1) % 8 == 0 && i + 1 < size ? "\n" : " ");
|
||||||
}
|
}
|
||||||
@@ -43,20 +46,20 @@ int l_debug_print(lua::State* L) {
|
|||||||
auto printEscapedString = [](const char* str) {
|
auto printEscapedString = [](const char* str) {
|
||||||
while (*str) {
|
while (*str) {
|
||||||
switch (*str) {
|
switch (*str) {
|
||||||
case '\\': std::cout << "\\\\"; break;
|
case '\\': *output_stream << "\\\\"; break;
|
||||||
case '\"': std::cout << "\\\""; break;
|
case '\"': *output_stream << "\\\""; break;
|
||||||
case '\n': std::cout << "\\n"; break;
|
case '\n': *output_stream << "\\n"; break;
|
||||||
case '\t': std::cout << "\\t"; break;
|
case '\t': *output_stream << "\\t"; break;
|
||||||
case '\r': std::cout << "\\r"; break;
|
case '\r': *output_stream << "\\r"; break;
|
||||||
case '\b': std::cout << "\\b"; break;
|
case '\b': *output_stream << "\\b"; break;
|
||||||
case '\f': std::cout << "\\f"; break;
|
case '\f': *output_stream << "\\f"; break;
|
||||||
default:
|
default:
|
||||||
if (iscntrl(static_cast<unsigned char>(*str))) {
|
if (iscntrl(static_cast<unsigned char>(*str))) {
|
||||||
// Print other control characters in \xHH format
|
// Print other control characters in \xHH format
|
||||||
std::cout << "\\x" << std::hex << std::setw(2) << std::setfill('0')
|
*output_stream << "\\x" << std::hex << std::setw(2) << std::setfill('0')
|
||||||
<< static_cast<int>(static_cast<unsigned char>(*str)) << std::dec;
|
<< static_cast<int>(static_cast<unsigned char>(*str)) << std::dec;
|
||||||
} else {
|
} else {
|
||||||
std::cout << *str;
|
*output_stream << *str;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -68,80 +71,80 @@ int l_debug_print(lua::State* L) {
|
|||||||
int depth,
|
int depth,
|
||||||
bool is_key) {
|
bool is_key) {
|
||||||
if (depth > MAX_DEPTH) {
|
if (depth > MAX_DEPTH) {
|
||||||
std::cout << "{...}";
|
*output_stream << "{...}";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (lua::type(L, index)) {
|
switch (lua::type(L, index)) {
|
||||||
case LUA_TSTRING:
|
case LUA_TSTRING:
|
||||||
if (is_key){
|
if (is_key){
|
||||||
std::cout << lua::tostring(L, index);
|
*output_stream << lua::tostring(L, index);
|
||||||
}else{
|
}else{
|
||||||
std::cout << "\"";
|
*output_stream << "\"";
|
||||||
printEscapedString(lua::tostring(L, index));
|
printEscapedString(lua::tostring(L, index));
|
||||||
std::cout << "\"";
|
*output_stream << "\"";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LUA_TBOOLEAN:
|
case LUA_TBOOLEAN:
|
||||||
std::cout << (lua::toboolean(L, index) ? "true" : "false");
|
*output_stream << (lua::toboolean(L, index) ? "true" : "false");
|
||||||
break;
|
break;
|
||||||
case LUA_TNUMBER:
|
case LUA_TNUMBER:
|
||||||
std::cout << lua::tonumber(L, index);
|
*output_stream << lua::tonumber(L, index);
|
||||||
break;
|
break;
|
||||||
case LUA_TTABLE: {
|
case LUA_TTABLE: {
|
||||||
bool is_list = lua::objlen(L, index) > 0, hadItems = false;
|
bool is_list = lua::objlen(L, index) > 0, hadItems = false;
|
||||||
int absTableIndex =
|
int absTableIndex =
|
||||||
index > 0 ? index : lua::gettop(L) + index + 1;
|
index > 0 ? index : lua::gettop(L) + index + 1;
|
||||||
std::cout << "{";
|
*output_stream << "{";
|
||||||
lua::pushnil(L);
|
lua::pushnil(L);
|
||||||
while (lua::next(L, absTableIndex) != 0) {
|
while (lua::next(L, absTableIndex) != 0) {
|
||||||
if (hadItems)
|
if (hadItems)
|
||||||
std::cout << "," << '\n';
|
*output_stream << "," << '\n';
|
||||||
else
|
else
|
||||||
std::cout << '\n';
|
*output_stream << '\n';
|
||||||
|
|
||||||
addIndentation(depth + 1);
|
addIndentation(depth + 1);
|
||||||
if (!is_list) {
|
if (!is_list) {
|
||||||
debugPrint(-2, depth, true);
|
debugPrint(-2, depth, true);
|
||||||
std::cout << " = ";
|
*output_stream << " = ";
|
||||||
}
|
}
|
||||||
debugPrint(-1, depth + 1, false);
|
debugPrint(-1, depth + 1, false);
|
||||||
lua::pop(L, 1);
|
lua::pop(L, 1);
|
||||||
hadItems = true;
|
hadItems = true;
|
||||||
}
|
}
|
||||||
if (hadItems) std::cout << '\n';
|
if (hadItems) *output_stream << '\n';
|
||||||
addIndentation(depth);
|
addIndentation(depth);
|
||||||
std::cout << "}";
|
*output_stream << "}";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TFUNCTION:
|
case LUA_TFUNCTION:
|
||||||
std::cout << "function(0x" << std::hex
|
*output_stream << "function(0x" << std::hex
|
||||||
<< lua::topointer(L, index) << std::dec << ")";
|
<< lua::topointer(L, index) << std::dec << ")";
|
||||||
break;
|
break;
|
||||||
case LUA_TUSERDATA:
|
case LUA_TUSERDATA:
|
||||||
std::cout << "userdata:\n";
|
*output_stream << "userdata:\n";
|
||||||
printHexData(lua::topointer(L, index), lua::objlen(L, index));
|
printHexData(lua::topointer(L, index), lua::objlen(L, index));
|
||||||
break;
|
break;
|
||||||
case LUA_TLIGHTUSERDATA:
|
case LUA_TLIGHTUSERDATA:
|
||||||
std::cout << "lightuserdata:\n";
|
*output_stream << "lightuserdata:\n";
|
||||||
printHexData(lua::topointer(L, index), sizeof(void*));
|
printHexData(lua::topointer(L, index), sizeof(void*));
|
||||||
break;
|
break;
|
||||||
case LUA_TNIL:
|
case LUA_TNIL:
|
||||||
std::cout << "nil";
|
*output_stream << "nil";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cout << lua::type_name(L, lua::type(L, index));
|
*output_stream << lua::type_name(L, lua::type(L, index));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int n = lua::gettop(L);
|
int n = lua::gettop(L);
|
||||||
std::cout << "debug.print(" << '\n';
|
*output_stream << "debug.print(" << '\n';
|
||||||
for (int i = 1; i <= n; ++i) {
|
for (int i = 1; i <= n; ++i) {
|
||||||
addIndentation(1);
|
addIndentation(1);
|
||||||
debugPrint(i, 1, false);
|
debugPrint(i, 1, false);
|
||||||
if (i < n) std::cout << "," << '\n';
|
if (i < n) *output_stream << "," << '\n';
|
||||||
}
|
}
|
||||||
std::cout << '\n' << ")" << std::endl;
|
*output_stream << '\n' << ")" << std::endl;
|
||||||
lua::pop(L, n);
|
lua::pop(L, n);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user