Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit 2594d83

Browse files
committed
Add a parameter to specify logging level. Fix #174
1 parent 57d8d23 commit 2594d83

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

shadowsocks-libqss/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ Options:
2828
-H, --http-proxy run in HTTP(S) proxy mode. ignored in server mode.
2929
-S, --server-mode run as shadowsocks server.
3030
-T, --speed-test test encrypt/decrypt speed.
31-
-D, --debug debug-level log.
32-
--autoban automatically ban IPs that send malformed header. ignored in local mode.
31+
-L <log_level> logging level. Valid levels are: debug, info, warn,
32+
error, fatal.
33+
--autoban automatically ban IPs that send malformed header.
34+
ignored in local mode.
3335
```
3436

3537
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
4143
License
4244
-------
4345

44-
Copyright (C) 2014-2016 Symeon Huang
46+
Copyright (C) 2014-2018 Symeon Huang
4547

4648
This program is free software: you can redistribute it and/or modify
4749
it under the terms of the GNU Lesser General Public License as

shadowsocks-libqss/main.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ static void onSIGINT_TERM(int sig)
3232
if (sig == SIGINT || sig == SIGTERM) qApp->quit();
3333
}
3434

35+
Utils::LogLevel stringToLogLevel(const QString& str)
36+
{
37+
if (str.compare("DEBUG", Qt::CaseInsensitive) == 0) {
38+
return Utils::LogLevel::DEBUG;
39+
} else if (str.compare("INFO", Qt::CaseInsensitive) == 0) {
40+
return Utils::LogLevel::INFO;
41+
} else if (str.compare("WARN", Qt::CaseInsensitive) == 0) {
42+
return Utils::LogLevel::WARN;
43+
} else if (str.compare("ERROR", Qt::CaseInsensitive) == 0) {
44+
return Utils::LogLevel::ERROR;
45+
} else if (str.compare("FATAL", Qt::CaseInsensitive) == 0) {
46+
return Utils::LogLevel::FATAL;
47+
}
48+
std::cerr << "Log level " << str.toStdString()
49+
<< " is not recognised, default to INFO" << std::endl;
50+
return Utils::LogLevel::INFO;
51+
}
52+
3553
int main(int argc, char *argv[])
3654
{
3755
qInstallMessageHandler(Utils::messageHandler);
@@ -80,9 +98,11 @@ int main(int argc, char *argv[])
8098
QCommandLineOption testSpeed(
8199
QStringList() << "T" << "speed-test",
82100
"test encrypt/decrypt speed.");
83-
QCommandLineOption debug(
84-
QStringList() << "D" << "debug",
85-
"debug-level log.");
101+
QCommandLineOption log("L",
102+
"logging level. Valid levels are: debug, info, "
103+
"warn, error, fatal.",
104+
"log_level",
105+
"info");
86106
QCommandLineOption autoBan("autoban",
87107
"automatically ban IPs that send malformed header. "
88108
"ignored in local mode.");
@@ -97,11 +117,11 @@ int main(int argc, char *argv[])
97117
parser.addOption(http);
98118
parser.addOption(serverMode);
99119
parser.addOption(testSpeed);
100-
parser.addOption(debug);
120+
parser.addOption(log);
101121
parser.addOption(autoBan);
102122
parser.process(a);
103123

104-
Utils::debugEnabled = parser.isSet(debug);
124+
Utils::logLevel = stringToLogLevel(parser.value(log));
105125
Client c;
106126
if (!c.readConfig(parser.value(configFile))) {
107127
c.setup(parser.value(serverAddress),

shadowsocks-libqss/utils.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <iostream>
55
#include "utils.h"
66

7-
bool Utils::debugEnabled = false;
7+
Utils::LogLevel Utils::logLevel = Utils::LogLevel::INFO;
88

99
void Utils::testSpeed(const std::string &method, uint32_t data_size_mb)
1010
{
@@ -41,20 +41,27 @@ void Utils::messageHandler(QtMsgType type, const QMessageLogContext &, const QSt
4141
const std::string message = msg.toStdString();
4242
switch(type) {
4343
case QtDebugMsg:
44-
if (Utils::debugEnabled) {
44+
if (Utils::logLevel <= LogLevel::DEBUG) {
4545
std::cout << timestamp << " DEBUG: " << message << std::endl;
4646
}
4747
break;
4848
case QtInfoMsg:
49-
std::cout << timestamp << " INFO: " << message << std::endl;
49+
if (Utils::logLevel <= LogLevel::INFO) {
50+
std::cout << timestamp << " INFO: " << message << std::endl;
51+
}
5052
break;
5153
case QtWarningMsg:
52-
std::cerr << timestamp << " WARN: " << message << std::endl;
54+
if (Utils::logLevel <= LogLevel::WARN) {
55+
std::cerr << timestamp << " WARN: " << message << std::endl;
56+
}
5357
break;
5458
case QtCriticalMsg:
55-
std::cerr << timestamp << " ERROR: " << message << std::endl;
59+
if (Utils::logLevel <= LogLevel::ERROR) {
60+
std::cerr << timestamp << " ERROR: " << message << std::endl;
61+
}
5662
break;
5763
case QtFatalMsg:
64+
// FATAL is not allowed to skip
5865
std::cerr << timestamp << " FATAL: " << message << std::endl;
5966
abort();
6067
}

shadowsocks-libqss/utils.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
#include <QtGlobal>
2525
#include <QStringList>
2626

27-
class Utils
27+
struct Utils
2828
{
29-
public:
3029
//test data encrypt/decrypt speed. print result to terminal
3130
static void testSpeed(const std::string &method, uint32_t data_size_mb);
3231
static void testSpeed(uint32_t data_size_mb);//test all methods
@@ -39,7 +38,14 @@ class Utils
3938
*/
4039
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
4140

42-
static bool debugEnabled;
41+
enum class LogLevel {
42+
DEBUG,
43+
INFO,
44+
WARN,
45+
ERROR,
46+
FATAL
47+
};
48+
static LogLevel logLevel;
4349
};
4450

4551
#endif // UTILS_H

0 commit comments

Comments
 (0)