Skip to content

More debug and error printing options#5457

Merged
demonnic merged 13 commits intoMudlet:developmentfrom
demonnic:more_debug_options
Oct 2, 2021
Merged

More debug and error printing options#5457
demonnic merged 13 commits intoMudlet:developmentfrom
demonnic:more_debug_options

Conversation

@demonnic
Copy link
Copy Markdown
Member

@demonnic demonnic commented Sep 25, 2021

Brief overview of PR changes/additions

I use debugc a fair bit for printing information to the error console when I don't necessarily wish to halt execution, but either something has gone wrong or the information could be important for troubleshooting if they come looking for help.

But I have always kind of wanted the ability to print a more urgent error to the console without necessarily halting execution, and that snowballed to wanting something a bit more robust as well for both debug and error messages.

Adds an internal only function, errorc, which allows for printing detailed errors messages.

On the back of errorc and debugc, adds the following functions in Lua which provide extended functionality

  • printDebug(msg, showTrace)

    • Prints msg using debugc, with basic information on where the printDebug was called from
    • if showTrace is truthy, will also print the stacktrace beneath
      image
  • printError(msg, showTrace, haltExecution)

    • Prints msg as an error, with basic information on where printError was called from
    • if showTrace is truthy, will also print the stacktrace beneath
    • if haltExecution is truthy, printError will use error with the appropriate level set to where printError was called (not printError itself)
    • if haltExecution is nil or false, printError will use errorc to print the information

The following image shows

  • printError with stackTrace false, haltExecution false
  • printError with stacktrace true, haltExecution false
  • error, just standard vanilla Lua error
  • printError with stacktrace true, haltExecution true

image

And the same, but from the main console

image

Altogether, from the error console in the script editor

image

Motivation for adding to Mudlet

I wanted to be able to register 'error' level messages without having to halt execution. And then I started thinking about all the information I usually want in such instances and decided to wrap the basic functionality to provide this information

Other info (issues closed, discussion etc)

I could not for the life of me figure out where the colors for printMessage were being set, so I used a color picker to match the r,g,b values. Open to suggestions on making this better, again focused on functionality to start.

After some feedback, this was changed to not expose errorc() to the user, but rather to use it as an entry point for printError. Since that is the case I rejiggered it a little bit to make it look more in line with the existing error messages.

Release post highlight

New functions printError and printDebug provide more information for debugging your scripts

@demonnic demonnic added the needs documentation This pull request changes things the players would use/see and thus needs an update in the manual label Sep 25, 2021
@demonnic demonnic requested a review from a team as a code owner September 25, 2021 16:44
@demonnic demonnic requested review from a team September 25, 2021 16:44
@add-deployment-links
Copy link
Copy Markdown

add-deployment-links bot commented Sep 25, 2021

Hey there! Thanks for helping Mudlet improve. 🌟

Test versions

You can directly test the changes here:

No need to install anything - just unzip and run.
Let us know if it works well, and if it doesn't, please give details.

@demonnic
Copy link
Copy Markdown
Member Author

I... am not sure why it thinks it is grabbing the closest color changes as well. These were already merged before and shouldn't be showing up as diff either, I wouldn't think.

@github-actions
Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@demonnic
Copy link
Copy Markdown
Member Author

There we go, that looks better.

@Delra
Copy link
Copy Markdown
Contributor

Delra commented Sep 25, 2021

It's working fine for me! I really like this addition. The only thing I would say is that it would be nice to have more color in the messages using a scheme similar to what we already get from Mudlet errors- (Script: blah blah) in green and the rest of the text in red, really.

@demonnic
Copy link
Copy Markdown
Member Author

The problem with that is errorc is meant to be a more direct 1:1 of debugc, which doesn't include any of that kind of information. I'm then just using errorc to generate the messages for printError, except when haltExecution is specified, then it uses error.

If I'd managed to find a decent example of lua_Debug in use (and not just the #&$ lutok wrapper code) I might have tried to implement printError and printDebug in C++ but without that trying to suss it out from scratch/docs would have doubled or more the time to write it for me.

If you've got any insights/hints into how to make that happen or wanna try tackling it that'd be awesome, though. I'm not against implementing it all in C++ if it's not going to make it a 3 day task instead of the 1 day task it's already been.

@demonnic
Copy link
Copy Markdown
Member Author

demonnic commented Sep 25, 2021

hmm, actually, I may've just had an idea...

Could perhaps change errorc's behaviour such that the last argument supplied to it is the top of the stack/calling function info... IE the (Alias: run lua code:line 12) part of the printError output.

As it is right now both debugc and errorc combine their arguments into a single message and assigns them numbers. So I don't know how feasible it would be to change debugc now, but perhaps errorc could be different in this regard?

I'm open to suggestions.

@demonnic
Copy link
Copy Markdown
Member Author

image

in the meantime, yay tests

@Delra
Copy link
Copy Markdown
Contributor

Delra commented Sep 26, 2021

I don't think it's important enough to warrant adding time before merge! It's genuinely great the way it is.

@vadi2 vadi2 added this to the 4.14.0 milestone Sep 26, 2021
@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Sep 26, 2021

I really like the improvement here, and while I haven't dived into the code just yet, I'm first just stepping back and looking at the thousand foot view which gives us:

  • assert() (from Lua)
  • error() (from Lua)
  • errorc()
  • debugc() (existing function)
  • printDebug()
  • printError()

Do we think that it's intuitive for someone new coming to know which function should be used when?

@demonnic
Copy link
Copy Markdown
Member Author

I'm not sure if I think that's the right question. Someone new probably isn't going to know about handling errors at all, and someone who does know error handling is likely to start at the Mudlet wiki I think, where we can funnel people to printError and printDebug. Which I honestly think I personally will be using in place of debugc and error.

It's a reasonable question, but I'm not sure if it applies here. At least I didn't go with Error and Debug?

In all seriousness, I think they're useful enough to leave in, and any confusion the extra set of functions might cause we can smooth over with documentation and support. But given how few people coming in seem to even know debugc is a thing I'm not sure most of the people who would be confused by it will notice it.

… formatting is more in line with the existing errors messages
@demonnic
Copy link
Copy Markdown
Member Author

PR updated, along with opening comment. In short:

  • errorc not exposed to users
  • adds printError(msg, stackTrace, haltExecution) and printDebug(msg, stackTrace) as the only new exposed functions
  • printError errors now look more like errors generated by accident/using error()

@demonnic
Copy link
Copy Markdown
Member Author

hmm, is making some tests a bit problematic due to how I was spying on things. will look into that in a little bit

…test' in order for tests to be able to spy on it.
@demonnic
Copy link
Copy Markdown
Member Author

Slight adjustment to allow errorc to stick around in the self-test profile has the tests passing again. Remains nil in other profiles not named "Mudlet self-test"

local showTrace = options.showTrace
local msg = options.msg or ""
local halt = options.halt
local stackTable = debug.traceback():gsub("\t", " "):gsub("%[string ",""):split("\n")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be optimized to only retrieve the traceback if it's needed? Afaik it's not a cheap function

Copy link
Copy Markdown
Member Author

@demonnic demonnic Sep 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I can find another way of reliably determining what level of the stack to inspect for the function information, then yes. But I was not able to find that when I went looking. That being said, in testing just now it only took 0.022 seconds to run 1000 printDebug statements, so I'm not sure at 0.022 microseconds per printDebug it's adding significant overhead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

local epoch = getEpoch()
for i=1,1000 do
  printDebug("This is a test!")
end
local totalTime = getEpoch() - epoch
cecho(f"Took {totalTime} seconds to run 1000 printDebugs")

If you want to repeat the test

@demonnic demonnic merged commit 8061fbc into Mudlet:development Oct 2, 2021
@demonnic demonnic deleted the more_debug_options branch October 2, 2021 15:37
@demonnic demonnic removed the needs documentation This pull request changes things the players would use/see and thus needs an update in the manual label Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants