Need to handle debug-mode-only print statements

Hi, all,
I’m finishing up an add-on and would like to have debug-only print statements for debugging. So like if I have debugging=true, it’ll only print the debug statements. However, I’m having trouble figuring out how to do this. I did a search and tried some experiments and nothing really helped. I tried this:

function debugprint(...)
  if debugging == true then
    local arg1 = ...;
    print(arg1)
  end
end

Which works great if I call debugprint(“hello”), but if I want to do something like debugprint("The thing is: ", x), then all I get from debugprint is "The thing is: " and the value of x is lost.

If I add an arg2 to debugprint, it winds up being nil, so what exactly am I missing here?

1 Like

You’ve made debugprint global which is not great. Either make it local or a function in your addons private table if you want it usable in other lua files in your addon.

local function debugprint(...)
  if debugging == true then
    local arg1 = ...;
    print("The thing is:", arg1)
    print("and all the things are:", ...)
  end
end

by doing

arg1 = ...
print(arg1)

you’ve selected the first argument from the vararg (…) and printed only that.

1 Like

OK, it was print(…) that fixed it. I was overthinking the args list. Thanks.