Skip to content

UNSCOPED_CAPTURE #2954

Closed
Closed

Description

@TysonRayJones

It might be useful to have a new logger called UNSCOPED_CAPTURE, which relates to CAPTURE in the same way that UNSCOPED_INFO relates to INFO. That is, it works like CAPTURE but its lifetime is not limited to its scope. This would permit invoking CAPTURE inside control flow or inner functions which do not encounter an assertion (like REQUIRE) before the end-of-scope, yet still have the output displayed when the calling scope (like a TEST_CASE) hits the assertion.

Motivation
Currently, CAPTURE's limited lifetime has the same limitations of INFO. This means running code like:

void inner(x) {
    CAPTURE( x );

    // no assertions here, so CAPTURE silent
}

TEST_CASE( "demo" ) {

    int x = 2;
    inner(x);

    FAIL( );
}

will not display x := 2. Such a use-case is motivated by inner() being a customisation of CAPTURE which (for example) chooses which variables to display based on compile-time constants. For example:

template <bool Show>
void runTest() {

    int x = 2;

    // this will never show
    if constexpr (Show) {
        CAPTURE( x );
    }

    FAIL();
}

TEST_CASE( "demoA" ) {

    runTest<true>();
}

TEST_CASE( "demoB" ) {

    runTest<false>();
}

Even the constexpr branch has a scope, so CAPTURE's output is lost. Above is akin to my situation; I have a templated function used to automate many TEST_CASEs (yucky I know - woe scientific software) and I must consult the template parameters to choose which variables CAPTURE should show if/when the test subsequently fails.

In principle, I can manually invoke UNSCOPED_INFO myself, but then I have to re-implement CAPTURE's lovely printing of nested vectors, for example.

Description
A new macro UNSCOPED_CAPTURE which works exactly like CAPTURE, but which has the scoping behaviour of UNSCOPED_INFO.

Activity

  1. horenmar commented on Jul 28, 2025

    @horenmar
    Member

    I personally never used the unscoped messages, but I am willing to merge the PR to do this.

  2. added
    Good First Issue
    on Jul 28, 2025
  3. added a commit that references this issue on Jan 11, 2026
    2580ead
  4. TysonRayJones commented on Jan 14, 2026

    @TysonRayJones
    ContributorAuthor

    Wew you are a scholar and a saint, thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature RequestGood First Issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions