Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
bob
bob.core
Merge requests
!9
More robust cout/cerr redirection
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
More robust cout/cerr redirection
6-random-failures
into
master
Overview
0
Commits
1
Pipelines
1
Changes
1
Merged
André Anjos
requested to merge
6-random-failures
into
master
8 years ago
Overview
0
Commits
1
Pipelines
1
Changes
1
Expand
Closes
#6 (closed)
0
0
Merge request reports
Compare
master
master (base)
and
latest version
latest version
035ff004
1 commit,
8 years ago
1 file
+
35
−
10
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
bob/core/test.cpp
+
35
−
10
Options
@@ -202,11 +202,42 @@ BOB_CATCH_FUNCTION("_log_message_mt", 0)
}
static
void
_test
(
const
std
::
string
&
expect
ed
,
const
std
::
string
&
obtain
ed
,
const
std
::
string
&
step
){
static
void
_test
(
const
std
::
string
&
obtain
ed
,
const
std
::
string
&
expect
ed
,
const
std
::
string
&
step
){
if
(
expected
!=
obtained
)
throw
std
::
runtime_error
((
boost
::
format
(
"The string
'%s'
in step %swas not
'%s'
as expected"
)
%
obtained
%
step
%
expected
).
str
());
throw
std
::
runtime_error
((
boost
::
format
(
"The string
\"
%s
\"
in step %s
was not
\"
%s
\"
as expected"
)
%
obtained
%
step
%
expected
).
str
());
}
// Proper redirection of a stream. If exception or similar is thrown, correctly
// resets the modified standard stream.
// See: http://stackoverflow.com/questions/5419356/redirect-stdout-stderr-to-a-string
class
_ostream_redirect
{
public:
_ostream_redirect
(
std
::
ostream
&
s
)
:
m_stream
(
s
),
m_buffer
(),
m_old
(
m_stream
.
rdbuf
(
m_buffer
.
rdbuf
()))
{
}
void
str
(
const
char
*
v
)
{
m_buffer
.
str
(
v
);
}
std
::
string
str
()
{
return
m_buffer
.
str
();
}
~
_ostream_redirect
()
{
m_stream
.
rdbuf
(
m_old
);
}
private
:
std
::
ostream
&
m_stream
;
std
::
stringstream
m_buffer
;
std
::
streambuf
*
m_old
;
};
static
auto
_output_disable_doc
=
bob
::
extension
::
FunctionDoc
(
"_test_output_disable"
,
"Writes C++ messages with and without being visible and raises exceptions when an error occurs."
@@ -219,10 +250,8 @@ BOB_TRY
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
""
,
kwlist
))
return
0
;
// redirect std::cout and std::cerr, see http://stackoverflow.com/a/5419388/3301902
std
::
stringstream
out
,
err
;
std
::
streambuf
*
oldout
=
std
::
cout
.
rdbuf
(
out
.
rdbuf
());
std
::
streambuf
*
olderr
=
std
::
cerr
.
rdbuf
(
err
.
rdbuf
());
_ostream_redirect
out
(
std
::
cout
);
_ostream_redirect
err
(
std
::
cerr
);
bob
::
core
::
log_level
(
bob
::
core
::
DEBUG
);
bob
::
core
::
debug
<<
"This is a debug message"
<<
std
::
endl
;
@@ -252,10 +281,6 @@ BOB_TRY
_test
(
out
.
str
(),
""
,
"disable"
);
_test
(
err
.
str
(),
""
,
"disable"
);
// make sure that cout and cerr are redirected to their original streams
std
::
cout
.
rdbuf
(
oldout
);
std
::
cerr
.
rdbuf
(
olderr
);
bob
::
core
::
log_level
(
bob
::
core
::
DEBUG
);
Py_RETURN_NONE
;
Loading