Skip to content

Commit

Permalink
Split debug and assert code into separate headers.
Browse files Browse the repository at this point in the history
Assert can be used earlier because it only depends on the error-handler and not logging.
  • Loading branch information
dwsteele committed Apr 7, 2018
1 parent e00f2dd commit 82751b3
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 45 deletions.
2 changes: 1 addition & 1 deletion doc/xml/release.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/command/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 32 additions & 0 deletions src/common/assert.h
Original file line number Diff line number Diff line change
@@ -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
24 changes: 0 additions & 24 deletions src/common/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,13 @@ 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
***********************************************************************************************************************************/
#ifndef NDEBUG
#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
***********************************************************************************************************************************/
Expand Down
1 change: 1 addition & 0 deletions src/common/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/common/memContext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion src/common/type/variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/config/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/storage/file.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/***********************************************************************************************************************************
Storage File
***********************************************************************************************************************************/
#include "common/debug.h"
#include "common/assert.h"
#include "common/memContext.h"
#include "storage/file.h"

Expand Down
2 changes: 1 addition & 1 deletion src/storage/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions test/lib/pgBackRestTest/Common/DefineTest.pm
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ my $oTestDef =
&TESTDEF_NAME => 'time',
&TESTDEF_TOTAL => 2,
&TESTDEF_C => true,
&TESTDEF_CDEF => '-DNO_ERROR -DNO_LOG',

&TESTDEF_COVERAGE =>
{
Expand All @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions test/src/module/common/assertOffTest.c
Original file line number Diff line number Diff line change
@@ -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");
}
}
24 changes: 24 additions & 0 deletions test/src/module/common/assertOnTest.c
Original file line number Diff line number Diff line change
@@ -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");
}
}
17 changes: 11 additions & 6 deletions test/src/module/common/debugOffTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
17 changes: 11 additions & 6 deletions test/src/module/common/debugOnTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
}
}
2 changes: 1 addition & 1 deletion test/src/module/postgres/pageChecksumTest.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 82751b3

Please sign in to comment.