Move assert to a dedicated header
This commit is contained in:
parent
0ce3dc8f31
commit
cfdcb4f39c
5 changed files with 81 additions and 70 deletions
|
|
@ -21,6 +21,8 @@ add_library(
|
|||
${platform_sources}
|
||||
asio.hpp
|
||||
asio.cpp
|
||||
assert.hpp
|
||||
assert.cpp
|
||||
block_sideband.hpp
|
||||
block_sideband.cpp
|
||||
block_type.hpp
|
||||
|
|
|
|||
45
nano/lib/assert.cpp
Normal file
45
nano/lib/assert.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <nano/lib/assert.hpp>
|
||||
#include <nano/lib/stacktrace.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/*
|
||||
* Backing code for "release_assert" & "debug_assert", which are macros
|
||||
*/
|
||||
void assert_internal (char const * check_expr, char const * func, char const * file, unsigned int line, bool is_release_assert, std::string_view error_msg)
|
||||
{
|
||||
std::cerr << "Assertion (" << check_expr << ") failed\n"
|
||||
<< func << "\n"
|
||||
<< file << ":" << line << "\n";
|
||||
if (!error_msg.empty ())
|
||||
{
|
||||
std::cerr << "Error: " << error_msg << "\n";
|
||||
}
|
||||
std::cerr << "\n";
|
||||
|
||||
// Output stack trace to cerr
|
||||
auto backtrace_str = nano::generate_stacktrace ();
|
||||
std::cerr << backtrace_str << std::endl;
|
||||
|
||||
// "abort" at the end of this function will go into any signal handlers (the daemon ones will generate a stack trace and load memory address files on non-Windows systems).
|
||||
// As there is no async-signal-safe way to generate stacktraces on Windows it must be done before aborting
|
||||
#ifdef _WIN32
|
||||
{
|
||||
// Try construct the stacktrace dump in the same folder as the running executable, otherwise use the current directory.
|
||||
boost::system::error_code err;
|
||||
auto running_executable_filepath = boost::dll::program_location (err);
|
||||
std::string filename = is_release_assert ? "nano_node_backtrace_release_assert.txt" : "nano_node_backtrace_assert.txt";
|
||||
std::string filepath = filename;
|
||||
if (!err)
|
||||
{
|
||||
filepath = (running_executable_filepath.parent_path () / filename).string ();
|
||||
}
|
||||
|
||||
std::ofstream file (filepath);
|
||||
nano::set_secure_perm_file (filepath);
|
||||
file << backtrace_str;
|
||||
}
|
||||
#endif
|
||||
|
||||
abort ();
|
||||
}
|
||||
33
nano/lib/assert.hpp
Normal file
33
nano/lib/assert.hpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/current_function.hpp>
|
||||
#include <boost/preprocessor/facilities/empty.hpp>
|
||||
#include <boost/preprocessor/facilities/overload.hpp>
|
||||
|
||||
#include <string_view>
|
||||
|
||||
[[noreturn]] void assert_internal (char const * check_expr, char const * func, char const * file, unsigned int line, bool is_release_assert, std::string_view error = "");
|
||||
|
||||
#define release_assert_1(check) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true)
|
||||
#define release_assert_2(check, error_msg) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true, error_msg)
|
||||
#if !BOOST_PP_VARIADICS_MSVC
|
||||
#define release_assert(...) \
|
||||
BOOST_PP_OVERLOAD (release_assert_, __VA_ARGS__) \
|
||||
(__VA_ARGS__)
|
||||
#else
|
||||
#define release_assert(...) BOOST_PP_CAT (BOOST_PP_OVERLOAD (release_assert_, __VA_ARGS__) (__VA_ARGS__), BOOST_PP_EMPTY ())
|
||||
#endif
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define debug_assert(...) (void)0
|
||||
#else
|
||||
#define debug_assert_1(check) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, false)
|
||||
#define debug_assert_2(check, error_msg) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, false, error_msg)
|
||||
#if !BOOST_PP_VARIADICS_MSVC
|
||||
#define debug_assert(...) \
|
||||
BOOST_PP_OVERLOAD (debug_assert_, __VA_ARGS__) \
|
||||
(__VA_ARGS__)
|
||||
#else
|
||||
#define debug_assert(...) BOOST_PP_CAT (BOOST_PP_OVERLOAD (debug_assert_, __VA_ARGS__) (__VA_ARGS__), BOOST_PP_EMPTY ())
|
||||
#endif
|
||||
#endif
|
||||
|
|
@ -87,47 +87,6 @@ void nano::move_all_files_to_dir (std::filesystem::path const & from, std::files
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Backing code for "release_assert" & "debug_assert", which are macros
|
||||
*/
|
||||
void assert_internal (char const * check_expr, char const * func, char const * file, unsigned int line, bool is_release_assert, std::string_view error_msg)
|
||||
{
|
||||
std::cerr << "Assertion (" << check_expr << ") failed\n"
|
||||
<< func << "\n"
|
||||
<< file << ":" << line << "\n";
|
||||
if (!error_msg.empty ())
|
||||
{
|
||||
std::cerr << "Error: " << error_msg << "\n";
|
||||
}
|
||||
std::cerr << "\n";
|
||||
|
||||
// Output stack trace to cerr
|
||||
auto backtrace_str = nano::generate_stacktrace ();
|
||||
std::cerr << backtrace_str << std::endl;
|
||||
|
||||
// "abort" at the end of this function will go into any signal handlers (the daemon ones will generate a stack trace and load memory address files on non-Windows systems).
|
||||
// As there is no async-signal-safe way to generate stacktraces on Windows it must be done before aborting
|
||||
#ifdef _WIN32
|
||||
{
|
||||
// Try construct the stacktrace dump in the same folder as the running executable, otherwise use the current directory.
|
||||
boost::system::error_code err;
|
||||
auto running_executable_filepath = boost::dll::program_location (err);
|
||||
std::string filename = is_release_assert ? "nano_node_backtrace_release_assert.txt" : "nano_node_backtrace_assert.txt";
|
||||
std::string filepath = filename;
|
||||
if (!err)
|
||||
{
|
||||
filepath = (running_executable_filepath.parent_path () / filename).string ();
|
||||
}
|
||||
|
||||
std::ofstream file (filepath);
|
||||
nano::set_secure_perm_file (filepath);
|
||||
file << backtrace_str;
|
||||
}
|
||||
#endif
|
||||
|
||||
abort ();
|
||||
}
|
||||
|
||||
// Issue #3748
|
||||
void nano::sort_options_description (const boost::program_options::options_description & source, boost::program_options::options_description & target)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <nano/lib/assert.hpp>
|
||||
#include <nano/lib/container_info.hpp>
|
||||
#include <nano/lib/locks.hpp>
|
||||
|
||||
#include <boost/current_function.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/preprocessor/facilities/empty.hpp>
|
||||
#include <boost/preprocessor/facilities/overload.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
|
@ -28,32 +26,6 @@ namespace program_options
|
|||
}
|
||||
}
|
||||
|
||||
[[noreturn]] void assert_internal (char const * check_expr, char const * func, char const * file, unsigned int line, bool is_release_assert, std::string_view error = "");
|
||||
|
||||
#define release_assert_1(check) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true)
|
||||
#define release_assert_2(check, error_msg) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true, error_msg)
|
||||
#if !BOOST_PP_VARIADICS_MSVC
|
||||
#define release_assert(...) \
|
||||
BOOST_PP_OVERLOAD (release_assert_, __VA_ARGS__) \
|
||||
(__VA_ARGS__)
|
||||
#else
|
||||
#define release_assert(...) BOOST_PP_CAT (BOOST_PP_OVERLOAD (release_assert_, __VA_ARGS__) (__VA_ARGS__), BOOST_PP_EMPTY ())
|
||||
#endif
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define debug_assert(...) (void)0
|
||||
#else
|
||||
#define debug_assert_1(check) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, false)
|
||||
#define debug_assert_2(check, error_msg) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, false, error_msg)
|
||||
#if !BOOST_PP_VARIADICS_MSVC
|
||||
#define debug_assert(...) \
|
||||
BOOST_PP_OVERLOAD (debug_assert_, __VA_ARGS__) \
|
||||
(__VA_ARGS__)
|
||||
#else
|
||||
#define debug_assert(...) BOOST_PP_CAT (BOOST_PP_OVERLOAD (debug_assert_, __VA_ARGS__) (__VA_ARGS__), BOOST_PP_EMPTY ())
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace nano
|
||||
{
|
||||
// Lower priority of calling work generating thread
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue