-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bbcode support for printing output (colored debug messages, better logging) #33541
Conversation
How well does this perform if you print large amounts of text every frame (while staying below the 4,096 characters per second limit)? See also #2308. We could parse ANSI escape codes and convert them to BBCode for use in the editor output. |
Here are the test results, I recorded the fps every frame and took the average at the end. Let me know if that method of testing is flawed.
Regarding the ANSI escapes, I find them rather cryptic to write and could never remember them after a while. Wrapping those in a print_color function could be done, but using the existing bbcode functionality goes beyond just color (even though the default output font does not support italic or bolds that well). Here is the code I used for testing:
|
I agree, this is mostly about providing a way to print colored text that works both in terminal output and the editor's Output tab. We could also strip those automatically when the terminal output is not a TTY (e.g. writing to a file). Manual |
Looking at the user side, I would be against a The BBCode approach would potentially open up other uses, like opening more things in the editor via URLs that are clickable in the output log. I would suggest the following: If the user wants to print out colored text, they would include the bbcode (like in this PR) in a desired print variant. Then, the bbcode gets stripped for file logging. If the color tag is a predefined color, convert it to ANSI escapes for TTY, otherwise print it without color. Besides the automatic coloring of errors, warnings and stack_traces (#33505) I don't think we need a dedicated function for coloring TTY output if we take this editor first approach. If the user or plugin writer wants colors, they use the BBCode and it would trickle down to TTY and file log, while also coloring the editor output. For a headless mode, this would mean to still use BBCode which gets converted to the matching ANSI codes for TTY. |
@georgwacker Is this still desired? If so, it needs to be rebased on the latest master branch. While there are no conflicts, rebasing is important so that reviewers can easily test the PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still needs rebase.
d61875d
to
8e17333
Compare
Rebased and tested |
I am a bit concerned about the side effects this PR can have. |
But does the bbcode need to be applied to the values inside |
Well, as the log is just a rich text label, that would be tricky to separate, no? We'd need to have a way to sanitize user input but still add it as a part of bbcode-able RTL control. |
Well, the fact that it's tricky to implement safely, doesn't mean we should implement it in an unsafe way. |
I kind of agree with @Faless here. Unless I am wrong, there's no need for something else than colors and bold/italic support there. I am not against adding support for those in the output panel, but I am not sure it should go through a subset of the bbcode-style syntax. Also, I think we should make it so a standard terminal and the output panel stay consistent. Thus I'd be maybe more in favor of supporting ANSI escape code instead. |
The ANSI code solution will work also because Windows and Linux or different operating systems/terminals can use different color codes. So by providing a unified color command it will be sorted out automatically before being sent to the terminal/output. |
@filipworksdev: Note that Windows prior to some version doesn't really recognize ANSI :( |
We can enable virtual terminal processing on Windows 10 and later, and strip ANSI escape codes from printed text on older Windows versions. This way, printing to the console doesn't look broken on old Windows versions, but it won't be colored (which is probably fine, given Windows versions prior to 10 are now EOL). |
We discussed this in a PR review meeting and suggest adding a separate path for bbcode formatted prints, keeping the default print behavior unchanged. So users who really want colored output can opt-in to it for their own code (and not for user-provided strings). |
What is the progress on this? Kind of looking forward to this feature. |
@voylin This pull request needs to be amended to use a separate method for BBCode-enabled printing before it can be merged. See Akien's comment above. I'd suggest This approach means it's no longer possible to use Feel free to do this work on your end and open a pull request against |
I think this could be a good first issue to work on, I'll try my best 😉 |
I've been trying all day, but my skills are apparently not good enough. Was able to create a function, that can print in the console and the terminal. However, I just can't find away to give it a type that gets passed to the add_message function. It gets stuck somewhere in the error checking so it never gets the function. Sorry @Calinou , I still have some work on my c++ (But also breakpoints don't work to trace how the function goes) For the next person taking over: In that function before the string get's send to OS::get_singleton()->print() I copy the string to another variable and exchange all the bbcode with ANSI code. From there it got impossible for me to track how it goes further and how to give it a different type in editor/editor_log.cpp. I tried adding an extra bool (typedef void (*PrintHandlerFunc)(void *, const String &p_string, bool p_error, bool p_rich);), however did not work. And in editor/editor_log.h I made a new message type called MSG_TYPE_RICH, so when the message has that type, the string gets append_text instead of add_text to the log. Not sure if this explanation is helpful to anybody, but because breakpoints just didn't work, I had to spend the first 4 hours searching the pattern in which it goes, and I still couldn't find all the in between functions (my guess is that those functions are probably for error checking.) |
Note that the BBCode → ANSI code translation is an optional step. It doesn't have to be done for an initial revision of a pull request. Printing the text to the console as-is is fine for now, even if it's not very readable due to BBCode tags appearing in plain text in the console. This is mostly a debugging helper anyway 🙂 Implementing stripping of BBCode tags when printing to stdout/stderr would improve readability while keeping functionality intact within the editor Output panel. BBCode → ANSI translation is icing on the cake here. If anyone else is interested in going that route, I wrote a BBCode to ANSI converter in GDScript. It only replies on string replacement, but it seems fairly robust already: Relying on string replacement means that nested color tags won't behave correctly, but this can be worked around by not relying on nested color tags (which looks identical to the end user). Nested style tags are supported by using ANSI escape codes that allow resetting only the style, not the color. This code should be easy to translate to C++, making it slightly faster in the process. |
@Calinou I've tried my best today to make it work. #60675 Thanks for the example of the BBCodes to ANSI, I forgot about some of the BBCodes in my first try so I used your script as a reference.
|
Not sure if this can be closed or not, but I feel confident that my pull request #60675 does everything that the creator of this pull request desired. Thanks again for motivating me! I'm glad that I didn't give up after all. ^^ |
Superseded by #60675. Thanks for the contribution nonetheless! |
In reference to better logging #19122 and #10870, this simple change enables styling of print messages.
The editor output log already uses a RichTextLabel, so why not use the bbcode functionality?
If performance is a concern (
add_text
vsappend_bbcode
) perhaps we could make it an editor setting?If used, the bbcode will of course show up in the log files, I believe the same is true for coloring output in Unity. I don't think this is an issue if you want colored output for debugging, though.
This is how it looks: