Skip to content

Commit

Permalink
Summary:
Browse files Browse the repository at this point in the history
- Introduce lv_log
- Refine logging mechanism
  • Loading branch information
micl2e2 committed Oct 26, 2023
1 parent fb93094 commit 2368dd3
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 90 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions shared/common.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <optional>
#include <string>
#include <vector>
#include <algorithm>

#include <fmt/core.h>
Expand Down
5 changes: 5 additions & 0 deletions shared/lv_log.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <iostream>
#include "fmt/core.h"
#include "lv_log.hh"

LogLv g_log_lv = LogLv::INFO;
45 changes: 45 additions & 0 deletions shared/lv_log.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef _LV_LOG_H
#define _LV_LOG_H

#include <iostream>
#include "fmt/core.h"

enum LogLv
{
ERROR = 1,
WARNING = 2,
INFO = 3,
DEBUG = 4,
};

extern LogLv g_log_lv;

template <class... Args>
void
lv_log (LogLv lv, fmt::format_string<Args...> fmt, Args &&...args);

template <class... Args>
void
log_flush (fmt::format_string<Args...> fmt, Args &&...args);

template <class... Args>
void
lv_log (LogLv lv, fmt::format_string<Args...> fmt, Args &&...args)
{
if (g_log_lv >= lv)
{
constexpr std::string_view header{"[tf-focusd] "};
std::cout << header << fmt::format (fmt, std::forward<Args> (args)...)
<< std::endl;
}
}

template <class... Args>
void
log_flush (fmt::format_string<Args...> fmt, Args &&...args)
{
constexpr std::string_view header{"[tf-focusd] "};
std::cout << header << fmt::format (fmt, args...) << std::flush;
}

#endif
11 changes: 11 additions & 0 deletions shared/tf_msg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ operator<< (std::ostream &os, const TgMsg &msg)
return os;
}

std::string
decor_pos_to_str (const std::vector<std::tuple<int, int>> &pos)
{
std::string ret;
for (auto ele : pos)
{
ret += fmt::format ("<{},{}>,", std::get<0> (ele), std::get<1> (ele));
}
return ret;
}

std::vector<std::tuple<int, int>>
get_decor_pos (const std::string &str)
{
Expand Down
14 changes: 14 additions & 0 deletions shared/tf_msg.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <iostream>
#include <string>

#include "fmt/core.h"

class TgMsg
{
public:
Expand All @@ -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-<title,{}>-<sender,{}>-<txt,{}>-<tstamp,{}>",
this->title_, this->sender_, this->txt_, this->tstamp_);

return ret;
}

friend std::ostream &operator<< (std::ostream &os, const TgMsg &msg);

private:
Expand All @@ -45,4 +56,7 @@ private:
std::vector<std::tuple<int, int>>
get_decor_pos (const std::string &str);

std::string
decor_pos_to_str (const std::vector<std::tuple<int, int>> &pos);

#endif
50 changes: 23 additions & 27 deletions tf-focusd/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <td/telegram/td_api.hpp>

#include "common.hh"
#include "lv_log.hh"
#include "tf_data.hh"
#include "collector.hh"

Expand Down Expand Up @@ -101,10 +102,9 @@ no message!
{
auto chat
= td::move_tl_object_as<td_api::chat> (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;
}
Expand All @@ -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<td_api::object_ptr<td_api::textEntity>> ();

Expand Down Expand Up @@ -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 ());
}
});
}
Expand All @@ -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));
}
}
Expand All @@ -195,7 +191,7 @@ TdCollector::send_query (td_api::object_ptr<td_api::Function> f,
std::function<void (Object)> 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));
Expand All @@ -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);
}
Expand Down Expand Up @@ -375,10 +371,10 @@ TdCollector::process_update (td_api::object_ptr<td_api::Object> 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;
}
}
Expand Down
37 changes: 19 additions & 18 deletions tf-focusd/tf_focusd.cc
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
#include <iostream>
#include <thread>
#include <fmt/core.h>

#include "lv_log.hh"
#include "state.hh"
#include "worker.hh"

template <class... Args>
void
log (fmt::format_string<Args...> 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 <class... Args>
void
log_flush (fmt::format_string<Args...> 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;
}

Expand Down
Loading

0 comments on commit 2368dd3

Please sign in to comment.