Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 93861b96 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

SCRIPT: Improve logging for scriptable objects

parent abf310a9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!16Add a way to execute a Lua file with Vivy in a click-click way
...@@ -4,28 +4,20 @@ ...@@ -4,28 +4,20 @@
// SampleObject implementation // SampleObject implementation
using namespace Vivy::Script; using namespace Vivy::Script;
SampleObject::SampleObject() noexcept { fprintf(stderr, "CREATE SampleObject\n"); }
SampleObject::~SampleObject() noexcept { fprintf(stderr, "DESTROY SampleObject\n"); }
int int
SampleObject::foo(lua_State *L) noexcept SampleObject::foo(lua_State *L) noexcept
{ {
auto *context = LuaContext::getContext(L);
SampleObject *obj = CHECK(L, 1); SampleObject *obj = CHECK(L, 1);
double bar = luaL_checknumber(L, 2); double bar = luaL_checknumber(L, 2);
fprintf(stderr, "SampleObject(%p)::foo -> %f\n", static_cast<void *>(obj), bar); out(context) << "SampleObject(" << static_cast<void *>(obj) << ")::foo -> " << bar << "\n";
return 0; return 0;
} }
FreeFunctions::FreeFunctions() noexcept {}
FreeFunctions::~FreeFunctions() noexcept {}
int int
FreeFunctions::prettyPrint(lua_State *L) noexcept FreeFunctions::prettyPrint(lua_State *L) noexcept
{ {
auto *context = LuaContext::getContext(L); auto *context = LuaContext::getContext(L);
auto &out = context->getOutputStream(); out(context) << "Pretty printer!\n";
out << "prettyPrint!!\n";
return 0; return 0;
} }
...@@ -27,7 +27,8 @@ protected: ...@@ -27,7 +27,8 @@ protected:
static int GC(lua_State *L) noexcept static int GC(lua_State *L) noexcept
{ {
const Object *obj = reinterpret_cast<const Object *>(lua_topointer(L, 1)); const Object *obj = reinterpret_cast<const Object *>(lua_topointer(L, 1));
puts("CALL DESTRUCTOR"); auto *context = LuaContext::getContext(L);
err(context) << "Call destructor for " << Object::className << "\n";
obj->~Object(); obj->~Object();
return 0; return 0;
} }
...@@ -35,8 +36,7 @@ protected: ...@@ -35,8 +36,7 @@ protected:
static int CREATE(lua_State *L) noexcept static int CREATE(lua_State *L) noexcept
{ {
Object *obj = reinterpret_cast<Object *>(lua_newuserdata(L, sizeof(Object))); Object *obj = reinterpret_cast<Object *>(lua_newuserdata(L, sizeof(Object)));
puts("CALL CONSTRUCTOR"); new (obj) Object(L);
new (obj) Object;
luaL_getmetatable(L, Object::className); luaL_getmetatable(L, Object::className);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
return 1; return 1;
...@@ -45,9 +45,10 @@ protected: ...@@ -45,9 +45,10 @@ protected:
static Object *CHECK(lua_State *L, int index) noexcept static Object *CHECK(lua_State *L, int index) noexcept
{ {
luaL_checktype(L, index, LUA_TUSERDATA); luaL_checktype(L, index, LUA_TUSERDATA);
auto *context = LuaContext::getContext(L);
Object *obj = reinterpret_cast<Object *>(luaL_checkudata(L, index, Object::className)); Object *obj = reinterpret_cast<Object *>(luaL_checkudata(L, index, Object::className));
if (obj == nullptr) { if (obj == nullptr) {
puts("TYPE ERROR"); err(context) << "Type error, userdata is not of class " << Object::className << "\n";
luaL_typeerror(L, index, Object::className); luaL_typeerror(L, index, Object::className);
} }
return obj; return obj;
...@@ -58,6 +59,8 @@ protected: ...@@ -58,6 +59,8 @@ protected:
public: public:
static void Register(lua_State *L) noexcept static void Register(lua_State *L) noexcept
{ {
auto *context = LuaContext::getContext(L);
// Fill the method table, newclass = {} // Fill the method table, newclass = {}
lua_newtable(L); lua_newtable(L);
const int methodtable = lua_gettop(L); const int methodtable = lua_gettop(L);
...@@ -76,7 +79,7 @@ public: ...@@ -76,7 +79,7 @@ public:
lua_settable(L, metatable); lua_settable(L, metatable);
for (const luaL_Reg &meta : Object::metaMethods) { for (const luaL_Reg &meta : Object::metaMethods) {
fprintf(stderr, "LUA: REGISTER %s\n", meta.name); err(context) << "Register " << Object::className << "::" << meta.name << "\n";
lua_pushstring(L, meta.name); lua_pushstring(L, meta.name);
lua_pushcfunction(L, meta.func); lua_pushcfunction(L, meta.func);
lua_settable(L, metatable); lua_settable(L, metatable);
...@@ -87,7 +90,7 @@ public: ...@@ -87,7 +90,7 @@ public:
// Fill the method table with specified methods // Fill the method table with specified methods
for (const luaL_Reg &method : Object::methods) { for (const luaL_Reg &method : Object::methods) {
fprintf(stderr, "LUA: REGISTER %s\n", method.name); err(context) << "Register " << Object::className << "::" << method.name << "\n";
lua_pushstring(L, method.name); lua_pushstring(L, method.name);
lua_pushcfunction(L, method.func); lua_pushcfunction(L, method.func);
lua_settable(L, methodtable); lua_settable(L, methodtable);
...@@ -96,6 +99,7 @@ public: ...@@ -96,6 +99,7 @@ public:
// Last thing to do it seems... // Last thing to do it seems...
lua_pop(L, 1); // drop method-table lua_pop(L, 1); // drop method-table
lua_register(L, Object::className, CREATE); // Register the create method lua_register(L, Object::className, CREATE); // Register the create method
err(context) << "Register constructor " << Object::className << "()\n";
} }
}; };
...@@ -105,7 +109,7 @@ script_class (SampleObject) { ...@@ -105,7 +109,7 @@ script_class (SampleObject) {
LUA_SCRIPTABLE_CLASS(SampleObject) LUA_SCRIPTABLE_CLASS(SampleObject)
// CTors and DTors // CTors and DTors
SampleObject() noexcept; SampleObject(lua_State *) noexcept;
~SampleObject() noexcept; ~SampleObject() noexcept;
// The methods must be static with Lua interface // The methods must be static with Lua interface
...@@ -120,7 +124,7 @@ script_class (SampleObject) { ...@@ -120,7 +124,7 @@ script_class (SampleObject) {
script_class (FreeFunctions) { script_class (FreeFunctions) {
LUA_SCRIPTABLE_CLASS(FreeFunctions) LUA_SCRIPTABLE_CLASS(FreeFunctions)
FreeFunctions() noexcept; FreeFunctions(lua_State *) noexcept;
~FreeFunctions() noexcept; ~FreeFunctions() noexcept;
static int prettyPrint(lua_State *) noexcept; static int prettyPrint(lua_State *) noexcept;
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
// LuaContext implementation // LuaContext implementation
using namespace Vivy::Script; using namespace Vivy::Script;
LuaContext::LuaContext(std::stringstream &out, std::stringstream &err) noexcept LuaContext::LuaContext(ScriptStore::LoggerType &out, ScriptStore::LoggerType &err) noexcept
: L(luaL_newstate()) : L(luaL_newstate())
, streamOut(out) , streamOut(out)
, streamErr(err) , streamErr(err)
...@@ -59,8 +59,11 @@ LuaContext::Code ...@@ -59,8 +59,11 @@ LuaContext::Code
LuaContext::loadDocument(std::shared_ptr<ScriptDocument> doc) noexcept LuaContext::loadDocument(std::shared_ptr<ScriptDocument> doc) noexcept
{ {
QFileInfo fileCheck(doc->getName()); QFileInfo fileCheck(doc->getName());
if (!fileCheck.exists()) if (!fileCheck.exists()) {
err(this) << "File " << doc->getName().toStdString() << " for document "
<< doc->getUuid().toString().toStdString() << " doesn't exists\n";
return Code::Error; return Code::Error;
}
return loadFile(fileCheck.absoluteFilePath().toStdString().c_str()); return loadFile(fileCheck.absoluteFilePath().toStdString().c_str());
} }
...@@ -75,7 +78,7 @@ LuaContext::Code ...@@ -75,7 +78,7 @@ LuaContext::Code
LuaContext::exec() noexcept LuaContext::exec() noexcept
{ {
if (lua_pcall(L, 0, 0, 0) != LUA_OK) { if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
puts(getLastError().toStdString().c_str()); err(this) << getLastError().toStdString().c_str();
return Code::Error; return Code::Error;
} }
return Code::Success; return Code::Success;
...@@ -85,7 +88,7 @@ LuaContext::Code ...@@ -85,7 +88,7 @@ LuaContext::Code
LuaContext::loadString(const char *str) noexcept LuaContext::loadString(const char *str) noexcept
{ {
if (luaL_loadstring(L, str) != LUA_OK) { if (luaL_loadstring(L, str) != LUA_OK) {
puts("Error loading string"); err(this) << "Error loading string\n";
return Code::Error; return Code::Error;
} }
return exec(); return exec();
...@@ -95,7 +98,7 @@ LuaContext::Code ...@@ -95,7 +98,7 @@ LuaContext::Code
LuaContext::loadFile(const char *file) noexcept LuaContext::loadFile(const char *file) noexcept
{ {
if (luaL_loadfile(L, file) != LUA_OK) { if (luaL_loadfile(L, file) != LUA_OK) {
puts("Error loading file"); err(this) << "Error loading file " << file << "\n";
return Code::Error; return Code::Error;
} }
return exec(); return exec();
......
#pragma once #pragma once
#include "ScriptStore.hh"
#include <sstream> #include <sstream>
#include <map> #include <map>
...@@ -18,8 +19,8 @@ class LuaContext final { ...@@ -18,8 +19,8 @@ class LuaContext final {
lua_State *L; lua_State *L;
static inline std::map<const lua_State *const, LuaContext *> contextList = {}; static inline std::map<const lua_State *const, LuaContext *> contextList = {};
std::stringstream &streamOut; ScriptStore::LoggerType &streamOut;
std::stringstream &streamErr; ScriptStore::LoggerType &streamErr;
public: public:
enum class Code { enum class Code {
...@@ -27,14 +28,22 @@ public: ...@@ -27,14 +28,22 @@ public:
Error, Error,
}; };
LuaContext(std::stringstream &out, std::stringstream &err) noexcept; LuaContext(ScriptStore::LoggerType &out, ScriptStore::LoggerType &err) noexcept;
~LuaContext() noexcept; ~LuaContext() noexcept;
Code loadDocument(std::shared_ptr<ScriptDocument>) noexcept; Code loadDocument(std::shared_ptr<ScriptDocument>) noexcept;
QString getLastError() noexcept; QString getLastError() noexcept;
lua_State *getState() noexcept; lua_State *getState() noexcept;
std::stringstream &getOutputStream() noexcept;
std::stringstream &getErrorStream() noexcept; auto &getOutputStream() noexcept
{
return streamOut;
}
auto &getErrorStream() noexcept
{
return streamErr;
}
operator lua_State *() noexcept { return L; } operator lua_State *() noexcept { return L; }
static LuaContext *getContext(const lua_State *const) noexcept; static LuaContext *getContext(const lua_State *const) noexcept;
...@@ -50,4 +59,16 @@ private: ...@@ -50,4 +59,16 @@ private:
void swapEnv() noexcept; void swapEnv() noexcept;
}; };
static inline auto &
out(LuaContext *const context) noexcept
{
return context->getOutputStream();
}
static inline auto &
err(LuaContext *const context) noexcept
{
return context->getErrorStream();
}
} }
...@@ -2,25 +2,31 @@ ...@@ -2,25 +2,31 @@
#include "../CRTPStore.hh" #include "../CRTPStore.hh"
#include "ScriptDocument.hh" #include "ScriptDocument.hh"
#include "LuaContext.hh"
#include <sstream> #include <sstream>
namespace Vivy::Script
{
class LuaContext;
}
namespace Vivy namespace Vivy
{ {
class ScriptStore final : public CRTPStore<ScriptStore, ScriptDocument> { class ScriptStore final : public CRTPStore<ScriptStore, ScriptDocument> {
VIVY_STORAGE_CLASS(ScriptStore, ScriptDocument) VIVY_STORAGE_CLASS(ScriptStore, ScriptDocument)
std::unique_ptr<Script::LuaContext> luaContext{ nullptr };
std::stringstream streamOut;
std::stringstream streamErr;
public: public:
using Item = std::tuple<Uuid, QString>; using Item = std::tuple<Uuid, QString>;
using LoggerType = std::stringstream;
explicit ScriptStore() noexcept; explicit ScriptStore() noexcept;
void resetLoadedScripts() noexcept; void resetLoadedScripts() noexcept;
void loadScriptFolder(const QString &folderPath); void loadScriptFolder(const QString &folderPath);
std::vector<Item> getLoadedScripts() const noexcept; std::vector<Item> getLoadedScripts() const noexcept;
private:
std::unique_ptr<Script::LuaContext> luaContext{ nullptr };
LoggerType streamOut;
LoggerType streamErr;
}; };
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter