diff --git a/include/openspace/engine/configurationmanager.h b/include/openspace/engine/configurationmanager.h index c82ecb07fa..da899ce3bb 100644 --- a/include/openspace/engine/configurationmanager.h +++ b/include/openspace/engine/configurationmanager.h @@ -53,6 +53,10 @@ public: static const std::string KeyLuaDocumentationType; /// The key that stores the save location of the Lua documentation static const std::string KeyLuaDocumentationFile; + /// The key that stores the type of scripting log that should be stored + static const std::string KeyScriptLogType; + /// The key that stores the save location of the scripting log + static const std::string KeyScriptLogFile; /// The key that stores the type of Property documentation that should be stored static const std::string KeyPropertyDocumentationType; /// The key that stores the save location of the Property documentation diff --git a/include/openspace/scripting/scriptengine.h b/include/openspace/scripting/scriptengine.h index feab50c7e7..54242e7081 100644 --- a/include/openspace/scripting/scriptengine.h +++ b/include/openspace/scripting/scriptengine.h @@ -102,6 +102,8 @@ public: bool writeDocumentation(const std::string& filename, const std::string& type) const; + bool writeLog(const std::string& script); + void serialize(SyncBuffer* syncBuffer); void deserialize(SyncBuffer* syncBuffer); @@ -111,7 +113,9 @@ public: void preSynchronization(); void queueScript(const std::string &script); - + + void setLogFile(const std::string& filename, const std::string& type); + std::vector cachedScripts(); std::vector allLuaFunctions() const; @@ -122,6 +126,7 @@ public: void cacheScript(const std::string &library, const std::string &function, const std::string &script); private: + bool registerLuaLibrary(lua_State* state, const LuaLibrary& library); void addLibraryFunctions(lua_State* state, const LuaLibrary& library, bool replace); @@ -142,6 +147,14 @@ private: //parallel variables std::map> _cachedScripts; std::mutex _cachedScriptsMutex; + + //logging variables + bool _logFileExists = false; + bool _logScripts = true; + std::string _logType; + std::string _logFilename; + + }; } // namespace scripting diff --git a/openspace.cfg b/openspace.cfg index 6b52c5d47e..8af6c24e23 100644 --- a/openspace.cfg +++ b/openspace.cfg @@ -52,6 +52,10 @@ return { Type = "text", File = "${BASE_PATH}/Properties.txt" }, + ScriptLogFile = { + Type = "text", + File = "${BASE_PATH}/ScriptLog.txt" + }, DownloadRequestURL = "http://openspace.itn.liu.se/request.cgi", RenderingMethod = "Framebuffer" --RenderingMethod = "ABuffer" -- alternative: "Framebuffer" diff --git a/src/engine/configurationmanager.cpp b/src/engine/configurationmanager.cpp index f868aa5316..55211d1074 100644 --- a/src/engine/configurationmanager.cpp +++ b/src/engine/configurationmanager.cpp @@ -45,6 +45,8 @@ const string ConfigurationManager::KeyFonts = "Fonts"; const string ConfigurationManager::KeyConfigSgct = "SGCTConfig"; const string ConfigurationManager::KeyLuaDocumentationType = "LuaDocumentationFile.Type"; const string ConfigurationManager::KeyLuaDocumentationFile = "LuaDocumentationFile.File"; +const string ConfigurationManager::KeyScriptLogType = "ScriptLogFile.Type"; +const string ConfigurationManager::KeyScriptLogFile = "ScriptLogFile.File"; const string ConfigurationManager::KeyPropertyDocumentationType = "PropertyDocumentationFile.Type"; const string ConfigurationManager::KeyPropertyDocumentationFile = diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index 8230b2087c..6e025d861e 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -357,7 +357,7 @@ bool Scene::loadSceneInternal(const std::string& sceneDescriptionFilePath) { } } - // If a LuaDocumentationFile was specified, generate it now + // If a PropertyDocumentationFile was specified, generate it now const bool hasType = OsEng.configurationManager().hasKey(ConfigurationManager::KeyPropertyDocumentationType); const bool hasFile = OsEng.configurationManager().hasKey(ConfigurationManager::KeyPropertyDocumentationFile); if (hasType && hasFile) { diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index ae6760e394..9fa06d3558 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -24,12 +24,15 @@ #include -#include #include -#include +#include +#include + +#include #include #include -#include +#include + #include #include @@ -140,7 +143,12 @@ bool ScriptEngine::runScript(const std::string& script) { LWARNING("Script was empty"); return false; } - + + if (_logScripts) { + // Write command to log before it's executed + writeLog(script); + } + try { ghoul::lua::runScript(_state, script); } @@ -561,6 +569,68 @@ bool ScriptEngine::writeDocumentation(const std::string& filename, const std::st } } +bool ScriptEngine::writeLog(const std::string& script) { + // Check that logging is enabled and initialize if necessary + if (!_logFileExists) { + // If a ScriptLogFile was specified, generate it now + const bool hasType = OsEng.configurationManager() + .hasKey(ConfigurationManager::KeyScriptLogType); + const bool hasFile = OsEng.configurationManager() + .hasKey(ConfigurationManager::KeyScriptLogFile); + if (hasType && hasFile) { + OsEng.configurationManager() + .getValue(ConfigurationManager::KeyScriptLogType, _logType); + OsEng.configurationManager() + .getValue(ConfigurationManager::KeyScriptLogFile, _logFilename); + + _logFilename = absPath(_logFilename); + _logFileExists = true; + + LDEBUG("Using script log of type '" << _logType << + "' to file '" << _logFilename << "'"); + + // Test file and clear previous input + std::ofstream file(_logFilename, std::ofstream::out | std::ofstream::trunc); + + if (!file.good()) { + LERROR("Could not open file '" << _logFilename + << "' for logging scripts"); + + return false; + } + } else { + LDEBUG("No script log specified in 'openspace.cfg.' To log, set '" + << ConfigurationManager::KeyScriptLogType << " and " + << ConfigurationManager::KeyScriptLogFile + << " in configuration table."); + _logScripts = false; + return false; + } + } + + if (_logType == "text") { + // Simple text output to logfile + std::ofstream file(_logFilename, std::ofstream::app); + if (!file.good()) { + LERROR("Could not open file '" << _logFilename << "' for logging scripts"); + return false; + } + + + LDEBUG("Writing " << script); + file << script << std::endl; + file.close(); + } + else { + LERROR("Undefined type '" << _logType << "' for script documentation"); + _logScripts = false; + return false; + } + + return true; +} + + void ScriptEngine::serialize(SyncBuffer* syncBuffer){ syncBuffer->encode(_currentSyncedScript); _currentSyncedScript.clear();