refactor: move setting of log level to CoreArgParser
This commit is contained in:
committed by
Chris Rizzitello
parent
05f377e21b
commit
f3a1bbaf5b
@ -72,6 +72,44 @@ void Settings::cleanSettings()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Settings::logLevelToInt(const QString &level)
|
||||||
|
{
|
||||||
|
// Can do this better later ?
|
||||||
|
if (level.toUpper() == "FATAL") {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level.toUpper() == "ERROR") {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level.toUpper() == "WARNING") {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level.toUpper() == "NOTE") {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level.toUpper() == "INFO") {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level.toUpper() == "DEBUG") {
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level.toUpper() == "DEBUG1") {
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level.toUpper() == "DEBUG2") {
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 4; // If all else fail return info
|
||||||
|
}
|
||||||
|
|
||||||
QVariant Settings::defaultValue(const QString &key)
|
QVariant Settings::defaultValue(const QString &key)
|
||||||
{
|
{
|
||||||
if ((key == Gui::Autohide) || (key == Core::StartedBefore) || (key == Core::PreventSleep) ||
|
if ((key == Gui::Autohide) || (key == Core::StartedBefore) || (key == Core::PreventSleep) ||
|
||||||
|
|||||||
@ -128,6 +128,7 @@ public:
|
|||||||
static QSettingsProxy &proxy();
|
static QSettingsProxy &proxy();
|
||||||
static void save(bool emitSaving = true);
|
static void save(bool emitSaving = true);
|
||||||
static QStringList validKeys();
|
static QStringList validKeys();
|
||||||
|
static int logLevelToInt(const QString &level = "INFO");
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void settingsChanged(const QString key);
|
void settingsChanged(const QString key);
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
#include "base/LogOutputters.h"
|
#include "base/LogOutputters.h"
|
||||||
#include "common/Constants.h"
|
#include "common/Constants.h"
|
||||||
#include "common/ExitCodes.h"
|
#include "common/ExitCodes.h"
|
||||||
|
#include "common/Settings.h"
|
||||||
#include "deskflow/ArgsBase.h"
|
#include "deskflow/ArgsBase.h"
|
||||||
#include "deskflow/Config.h"
|
#include "deskflow/Config.h"
|
||||||
#include "deskflow/DeskflowException.h"
|
#include "deskflow/DeskflowException.h"
|
||||||
@ -165,10 +166,8 @@ void App::initApp(int argc, const char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set log filter
|
// set log filter
|
||||||
if (!CLOG->setFilter(argsBase().m_logFilter)) {
|
if (const auto logLevel = qPrintable(Settings::logLevelText()); !CLOG->setFilter(logLevel)) {
|
||||||
LOG((
|
LOG_CRIT("%s: unrecognized log level `%s'" BYE, argsBase().m_pname, logLevel, argsBase().m_pname);
|
||||||
CLOG_CRIT "%s: unrecognized log level `%s'" BYE, argsBase().m_pname, argsBase().m_logFilter, argsBase().m_pname
|
|
||||||
));
|
|
||||||
m_bye(s_exitArgs);
|
m_bye(s_exitArgs);
|
||||||
}
|
}
|
||||||
loggingFilterWarning();
|
loggingFilterWarning();
|
||||||
|
|||||||
@ -129,9 +129,6 @@ private:
|
|||||||
#define DAEMON_RUNNING(running_)
|
#define DAEMON_RUNNING(running_)
|
||||||
#endif
|
#endif
|
||||||
constexpr static auto s_helpGeneralArgs = //
|
constexpr static auto s_helpGeneralArgs = //
|
||||||
" -d, --debug <level> filter out log messages with priority below level.\n"
|
|
||||||
" level may be: FATAL, ERROR, WARNING, NOTE, INFO,\n"
|
|
||||||
" DEBUG, DEBUG1, DEBUG2.\n"
|
|
||||||
" -1, --no-restart do not try to restart on failure.\n"
|
" -1, --no-restart do not try to restart on failure.\n"
|
||||||
"* --restart restart the server automatically if it fails.\n"
|
"* --restart restart the server automatically if it fails.\n"
|
||||||
" -l --log <file> write log messages to file.\n"
|
" -l --log <file> write log messages to file.\n"
|
||||||
@ -141,9 +138,7 @@ constexpr static auto s_helpGeneralArgs = //
|
|||||||
constexpr static auto s_helpVersionArgs = //
|
constexpr static auto s_helpVersionArgs = //
|
||||||
" -h, --help display this help and exit.\n";
|
" -h, --help display this help and exit.\n";
|
||||||
|
|
||||||
constexpr static auto s_helpCommonArgs = //
|
constexpr static auto s_helpCommonArgs = " [--restart|--no-restart]";
|
||||||
" [--restart|--no-restart]"
|
|
||||||
" [--debug <level>]";
|
|
||||||
|
|
||||||
#if !defined(WINAPI_LIBEI) && WINAPI_XWINDOWS
|
#if !defined(WINAPI_LIBEI) && WINAPI_XWINDOWS
|
||||||
constexpr static auto s_helpNoWayland = //
|
constexpr static auto s_helpNoWayland = //
|
||||||
|
|||||||
@ -122,10 +122,7 @@ bool ArgParser::parsePlatformArgs(deskflow::ArgsBase &argsBase, const int &argc,
|
|||||||
|
|
||||||
bool ArgParser::parseGenericArgs(int argc, const char *const *argv, int &i) const
|
bool ArgParser::parseGenericArgs(int argc, const char *const *argv, int &i) const
|
||||||
{
|
{
|
||||||
if (isArg(i, argc, argv, "-d", "--debug", 1)) {
|
if (isArg(i, argc, argv, "-l", "--log", 1)) {
|
||||||
// change logging level
|
|
||||||
argsBase().m_logFilter = argv[++i];
|
|
||||||
} else if (isArg(i, argc, argv, "-l", "--log", 1)) {
|
|
||||||
argsBase().m_logFile = argv[++i];
|
argsBase().m_logFile = argv[++i];
|
||||||
} else if (isArg(i, argc, argv, "-1", "--no-restart")) {
|
} else if (isArg(i, argc, argv, "-1", "--no-restart")) {
|
||||||
// don't try to restart
|
// don't try to restart
|
||||||
|
|||||||
@ -41,9 +41,6 @@ public:
|
|||||||
/// @brief The filename of the running process
|
/// @brief The filename of the running process
|
||||||
const char *m_pname = nullptr;
|
const char *m_pname = nullptr;
|
||||||
|
|
||||||
/// @brief The logging level of the application
|
|
||||||
const char *m_logFilter = nullptr;
|
|
||||||
|
|
||||||
/// @brief The full path to the logfile
|
/// @brief The full path to the logfile
|
||||||
const char *m_logFile = nullptr;
|
const char *m_logFile = nullptr;
|
||||||
|
|
||||||
|
|||||||
@ -61,6 +61,10 @@ void CoreArgParser::parse()
|
|||||||
if (m_parser.isSet(CoreArgs::nameOption)) {
|
if (m_parser.isSet(CoreArgs::nameOption)) {
|
||||||
Settings::setValue(Settings::Core::ScreenName, m_parser.value(CoreArgs::nameOption));
|
Settings::setValue(Settings::Core::ScreenName, m_parser.value(CoreArgs::nameOption));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_parser.isSet(CoreArgs::logLevelOption)) {
|
||||||
|
Settings::setValue(Settings::Log::Level, Settings::logLevelToInt(m_parser.value(CoreArgs::logLevelOption)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[[noreturn]] void CoreArgParser::showHelpText() const
|
[[noreturn]] void CoreArgParser::showHelpText() const
|
||||||
|
|||||||
@ -29,5 +29,13 @@ struct CoreArgs
|
|||||||
inline static const auto nameOption =
|
inline static const auto nameOption =
|
||||||
QCommandLineOption({"n", "name"}, "use screen-name instead the hostname to identify this screen", "screen-name");
|
QCommandLineOption({"n", "name"}, "use screen-name instead the hostname to identify this screen", "screen-name");
|
||||||
|
|
||||||
inline static const auto options = {helpOption, versionOption, configOption, interfaceOption, portOption, nameOption};
|
inline static const auto logLevelOption = QCommandLineOption(
|
||||||
|
"log-level",
|
||||||
|
"filter out log messages with priority below level.\nlevel may be:\nFATAL, ERROR, WARNING, NOTE, "
|
||||||
|
"INFO, DEBUG, DEBUG1, DEBUG2.",
|
||||||
|
"level"
|
||||||
|
);
|
||||||
|
|
||||||
|
inline static const auto options = {helpOption, versionOption, configOption, interfaceOption,
|
||||||
|
portOption, nameOption, logLevelOption};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -456,8 +456,6 @@ void CoreProcess::cleanup()
|
|||||||
|
|
||||||
bool CoreProcess::addGenericArgs(QStringList &args) const
|
bool CoreProcess::addGenericArgs(QStringList &args) const
|
||||||
{
|
{
|
||||||
args << "--debug" << Settings::logLevelText();
|
|
||||||
|
|
||||||
if (Settings::value(Settings::Security::TlsEnabled).toBool()) {
|
if (Settings::value(Settings::Security::TlsEnabled).toBool()) {
|
||||||
args << "--enable-crypto";
|
args << "--enable-crypto";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -290,18 +290,6 @@ void ArgParserTests::deprecatedArg_crypoPass_false()
|
|||||||
QCOMPARE(i, 1);
|
QCOMPARE(i, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArgParserTests::generic_logLevel()
|
|
||||||
{
|
|
||||||
int i = 1;
|
|
||||||
const int argc = 3;
|
|
||||||
const char *kLogLevelCmd[argc] = {"stub", "--debug", "DEBUG"};
|
|
||||||
|
|
||||||
m_parser.parseGenericArgs(argc, kLogLevelCmd, i);
|
|
||||||
|
|
||||||
QCOMPARE(m_parser.argsBase().m_logFilter, "DEBUG");
|
|
||||||
QCOMPARE(i, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ArgParserTests::generic_logFile()
|
void ArgParserTests::generic_logFile()
|
||||||
{
|
{
|
||||||
int i = 1;
|
int i = 1;
|
||||||
|
|||||||
@ -33,7 +33,6 @@ private Q_SLOTS:
|
|||||||
void client_badArgs();
|
void client_badArgs();
|
||||||
void deprecatedArg_crypoPass_true();
|
void deprecatedArg_crypoPass_true();
|
||||||
void deprecatedArg_crypoPass_false();
|
void deprecatedArg_crypoPass_false();
|
||||||
void generic_logLevel();
|
|
||||||
void generic_logFile();
|
void generic_logFile();
|
||||||
void generic_logFileWithSpace();
|
void generic_logFileWithSpace();
|
||||||
void generic_noRestart();
|
void generic_noRestart();
|
||||||
|
|||||||
@ -80,4 +80,14 @@ void CoreArgParserTests::nameShort()
|
|||||||
QCOMPARE(Settings::value(Settings::Core::ScreenName).toString(), "ShortName");
|
QCOMPARE(Settings::value(Settings::Core::ScreenName).toString(), "ShortName");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CoreArgParserTests::logLevel()
|
||||||
|
{
|
||||||
|
QStringList args = {"stub", "client", "--log-level", "DEBUG1"};
|
||||||
|
|
||||||
|
CoreArgParser parser(args);
|
||||||
|
parser.parse();
|
||||||
|
|
||||||
|
QCOMPARE(Settings::value(Settings::Log::Level).toInt(), 6);
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_MAIN(CoreArgParserTests)
|
QTEST_MAIN(CoreArgParserTests)
|
||||||
|
|||||||
@ -20,6 +20,7 @@ private Q_SLOTS:
|
|||||||
void portShort();
|
void portShort();
|
||||||
void nameLong();
|
void nameLong();
|
||||||
void nameShort();
|
void nameShort();
|
||||||
|
void logLevel();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline static const QString m_settingsPath = QStringLiteral("tmp/test");
|
inline static const QString m_settingsPath = QStringLiteral("tmp/test");
|
||||||
|
|||||||
Reference in New Issue
Block a user