From a0728a947669c967231667c45bffa660553213ab Mon Sep 17 00:00:00 2001 From: Fela Ameghino Date: Thu, 20 May 2021 01:36:47 +0200 Subject: [PATCH] Added SetFatalErrorCallback to CX/CLI Client (#1490) --- td/telegram/ClientDotNet.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/td/telegram/ClientDotNet.cpp b/td/telegram/ClientDotNet.cpp index 5eec5ad55..72a4c229a 100644 --- a/td/telegram/ClientDotNet.cpp +++ b/td/telegram/ClientDotNet.cpp @@ -6,6 +6,7 @@ // #pragma managed(push, off) #include "td/telegram/Client.h" +#include "td/telegram/Log.h" #pragma managed(pop) #include "td/telegram/TdDotNetApi.h" @@ -21,6 +22,12 @@ namespace Td { using namespace CxCli; +/// +/// A type of callback function that will be called when a fatal error happens. +/// +/// Null-terminated string with a description of a happened fatal error. +public delegate void FatalErrorCallback(String^ errorMessage); + /// /// Interface for handler for results of queries to TDLib and incoming updates from TDLib. /// @@ -105,6 +112,21 @@ public: return REF_NEW Client(updateHandler); } + /// + /// Sets the callback that will be called when a fatal error happens. + /// + /// Callback that will be called when a fatal error happens. Pass null to remove the callback. + static void SetFatalErrorCallback(FatalErrorCallback^ callback) { + std::lock_guard lock(logMutex); + if (callback == nullptr) { + ::td::Log::set_fatal_error_callback(nullptr); + fatalErrorCallback = nullptr; + } else { + fatalErrorCallback = callback; + ::td::Log::set_fatal_error_callback(FatalErrorCallbackWrapper); + } + } + private: Client(ClientResultHandler^ updateHandler) { client = new td::Client(); @@ -127,7 +149,18 @@ private: handler->OnResult(object); } } + + static std::mutex logMutex; + static FatalErrorCallback^ fatalErrorCallback; + + static void FatalErrorCallbackWrapper(const char* message) { + CHECK(fatalErrorCallback != nullptr); + fatalErrorCallback(string_from_unmanaged(message)); + } }; +std::mutex Client::logMutex; +FatalErrorCallback^ Client::fatalErrorCallback; + } // namespace Td } // namespace Telegram