diff --git a/aegisub/auto4_base.cpp b/aegisub/auto4_base.cpp index 59ce88e5a..4122b84c5 100644 --- a/aegisub/auto4_base.cpp +++ b/aegisub/auto4_base.cpp @@ -347,6 +347,9 @@ namespace Automation4 { sizer->SetSizeHints(this); SetSizer(sizer); Center(); + + // Init trace level + trace_level = Options.AsInt(_T("Automation Trace Level")); } ProgressSink::~ProgressSink() diff --git a/aegisub/auto4_base.h b/aegisub/auto4_base.h index 42ce7047a..f5dd180f0 100644 --- a/aegisub/auto4_base.h +++ b/aegisub/auto4_base.h @@ -236,6 +236,7 @@ namespace Automation4 { protected: volatile bool cancelled; + int trace_level; ProgressSink(wxWindow *parent); virtual ~ProgressSink(); diff --git a/aegisub/auto4_lua.cpp b/aegisub/auto4_lua.cpp index af02c0edb..4541bd965 100644 --- a/aegisub/auto4_lua.cpp +++ b/aegisub/auto4_lua.cpp @@ -788,6 +788,32 @@ namespace Automation4 { int LuaProgressSink::LuaDebugOut(lua_State *L) { LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1)); + + // Check trace level + if (lua_isnumber(L, 1)) { + int level = lua_tointeger(L, 1); + if (level > ps->trace_level) + return 0; + // remove trace level + lua_remove(L, 1); + } + + // Only do format-string handling if there's more than one argument left + // (If there's more than one argument left, assume first is a format string and rest are format arguments) + if (lua_gettop(L) > 1) { + // Format the string + lua_getglobal(L, "string"); + lua_getfield(L, -1, "format"); + // Here stack contains format string, format arguments, 'string' table, format function + // remove 'string' table + lua_remove(L, -2); + // put the format function into place + lua_insert(L, 1); + // call format function + lua_call(L, lua_gettop(L)-1, 1); + } + + // Top of stack is now a string to output wxString msg(lua_tostring(L, 1), wxConvUTF8); ps->AddDebugOutput(msg); return 0; diff --git a/automation/tests/test9.lua b/automation/tests/test9.lua new file mode 100644 index 000000000..74d2a8420 --- /dev/null +++ b/automation/tests/test9.lua @@ -0,0 +1,17 @@ +script_name = "Automation 4 test 9" +script_description = "Test debug out function" +script_author = "Niels Martin Hansen" +script_version = "1" + + +function test9(subtitles, selected_lines, active_line) + aegisub.debug.out("Only string argument\n") + aegisub.debug.out("Hello %s!\n", "format string world") + aegisub.debug.out("Now going to output 7 strings with trace levels 0 to 6:\n") + for i = 0, 6 do + aegisub.debug.out(i, "Trace level %d...\n", i) + end + aegisub.debug.out(3, "Finished!") +end + +aegisub.register_macro("Test debug out", "Tests the aegisub.debug.out function", test9) diff --git a/automation/v4-docs/progress-reporting.txt b/automation/v4-docs/progress-reporting.txt index 9272e646a..3d12affec 100644 --- a/automation/v4-docs/progress-reporting.txt +++ b/automation/v4-docs/progress-reporting.txt @@ -69,7 +69,7 @@ Returns: Boolean. True is the user has clicked the Cancel button, false if it Outputting text to the debug log -function aegisub.debug.out(level, msg, ...) +function aegisub.debug.out([level,] msg, ...) @level (number) Integer describing the verbosity of this message. Here are some suggested @@ -85,6 +85,9 @@ function aegisub.debug.out(level, msg, ...) The level can be used to let the user limit how severe messages will be shown, so you eg. can leave trace messages in, yet the casual user of the script won't see them unless he explicitly enables it. + This argument is optional and can be left out, in which case the message + will always be displayed, regardless of the current trace level set in + Aegisub. @msg (string) A format string used for the message.