BeamMP / BeamMP-Server

Server for the multiplayer mod BeamMP for BeamNG.drive
https://beammp.com
GNU Affero General Public License v3.0
127 stars 53 forks source link

[Feature Request] Add "Server ready" and "Server terminated" status messages to console output #21

Closed Hambone68 closed 3 years ago

Hambone68 commented 3 years ago

Using BeamMP pre-compiled Linux dynamic 2.0.3 on Debian 10 with AMP.

I'm working on integrating the BeamMP server into CubeCoders' AMP platform and am running into problems with BeamMP's console output being inconsistent. AMP has a feature that parses console output and displays it in its web interface, and it uses Javascript-style regexes for parsing the console. It requires the regexes to match an entire line of output (so starts with ^ and ends with $. While I've been able to develop regex patterns to match what the BeamMP server is outputting, there are a few potential changes to the output that would make it easier to parse, so this is a feature request to clean up the console output.

First, the beginning of the output line seems to be variable - sometimes it's prefixed by "> ", sometimes not. I haven't figured out what determines whether it outputs the > or not, but can it be made more consistent? This is followed by a consistent pattern of "{ESC}[2K{ESC}[1000D" where {ESC} is the single Unicode escape character U+001B. What is the purpose of the escape codes? Can they be removed or suppressed?

Also, would it be possible to add more consistent "Server is started/ready" and "Server terminated/exited" messages? When starting up, I have to check for two different versions of the final "authenticated" messsage depending on how long the server was down - it either shows "Authenticated!" or "Resumed authenticated session!" - which makes for a rather long regex expression. Similarly, when stopping the server via SIGTERM, the last message the server outputs is "please wait while all subsystems are shutting down..." but never signifies that that has happened - how long does that process normally take to complete? Can I assume that the server is terminated when I see the "please wait" message, or do I have to wait for it to finish?

lionkor commented 3 years ago

Hello @Hambone68!

The weird escape codes are ANSI control characters. You can ignore these, they can be removed after the fact, but they are an irreplacable part of delivering a usable async I/O in the console. They are part of the commandline library we use. The BeamMP-Server's stdout was never meant to be used in a unix-y pipe context. If you want clean output without escape characters, use the Server.log file. It should usually get flushed after every write (iirc).

I like the idea of Server ready! and Server terminated! messages, and they are certainly possibly. The only issue is that the server consists of a lot of threaded subsystems, so we'd have to check up with each subsystem to see if it has finished, as they all do it at the same time and some take longer than others. The "Authenticated" messages are related to the heartbeat reaching the backend and receiving a response. We'll look into making a more informative "server is ready" kind of message.

The entire server has usually started up and started to listen & operate normally by the time the output is printed to the console, but authentication obviously takes a while. You could reasonably just assume that, if the server is up for >10 seconds, it's up. Assuming no failure, you could enable debug mode and check for the first message containing "Heartbeat" between ] and [, like this:

[20/4/2021 22:07:43] Heartbeat [INFO] Resumed authenticated session!

lionkor commented 3 years ago

@Hambone68 I'm going to need some feedback on this. Otherwise, I'll consider it non-fixable.

lionkor commented 3 years ago

For the "server ready" and "server terminated" you can feel free to open a new issue or clarify this and change the title.

Hambone68 commented 3 years ago

So sorry, I never got a notification about the original answer, and had managed to set up enough filtering in AMP to make things work with the server, so I wound up forgetting about this issue. The control characters make more sense now and can be filtered, so it's no problem to leave them now that I know what they're for!

As for the server status messages, I realize that the threading can make for a difficult time making sure everything is synchronized or at least far enough along to be considered "ready". Again I've managed to filter for the various messages and have everything working in AMP, so it's definitely a low priority issue, but it would still be nice to have. I edited the title to reflect the change, should I also edit the original comment to reflect the change, or leave it as-is?

lionkor commented 3 years ago

FYI We are about to make some larger changes to some core systems in the next release(s), so we're going to wait with this, for now.

lionkor commented 3 years ago

After further review, we decided:

Good luck.

lionkor commented 3 years ago

You could also do it from Lua like it's designed to be done, for which you can use onInit and the upcoming (new lua branch) onShutdown.

lionkor commented 3 years ago

Since there will also be a rawPrint in the new lua branch, which makes a log line's output arbitrary, I'm closing this issue for now.