Support logMessageCallback in .NET bindings.

This commit is contained in:
levlam 2021-05-20 02:54:49 +03:00
parent a0728a9476
commit 58a12aca83
2 changed files with 25 additions and 19 deletions

View File

@ -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. * 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 verbosity_level Log verbosity level with which the message was added. If 0, then TDLib will crash
* \param message Null-terminated string with the logged message. * 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); 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. * 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. * 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. * By default the callback is not set.
* *
* \param[in] max_verbosity_level Maximum verbosity level of messages for which the callback will be called. * \param[in] max_verbosity_level Maximum verbosity level of messages for which the callback will be called.

View File

@ -6,7 +6,6 @@
// //
#pragma managed(push, off) #pragma managed(push, off)
#include "td/telegram/Client.h" #include "td/telegram/Client.h"
#include "td/telegram/Log.h"
#pragma managed(pop) #pragma managed(pop)
#include "td/telegram/TdDotNetApi.h" #include "td/telegram/TdDotNetApi.h"
@ -23,10 +22,12 @@ namespace Td {
using namespace CxCli; using namespace CxCli;
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="errorMessage">Null-terminated string with a description of a happened fatal error.</param> /// <param name="verbosityLevel">Log verbosity level with which the message was added. If 0,
public delegate void FatalErrorCallback(String^ errorMessage); /// then TDLib will crash as soon as the callback returns.</param>
/// <param name="message">Null-terminated string with the message added to the log.</param>
public delegate void LogMessageCallback(int verbosityLevel, String^ message);
/// <summary> /// <summary>
/// Interface for handler for results of queries to TDLib and incoming updates from TDLib. /// Interface for handler for results of queries to TDLib and incoming updates from TDLib.
@ -113,17 +114,20 @@ public:
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="callback">Callback that will be called when a fatal error happens. Pass null to remove the callback.</param> /// <param name="max_verbosity_level">Maximum verbosity level of messages for which the callback will be called.</param>
static void SetFatalErrorCallback(FatalErrorCallback^ callback) { /// <param name="callback">Callback that will be called when a message is added to the internal TDLib log.
/// Pass null to remove the callback.</param>
static void SetLogMessageCallback(std::int32_t max_verbosity_level, LogMessageCallback^ callback) {
std::lock_guard<std::mutex> lock(logMutex); std::lock_guard<std::mutex> lock(logMutex);
if (callback == nullptr) { if (callback == nullptr) {
::td::Log::set_fatal_error_callback(nullptr); ::td::ClientManager::set_log_message_callback(max_verbosity_level, nullptr);
fatalErrorCallback = nullptr; logMessageCallback = nullptr;
} else { } else {
fatalErrorCallback = callback; logMessageCallback = callback;
::td::Log::set_fatal_error_callback(FatalErrorCallbackWrapper); ::td::ClientManager::set_log_message_callback(max_verbosity_level, LogMessageCallbackWrapper);
} }
} }
@ -151,16 +155,18 @@ private:
} }
static std::mutex logMutex; static std::mutex logMutex;
static FatalErrorCallback^ fatalErrorCallback; static LogMessageCallback^ logMessageCallback;
static void FatalErrorCallbackWrapper(const char* message) { static void LogMessageCallbackWrapper(int verbosity_level, const char *message) {
CHECK(fatalErrorCallback != nullptr); auto callback = logMessageCallback;
fatalErrorCallback(string_from_unmanaged(message)); if (callback != nullptr) {
callback(verbosity_level, string_from_unmanaged(message));
}
} }
}; };
std::mutex Client::logMutex; std::mutex Client::logMutex;
FatalErrorCallback^ Client::fatalErrorCallback; LogMessageCallback^ Client::logMessageCallback;
} // namespace Td } // namespace Td
} // namespace Telegram } // namespace Telegram