make the assert macro also be able to print a value

This commit is contained in:
Arvid Norberg 2010-09-25 17:46:13 +00:00
parent a1567458ca
commit c341f15f71
3 changed files with 21 additions and 15 deletions

View File

@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
#if (!defined TORRENT_DEBUG && !TORRENT_PRODUCTION_ASSERTS) || TORRENT_NO_ASSERTS
#define TORRENT_ASSERT(a) do {} while(false)
#define TORRENT_ASSERT_VAL(a, b) do {} while(false)
#else
#if TORRENT_PRODUCTION_ASSERTS
@ -50,12 +51,14 @@ std::string demangle(char const* name);
#if (defined __linux__ || defined __MACH__) && defined __GNUC__
TORRENT_EXPORT void assert_fail(const char* expr, int line, char const* file, char const* function);
#define TORRENT_ASSERT(x) do { if (x) {} else assert_fail(#x, __LINE__, __FILE__, __PRETTY_FUNCTION__); } while (false)
TORRENT_EXPORT void assert_fail(const char* expr, int line, char const* file, char const* function, char const* val);
#define TORRENT_ASSERT(x) do { if (x) {} else assert_fail(#x, __LINE__, __FILE__, __PRETTY_FUNCTION__, 0); } while (false)
#define TORRENT_ASSERT_VAL(x, y) do { if (x) {} else { std::stringstream __s__; __s__ << #y ": " << y; assert_fail(#x, __LINE__, __FILE__, __PRETTY_FUNCTION__, __s__.str().c_str()); } } while (false)
#else
#include <cassert>
#define TORRENT_ASSERT(x) assert(x)
#define TORRENT_ASSERT_VAL(x, y) assert(x)
#endif
#endif

View File

@ -121,7 +121,7 @@ void print_backtrace(FILE* out, char const* label) {}
char const* libtorrent_assert_log = "asserts.log";
#endif
void assert_fail(char const* expr, int line, char const* file, char const* function)
void assert_fail(char const* expr, int line, char const* file, char const* function, char const* value)
{
#if TORRENT_PRODUCTION_ASSERTS
@ -139,7 +139,10 @@ void assert_fail(char const* expr, int line, char const* file, char const* funct
"file: '%s'\n"
"line: %d\n"
"function: %s\n"
"expression: %s\n", LIBTORRENT_REVISION, file, line, function, expr);
"expression: %s\n"
"%s%s"
, LIBTORRENT_REVISION, file, line, function, expr
, value ? value : "", value ? "\n" : "");
print_backtrace(out, "stack:");

View File

@ -432,7 +432,7 @@ namespace aux {
char const* key = m[i].name;
void const* src = ((char*)s) + m[i].offset;
entry& val = e[key];
TORRENT_ASSERT(val.type() == entry::undefined_t);
TORRENT_ASSERT_VAL(val.type() == entry::undefined_t, val.type());
switch (m[i].type)
{
case std_string:
@ -537,10 +537,10 @@ namespace aux {
m_next_lsd_torrent = m_torrents.begin();
m_next_connect_torrent = m_torrents.begin();
TORRENT_ASSERT(listen_interface);
TORRENT_ASSERT_VAL(listen_interface, listen_interface);
error_code ec;
m_listen_interface = tcp::endpoint(address::from_string(listen_interface, ec), listen_port_range.first);
TORRENT_ASSERT(!ec);
TORRENT_ASSERT_VAL(!ec, ec);
m_tcp_mapping[0] = -1;
m_tcp_mapping[1] = -1;
@ -740,7 +740,7 @@ namespace aux {
m_key = rand() + (rand() << 15) + (rand() << 30);
std::string print = cl_fprint.to_string();
TORRENT_ASSERT(print.length() <= 20);
TORRENT_ASSERT_VAL(print.length() <= 20, print.length());
// the client's fingerprint
std::copy(
@ -1004,7 +1004,7 @@ namespace aux {
void session_impl::add_extension(
boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> ext)
{
TORRENT_ASSERT(ext);
TORRENT_ASSERT_VAL(ext, ext);
typedef boost::shared_ptr<torrent_plugin>(*function_t)(torrent*, void*);
function_t const* f = ext.target<function_t>();
@ -1126,7 +1126,7 @@ namespace aux {
int conn = m_connections.size();
#endif
(*m_connections.begin())->disconnect(errors::stopping_torrent);
TORRENT_ASSERT(conn == int(m_connections.size()) + 1);
TORRENT_ASSERT_VAL(conn == int(m_connections.size()) + 1, conn);
}
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
@ -1191,10 +1191,10 @@ namespace aux {
{
INVARIANT_CHECK;
TORRENT_ASSERT(s.file_pool_size > 0);
TORRENT_ASSERT_VAL(s.file_pool_size > 0, s.file_pool_size);
// less than 5 seconds unchoke interval is insane
TORRENT_ASSERT(s.unchoke_interval >= 5);
TORRENT_ASSERT_VAL(s.unchoke_interval >= 5, s.unchoke_interval);
// if disk io thread settings were changed
// post a notification to that thread
@ -1396,7 +1396,7 @@ namespace aux {
(*m_logger) << msg << "\n";
#endif
ec = error_code();
TORRENT_ASSERT(!ec);
TORRENT_ASSERT_VAL(!ec, ec);
--retries;
ep.port(ep.port() + 1);
s.sock->bind(ep, ec);
@ -1591,7 +1591,7 @@ namespace aux {
m_socks_listen_socket = boost::shared_ptr<socket_type>(new socket_type(m_io_service));
bool ret = instantiate_connection(m_io_service, m_proxy
, *m_socks_listen_socket);
TORRENT_ASSERT(ret);
TORRENT_ASSERT_VAL(ret, ret);
socks5_stream& s = *m_socks_listen_socket->get<socks5_stream>();
s.set_command(2); // 2 means BIND (as opposed to CONNECT)
@ -1616,7 +1616,7 @@ namespace aux {
m_i2p_listen_socket = boost::shared_ptr<socket_type>(new socket_type(m_io_service));
bool ret = instantiate_connection(m_io_service, m_i2p_conn.proxy()
, *m_i2p_listen_socket);
TORRENT_ASSERT(ret);
TORRENT_ASSERT_VAL(ret, ret);
i2p_stream& s = *m_i2p_listen_socket->get<i2p_stream>();
s.set_command(i2p_stream::cmd_accept);