Skip to content

Commit d34680b

Browse files
committed
Remove set_log_fatal_error_callback. Add td_set_log_message_callback, deprecate td_set_log_fatal_error_callback.
1 parent 41d75d8 commit d34680b

14 files changed

+104
-32
lines changed

td/telegram/Client.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,25 @@ td_api::object_ptr<td_api::Object> ClientManager::execute(td_api::object_ptr<td_
659659
return Td::static_request(std::move(request));
660660
}
661661

662+
static std::atomic<ClientManager::LogMessageCallbackPtr> log_message_callback;
663+
664+
static void log_message_callback_wrapper(int verbosity_level, CSlice message) {
665+
auto callback = log_message_callback.load(std::memory_order_relaxed);
666+
if (callback != nullptr) {
667+
callback(verbosity_level, message.c_str());
668+
}
669+
}
670+
671+
void ClientManager::set_log_message_callback(int max_verbosity_level, LogMessageCallbackPtr callback) {
672+
if (callback == nullptr) {
673+
::td::set_log_message_callback(max_verbosity_level, nullptr);
674+
log_message_callback = nullptr;
675+
} else {
676+
log_message_callback = callback;
677+
::td::set_log_message_callback(max_verbosity_level, log_message_callback_wrapper);
678+
}
679+
}
680+
662681
ClientManager::~ClientManager() = default;
663682
ClientManager::ClientManager(ClientManager &&other) = default;
664683
ClientManager &ClientManager::operator=(ClientManager &&other) = default;

td/telegram/Client.h

+20
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,26 @@ class ClientManager final {
238238
*/
239239
static td_api::object_ptr<td_api::Object> execute(td_api::object_ptr<td_api::Function> &&request);
240240

241+
/**
242+
* A type of callback function that will be called when a message is added to the internal TDLib log.
243+
*
244+
* \param verbosity_level Log verbosity level with which the message was added.
245+
* \param message Null-terminated string with the logged message.
246+
*/
247+
using LogMessageCallbackPtr = void (*)(int verbosity_level, const char *message);
248+
249+
/**
250+
* Sets the callback that will be called when a message is added to the internal TDLib log.
251+
* None of the TDLib methods can be called from the callback.
252+
* If message verbosity level is 0, then TDLib will crash as soon as callback returns.
253+
* By default the callback is not set.
254+
*
255+
* \param[in] max_verbosity_level Maximum verbosity level of messages for which the callback will be called.
256+
* \param[in] callback Callback that will be called when a message is added to the internal TDLib log.
257+
* Pass nullptr to remove the callback.
258+
*/
259+
static void set_log_message_callback(int max_verbosity_level, LogMessageCallbackPtr callback);
260+
241261
/**
242262
* Destroys the client manager and all TDLib client instances managed by it.
243263
*/

td/telegram/ClientJson.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace td {
2020

21+
// TODO can be made private in TDLib 2.0
2122
class ClientJson final {
2223
public:
2324
void send(Slice request);

td/telegram/Log.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
#include "td/telegram/Log.h"
88

9+
#include "td/telegram/Client.h"
910
#include "td/telegram/Logging.h"
1011

1112
#include "td/telegram/td_api.h"
@@ -23,9 +24,13 @@ static string log_file_path;
2324
static int64 max_log_file_size = 10 << 20;
2425
static Log::FatalErrorCallbackPtr fatal_error_callback;
2526

26-
static void fatal_error_callback_wrapper(CSlice message) {
27-
CHECK(fatal_error_callback != nullptr);
28-
fatal_error_callback(message.c_str());
27+
static void fatal_error_callback_wrapper(int verbosity_level, const char *message) {
28+
if (verbosity_level == 0) {
29+
auto callback = fatal_error_callback;
30+
if (callback != nullptr) {
31+
callback(message);
32+
}
33+
}
2934
}
3035

3136
bool Log::set_file_path(string file_path) {
@@ -59,11 +64,11 @@ void Log::set_verbosity_level(int new_verbosity_level) {
5964
void Log::set_fatal_error_callback(FatalErrorCallbackPtr callback) {
6065
std::lock_guard<std::mutex> lock(log_mutex);
6166
if (callback == nullptr) {
67+
ClientManager::set_log_message_callback(0, nullptr);
6268
fatal_error_callback = nullptr;
63-
set_log_fatal_error_callback(nullptr);
6469
} else {
6570
fatal_error_callback = callback;
66-
set_log_fatal_error_callback(fatal_error_callback_wrapper);
71+
ClientManager::set_log_message_callback(0, fatal_error_callback_wrapper);
6772
}
6873
}
6974

td/telegram/Log.h

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Log {
7676
* The TDLib will crash as soon as callback returns.
7777
* By default the callback is not set.
7878
*
79+
* \deprecated Use ClientManager::set_log_message_callback instead.
7980
* \param[in] callback Callback that will be called when a fatal error happens.
8081
* Pass nullptr to remove the callback.
8182
*/

td/telegram/cli.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "td/net/HttpReader.h"
1414

1515
#include "td/telegram/ClientActor.h"
16-
#include "td/telegram/Log.h"
16+
#include "td/telegram/Client.h"
1717
#include "td/telegram/Td.h" // for VERBOSITY_NAME(td_requests)
1818
#include "td/telegram/td_api_json.h"
1919

@@ -4366,8 +4366,11 @@ static void fail_signal(int sig) {
43664366
}
43674367
}
43684368

4369-
static void on_fatal_error(const char *error) {
4370-
std::cerr << "Fatal error: " << error << std::endl;
4369+
static void on_log_message(int verbosity_level, const char *message) {
4370+
if (verbosity_level == 0) {
4371+
std::cerr << "Fatal error: " << message;
4372+
}
4373+
std::cerr << "Log message: " << message;
43714374
}
43724375

43734376
void main(int argc, char **argv) {
@@ -4376,7 +4379,7 @@ void main(int argc, char **argv) {
43764379
ignore_signal(SignalType::Pipe).ensure();
43774380
set_signal_handler(SignalType::Error, fail_signal).ensure();
43784381
set_signal_handler(SignalType::Abort, fail_signal).ensure();
4379-
Log::set_fatal_error_callback(on_fatal_error);
4382+
ClientManager::set_log_message_callback(0, on_log_message);
43804383
init_openssl_threads();
43814384

43824385
const char *locale_name = (std::setlocale(LC_ALL, "fr-FR") == nullptr ? "C" : "fr-FR");

td/telegram/td_json_client.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
#include "td/telegram/td_json_client.h"
88

9+
#include "td/telegram/Client.h"
910
#include "td/telegram/ClientJson.h"
1011

1112
#include "td/utils/Slice.h"
@@ -45,3 +46,7 @@ const char *td_receive(double timeout) {
4546
const char *td_execute(const char *request) {
4647
return td::json_execute(td::Slice(request == nullptr ? "" : request));
4748
}
49+
50+
void td_set_log_message_callback(int max_verbosity_level, td_log_message_callback_ptr callback) {
51+
td::ClientManager::set_log_message_callback(max_verbosity_level, callback);
52+
}

td/telegram/td_json_client.h

+20
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ TDJSON_EXPORT const char *td_receive(double timeout);
156156
*/
157157
TDJSON_EXPORT const char *td_execute(const char *request);
158158

159+
/**
160+
* A type of callback function that will be called when a message is added to the internal TDLib log.
161+
*
162+
* \param verbosity_level Log verbosity level with which the message was added.
163+
* \param message Null-terminated string with the logged message.
164+
*/
165+
typedef void (*td_log_message_callback_ptr)(int verbosity_level, const char *message);
166+
167+
/**
168+
* Sets the callback that will be called when a message is added to the internal TDLib log.
169+
* None of the TDLib methods can be called from the callback.
170+
* If message verbosity level is 0, then TDLib will crash as soon as callback returns.
171+
* By default the callback is not set.
172+
*
173+
* \param[in] max_verbosity_level Maximum verbosity level of messages for which the callback will be called.
174+
* \param[in] callback Callback that will be called when a message is added to the internal TDLib log.
175+
* Pass nullptr to remove the callback.
176+
*/
177+
TDJSON_EXPORT void td_set_log_message_callback(int max_verbosity_level, td_log_message_callback_ptr callback);
178+
159179
#ifdef __cplusplus
160180
} // extern "C"
161181
#endif

td/telegram/td_log.h

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ typedef void (*td_log_fatal_error_callback_ptr)(const char *error_message);
7171
* The TDLib will crash as soon as callback returns.
7272
* By default the callback is not set.
7373
*
74+
* \deprecated Use td_set_log_message_callback instead.
7475
* \param[in] callback Callback that will be called when a fatal error happens.
7576
* Pass NULL to remove the callback.
7677
*/

tdclientjson_export_list

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ _td_create_client_id
1111
_td_send
1212
_td_receive
1313
_td_execute
14+
_td_set_log_message_callback

tdutils/td/utils/FileLog.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ void FileLog::do_append(int log_level, CSlice slice) {
7474
if (size_ > rotate_threshold_ || want_rotate_.load(std::memory_order_relaxed)) {
7575
auto status = rename(path_, PSLICE() << path_ << ".old");
7676
if (status.is_error()) {
77-
process_fatal_error(PSLICE() << status.error() << " in " << __FILE__ << " at " << __LINE__);
77+
process_fatal_error(PSLICE() << status.error() << " in " << __FILE__ << " at " << __LINE__ << '\n');
7878
}
7979
do_after_rotation();
8080
}
8181
while (!slice.empty()) {
8282
auto r_size = fd_.write(slice);
8383
if (r_size.is_error()) {
84-
process_fatal_error(PSLICE() << r_size.error() << " in " << __FILE__ << " at " << __LINE__);
84+
process_fatal_error(PSLICE() << r_size.error() << " in " << __FILE__ << " at " << __LINE__ << '\n');
8585
}
8686
auto written = r_size.ok();
8787
size_ += static_cast<int64>(written);
@@ -107,7 +107,7 @@ void FileLog::do_after_rotation() {
107107
fd_.close();
108108
auto r_fd = FileFd::open(path_, FileFd::Create | FileFd::Truncate | FileFd::Write);
109109
if (r_fd.is_error()) {
110-
process_fatal_error(PSLICE() << r_fd.error() << " in " << __FILE__ << " at " << __LINE__);
110+
process_fatal_error(PSLICE() << r_fd.error() << " in " << __FILE__ << " at " << __LINE__ << '\n');
111111
}
112112
fd_ = r_fd.move_as_ok();
113113
if (!Stderr().empty() && redirect_stderr_) {

tdutils/td/utils/check.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace detail {
1616
void process_check_error(const char *message, const char *file, int line) {
1717
::td::Logger(*log_interface, log_options, VERBOSITY_NAME(FATAL), Slice(file), line, Slice())
1818
<< "Check `" << message << "` failed";
19-
::td::process_fatal_error(PSLICE() << "Check `" << message << "` failed in " << file << " at " << line);
19+
::td::process_fatal_error(PSLICE() << "Check `" << message << "` failed in " << file << " at " << line << '\n');
2020
}
2121

2222
} // namespace detail

tdutils/td/utils/logging.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,28 @@ namespace td {
3232

3333
LogOptions log_options;
3434

35-
static std::atomic<int> max_callback_verbosity_level = 0;
36-
static std::atomic<OnLogMessageCallback> on_log_message_callback = nullptr;
35+
static std::atomic<int> max_callback_verbosity_level{-2};
36+
static std::atomic<OnLogMessageCallback> on_log_message_callback{nullptr};
3737

3838
void set_log_message_callback(int max_verbosity_level, OnLogMessageCallback callback) {
39+
if (callback == nullptr) {
40+
max_verbosity_level = -2;
41+
}
42+
3943
max_callback_verbosity_level = max_verbosity_level;
4044
on_log_message_callback = callback;
4145
}
4246

4347
void LogInterface::append(int log_level, CSlice slice) {
4448
do_append(log_level, slice);
45-
if (log_level <= max_callback_verbosity_level.load(std::memory_order_relaxed)) {
49+
if (log_level == VERBOSITY_NAME(FATAL)) {
50+
process_fatal_error(slice);
51+
} else if (log_level <= max_callback_verbosity_level.load(std::memory_order_relaxed)) {
4652
auto callback = on_log_message_callback.load(std::memory_order_relaxed);
4753
if (callback != nullptr) {
4854
callback(log_level, slice);
4955
}
5056
}
51-
if (log_level == VERBOSITY_NAME(FATAL)) {
52-
process_fatal_error(slice);
53-
}
5457
}
5558

5659
TD_THREAD_LOCAL const char *Logger::tag_ = nullptr;
@@ -286,17 +289,14 @@ static DefaultLog default_log;
286289
LogInterface *const default_log_interface = &default_log;
287290
LogInterface *log_interface = default_log_interface;
288291

289-
static OnFatalErrorCallback on_fatal_error_callback = nullptr;
290-
291-
void set_log_fatal_error_callback(OnFatalErrorCallback callback) {
292-
on_fatal_error_callback = callback;
293-
}
294-
295292
void process_fatal_error(CSlice message) {
296-
auto callback = on_fatal_error_callback;
297-
if (callback) {
298-
callback(message);
293+
if (0 <= max_callback_verbosity_level.load(std::memory_order_relaxed)) {
294+
auto callback = on_log_message_callback.load(std::memory_order_relaxed);
295+
if (callback != nullptr) {
296+
callback(0, message);
297+
}
299298
}
299+
300300
std::abort();
301301
}
302302

tdutils/td/utils/logging.h

-4
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,6 @@ extern LogInterface *log_interface;
187187

188188
[[noreturn]] void process_fatal_error(CSlice message);
189189

190-
// deprecated in favor of set_log_message_callback
191-
using OnFatalErrorCallback = void (*)(CSlice message);
192-
void set_log_fatal_error_callback(OnFatalErrorCallback callback);
193-
194190
using OnLogMessageCallback = void (*)(int verbosity_level, CSlice message);
195191
void set_log_message_callback(int max_verbosity_level, OnLogMessageCallback callback);
196192

0 commit comments

Comments
 (0)