[logging] USER null fallback (#3021)

If `USER` was unset, logging would crash immediately. `USER` is *not* a guaranteed variable, so to get around this we add a null fallback and also prefer `LOGNAME`, which is "standard" on POSIX systems (yet half the time isn't set because fuck me I guess)

Signed-off-by: crueter <crueter@eden-emu.dev>
Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/3021
Reviewed-by: CamilleLaVey <camillelavey99@gmail.com>
Reviewed-by: Maufeat <sahyno1996@gmail.com>
This commit is contained in:
crueter 2025-11-17 22:42:51 +01:00
parent 2e4ee8d9a4
commit 91d41d1c34
No known key found for this signature in database
GPG key ID: 425ACD2D4830EBC6

View file

@ -112,10 +112,20 @@ public:
// This must be a static otherwise it would get checked on EVERY
// instance of logging an entry...
static std::string username = []() -> std::string {
auto* s = getenv("USER");
if (s == nullptr)
s = getenv("USERNAME");
return std::string{s};
// in order of precedence
// LOGNAME usually works on UNIX, USERNAME on Windows
// Some UNIX systems suck and don't use LOGNAME so we also
// need USER :(
for (auto const var : {
"LOGNAME",
"USERNAME",
"USER",
}) {
if (auto const s = getenv(var); s != nullptr)
return std::string{s};
}
return std::string{};
}();
if (!username.empty())
boost::replace_all(message, username, "user");