diff --git a/CMakeLists.txt b/CMakeLists.txt index 0aefc4c..07e4422 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(tg-focus) set(CMAKE_CXX_FLAGS_RELEASE "-O3" CACHE STRING "" FORCE) + # set(CMAKE_CXX_COMPILER clang++) # cpp std @@ -59,6 +60,7 @@ add_executable( ${PROJECT_SOURCE_DIR}/shared/posix_regex.cc ${PROJECT_SOURCE_DIR}/shared/focus_filter.cc ${PROJECT_SOURCE_DIR}/shared/tf_msg.cc + ${PROJECT_SOURCE_DIR}/shared/lv_log.cc ) target_include_directories( tf-focusd diff --git a/shared/common.hh b/shared/common.hh index 76952b7..476d303 100644 --- a/shared/common.hh +++ b/shared/common.hh @@ -8,6 +8,7 @@ #include #include +#include #include #include diff --git a/shared/lv_log.cc b/shared/lv_log.cc new file mode 100644 index 0000000..fe28b75 --- /dev/null +++ b/shared/lv_log.cc @@ -0,0 +1,5 @@ +#include +#include "fmt/core.h" +#include "lv_log.hh" + +LogLv g_log_lv = LogLv::INFO; diff --git a/shared/lv_log.hh b/shared/lv_log.hh new file mode 100644 index 0000000..b26c3da --- /dev/null +++ b/shared/lv_log.hh @@ -0,0 +1,45 @@ +#ifndef _LV_LOG_H +#define _LV_LOG_H + +#include +#include "fmt/core.h" + +enum LogLv +{ + ERROR = 1, + WARNING = 2, + INFO = 3, + DEBUG = 4, +}; + +extern LogLv g_log_lv; + +template +void +lv_log (LogLv lv, fmt::format_string fmt, Args &&...args); + +template +void +log_flush (fmt::format_string fmt, Args &&...args); + +template +void +lv_log (LogLv lv, fmt::format_string fmt, Args &&...args) +{ + if (g_log_lv >= lv) + { + constexpr std::string_view header{"[tf-focusd] "}; + std::cout << header << fmt::format (fmt, std::forward (args)...) + << std::endl; + } +} + +template +void +log_flush (fmt::format_string fmt, Args &&...args) +{ + constexpr std::string_view header{"[tf-focusd] "}; + std::cout << header << fmt::format (fmt, args...) << std::flush; +} + +#endif diff --git a/shared/tf_msg.cc b/shared/tf_msg.cc index 5413dea..b980fe2 100644 --- a/shared/tf_msg.cc +++ b/shared/tf_msg.cc @@ -42,6 +42,17 @@ operator<< (std::ostream &os, const TgMsg &msg) return os; } +std::string +decor_pos_to_str (const std::vector> &pos) +{ + std::string ret; + for (auto ele : pos) + { + ret += fmt::format ("<{},{}>,", std::get<0> (ele), std::get<1> (ele)); + } + return ret; +} + std::vector> get_decor_pos (const std::string &str) { diff --git a/shared/tf_msg.hh b/shared/tf_msg.hh index 56975ef..b11dd63 100644 --- a/shared/tf_msg.hh +++ b/shared/tf_msg.hh @@ -9,6 +9,8 @@ #include #include +#include "fmt/core.h" + class TgMsg { public: @@ -33,6 +35,15 @@ public: inline const std::string &get_timestamp () const { return this->tstamp_; } + std::string to_string () const + { + std::string ret = fmt::format ( + "Telegram Message----", + this->title_, this->sender_, this->txt_, this->tstamp_); + + return ret; + } + friend std::ostream &operator<< (std::ostream &os, const TgMsg &msg); private: @@ -45,4 +56,7 @@ private: std::vector> get_decor_pos (const std::string &str); +std::string +decor_pos_to_str (const std::vector> &pos); + #endif diff --git a/tf-focusd/collector.cc b/tf-focusd/collector.cc index 789effc..41dd132 100644 --- a/tf-focusd/collector.cc +++ b/tf-focusd/collector.cc @@ -18,6 +18,7 @@ #include #include "common.hh" +#include "lv_log.hh" #include "tf_data.hh" #include "collector.hh" @@ -101,10 +102,9 @@ no message! { auto chat = td::move_tl_object_as (object); - std::cout << "group created! " - << " chat id:" << chat->id_ - << " chat title:" << chat->title_ - << std::endl; + lv_log (LogLv::INFO, + "group created, chat id:{}, chat title:{}", + chat->id_, chat->title_); this->collector_id = chat->id_; this->done_create_collector = true; } @@ -120,10 +120,9 @@ decorate_msg (const std::string &msg) auto pos_info = get_decor_pos (msg); // FIXME: only when very verbose - std::cout << fmt::format ("[CONSUMER {}] decorating u8str:{} pos_info:", - it_cnt_consumer.load (std::memory_order_relaxed), - msg) - << pos_info << std::endl; + lv_log (LogLv::DEBUG, "consumer_cnt:{}, decorating u8str:{} pos_info:{}", + it_cnt_consumer.load (std::memory_order_relaxed), msg, + decor_pos_to_str (pos_info)); auto deco_list = td_api::array> (); @@ -165,10 +164,9 @@ TdCollector::collect_msg (const TgMsg &msg, size_t c_count) if (object->get_id () == td_api::message::ID) { // FIXME: do not use operator << - std::cout << fmt::format ("[CONSUMER {}] msg collected:", - it_cnt_consumer.load ( - std::memory_order_relaxed)) - << msg << std::endl; + lv_log (LogLv::INFO, "consumer_cnt:{} msg collected:{}", + it_cnt_consumer.load (std::memory_order_relaxed), + msg.to_string ()); } }); } @@ -179,11 +177,9 @@ TdCollector::fetch_updates () auto response = client_manager_->receive (60); if (response.object) { - std::cerr << fmt::format ("[PRODUCER {}] td-client, resp recv id:{}", - it_cnt_producer.load ( - std::memory_order_relaxed), - response.object->get_id ()) - << std::endl; + lv_log (LogLv::DEBUG, "producer_iter:{}, td-client, resp recv id:{}", + it_cnt_producer.load (std::memory_order_relaxed), + response.object->get_id ()); process_response (std::move (response)); } } @@ -195,7 +191,7 @@ TdCollector::send_query (td_api::object_ptr f, std::function handler) { auto query_id = next_query_id (); - std::cout << "send_query!!!" << std::endl; + lv_log (LogLv::DEBUG, "TdCollector::send_query !!!"); if (handler) { handlers_.emplace (query_id, std::move (handler)); @@ -219,11 +215,11 @@ TdCollector::process_response (td::ClientManager::Response response) auto it = handlers_.find (response.request_id); if (it != handlers_.end ()) { - std::cerr << fmt::format ( - "[PRODUCER {}]td-client, handlers_.size():{} it->first:{}", - it_cnt_producer.load (std::memory_order_relaxed), handlers_.size (), - it->first) - << std::endl; + lv_log (LogLv::DEBUG, + "producer_iter:{}, td-client, handlers_.size():{} it->first:{}", + it_cnt_producer.load (std::memory_order_relaxed), + handlers_.size (), it->first); + it->second (std::move (response.object)); handlers_.erase (it); } @@ -375,10 +371,10 @@ TdCollector::process_update (td_api::object_ptr update) } default: { - std::cerr << fmt::format ( - "[PRODUCER {}] td-client, ignored update with id:{}", - it_cnt_producer.load (std::memory_order_relaxed), update->get_id ()) - << std::endl; + lv_log (LogLv::DEBUG, + "producer_iter:{}, td-client, ignored update with id:{}", + it_cnt_producer.load (std::memory_order_relaxed), + update->get_id ()); break; } } diff --git a/tf-focusd/tf_focusd.cc b/tf-focusd/tf_focusd.cc index 8c25409..8c619fa 100644 --- a/tf-focusd/tf_focusd.cc +++ b/tf-focusd/tf_focusd.cc @@ -1,41 +1,42 @@ +#include #include -#include +#include "lv_log.hh" #include "state.hh" #include "worker.hh" -template void -log (fmt::format_string fmt, Args &&...args) +handle_opts (int argc, char *argv[]) { - constexpr std::string_view header{"[tf-focusd] "}; - std::cout << header << fmt::format (fmt, args...) << std::endl; -} - -template -void -log_flush (fmt::format_string fmt, Args &&...args) -{ - constexpr std::string_view header{"[tf-focusd] "}; - std::cout << header << fmt::format (fmt, args...) << std::flush; + if (argc > 1) + { + if (strcmp (argv[1], "--verbose") == 0) + { + std::cout << "argc:" << argc << std::endl; + std::cout << "argv1:" << argv[1] << std::endl; + g_log_lv = LogLv::DEBUG; + } + } } int -main () +main (int argc, char *argv[]) { - std::setlocale (LC_ALL, "en_US.UTF-8"); - using namespace std; + setlocale (LC_ALL, "en_US.UTF-8"); + + handle_opts (argc, argv); + while (!tf_data.get_auth_hint ()) { - log ("Waiting for authorization"); + lv_log (LogLv::INFO, "Waiting for authorization"); std::this_thread::sleep_for (std::chrono::seconds (3)); } if (!tf_data.get_auth_hint ()) { - log ("Not authorized"); + lv_log (LogLv::INFO, "Not authorized"); return 1; } diff --git a/tf-focusd/worker.cc b/tf-focusd/worker.cc index 1e8677b..573da88 100644 --- a/tf-focusd/worker.cc +++ b/tf-focusd/worker.cc @@ -4,6 +4,7 @@ #include #include +#include "lv_log.hh" #include "state.hh" #include "focus_filter.hh" #include "tf_msg.hh" @@ -15,11 +16,8 @@ focusd_producer () { is_csm_mq.wait (true, std::memory_order_acquire); - std::cerr << fmt::format ("[PRODUCER {}] mq size: {}", - it_cnt_producer.load ( - std::memory_order_relaxed), - mq.size ()) - << std::endl; + lv_log (LogLv::DEBUG, "producer_iter:{}, mq size: {}", + it_cnt_producer.load (std::memory_order_relaxed), mq.size ()); collector.fetch_updates (); @@ -30,19 +28,12 @@ focusd_producer () bool need_collect (const TgMsg &msg) { - // FileReader freader{"filters.toml"}; - - // if (auto tomlstr = freader.read_to_string ()) - { - auto tomlstr = tf_data.get_filters (); - // std::cerr << "filters reloaded: " << *tomlstr << std::endl; - std::cerr << fmt::format ("[CONSUMER {}] filters reloaded...", - it_cnt_consumer.load (std::memory_order_relaxed)) - << std::endl; - auto fcf_list = FocusFilterList (tomlstr); - if (fcf_list.is_tgmsg_match (msg)) - return true; - } + auto tomlstr = tf_data.get_filters (); + lv_log (LogLv::INFO, "consumer_iter:{}, filters reloaded", + it_cnt_consumer.load (std::memory_order_relaxed)); + auto fcf_list = FocusFilterList (tomlstr); + if (fcf_list.is_tgmsg_match (msg)) + return true; return false; } @@ -70,16 +61,17 @@ focusd_consumer () if (mq.size () > 0 && collector.done_create_collector) { - std::cerr << fmt::format ( - "[CONSUMER {}] mq consumable, mq.size():{} ", - it_cnt_consumer.load (std::memory_order_relaxed), mq.size ()) - << std::endl; + lv_log (LogLv::DEBUG, + "consumer_iter:{}, mq consumable, mq.size():{} ", + it_cnt_consumer.load (std::memory_order_relaxed), + mq.size ()); for (auto it = mq.begin (); it != mq.end (); it += 1) { auto curr_msg = *it; if (!curr_msg.is_from_tgfocus ()) { + // FIXME: too many disk io incurred by need_collect if (need_collect (curr_msg)) { collector.collect_msg (std::move (curr_msg), @@ -87,11 +79,10 @@ focusd_consumer () consume_cnt++; } else - std::cerr - << fmt::format ("[CONSUMER,{}] message not collected: ", - it_cnt_consumer.load ( - std::memory_order_relaxed)) - << curr_msg << std::endl; + lv_log (LogLv::DEBUG, + "consume cnt:{} message not collected: {}", + it_cnt_consumer.load (std::memory_order_relaxed), + curr_msg.to_string ()); } } @@ -101,10 +92,10 @@ focusd_consumer () } else { - std::cerr << fmt::format ( - "[CONSUMER {}] mq not consumable, mq.size():{} ", - it_cnt_consumer.load (std::memory_order_relaxed), mq.size ()) - << std::endl; + lv_log (LogLv::DEBUG, + "consumer_iter:{}, mq not consumable, mq.size():{} ", + it_cnt_consumer.load (std::memory_order_relaxed), + mq.size ()); } } @@ -122,24 +113,21 @@ focusd_switcher () std::this_thread::sleep_for (std::chrono::seconds (5)); { - std::cout << fmt::format ( - "[SWITCHER {}] P,{} C,{} S,{} nhandle:{},nuser:{},nchattitle:{} " - "check for switch...", - it_cnt_switcher.load (std::memory_order_relaxed), - it_cnt_producer.load (std::memory_order_relaxed), - it_cnt_consumer.load (std::memory_order_relaxed), - it_cnt_switcher.load (std::memory_order_relaxed), - collector.n_handlers (), collector.n_users (), - collector.n_chat_titles ()) - << std::endl; + lv_log (LogLv::INFO, + "switcher summary, P,{} C,{} S,{} mqsize:{} " + "nhandle:{},nuser:{},nchattitle:{} ", + it_cnt_producer.load (std::memory_order_relaxed), + it_cnt_consumer.load (std::memory_order_relaxed), + it_cnt_switcher.load (std::memory_order_relaxed), mq.size (), + collector.n_handlers (), collector.n_users (), + collector.n_chat_titles ()); } if (is_csm_mq.load (std::memory_order_acquire)) { - std::cout - << fmt::format ("[SWITCHER {}] has msg, consumer maybe handling...", - it_cnt_switcher.load (std::memory_order_relaxed)) - << std::endl; + lv_log (LogLv::INFO, + "switcher cnt:{}, has msg, consumer maybe handling...", + it_cnt_switcher.load (std::memory_order_relaxed)); continue; }