Skip to content

Commit 26edcbd

Browse files
author
Omer Ozarslan
committed
Document unscoped info
1 parent 093b3b8 commit 26edcbd

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

docs/logging.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<a id="top"></a>
22
# Logging macros
33

4-
Additional messages can be logged during a test case. Note that the messages are scoped and thus will not be reported if failure occurs in scope preceding the message declaration. An example:
4+
Additional messages can be logged during a test case. Note that the messages logged with `INFO` are scoped and thus will not be reported if failure occurs in scope preceding the message declaration. An example:
55

66
```cpp
77
TEST_CASE("Foo") {
@@ -28,6 +28,58 @@ The number is 1
2828
```
2929
When the last `CHECK` fails in the "Bar" test case, then only one message will be printed: `Test case start`.
3030
31+
## Logging without local scope
32+
33+
`UNSCOPED_INFO` is similar to `INFO` with two key differences:
34+
35+
- Lifetime of unscoped info is not tied to its own scope.
36+
- Unscoped info can be reported by the first following assertion only, regardless of the result of that assertion.
37+
38+
This macro can be used for reporting information from helper functions or inner scopes because lifetime of `UNSCOPED_INFO` is limited by the assertion following whereas lifetime of `INFO` is limited by its own scope. An example:
39+
40+
```cpp
41+
void print_some_info() {
42+
UNSCOPED_INFO("Info from helper");
43+
}
44+
45+
TEST_CASE("Baz") {
46+
print_some_info();
47+
for (int i = 0; i < 2; ++i) {
48+
UNSCOPED_INFO("The number is " << i);
49+
}
50+
CHECK(false);
51+
}
52+
53+
TEST_CASE( "Qux" ) {
54+
INFO("First info");
55+
UNSCOPED_INFO("First unscoped info");
56+
CHECK(false);
57+
58+
INFO("Second info");
59+
UNSCOPED_INFO("Second unscoped info");
60+
CHECK(false);
61+
}
62+
```
63+
64+
"Baz" test case prints:
65+
```
66+
Info from helper
67+
The number is 0
68+
The number is 1
69+
```
70+
71+
With "Qux" test case, two messages will be printed when the first `CHECK` fails:
72+
```
73+
First info
74+
First unscoped info
75+
```
76+
77+
"First unscoped info" message will be cleared after the first `CHECK`, while "First info" message will persist until the end of the test case. Therefore, when the second `CHECK` fails, three messages will be printed:
78+
```
79+
First info
80+
Second info
81+
Second unscoped info
82+
```
3183

3284
## Streaming macros
3385

@@ -43,11 +95,17 @@ These macros come in three forms:
4395
4496
**INFO(** _message expression_ **)**
4597
46-
The message is logged to a buffer, but only reported with the next assertion that is logged. This allows you to log contextual information in case of failures which is not shown during a successful test run (for the console reporter, without -s). Messages are removed from the buffer at the end of their scope, so may be used, for example, in loops.
98+
The message is logged to a buffer, but only reported with next assertions that are logged. This allows you to log contextual information in case of failures which is not shown during a successful test run (for the console reporter, without -s). Messages are removed from the buffer at the end of their scope, so may be used, for example, in loops.
4799
48100
_Note that in Catch2 2.x.x `INFO` can be used without a trailing semicolon as there is a trailing semicolon inside macro.
49101
This semicolon will be removed with next major version. It is highly advised to use a trailing semicolon after `INFO` macro._
50102
103+
**UNSCOPED_INFO(** _message expression_ **)**
104+
105+
Similar to `INFO`, but messages are removed from the buffer after each assertion and they are not limited to their own scopes.
106+
107+
_Note: Unscoped messages are cleared after `WARN` as well._
108+
51109
**WARN(** _message expression_ **)**
52110
53111
The message is always reported but does not fail the test.

0 commit comments

Comments
 (0)