Line data Source code
1 : // *****************************************************************************
2 : /*!
3 : \file src/UnitTest/QuietCerr.cpp
4 : \copyright 2012-2015 J. Bakosi,
5 : 2016-2018 Los Alamos National Security, LLC.,
6 : 2019-2021 Triad National Security, LLC.,
7 : 2022-2025 J. Bakosi
8 : All rights reserved. See the LICENSE file for details.
9 : \brief Charm++ nodegroup to quiet std::cerr in a thread-safe fashion
10 : \details Charm++ nodegroup to quiet std::cerr in a thread-safe fashion.
11 : */
12 : // *****************************************************************************
13 :
14 : #include <iostream>
15 : #include <sstream>
16 :
17 : #include "QuietCerr.hpp"
18 :
19 : namespace tk {
20 :
21 : #if defined(__clang__)
22 : #pragma clang diagnostic push
23 : #pragma clang diagnostic ignored "-Wmissing-variable-declarations"
24 : #endif
25 :
26 : //! std::tringstream used to quiet std::cerr's stream by redirecting to it
27 : static std::stringstream cerr_quiet;
28 : //! std::streambuf used to store state of std::cerr before redirecting it
29 : static std::streambuf* cerr_old;
30 :
31 : #if defined(__clang__)
32 : #pragma clang diagnostic pop
33 : #endif
34 :
35 : }
36 :
37 : using tk::QuietCerr;
38 :
39 : void
40 : // cppcheck-suppress unusedFunction
41 20 : QuietCerr::quiet()
42 : // *****************************************************************************
43 : //! Section "Initializations at Program Startup" at in the Charm++ manual
44 : //! http://charm.cs.illinois.edu/manuals/html/charm++/manual.html. Since it
45 : //! is executed once every logical node, it is thread-safe.
46 : // *****************************************************************************
47 : {
48 20 : tk::cerr_old = std::cerr.rdbuf( tk::cerr_quiet.rdbuf() );
49 20 : }
50 :
51 0 : QuietCerr::~QuietCerr()
52 : // *****************************************************************************
53 : // Destructor: restore std::cerr's stream state
54 : // *****************************************************************************
55 : {
56 0 : std::cerr.rdbuf( tk::cerr_old );
57 0 : }
58 :
59 : #include "NoWarning/quietcerr.def.h"
|