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

Commit

Permalink
Add a parameter to specify logging level. Fix #174
Browse files Browse the repository at this point in the history
  • Loading branch information
librehat committed Apr 20, 2018
1 parent 57d8d23 commit 2594d83
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
8 changes: 5 additions & 3 deletions shadowsocks-libqss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <log_level> 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._
Expand All @@ -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
Expand Down
30 changes: 25 additions & 5 deletions shadowsocks-libqss/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.");
Expand All @@ -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),
Expand Down
17 changes: 12 additions & 5 deletions shadowsocks-libqss/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iostream>
#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)
{
Expand Down Expand Up @@ -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();
}
Expand Down
12 changes: 9 additions & 3 deletions shadowsocks-libqss/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
#include <QtGlobal>
#include <QStringList>

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
Expand All @@ -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

0 comments on commit 2594d83

Please sign in to comment.