From 817864ce74f51b2aceaf4deb868eb4aa27b63637 Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Sat, 13 Nov 2021 17:09:09 +0000 Subject: [PATCH] AP_Scripting: convert DEBUG_LVL to DEBUG_OPTS bitmask --- libraries/AP_Scripting/AP_Scripting.cpp | 11 ++++++----- libraries/AP_Scripting/AP_Scripting.h | 2 +- libraries/AP_Scripting/lua_scripts.cpp | 10 +++++----- libraries/AP_Scripting/lua_scripts.h | 10 ++++++++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/libraries/AP_Scripting/AP_Scripting.cpp b/libraries/AP_Scripting/AP_Scripting.cpp index c6ce680820..5c0ef4b7d3 100644 --- a/libraries/AP_Scripting/AP_Scripting.cpp +++ b/libraries/AP_Scripting/AP_Scripting.cpp @@ -75,11 +75,12 @@ const AP_Param::GroupInfo AP_Scripting::var_info[] = { // @RebootRequired: True AP_GROUPINFO("HEAP_SIZE", 3, AP_Scripting, _script_heap_size, SCRIPTING_HEAP_SIZE), - // @Param: DEBUG_LVL + // @Param: DEBUG_OPTS // @DisplayName: Scripting Debug Level - // @Description: The higher the number the more verbose builtin scripting debug will be. + // @Description: Debugging options + // @Bitmask: 0:No Scripts to run message if all scripts have stopped, 1:Runtime messages for memory usage and execution time // @User: Advanced - AP_GROUPINFO("DEBUG_LVL", 4, AP_Scripting, _debug_level, 0), + AP_GROUPINFO("DEBUG_OPTS", 4, AP_Scripting, _debug_options, 0), // @Param: USER1 // @DisplayName: Scripting User Parameter1 @@ -210,7 +211,7 @@ void AP_Scripting::thread(void) { _stop = false; _restart = false; - lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_level, terminal); + lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_options, terminal); if (lua == nullptr || !lua->heap_allocated()) { gcs().send_text(MAV_SEVERITY_CRITICAL, "Unable to allocate scripting memory"); _init_failed = true; @@ -237,7 +238,7 @@ void AP_Scripting::thread(void) { gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting restated"); break; } - if (_debug_level > 0) { + if ((_debug_options.get() & uint8_t(lua_scripts::DebugLevel::NO_SCRIPTS_TO_RUN)) != 0) { gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: scripting stopped"); } } diff --git a/libraries/AP_Scripting/AP_Scripting.h b/libraries/AP_Scripting/AP_Scripting.h index 9e3b390510..d8b0cb02d9 100644 --- a/libraries/AP_Scripting/AP_Scripting.h +++ b/libraries/AP_Scripting/AP_Scripting.h @@ -97,7 +97,7 @@ private: AP_Int8 _enable; AP_Int32 _script_vm_exec_count; AP_Int32 _script_heap_size; - AP_Int8 _debug_level; + AP_Int8 _debug_options; AP_Int16 _dir_disable; bool _init_failed; // true if memory allocation failed diff --git a/libraries/AP_Scripting/lua_scripts.cpp b/libraries/AP_Scripting/lua_scripts.cpp index 621424c48e..eaf6605d61 100644 --- a/libraries/AP_Scripting/lua_scripts.cpp +++ b/libraries/AP_Scripting/lua_scripts.cpp @@ -27,9 +27,9 @@ char *lua_scripts::error_msg_buf; uint8_t lua_scripts::print_error_count; uint32_t lua_scripts::last_print_ms; -lua_scripts::lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_level, struct AP_Scripting::terminal_s &_terminal) +lua_scripts::lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_options, struct AP_Scripting::terminal_s &_terminal) : _vm_steps(vm_steps), - _debug_level(debug_level), + _debug_options(debug_options), terminal(_terminal) { _heap = hal.util->allocate_heap_memory(heap_size); } @@ -460,7 +460,7 @@ void lua_scripts::run(void) { hal.scheduler->delay(scripts->next_run_ms - now_ms); } - if (_debug_level > 1) { + if ((_debug_options.get() & uint8_t(DebugLevel::RUNTIME_MSG)) != 0) { gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: Running %s", scripts->name); } @@ -471,7 +471,7 @@ void lua_scripts::run(void) { const uint32_t runEnd = AP_HAL::micros(); const int endMem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0); - if (_debug_level > 1) { + if ((_debug_options.get() & uint8_t(DebugLevel::RUNTIME_MSG)) != 0) { gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: Time: %u Mem: %d + %d", (unsigned int)(runEnd - loadEnd), (int)endMem, @@ -482,7 +482,7 @@ void lua_scripts::run(void) { lua_gc(L, LUA_GCCOLLECT, 0); } else { - if (_debug_level > 0) { + if ((_debug_options.get() & uint8_t(DebugLevel::NO_SCRIPTS_TO_RUN)) != 0) { gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: No scripts to run"); } hal.scheduler->delay(1000); diff --git a/libraries/AP_Scripting/lua_scripts.h b/libraries/AP_Scripting/lua_scripts.h index 2eff57e1f4..310a1599d9 100644 --- a/libraries/AP_Scripting/lua_scripts.h +++ b/libraries/AP_Scripting/lua_scripts.h @@ -50,7 +50,7 @@ class lua_scripts { public: - lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_level, struct AP_Scripting::terminal_s &_terminal); + lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_options, struct AP_Scripting::terminal_s &_terminal); /* Do not allow copies */ lua_scripts(const lua_scripts &other) = delete; @@ -63,6 +63,12 @@ public: void run(void); static bool overtime; // script exceeded it's execution slot, and we are bailing out + + enum class DebugLevel { + NO_SCRIPTS_TO_RUN = 1U << 0, + RUNTIME_MSG = 1U << 1, + }; + private: void create_sandbox(lua_State *L); @@ -116,7 +122,7 @@ private: lua_State *lua_state; const AP_Int32 & _vm_steps; - const AP_Int8 & _debug_level; + const AP_Int8 & _debug_options; static void *alloc(void *ud, void *ptr, size_t osize, size_t nsize);