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
GitLab 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
Commits
53d7e27b
There was a problem fetching the pipeline summary.
Commit
53d7e27b
authored
8 years ago
by
André Anjos
Browse files
Options
Downloads
Plain Diff
Merge branch '6-random-failures' into 'master'
More robust cout/cerr redirection Closes
#6
See merge request
!9
parents
c7867eb0
035ff004
No related branches found
No related tags found
1 merge request
!9
More robust cout/cerr redirection
Pipeline
#
Changes
1
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
bob/core/test.cpp
+35
-10
35 additions, 10 deletions
bob/core/test.cpp
with
35 additions
and
10 deletions
bob/core/test.cpp
+
35
−
10
View file @
53d7e27b
...
@@ -202,11 +202,42 @@ BOB_CATCH_FUNCTION("_log_message_mt", 0)
...
@@ -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
)
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
(
static
auto
_output_disable_doc
=
bob
::
extension
::
FunctionDoc
(
"_test_output_disable"
,
"_test_output_disable"
,
"Writes C++ messages with and without being visible and raises exceptions when an error occurs."
"Writes C++ messages with and without being visible and raises exceptions when an error occurs."
...
@@ -219,10 +250,8 @@ BOB_TRY
...
@@ -219,10 +250,8 @@ BOB_TRY
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
""
,
kwlist
))
return
0
;
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
""
,
kwlist
))
return
0
;
// redirect std::cout and std::cerr, see http://stackoverflow.com/a/5419388/3301902
_ostream_redirect
out
(
std
::
cout
);
std
::
stringstream
out
,
err
;
_ostream_redirect
err
(
std
::
cerr
);
std
::
streambuf
*
oldout
=
std
::
cout
.
rdbuf
(
out
.
rdbuf
());
std
::
streambuf
*
olderr
=
std
::
cerr
.
rdbuf
(
err
.
rdbuf
());
bob
::
core
::
log_level
(
bob
::
core
::
DEBUG
);
bob
::
core
::
log_level
(
bob
::
core
::
DEBUG
);
bob
::
core
::
debug
<<
"This is a debug message"
<<
std
::
endl
;
bob
::
core
::
debug
<<
"This is a debug message"
<<
std
::
endl
;
...
@@ -252,10 +281,6 @@ BOB_TRY
...
@@ -252,10 +281,6 @@ BOB_TRY
_test
(
out
.
str
(),
""
,
"disable"
);
_test
(
out
.
str
(),
""
,
"disable"
);
_test
(
err
.
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
);
bob
::
core
::
log_level
(
bob
::
core
::
DEBUG
);
Py_RETURN_NONE
;
Py_RETURN_NONE
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment