From 908d8ec19d33de594380e68b80612d82089e6e70 Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Thu, 5 Mar 2020 13:57:58 +0800 Subject: [PATCH] Add log_level option to filter output logs --- base/pref-new.yml | 1 + base/pref.ini | 1 + src/interfaces.cpp | 195 ++++++++++++++++++++++++++++--------- src/logger.cpp | 28 +++++- src/logger.h | 37 ++++--- src/main.cpp | 13 ++- src/string_hash.h | 4 +- src/subexport.cpp | 25 +++-- src/webget.cpp | 3 +- src/webserver_libevent.cpp | 16 ++- 10 files changed, 244 insertions(+), 79 deletions(-) diff --git a/base/pref-new.yml b/base/pref-new.yml index 834e5cb..7185736 100644 --- a/base/pref-new.yml +++ b/base/pref-new.yml @@ -80,6 +80,7 @@ server: port: 25500 advanced: + log_level: info print_debug_info: false max_pending_connections: 10240 max_concurrent_threads: 2 diff --git a/base/pref.ini b/base/pref.ini index f43e0dd..0e42aae 100644 --- a/base/pref.ini +++ b/base/pref.ini @@ -166,6 +166,7 @@ listen=0.0.0.0 port=25500 [advanced] +log_level=info print_debug_info=false max_pending_connections=10240 max_concurrent_threads=2 diff --git a/src/interfaces.cpp b/src/interfaces.cpp index f1bcb09..396365c 100644 --- a/src/interfaces.cpp +++ b/src/interfaces.cpp @@ -15,6 +15,7 @@ #include "subexport.h" #include "multithread.h" #include "logger.h" +#include "string_hash.h" //common settings std::string pref_path = "pref.ini"; @@ -26,6 +27,7 @@ bool api_mode = true, write_managed_config = false, enable_rule_generator = true bool print_debug_info = false, cfw_child_process = false, append_userinfo = true, enable_base_gen = false; std::string access_token; extern std::string custom_group; +extern int global_log_level; //generator settings bool generator_mode = false; @@ -342,14 +344,16 @@ void refreshRulesets(string_array &ruleset_list, std::vector &r rule_url = trim(x.substr(x.find(",") + 1)); if(rule_url.find("[]") == 0) { - std::cerr<<"Adding rule '"< &r if(rc.rule_content.size()) rca.emplace_back(rc); else - std::cerr<<"Warning: No data was fetched from this link. Skipping..."<> log_level; node["advanced"]["print_debug_info"] >> print_debug_info; + if(print_debug_info) + global_log_level = LOG_LEVEL_VERBOSE; + else + { + switch(hash_(log_level)) + { + case "warn"_hash: + global_log_level = LOG_LEVEL_WARNING; + break; + case "error"_hash: + global_log_level = LOG_LEVEL_ERROR; + break; + case "fatal"_hash: + global_log_level = LOG_LEVEL_FATAL; + break; + case "verbose"_hash: + global_log_level = LOG_LEVEL_VERBOSE; + break; + case "debug"_hash: + global_log_level = LOG_LEVEL_DEBUG; + break; + default: + global_log_level = LOG_LEVEL_INFO; + } + } node["advanced"]["max_pending_connections"] >> max_pending_connections; node["advanced"]["max_concurrent_threads"] >> max_concurrent_threads; node["advanced"]["enable_base_gen"] >> enable_base_gen; @@ -524,7 +556,8 @@ void readYAMLConf(YAML::Node &node) void readConf() { guarded_mutex guard(on_configuring); - std::cerr<<"Reading preference settings..."< dummy_ruleset; - std::cerr<<"Generate target: "; + //std::cerr<<"Generate target: "; if(target == "clash" || target == "clashr") { - std::cerr<<"Clash"<<((target == "clashr") ? "R" : "")< global_log_level) + return; + switch(level) + { + case LOG_LEVEL_VERBOSE: + std::cerr< #include @@ -2446,21 +2447,24 @@ int uploadGist(std::string name, std::string path, std::string content, bool wri if(!fileExist("gistconf.ini")) { - std::cerr<<"gistconf.ini not found. Skipping...\n"; + //std::cerr<<"gistconf.ini not found. Skipping...\n"; + writeLog(0, "gistconf.ini not found. Skipping...", LOG_LEVEL_ERROR); return -1; } ini.ParseFile("gistconf.ini"); if(ini.EnterSection("common") != 0) { - std::cerr<<"gistconf.ini has incorrect format. Skipping...\n"; + //std::cerr<<"gistconf.ini has incorrect format. Skipping...\n"; + writeLog(0, "gistconf.ini has incorrect format. Skipping...", LOG_LEVEL_ERROR); return -1; } token = ini.Get("token"); if(!token.size()) { - std::cerr<<"No token is provided. Skipping...\n"; + //std::cerr<<"No token is provided. Skipping...\n"; + writeLog(0, "No token is provided. Skipping...", LOG_LEVEL_ERROR); return -1; } @@ -2476,24 +2480,28 @@ int uploadGist(std::string name, std::string path, std::string content, bool wri if(!id.size()) { - std::cerr<<"No gist id is provided. Creating new gist...\n"; + //std::cerr<<"No gist id is provided. Creating new gist...\n"; + writeLog(0, "No Gist id is provided. Creating new Gist...", LOG_LEVEL_ERROR); retVal = curlPost("https://api.github.com/gists", buildGistData(path, content), getSystemProxy(), token, &retData); if(retVal != 201) { - std::cerr<<"Create new Gist failed! Return data:\n"< guarded_mutex; std::mutex cache_rw_lock; @@ -47,7 +48,7 @@ static int writer(char *data, size_t size, size_t nmemb, std::string *writerData static inline void curl_set_common_options(CURL *curl_handle, const char *url) { curl_easy_setopt(curl_handle, CURLOPT_URL, url); - curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, print_debug_info ? 1L : 0L); + curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, global_log_level == LOG_LEVEL_VERBOSE ? 1L : 0L); curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); diff --git a/src/webserver_libevent.cpp b/src/webserver_libevent.cpp index 60de9ec..7bd6309 100644 --- a/src/webserver_libevent.cpp +++ b/src/webserver_libevent.cpp @@ -16,6 +16,7 @@ #include "misc.h" #include "webserver.h" #include "socket.h" +#include "logger.h" extern std::string user_agent_str; std::atomic_bool SERVER_EXIT_FLAG(false); @@ -41,7 +42,8 @@ static inline void buffer_cleanup(struct evbuffer *eb) static inline int process_request(const char *method_str, std::string uri, std::string &postdata, std::string &content_type, std::string &return_data, int *status_code, std::map &extra_headers) { std::string path, arguments; - std::cerr << "handle_cmd: " << method_str << std::endl << "handle_uri: " << uri << std::endl; + //std::cerr << "handle_cmd: " << method_str << std::endl << "handle_uri: " << uri << std::endl; + writeLog(0, "handle_cmd: " + std::string(method_str) + " handle_uri: " + uri, LOG_LEVEL_VERBOSE); if(strFind(uri, "?")) { @@ -77,7 +79,8 @@ void OnReq(evhttp_request *req, void *args) char *client_ip; u_short client_port; evhttp_connection_get_peer(evhttp_request_get_connection(req), &client_ip, &client_port); - std::cerr<<"Accept connection from client "<port; if (!event_init()) { - std::cerr << "Failed to init libevent." << std::endl; + //std::cerr << "Failed to init libevent." << std::endl; + writeLog(0, "Failed to init libevent.", LOG_LEVEL_FATAL); return -1; } const char *SrvAddress = listen_address.c_str(); @@ -162,7 +166,8 @@ int start_web_server(void *argv) std::unique_ptr Server(evhttp_start(SrvAddress, SrvPort), &evhttp_free); if (!Server) { - std::cerr << "Failed to init http server." << std::endl; + //std::cerr << "Failed to init http server." << std::endl; + writeLog(0, "Failed to init http server.", LOG_LEVEL_FATAL); return -1; } @@ -171,7 +176,8 @@ int start_web_server(void *argv) evhttp_set_timeout(Server.get(), 30); if (event_dispatch() == -1) { - std::cerr << "Failed to run message loop." << std::endl; + //std::cerr << "Failed to run message loop." << std::endl; + writeLog(0, "Failed to run message loop.", LOG_LEVEL_FATAL); return -1; }