From 112459042265626c4585878b4b531bbf7f8ee977 Mon Sep 17 00:00:00 2001 From: Dale Glass Date: Fri, 16 Oct 2020 20:16:44 +0200 Subject: [PATCH] Enable color by default on Unix, change to comma separators. Also emit a message if an unrecognized log option is used. --- libraries/shared/src/LogHandler.cpp | 40 +++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/libraries/shared/src/LogHandler.cpp b/libraries/shared/src/LogHandler.cpp index e8d296f136..5009fe9194 100644 --- a/libraries/shared/src/LogHandler.cpp +++ b/libraries/shared/src/LogHandler.cpp @@ -17,6 +17,10 @@ #ifdef Q_OS_WIN #include #endif +#ifdef Q_OS_UNIX +#include +#include +#endif #include #include @@ -33,22 +37,36 @@ LogHandler& LogHandler::getInstance() { } LogHandler::LogHandler() { - QString log_options = qgetenv("VIRCADIA_LOG_OPTIONS").toLower(); + QString logOptions = qgetenv("VIRCADIA_LOG_OPTIONS").toLower(); - if (log_options.contains("color")) { - _useColor = 1; +#ifdef Q_OS_UNIX + // Enable color by default if we're on Unix, and output is a tty (so we're not being piped into something) + // + // On Windows the situation is more complex, and color is supported or not depending on version and + // registry settings, so for now it's off by default and it's up to the user to do what's required. + if (isatty(fileno(stdout))) { + _useColor = true; } +#endif - if (log_options.contains("process_id")) { - _shouldOutputProcessID = true; - } + auto optionList = logOptions.split(","); - if (log_options.contains("thread_id")) { - _shouldOutputThreadID = true; - } + for(auto option : optionList) { + option = option.trimmed(); - if (log_options.contains("milliseconds")) { - _shouldDisplayMilliseconds = true; + if (option == "color") { + _useColor = true; + } else if (option == "nocolor") { + _useColor = false; + } else if (option == "process_id") { + _shouldOutputProcessID = true; + } else if (option == "thread_id") { + _shouldOutputThreadID = true; + } else if (option == "milliseconds") { + _shouldDisplayMilliseconds = true; + } else if (option != "") { + fprintf(stdout, "Unrecognized option in VIRCADIA_LOG_OPTIONS: '%s'\n", option.toUtf8().constData()); + } } }