1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-09-16 09:06:18 +02:00

Split debug and assert code into separate headers.

Assert can be used earlier because it only depends on the error-handler and not logging.
This commit is contained in:
David Steele
2018-04-07 13:12:35 -04:00
parent e00f2dd4ad
commit 82751b3b51
17 changed files with 135 additions and 45 deletions

View File

@@ -60,7 +60,7 @@
</release-item>
<release-item>
<p>Add <code>ASSERT()</code> that is preserved in production builds.</p>
<p>Split debug and assert code into separate headers. Assert can be used earlier because it only depends on the error-handler and not logging. Add <code>ASSERT()</code> that is preserved in production builds.</p>
</release-item>
<release-item>

View File

@@ -3,7 +3,7 @@ Common Command Routines
***********************************************************************************************************************************/
#include <string.h>
#include "common/debug.h"
#include "common/assert.h"
#include "common/log.h"
#include "common/memContext.h"
#include "config/config.h"

32
src/common/assert.h Normal file
View File

@@ -0,0 +1,32 @@
/***********************************************************************************************************************************
Assert Routines
***********************************************************************************************************************************/
#ifndef COMMON_ASSERT_H
#define COMMON_ASSERT_H
#include "common/error.h"
/***********************************************************************************************************************************
For very important asserts that are shipped with the production code
***********************************************************************************************************************************/
#define ASSERT(condition) \
{ \
if (!(condition)) \
THROW(AssertError, "assertion '%s' failed", #condition); \
}
/***********************************************************************************************************************************
Used for assertions that should only be run when debugging. Ideal for conditions that need to be tested during development but
be too expensive to ship with the production code.
***********************************************************************************************************************************/
#ifndef NDEBUG
#define ASSERT_DEBUG(condition) \
{ \
if (!(condition)) \
THROW(AssertError, "debug assertion '%s' failed", #condition); \
}
#else
#define ASSERT_DEBUG(condition)
#endif
#endif

View File

@@ -4,8 +4,6 @@ Debug Routines
#ifndef COMMON_DEBUG_H
#define COMMON_DEBUG_H
#include "common/log.h"
/***********************************************************************************************************************************
NDEBUG indicates to C library routines that debugging is off -- set a more readable flag to use when debugging is on
***********************************************************************************************************************************/
@@ -13,28 +11,6 @@ NDEBUG indicates to C library routines that debugging is off -- set a more reada
#define DEBUG
#endif
/***********************************************************************************************************************************
Assert Macros
***********************************************************************************************************************************/
// For very important asserts that are shipped with the production code.
#define ASSERT(condition) \
{ \
if (!(condition)) \
THROW(AssertError, "assertion '%s' failed", #condition); \
}
// Used for assertions that should only be run when debugging. Ideal for conditions that are not likely to happen in production but
// could occur during development.
#ifdef DEBUG
#define ASSERT_DEBUG(condition) \
{ \
if (!(condition)) \
THROW(AssertError, "debug assertion '%s' failed", #condition); \
}
#else
#define ASSERT_DEBUG(condition)
#endif
/***********************************************************************************************************************************
Extern variables that are needed for unit testing
***********************************************************************************************************************************/

View File

@@ -10,6 +10,7 @@ Log Handler
#include <time.h>
#include <unistd.h>
#include "common/assert.h"
#include "common/debug.h"
#include "common/error.h"
#include "common/log.h"

View File

@@ -4,7 +4,7 @@ Memory Context Manager
#include <stdlib.h>
#include <string.h>
#include "common/debug.h"
#include "common/assert.h"
#include "common/error.h"
#include "common/memContext.h"

View File

@@ -7,7 +7,7 @@ Variant Data Type
#include <string.h>
#include <strings.h>
#include "common/debug.h"
#include "common/assert.h"
#include "common/memContext.h"
#include "common/type/variant.h"

View File

@@ -3,7 +3,7 @@ Command and Option Configuration
***********************************************************************************************************************************/
#include <string.h>
#include "common/debug.h"
#include "common/assert.h"
#include "common/error.h"
#include "common/memContext.h"
#include "config/config.h"

View File

@@ -5,7 +5,7 @@ Command and Option Parse
#include <string.h>
#include <strings.h>
#include "common/debug.h"
#include "common/assert.h"
#include "common/error.h"
#include "common/ini.h"
#include "common/log.h"

View File

@@ -1,7 +1,7 @@
/***********************************************************************************************************************************
Storage File
***********************************************************************************************************************************/
#include "common/debug.h"
#include "common/assert.h"
#include "common/memContext.h"
#include "storage/file.h"

View File

@@ -3,7 +3,7 @@ Storage Manager
***********************************************************************************************************************************/
#include <string.h>
#include "common/debug.h"
#include "common/assert.h"
#include "common/memContext.h"
#include "common/wait.h"
#include "storage/driver/posix.h"

View File

@@ -104,6 +104,7 @@ my $oTestDef =
&TESTDEF_NAME => 'time',
&TESTDEF_TOTAL => 2,
&TESTDEF_C => true,
&TESTDEF_CDEF => '-DNO_ERROR -DNO_LOG',
&TESTDEF_COVERAGE =>
{
@@ -122,6 +123,28 @@ my $oTestDef =
'common/error.auto' => TESTDEF_COVERAGE_NOCODE,
},
},
{
&TESTDEF_NAME => 'assert-on',
&TESTDEF_TOTAL => 2,
&TESTDEF_C => true,
&TESTDEF_CDEF => '-DNO_LOG',
&TESTDEF_COVERAGE =>
{
'common/assert' => TESTDEF_COVERAGE_NOCODE,
},
},
{
&TESTDEF_NAME => 'assert-off',
&TESTDEF_TOTAL => 2,
&TESTDEF_C => true,
&TESTDEF_CDEF => '-DNDEBUG -DNO_LOG',
&TESTDEF_COVERAGE =>
{
'common/assert' => TESTDEF_COVERAGE_NOCODE,
},
},
{
&TESTDEF_NAME => 'mem-context',
&TESTDEF_TOTAL => 7,

View File

@@ -0,0 +1,24 @@
/***********************************************************************************************************************************
Test Assert Macros and Routines when Disabled
***********************************************************************************************************************************/
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
void
testRun()
{
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT()"))
{
TEST_RESULT_VOID(ASSERT(true), "assert true");
TEST_ERROR(ASSERT(false || false), AssertError, "assertion 'false || false' failed");
}
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT_DEBUG()"))
{
TEST_RESULT_VOID(ASSERT_DEBUG(true), "assert true ignored");
TEST_RESULT_VOID(ASSERT_DEBUG(false || false), "assert false ignored");
}
}

View File

@@ -0,0 +1,24 @@
/***********************************************************************************************************************************
Test Assert Macros and Routines
***********************************************************************************************************************************/
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
void
testRun()
{
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT()"))
{
TEST_RESULT_VOID(ASSERT(true), "assert true");
TEST_ERROR(ASSERT(false || false), AssertError, "assertion 'false || false' failed");
}
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT_DEBUG()"))
{
TEST_RESULT_VOID(ASSERT_DEBUG(true), "assert true");
TEST_ERROR(ASSERT_DEBUG(false || false), AssertError, "debug assertion 'false || false' failed");
}
}

View File

@@ -9,16 +9,21 @@ void
testRun()
{
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT()"))
if (testBegin("DEBUG"))
{
TEST_RESULT_VOID(ASSERT(true), "assert true");
TEST_ERROR(ASSERT(false || false), AssertError, "assertion 'false || false' failed");
#ifdef DEBUG
bool debug = true;
#else
bool debug = false;
#endif
TEST_RESULT_BOOL(debug, false, "DEBUG is not defined");
}
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT_DEBUG()"))
if (testBegin("DEBUG_UNIT_EXTERN"))
{
TEST_RESULT_VOID(ASSERT_DEBUG(true), "assert true ignored");
TEST_RESULT_VOID(ASSERT_DEBUG(false || false), "assert false ignored");
const char *debugUnitExtern = "" DEBUG_UNIT_EXTERN "";
TEST_RESULT_STR(debugUnitExtern, "", "DEBUG_UNIT_EXTERN is static");
}
}

View File

@@ -9,16 +9,21 @@ void
testRun()
{
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT()"))
if (testBegin("DEBUG"))
{
TEST_RESULT_VOID(ASSERT(true), "assert true");
TEST_ERROR(ASSERT(false || false), AssertError, "assertion 'false || false' failed");
#ifdef DEBUG
bool debug = true;
#else
bool debug = false;
#endif
TEST_RESULT_BOOL(debug, true, "DEBUG is defined");
}
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT_DEBUG()"))
if (testBegin("DEBUG_UNIT_EXTERN"))
{
TEST_RESULT_VOID(ASSERT_DEBUG(true), "assert true");
TEST_ERROR(ASSERT_DEBUG(false || false), AssertError, "debug assertion 'false || false' failed");
const char *debugUnitExtern = "" DEBUG_UNIT_EXTERN "";
TEST_RESULT_STR(debugUnitExtern, "", "DEBUG_UNIT_EXTERN is blank (extern)");
}
}

View File

@@ -1,7 +1,7 @@
/***********************************************************************************************************************************
Test Page Checksums
***********************************************************************************************************************************/
#include "common/debug.h"
#include "common/assert.h"
/***********************************************************************************************************************************
Page data for testing -- use 8192 for page size since this is the most common value