Print Statements with Script/Line References

As a roblox developer, it is too tedious to use print-style debugging.

  1. They don’t have trivial-to-use formatting capabilities (like python, where everything can format into the {} placeholder).
  2. They cannot be enabled/disabled categorically (e.g., something that prints every frame can be helpful for debugging, but you’ll generally want those logs turned off)

This is my immediate problem, but I don’t even use print these days because I have my own wrapper that lets me use simplified syntax and gives me the features above.

Example syntax
Debug(
    "some message with substitutions %s %s %t",
    5,   --%s casts everything to a string.
    nil, --including nil
    { foo = "bar"; } --%t has custom handling to dump tables, optionally recursive
); --> some-prefix: some message with substitutions 5 nil { foo = "bar"; }

--Doing this same logic with a print statement:
print(MY_PREFIX .. string.format("some message with substitutions %s %s %t", tostring(t), tostring(Nil), SomeHelperFunction({ foo = "bar"; })));

The one thing my wrapper lacks is being able to click the log in the output window & jump to the line of code which produced it. Therefore, my request is that we have a lower-level print function which allows specifying the script & line number to which the print statement belongs, in the style of TestService:Message. This would make it easy for me to cut over, and would keep the solution flexible so I can add whatever kind of formatters I want.

Caveats for the nitpicky
  1. Yes, you can kinda enable/disable prints for an entire file by blasting away the definition of print – local print = function() end. You can kinda enable/disable prints categorically (where a single file has multiple categories) by using constants. I still find this tedious.
local PHYSICS_DEBUG = true;
if PHYSICS_DEBUG then print(...); end
  1. I could work around this problem by making my prints a little more verbose and having my formatter function return a string. Again, more tedium than I want.
PhysicsDebug = ...; --instantiate some wrapper.
if PhysicsDebug.Enabled then print(PhysicsDebug(--[[printf style formatting]])); end

Edit: comments about tostring(nil) balking were fixed.

7 Likes

Let me be picky for a second: this isn’t the case.
A nice print debugging feature though would be… Well… Nice.

1 Like

It sounds like you’d be interested in Visual Studio’s Logpoints.

These act like break points but sends information to the output instead of breaking (with the benefit that you do not have to change your code)

Having something like this built into the Roblox editor would be more ideal for me.

1 Like

@ANSI_C
Ah, correct. I don’t know why that got in my head that if I didn’t treat nil as special it would throw errors. Must have been some other function. Edited the post. Same concept holds true, though.

@GollyGreg
Sounds like way more than I need, though truth be told, backtraces wouldn’t give me the script as far as I can tell, and what I’d really need is the “level” parameter like error has.
So swing and a miss for me, too. :slight_smile:

Can you elaborate? If you need the stack trace, that should be as simple as calling debug.traceback() as an expression in either a print statement or a logpoint

This is cool and all, but may be over-engineered. For starters, we can probably do with just a logging API – this is more standard to software development than Visual Studio’s logpoints, and has the added benefit of being usable in plugins.

3 Likes