Skip to content

Commit

Permalink
Add option for colored output
Browse files Browse the repository at this point in the history
Default is --colors auto which detects if user runs from terminal and
no NO_COLOR env is set.
Argument always forces colors, never prevents them
  • Loading branch information
poelzi committed Dec 14, 2020
1 parent 2446749 commit 152acc5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
75 changes: 56 additions & 19 deletions src/util/cmdlineargs.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
#include "util/cmdlineargs.h"

#include <stdio.h>
#include <unistd.h>
#ifdef __WINDOWS__
#include <io.h>
#endif

#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_useColors(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 All @@ -50,6 +57,7 @@ namespace {

bool CmdlineArgs::Parse(int &argc, char **argv) {
bool logLevelSet = false;
auto colorSettings = QString("auto");
for (int i = 0; i < argc; ++i) {
if ( argv[i] == QString("-h")
|| argv[i] == QString("--h")
Expand Down Expand Up @@ -103,10 +111,36 @@ 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(
"--color", Qt::CaseInsensitive) &&
i + 1 < argc) {
colorSettings = QString::fromLocal8Bit(argv[i + 1]);
} else {
m_musicFiles += QString::fromLocal8Bit(argv[i]);
}
}
if (colorSettings.compare(QLatin1String("auto"), Qt::CaseInsensitive) == 0) {
// see https://no-color.org/
if (QProcessEnvironment::systemEnvironment().contains(QLatin1String("NO_COLOR"))) {
m_useColors = false;
} else {
#ifndef __WINDOWS__
if (isatty(fileno(stderr))) {
m_useColors = true;
}
#else
if (_isatty(_fileno(stderr))) {
m_useColors = true;
}
#endif
}
} else if (colorSettings.compare(QLatin1String("always"), Qt::CaseInsensitive) == 0) {
m_useColors = true;
} else if (colorSettings.compare(QLatin1String("never"), Qt::CaseInsensitive) == 0) {
m_useColors = false;
} else {
qWarning() << "Unknown setting for color, ignoring";
}

// If --logLevel was unspecified and --developer is enabled then set
// logLevel to debug.
Expand Down Expand Up @@ -152,6 +186,8 @@ void CmdlineArgs::printUsage() {
and spinning vinyl widgets. Try this option if\n\
Mixxx is crashing on startup.\n\
\n\
--color auto [auto|always|never] Use 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 +205,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 useColors() const {
return m_useColors;
}
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_useColors; // should colors be used
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().useColors()) {
qSetMessagePattern(kDefaultMessagePatternColor);
} else {
qSetMessagePattern(kDefaultMessagePattern);
}
}

// Install the Qt message handler.
Expand Down

0 comments on commit 152acc5

Please sign in to comment.