diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index a1966dc8a1..70b913c200 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -112,11 +112,18 @@ public: // This must be a static otherwise it would get checked on EVERY // instance of logging an entry... static std::string username = []() -> std::string { - char* s; - - if ((s = getenv("USER")) != nullptr || (s = getenv("USERNAME")) != nullptr - || (s = getenv("LOGNAME")) != nullptr) - return std::string{s}; + // in order of precedence + static constexpr const std::array environment_variables = { + "LOGNAME", + "USERNAME", + "USER", + }; + + for (const char* var : environment_variables) { + const char* s = getenv(var); + if (s != nullptr) + return std::string{s}; + } return std::string{}; }();