1 //
2 // Copyright 2011 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #pragma once
9 
10 #include <uhd/config.hpp>
11 #include <uhd/exception.hpp>
12 #include <uhd/utils/log.hpp>
13 
14 //! helper macro for safe call to produce warnings
15 #define _UHD_SAFE_CALL_WARNING(code, what)                                           \
16     UHD_LOGGER_ERROR("UHD") << UHD_THROW_SITE_INFO("Exception caught in safe-call.") \
17                                    + #code + " -> " + what;
18 
19 /*!
20  * A safe-call catches all exceptions thrown by code,
21  * and creates a verbose warning about the exception.
22  * Usage: UHD_SAFE_CALL(some_code_to_call();)
23  * \param code the block of code to call safely
24  */
25 #define UHD_SAFE_CALL(code)                                \
26     try {                                                  \
27         code                                               \
28     } catch (const std::exception& e) {                    \
29         _UHD_SAFE_CALL_WARNING(code, e.what());            \
30     } catch (...) {                                        \
31         _UHD_SAFE_CALL_WARNING(code, "unknown exception"); \
32     }
33