From 58a12aca833b46f09fcf4ab6267b8cd5fdfe6b3b Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 20 May 2021 02:54:49 +0300 Subject: [PATCH] Support logMessageCallback in .NET bindings. --- td/telegram/Client.h | 6 +++--- td/telegram/ClientDotNet.cpp | 38 +++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/td/telegram/Client.h b/td/telegram/Client.h index 5eaf480d2..b61dc60d9 100644 --- a/td/telegram/Client.h +++ b/td/telegram/Client.h @@ -241,15 +241,15 @@ class ClientManager final { /** * A type of callback function that will be called when a message is added to the internal TDLib log. * - * \param verbosity_level Log verbosity level with which the message was added. - * \param message Null-terminated string with the logged message. + * \param verbosity_level Log verbosity level with which the message was added. If 0, then TDLib will crash + * as soon as the callback returns. + * \param message Null-terminated string with the message added to the log. */ using LogMessageCallbackPtr = void (*)(int verbosity_level, const char *message); /** * Sets the callback that will be called when a message is added to the internal TDLib log. * None of the TDLib methods can be called from the callback. - * If message verbosity level is 0, then TDLib will crash as soon as callback returns. * By default the callback is not set. * * \param[in] max_verbosity_level Maximum verbosity level of messages for which the callback will be called. diff --git a/td/telegram/ClientDotNet.cpp b/td/telegram/ClientDotNet.cpp index 72a4c229a..56cb0be31 100644 --- a/td/telegram/ClientDotNet.cpp +++ b/td/telegram/ClientDotNet.cpp @@ -6,7 +6,6 @@ // #pragma managed(push, off) #include "td/telegram/Client.h" -#include "td/telegram/Log.h" #pragma managed(pop) #include "td/telegram/TdDotNetApi.h" @@ -23,10 +22,12 @@ namespace Td { using namespace CxCli; /// -/// A type of callback function that will be called when a fatal error happens. +/// A type of callback function that will be called when a message is added to the internal TDLib log. /// -/// Null-terminated string with a description of a happened fatal error. -public delegate void FatalErrorCallback(String^ errorMessage); +/// Log verbosity level with which the message was added. If 0, +/// then TDLib will crash as soon as the callback returns. +/// Null-terminated string with the message added to the log. +public delegate void LogMessageCallback(int verbosityLevel, String^ message); /// /// Interface for handler for results of queries to TDLib and incoming updates from TDLib. @@ -113,17 +114,20 @@ public: } /// - /// Sets the callback that will be called when a fatal error happens. + /// Sets the callback that will be called when a message is added to the internal TDLib log. + /// None of the TDLib methods can be called from the callback. /// - /// Callback that will be called when a fatal error happens. Pass null to remove the callback. - static void SetFatalErrorCallback(FatalErrorCallback^ callback) { + /// Maximum verbosity level of messages for which the callback will be called. + /// Callback that will be called when a message is added to the internal TDLib log. + /// Pass null to remove the callback. + static void SetLogMessageCallback(std::int32_t max_verbosity_level, LogMessageCallback^ callback) { std::lock_guard lock(logMutex); if (callback == nullptr) { - ::td::Log::set_fatal_error_callback(nullptr); - fatalErrorCallback = nullptr; + ::td::ClientManager::set_log_message_callback(max_verbosity_level, nullptr); + logMessageCallback = nullptr; } else { - fatalErrorCallback = callback; - ::td::Log::set_fatal_error_callback(FatalErrorCallbackWrapper); + logMessageCallback = callback; + ::td::ClientManager::set_log_message_callback(max_verbosity_level, LogMessageCallbackWrapper); } } @@ -151,16 +155,18 @@ private: } static std::mutex logMutex; - static FatalErrorCallback^ fatalErrorCallback; + static LogMessageCallback^ logMessageCallback; - static void FatalErrorCallbackWrapper(const char* message) { - CHECK(fatalErrorCallback != nullptr); - fatalErrorCallback(string_from_unmanaged(message)); + static void LogMessageCallbackWrapper(int verbosity_level, const char *message) { + auto callback = logMessageCallback; + if (callback != nullptr) { + callback(verbosity_level, string_from_unmanaged(message)); + } } }; std::mutex Client::logMutex; -FatalErrorCallback^ Client::fatalErrorCallback; +LogMessageCallback^ Client::logMessageCallback; } // namespace Td } // namespace Telegram