MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / luaConsoleLog

Function luaConsoleLog

app/src/DataModel/Scripting/LuaCompat.cpp:258–291  ·  view source on GitHub ↗

* @brief Lua C closure that logs its arguments tab-separated through the Qt message * system (so output lands in the application console, like JS console.log); * the message type travels in the closure's first upvalue so one function * serves print() and every console.* level. */

Source from the content-addressed store, hash-verified

256 * serves print() and every console.* level.
257 */
258static int luaConsoleLog(lua_State* L)
259{
260 Q_ASSERT(L != nullptr);
261
262 const int argc = lua_gettop(L);
263 QByteArray message;
264 for (int i = 1; i <= argc; ++i) {
265 size_t length = 0;
266 const char* text = luaL_tolstring(L, i, &length);
267 Q_ASSERT(text != nullptr);
268 if (i > 1)
269 message.append('\t');
270
271 message.append(text, static_cast<qsizetype>(length));
272 lua_pop(L, 1);
273 }
274
275 switch (static_cast<QtMsgType>(lua_tointeger(L, lua_upvalueindex(1)))) {
276 case QtInfoMsg:
277 qInfo().noquote() << QString::fromUtf8(message);
278 break;
279 case QtWarningMsg:
280 qWarning().noquote() << QString::fromUtf8(message);
281 break;
282 case QtCriticalMsg:
283 qCritical().noquote() << QString::fromUtf8(message);
284 break;
285 default:
286 qDebug().noquote() << QString::fromUtf8(message);
287 break;
288 }
289
290 return 0;
291}
292
293/**
294 * @brief Installs a JS-style console table and replaces print() so script output is

Callers

nothing calls this directly

Calls 3

lua_gettopFunction · 0.85
luaL_tolstringFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected