Skip to content

Commit

Permalink
Use a colored console format if not disabled
Browse files Browse the repository at this point in the history
If --noColors argument is given or the NOCOLOR environment variable is set,
use the uncolored version.
  • Loading branch information
poelzi committed Dec 13, 2020
1 parent 2446749 commit 87c5440
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
50 changes: 31 additions & 19 deletions src/util/cmdlineargs.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
#include "util/cmdlineargs.h"

#include <stdio.h>

#include <QProcessEnvironment>
#include <QStandardPaths>

#include "util/cmdlineargs.h"
#include "util/version.h"

#include "sources/soundsourceproxy.h"

#include "util/version.h"

CmdlineArgs::CmdlineArgs()
: m_startInFullscreen(false), // Initialize vars
m_midiDebug(false),
m_developer(false),
m_safeMode(false),
m_debugAssertBreak(false),
m_settingsPathSet(false),
m_logLevel(mixxx::kLogLevelDefault),
m_logFlushLevel(mixxx::kLogFlushLevelDefault),
: m_startInFullscreen(false), // Initialize vars
m_midiDebug(false),
m_developer(false),
m_safeMode(false),
m_debugAssertBreak(false),
m_settingsPathSet(false),
m_noColors(false),
m_logLevel(mixxx::kLogLevelDefault),
m_logFlushLevel(mixxx::kLogFlushLevelDefault),
// We are not ready to switch to XDG folders under Linux, so keeping $HOME/.mixxx as preferences folder. see lp:1463273
#ifdef __LINUX__
m_settingsPath(QDir::homePath().append("/").append(SETTINGS_PATH)) {
m_settingsPath(QDir::homePath().append("/").append(SETTINGS_PATH)) {
#else
// TODO(XXX) Trailing slash not needed anymore as we switches from String::append
// to QDir::filePath elsewhere in the code. This is candidate for removal.
m_settingsPath(QStandardPaths::writableLocation(QStandardPaths::DataLocation).append("/")) {
// TODO(XXX) Trailing slash not needed anymore as we switches from String::append
// to QDir::filePath elsewhere in the code. This is candidate for removal.
m_settingsPath(
QStandardPaths::writableLocation(QStandardPaths::DataLocation)
.append("/")) {
#endif
}

Expand Down Expand Up @@ -103,10 +106,16 @@ when a critical error occurs unless this is set properly.\n", stdout);
m_safeMode = true;
} else if (QString::fromLocal8Bit(argv[i]).contains("--debugAssertBreak", Qt::CaseInsensitive)) {
m_debugAssertBreak = true;
} else if (QString::fromLocal8Bit(argv[i]).contains("--noColors", Qt::CaseInsensitive)) {
m_noColors = true;
} else {
m_musicFiles += QString::fromLocal8Bit(argv[i]);
}
}
// see https://no-color.org/
if (QProcessEnvironment::systemEnvironment().contains("NO_COLOR")) {
m_noColors = true;
}

// If --logLevel was unspecified and --developer is enabled then set
// logLevel to debug.
Expand Down Expand Up @@ -152,6 +161,8 @@ void CmdlineArgs::printUsage() {
and spinning vinyl widgets. Try this option if\n\
Mixxx is crashing on startup.\n\
\n\
--noColors Don't print colors on the console output.\n\
\n\
--locale LOCALE Use a custom locale for loading translations\n\
(e.g 'fr')\n\
\n\
Expand All @@ -169,14 +180,15 @@ void CmdlineArgs::printUsage() {
defined at --logLevel above.\n\
\n"
#ifdef MIXXX_BUILD_DEBUG
"\
"\
--debugAssertBreak Breaks (SIGINT) Mixxx, if a DEBUG_ASSERT\n\
evaluates to false. Under a debugger you can\n\
continue afterwards.\
\n"
#endif
"\
-h, --help Display this help message and exit", stdout);
"\
-h, --help Display this help message and exit",
stdout);

fputs("\n\n(For more information, see http://mixxx.org/wiki/doku.php/command_line_options)\n",stdout);
}
4 changes: 4 additions & 0 deletions src/util/cmdlineargs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class CmdlineArgs final {
bool getMidiDebug() const { return m_midiDebug; }
bool getDeveloper() const { return m_developer; }
bool getSafeMode() const { return m_safeMode; }
bool getNoColors() const {
return m_noColors;
}
bool getDebugAssertBreak() const { return m_debugAssertBreak; }
bool getSettingsPathSet() const { return m_settingsPathSet; }
mixxx::LogLevel getLogLevel() const { return m_logLevel; }
Expand All @@ -50,6 +53,7 @@ class CmdlineArgs final {
bool m_safeMode;
bool m_debugAssertBreak;
bool m_settingsPathSet; // has --settingsPath been set on command line ?
bool m_noColors; // no colors on console
mixxx::LogLevel m_logLevel; // Level of stderr logging message verbosity
mixxx::LogLevel m_logFlushLevel; // Level of mixx.log file flushing
QString m_locale;
Expand Down
23 changes: 22 additions & 1 deletion src/util/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "controllers/controllerdebug.h"
#include "util/assert.h"
#include "util/cmdlineargs.h"

namespace {

Expand All @@ -36,6 +37,22 @@ const QString kThreadNamePattern = QStringLiteral("{{threadname}}");
const QString kDefaultMessagePattern = QStringLiteral("%{type} [") +
kThreadNamePattern + QStringLiteral("] %{message}");

const QString kDefaultMessagePatternColor =
QStringLiteral(
"\033[32m%{time h:mm:ss.zzz}\033[0m "
"%{if-category}\033[35m %{category}:\033[35m%{endif}"
"[\033[97m") +
kThreadNamePattern +
QStringLiteral(
"\033[0m] "
"%{if-debug}\033[34m%{type} \033[36m%{function}%{endif}"
"%{if-info}\033[32m%{type}%{endif}"
"%{if-warning}\033[93m%{type}%{endif}"
"%{if-critical}\033[91m%{type}%{endif}"
"%{if-fatal}\033[97m\033[41m%{type} "
"\033[30m%{file}:%{line}%{endif}"
"\033[0m %{message}");

const QLoggingCategory kDefaultLoggingCategory = QLoggingCategory(nullptr);

enum class WriteFlag {
Expand Down Expand Up @@ -319,7 +336,11 @@ void Logging::initialize(
s_debugAssertBreak = debugAssertBreak;

if (qEnvironmentVariableIsEmpty("QT_MESSAGE_PATTERN")) {
qSetMessagePattern(kDefaultMessagePattern);
if (CmdlineArgs::Instance().getNoColors()) {
qSetMessagePattern(kDefaultMessagePattern);
} else {
qSetMessagePattern(kDefaultMessagePatternColor);
}
}

// Install the Qt message handler.
Expand Down

0 comments on commit 87c5440

Please sign in to comment.