From 08d145a6e9cdc7e2b9611c090c2f88579bb4c1ca Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 6 May 2010 02:18:08 +0000 Subject: [PATCH] added production assert mode --- Jamfile | 4 ++++ docs/building.rst | 6 ++++++ include/libtorrent/assert.hpp | 2 +- src/assert.cpp | 24 ++++++++++++++++++------ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Jamfile b/Jamfile index 514dd5d76..1c0a1c2e5 100755 --- a/Jamfile +++ b/Jamfile @@ -237,6 +237,10 @@ feature.compose on : HAVE_LINUX_FIEMAP_H ; feature full-stats : on off : composite propagated link-incompatible ; feature.compose off : TORRENT_DISABLE_FULL_STATS ; +feature asserts : on off production : composite propagated ; +feature.compose production : TORRENT_PRODUCTION_ASSERTS=1 ; +feature.compose off : TORRENT_NO_ASSERTS=1 ; + feature pool-allocators : on off : composite propagated link-incompatible ; feature.compose off : TORRENT_DISABLE_POOL_ALLOCATOR ; diff --git a/docs/building.rst b/docs/building.rst index 6317755b7..c1cb39538 100644 --- a/docs/building.rst +++ b/docs/building.rst @@ -266,6 +266,12 @@ Build features: | | requires you to link against librt.a. This is | | | typically the case on x86 64 bit systems. | +--------------------------+----------------------------------------------------+ +| ``asserts`` | * ``on`` - asserts are on if in debug mode | +| | * ``off`` - asserts are disabled | +| | * ``production`` - assertion failures are logged | +| | to ``asserts.log`` in the current working | +| | directory, but won't abort the process. | ++--------------------------+----------------------------------------------------+ | ``zlib`` | * ``system`` - links against the zlib supplied | | | with your operating system. | | | * ``shipped`` - links against the zlib bundled | diff --git a/include/libtorrent/assert.hpp b/include/libtorrent/assert.hpp index 0000b6d53..7b4b43bb9 100644 --- a/include/libtorrent/assert.hpp +++ b/include/libtorrent/assert.hpp @@ -34,7 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" -#if !defined TORRENT_DEBUG +#if !defined TORRENT_DEBUG || TORRENT_NO_ASSERTS #define TORRENT_ASSERT(a) do {} while(false) #else diff --git a/src/assert.cpp b/src/assert.cpp index 358a696f7..9088ebfad 100644 --- a/src/assert.cpp +++ b/src/assert.cpp @@ -97,30 +97,37 @@ std::string demangle(char const* name) { return name; } #if (defined __linux__ || (defined __APPLE__ && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050)) #include -void print_backtrace(char const* label) +void print_backtrace(FILE* out, char const* label) { void* stack[50]; int size = backtrace(stack, 50); char** symbols = backtrace_symbols(stack, size); - fprintf(stderr, "%s\n", label); + fprintf(out, "%s\n", label); for (int i = 1; i < size; ++i) { - fprintf(stderr, "%d: %s\n", i, demangle(symbols[i]).c_str()); + fprintf(out, "%d: %s\n", i, demangle(symbols[i]).c_str()); } free(symbols); } #else -void print_backtrace(char const* label) {} +void print_backtrace(FILE* out, char const* label) {} #endif void assert_fail(char const* expr, int line, char const* file, char const* function) { - fprintf(stderr, "assertion failed. Please file a bugreport at " +#if TORRENT_PRODUCTION_ASSERTS + FILE* out = fopen("asserts.log", "a+"); + if (out == 0) out = stderr; +#else + FILE* out = stderr; +#endif + + fprintf(out, "assertion failed. Please file a bugreport at " "http://code.rasterbar.com/libtorrent/newticket\n" "Please include the following information:\n\n" "version: " LIBTORRENT_VERSION "\n" @@ -130,12 +137,17 @@ void assert_fail(char const* expr, int line, char const* file, char const* funct "function: %s\n" "expression: %s\n", LIBTORRENT_REVISION, file, line, function, expr); - print_backtrace("stack:"); + print_backtrace(out, "stack:"); + // if production asserts are defined, don't abort, just print the error +#if TORRENT_PRODUCTION_ASSERTS + if (out != stderr) fclose(out); +#else // send SIGINT to the current process // to break into the debugger raise(SIGINT); abort(); +#endif } #else