forked from lthn/blockchain
epee: syncobj & try-catch macros: fix & minor refactoring
This commit is contained in:
parent
360c8bf6bd
commit
c1e75c3445
3 changed files with 122 additions and 95 deletions
115
contrib/epee/include/misc_helpers.h
Normal file
115
contrib/epee/include/misc_helpers.h
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
// Copyright (c) 2019, Zano Project
|
||||
// Copyright (c) 2006-2019, Andrey N. Sabelnikov, www.sabelnikov.net
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// * Neither the name of the Andrey N. Sabelnikov nor the
|
||||
// names of its contributors may be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
|
||||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#define COMBINE1(X,Y) X##Y // helper macro
|
||||
#define COMBINE(X,Y) COMBINE1(X,Y)
|
||||
#define _STR(X) #X
|
||||
#define STR(X) _STR(X)
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define LOCAL_FUNCTION_DEF__ __FUNCTION__
|
||||
#define UNUSED_ATTRIBUTE
|
||||
#else
|
||||
#define LOCAL_FUNCTION_DEF__ __PRETTY_FUNCTION__
|
||||
#define UNUSED_ATTRIBUTE __attribute__((unused))
|
||||
#endif
|
||||
|
||||
#define LOCATION_SS "[" << LOCAL_FUNCTION_DEF__ << ("] @ " __FILE__ ":" STR(__LINE__))
|
||||
#define LOCATION_CSTR ("[" LOCAL_FUNCTION_DEF__ "] @ " __FILE__ ":" STR(__LINE__))
|
||||
|
||||
|
||||
//
|
||||
// Try-catch helpers
|
||||
//
|
||||
|
||||
#define TRY_ENTRY() try {
|
||||
#define CATCH_ALL_DO_NOTHING() }catch(...) {}
|
||||
|
||||
#define CATCH_ENTRY_CUSTOM(location, custom_code, return_val) } \
|
||||
catch(const std::exception& ex) \
|
||||
{ \
|
||||
(void)(ex); \
|
||||
LOG_ERROR("Exception at [" << location << "], what=" << ex.what()); \
|
||||
custom_code; \
|
||||
return return_val; \
|
||||
} \
|
||||
catch(...) \
|
||||
{ \
|
||||
LOG_ERROR("Exception at [" << location << "], generic exception \"...\""); \
|
||||
custom_code; \
|
||||
return return_val; \
|
||||
}
|
||||
#define CATCH_ENTRY(location, return_val) CATCH_ENTRY_CUSTOM(location, (void)0, return_val)
|
||||
#define CATCH_ENTRY2(return_val) CATCH_ENTRY_CUSTOM(LOCATION_SS, (void)0, return_val)
|
||||
|
||||
#define CATCH_ENTRY_L0(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
#define CATCH_ENTRY_L1(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
#define CATCH_ENTRY_L2(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
#define CATCH_ENTRY_L3(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
#define CATCH_ENTRY_L4(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
|
||||
/// @brief Catches TRY_ENTRY without returning
|
||||
/// @details Useful within a dtor - but only if nested within another try block
|
||||
/// (since we can still potentially throw here). See NESTED_*ENTRY()
|
||||
/// @todo Exception dispatcher class
|
||||
#define CATCH_ENTRY_NO_RETURN(location, custom_code) } \
|
||||
catch(const std::exception& ex) \
|
||||
{ \
|
||||
(void)(ex); \
|
||||
LOG_ERROR("Exception at [" << location << "], what=" << ex.what()); \
|
||||
custom_code; \
|
||||
} \
|
||||
catch(...) \
|
||||
{ \
|
||||
LOG_ERROR("Exception at [" << location << "], generic exception \"...\""); \
|
||||
custom_code; \
|
||||
}
|
||||
|
||||
|
||||
#define CATCH_ENTRY_WITH_FORWARDING_EXCEPTION() } \
|
||||
catch(const std::exception& ex) \
|
||||
{ \
|
||||
LOG_ERROR("Exception at [" << LOCATION_SS << "], what=" << ex.what()); \
|
||||
throw std::runtime_error(std::string("[EXCEPTION FORWARDED]: ") + ex.what()); \
|
||||
} \
|
||||
catch(...) \
|
||||
{ \
|
||||
LOG_ERROR("Exception at [" << LOCATION_SS << "], generic unknown exception \"...\""); \
|
||||
throw std::runtime_error("[EXCEPTION FORWARDED]"); \
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define NESTED_TRY_ENTRY() try { TRY_ENTRY();
|
||||
|
||||
#define NESTED_CATCH_ENTRY(location) \
|
||||
CATCH_ENTRY_NO_RETURN(location, {}); \
|
||||
} catch (...) {}
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
// Copyright (c) 2019, Zano Project
|
||||
// Copyright (c) 2019, anonimal <anonimal@zano.org>
|
||||
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
||||
// All rights reserved.
|
||||
|
|
@ -59,6 +60,7 @@ PUSH_VS_WARNINGS
|
|||
DISABLE_VS_WARNINGS(4100)
|
||||
|
||||
|
||||
#include "misc_helpers.h"
|
||||
#include "static_initializer.h"
|
||||
#include "string_tools.h"
|
||||
#include "time_helper.h"
|
||||
|
|
@ -106,22 +108,6 @@ DISABLE_VS_WARNINGS(4100)
|
|||
#endif
|
||||
|
||||
|
||||
#define COMBINE1(X,Y) X##Y // helper macro
|
||||
#define COMBINE(X,Y) COMBINE1(X,Y)
|
||||
#define _STR(X) #X
|
||||
#define STR(X) _STR(X)
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define LOCAL_FUNCTION_DEF__ __FUNCTION__
|
||||
#define UNUSED_ATTRIBUTE
|
||||
#else
|
||||
#define LOCAL_FUNCTION_DEF__ __PRETTY_FUNCTION__
|
||||
#define UNUSED_ATTRIBUTE __attribute__((unused))
|
||||
#endif
|
||||
|
||||
#define LOCATION_SS "[" << LOCAL_FUNCTION_DEF__ << ("] @ " __FILE__ ":" STR(__LINE__))
|
||||
#define LOCATION_CSTR ("[" LOCAL_FUNCTION_DEF__ "] @ " __FILE__ ":" STR(__LINE__))
|
||||
|
||||
#if !defined(DISABLE_RELEASE_LOGGING)
|
||||
#define ENABLE_LOGGING_INTERNAL
|
||||
#endif
|
||||
|
|
@ -133,9 +119,6 @@ DISABLE_VS_WARNINGS(4100)
|
|||
});
|
||||
|
||||
|
||||
#define TRY_ENTRY() try {
|
||||
#define CATCH_ALL_DO_NOTHING() }catch(...) {}
|
||||
|
||||
#if defined(ENABLE_LOGGING_INTERNAL)
|
||||
|
||||
#define LOG_PRINT_CHANNEL_NO_PREFIX2(log_channel, log_name, x, y) {if ( y <= epee::log_space::log_singletone::get_log_detalisation_level() && epee::log_space::log_singletone::channel_enabled(log_channel))\
|
||||
|
|
@ -218,67 +201,6 @@ DISABLE_VS_WARNINGS(4100)
|
|||
#define ENDL std::endl
|
||||
|
||||
|
||||
#define CATCH_ENTRY_CUSTOM(location, custom_code, return_val) } \
|
||||
catch(const std::exception& ex) \
|
||||
{ \
|
||||
(void)(ex); \
|
||||
LOG_ERROR("Exception at [" << location << "], what=" << ex.what()); \
|
||||
custom_code; \
|
||||
return return_val; \
|
||||
} \
|
||||
catch(...) \
|
||||
{ \
|
||||
LOG_ERROR("Exception at [" << location << "], generic exception \"...\""); \
|
||||
custom_code; \
|
||||
return return_val; \
|
||||
}
|
||||
#define CATCH_ENTRY(location, return_val) CATCH_ENTRY_CUSTOM(location, (void)0, return_val)
|
||||
#define CATCH_ENTRY2(return_val) CATCH_ENTRY_CUSTOM(LOCATION_SS, (void)0, return_val)
|
||||
|
||||
#define CATCH_ENTRY_L0(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
#define CATCH_ENTRY_L1(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
#define CATCH_ENTRY_L2(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
#define CATCH_ENTRY_L3(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
#define CATCH_ENTRY_L4(location, return_val) CATCH_ENTRY(location, return_val)
|
||||
|
||||
/// @brief Catches TRY_ENTRY without returning
|
||||
/// @details Useful within a dtor - but only if nested within another try block
|
||||
/// (since we can still potentially throw here). See NESTED_*ENTRY()
|
||||
/// @todo Exception dispatcher class
|
||||
#define CATCH_ENTRY_NO_RETURN(location, custom_code) } \
|
||||
catch(const std::exception& ex) \
|
||||
{ \
|
||||
(void)(ex); \
|
||||
LOG_ERROR("Exception at [" << location << "], what=" << ex.what()); \
|
||||
custom_code; \
|
||||
} \
|
||||
catch(...) \
|
||||
{ \
|
||||
LOG_ERROR("Exception at [" << location << "], generic exception \"...\""); \
|
||||
custom_code; \
|
||||
}
|
||||
|
||||
|
||||
#define CATCH_ENTRY_WITH_FORWARDING_EXCEPTION() } \
|
||||
catch(const std::exception& ex) \
|
||||
{ \
|
||||
LOG_ERROR("Exception at [" << LOCATION_SS << "], what=" << ex.what()); \
|
||||
throw std::runtime_error(std::string("[EXCEPTION FORWARDED]: ") + ex.what()); \
|
||||
} \
|
||||
catch(...) \
|
||||
{ \
|
||||
LOG_ERROR("Exception at [" << LOCATION_SS << "], generic unknown exception \"...\""); \
|
||||
throw std::runtime_error("[EXCEPTION FORWARDED]"); \
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define NESTED_TRY_ENTRY() try { TRY_ENTRY();
|
||||
|
||||
#define NESTED_CATCH_ENTRY(location) \
|
||||
CATCH_ENTRY_NO_RETURN(location, {}); \
|
||||
} catch (...) {}
|
||||
|
||||
#define ASSERT_MES_AND_THROW(message) {LOG_ERROR(message); std::stringstream ss; ss << message; throw std::runtime_error(ss.str());}
|
||||
|
||||
#define CHECK_AND_ASSERT_THROW_MES(expr, message) {if(!(expr)) ASSERT_MES_AND_THROW(message << ENDL << "thrown from " << LOCATION_SS);}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// Copyright (c) 2019, anonimal, <anonimal@zano.org>
|
||||
// Copyright (c) 2019, Zano Project
|
||||
// Copyright (c) 2019, anonimal, <anonimal@zano.org>
|
||||
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
||||
// All rights reserved.
|
||||
//
|
||||
|
|
@ -24,12 +25,7 @@
|
|||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef __WINH_OBJ_H__
|
||||
#define __WINH_OBJ_H__
|
||||
#pragma once
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <atomic>
|
||||
|
|
@ -40,7 +36,7 @@
|
|||
|
||||
#include "singleton.h"
|
||||
#include "static_initializer.h"
|
||||
|
||||
#include "misc_helpers.h"
|
||||
|
||||
//#define DISABLE_DEADLOCK_GUARD
|
||||
|
||||
|
|
@ -57,9 +53,6 @@
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace epee
|
||||
{
|
||||
|
||||
|
|
@ -609,7 +602,7 @@ namespace epee
|
|||
{
|
||||
TRY_ENTRY();
|
||||
unlock();
|
||||
CATCH_ENTRY(void());
|
||||
CATCH_ALL_DO_NOTHING();
|
||||
}
|
||||
|
||||
void unlock()
|
||||
|
|
@ -713,6 +706,3 @@ namespace epee
|
|||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue