Added the ability to hook into logging system.

This commit is contained in:
levlam 2021-05-17 18:49:09 +03:00
parent 8231c58335
commit 3b0e2f5e85
3 changed files with 24 additions and 4 deletions

View File

@ -32,8 +32,22 @@ namespace td {
LogOptions log_options;
static std::atomic<int> max_callback_verbosity_level = 0;
static std::atomic<OnLogMessageCallback> on_log_message_callback = nullptr;
void set_log_message_callback(int max_verbosity_level, OnLogMessageCallback callback) {
max_callback_verbosity_level = max_verbosity_level;
on_log_message_callback = callback;
}
void LogInterface::append(int log_level, CSlice slice) {
do_append(log_level, slice);
if (log_level <= max_callback_verbosity_level.load(std::memory_order_relaxed)) {
auto callback = on_log_message_callback.load(std::memory_order_relaxed);
if (callback != nullptr) {
callback(log_level, slice);
}
}
if (log_level == VERBOSITY_NAME(FATAL)) {
process_fatal_error(slice);
}

View File

@ -185,10 +185,14 @@ class NullLog : public LogInterface {
extern LogInterface *const default_log_interface;
extern LogInterface *log_interface;
[[noreturn]] void process_fatal_error(CSlice message);
// deprecated in favor of set_log_message_callback
using OnFatalErrorCallback = void (*)(CSlice message);
void set_log_fatal_error_callback(OnFatalErrorCallback callback);
[[noreturn]] void process_fatal_error(CSlice message);
using OnLogMessageCallback = void (*)(int verbosity_level, CSlice message);
void set_log_message_callback(int max_verbosity_level, OnLogMessageCallback callback);
#define TC_RED "\x1b[1;31m"
#define TC_BLUE "\x1b[1;34m"

View File

@ -55,12 +55,14 @@ int KQueue::update(int nevents, const timespec *timeout, bool may_fail) {
if (err != -1) {
return false;
}
if (may_fail) {
return kevent_errno != ENOENT;
if (may_fail && kevent_errno == ENOENT) {
return false;
}
return kevent_errno != EINTR;
}();
LOG_IF(FATAL, is_fatal_error) << Status::PosixError(kevent_errno, "kevent failed");
if (is_fatal_error) {
LOG(FATAL) << Status::PosixError(kevent_errno, "kevent failed");
}
changes_n_ = 0;
if (err < 0) {