From 2594d83ea02f9ca6b19e682a228344205fb478bf Mon Sep 17 00:00:00 2001 From: Symeon Huang Date: Fri, 20 Apr 2018 23:51:05 +0100 Subject: [PATCH] Add a parameter to specify logging level. Fix #174 --- shadowsocks-libqss/README.md | 8 +++++--- shadowsocks-libqss/main.cpp | 30 +++++++++++++++++++++++++----- shadowsocks-libqss/utils.cpp | 17 ++++++++++++----- shadowsocks-libqss/utils.h | 12 +++++++++--- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/shadowsocks-libqss/README.md b/shadowsocks-libqss/README.md index deb83f2..5652ef5 100644 --- a/shadowsocks-libqss/README.md +++ b/shadowsocks-libqss/README.md @@ -28,8 +28,10 @@ Options: -H, --http-proxy run in HTTP(S) proxy mode. ignored in server mode. -S, --server-mode run as shadowsocks server. -T, --speed-test test encrypt/decrypt speed. - -D, --debug debug-level log. - --autoban automatically ban IPs that send malformed header. ignored in local mode. + -L logging level. Valid levels are: debug, info, warn, + error, fatal. + --autoban automatically ban IPs that send malformed header. + ignored in local mode. ``` If `-T` or `--speed-test` is specified, `shadowsocks-libqss` will do a speed test and print out the time used for specified encryption method. If no method is set, it'll test all encryption methods and print the results. _Note: `shadowsocks-libqss` will exit after the speed test._ @@ -41,7 +43,7 @@ If `config.json` is specified, most command-line options will be **ignored**. Th License ------- -Copyright (C) 2014-2016 Symeon Huang +Copyright (C) 2014-2018 Symeon Huang This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as diff --git a/shadowsocks-libqss/main.cpp b/shadowsocks-libqss/main.cpp index d057443..2f4ce65 100644 --- a/shadowsocks-libqss/main.cpp +++ b/shadowsocks-libqss/main.cpp @@ -32,6 +32,24 @@ static void onSIGINT_TERM(int sig) if (sig == SIGINT || sig == SIGTERM) qApp->quit(); } +Utils::LogLevel stringToLogLevel(const QString& str) +{ + if (str.compare("DEBUG", Qt::CaseInsensitive) == 0) { + return Utils::LogLevel::DEBUG; + } else if (str.compare("INFO", Qt::CaseInsensitive) == 0) { + return Utils::LogLevel::INFO; + } else if (str.compare("WARN", Qt::CaseInsensitive) == 0) { + return Utils::LogLevel::WARN; + } else if (str.compare("ERROR", Qt::CaseInsensitive) == 0) { + return Utils::LogLevel::ERROR; + } else if (str.compare("FATAL", Qt::CaseInsensitive) == 0) { + return Utils::LogLevel::FATAL; + } + std::cerr << "Log level " << str.toStdString() + << " is not recognised, default to INFO" << std::endl; + return Utils::LogLevel::INFO; +} + int main(int argc, char *argv[]) { qInstallMessageHandler(Utils::messageHandler); @@ -80,9 +98,11 @@ int main(int argc, char *argv[]) QCommandLineOption testSpeed( QStringList() << "T" << "speed-test", "test encrypt/decrypt speed."); - QCommandLineOption debug( - QStringList() << "D" << "debug", - "debug-level log."); + QCommandLineOption log("L", + "logging level. Valid levels are: debug, info, " + "warn, error, fatal.", + "log_level", + "info"); QCommandLineOption autoBan("autoban", "automatically ban IPs that send malformed header. " "ignored in local mode."); @@ -97,11 +117,11 @@ int main(int argc, char *argv[]) parser.addOption(http); parser.addOption(serverMode); parser.addOption(testSpeed); - parser.addOption(debug); + parser.addOption(log); parser.addOption(autoBan); parser.process(a); - Utils::debugEnabled = parser.isSet(debug); + Utils::logLevel = stringToLogLevel(parser.value(log)); Client c; if (!c.readConfig(parser.value(configFile))) { c.setup(parser.value(serverAddress), diff --git a/shadowsocks-libqss/utils.cpp b/shadowsocks-libqss/utils.cpp index e759eda..18ebc0c 100644 --- a/shadowsocks-libqss/utils.cpp +++ b/shadowsocks-libqss/utils.cpp @@ -4,7 +4,7 @@ #include #include "utils.h" -bool Utils::debugEnabled = false; +Utils::LogLevel Utils::logLevel = Utils::LogLevel::INFO; void Utils::testSpeed(const std::string &method, uint32_t data_size_mb) { @@ -41,20 +41,27 @@ void Utils::messageHandler(QtMsgType type, const QMessageLogContext &, const QSt const std::string message = msg.toStdString(); switch(type) { case QtDebugMsg: - if (Utils::debugEnabled) { + if (Utils::logLevel <= LogLevel::DEBUG) { std::cout << timestamp << " DEBUG: " << message << std::endl; } break; case QtInfoMsg: - std::cout << timestamp << " INFO: " << message << std::endl; + if (Utils::logLevel <= LogLevel::INFO) { + std::cout << timestamp << " INFO: " << message << std::endl; + } break; case QtWarningMsg: - std::cerr << timestamp << " WARN: " << message << std::endl; + if (Utils::logLevel <= LogLevel::WARN) { + std::cerr << timestamp << " WARN: " << message << std::endl; + } break; case QtCriticalMsg: - std::cerr << timestamp << " ERROR: " << message << std::endl; + if (Utils::logLevel <= LogLevel::ERROR) { + std::cerr << timestamp << " ERROR: " << message << std::endl; + } break; case QtFatalMsg: + // FATAL is not allowed to skip std::cerr << timestamp << " FATAL: " << message << std::endl; abort(); } diff --git a/shadowsocks-libqss/utils.h b/shadowsocks-libqss/utils.h index 4cbb295..6325394 100644 --- a/shadowsocks-libqss/utils.h +++ b/shadowsocks-libqss/utils.h @@ -24,9 +24,8 @@ #include #include -class Utils +struct Utils { -public: //test data encrypt/decrypt speed. print result to terminal static void testSpeed(const std::string &method, uint32_t data_size_mb); static void testSpeed(uint32_t data_size_mb);//test all methods @@ -39,7 +38,14 @@ class Utils */ static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); - static bool debugEnabled; + enum class LogLevel { + DEBUG, + INFO, + WARN, + ERROR, + FATAL + }; + static LogLevel logLevel; }; #endif // UTILS_H