| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include "gtest/gtest.h" |
| 36 |
#include "stk_util/parallel/Parallel.hpp" |
| 37 |
#include "stk_util/environment/OutputLog.hpp" |
| 38 |
#include "stk_util/parallel/OutputStreams.hpp" |
| 39 |
#include <iostream> |
| 40 |
#include <fstream> |
| 41 |
#include <stdexcept> |
| 42 |
|
| 43 |
|
| 44 |
class TestOutputStreams : public ::testing::Test |
| 45 |
{ |
| 46 |
protected: |
| 47 |
virtual void SetUp() override |
| 48 |
{ |
| 49 |
// We unregister any conflicting streams here. |
| 50 |
if (stk::get_log_ostream("cout") != NULL) { |
| 51 |
stk::unregister_log_ostream(*stk::get_log_ostream("cout")); |
| 52 |
} |
| 53 |
if (stk::get_log_ostream("cerr") != NULL) { |
| 54 |
stk::unregister_log_ostream(*stk::get_log_ostream("cerr")); |
| 55 |
} |
| 56 |
if (stk::get_ostream_ostream("out") != NULL) { |
| 57 |
stk::unregister_ostream(*stk::get_ostream_ostream("out")); |
| 58 |
} |
| 59 |
if (stk::get_ostream_ostream("pout") != NULL) { |
| 60 |
stk::unregister_ostream(*stk::get_ostream_ostream("pout")); |
| 61 |
} |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
TEST_F(TestOutputStreams, RegisterBindUnregister) |
| 66 |
{ |
| 67 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 68 |
|
| 69 |
// Make cout and cerr available as log stream targets. |
| 70 |
stk::register_log_ostream(std::cout, "cout"); |
| 71 |
stk::register_log_ostream(std::cerr, "cerr"); |
| 72 |
|
| 73 |
std::ostringstream log1; |
| 74 |
std::ostringstream log2; |
| 75 |
|
| 76 |
std::ostream out(std::cout.rdbuf()); |
| 77 |
|
| 78 |
stk::register_ostream(out, "out"); |
| 79 |
|
| 80 |
ASSERT_TRUE(stk::is_registered_ostream("out")); |
| 81 |
|
| 82 |
stk::register_log_ostream(log1, "log1"); |
| 83 |
stk::register_log_ostream(log2, "log2"); |
| 84 |
|
| 85 |
stk::bind_output_streams("out>log1"); |
| 86 |
|
| 87 |
out << "stk::bind_output_streams(\"out>log1\");" << std::endl; |
| 88 |
|
| 89 |
stk::bind_output_streams("out>+log2"); |
| 90 |
out << "stk::bind_output_streams(\"out>+log2\");" << std::endl; |
| 91 |
|
| 92 |
stk::bind_output_streams("out>-log1"); |
| 93 |
out << "stk::bind_output_streams(\"out>-log1\");" << std::endl; |
| 94 |
|
| 95 |
stk::bind_output_streams("out>-log2"); |
| 96 |
out << "stk::bind_output_streams(\"out>-log2\");" << std::endl; |
| 97 |
|
| 98 |
std::ostringstream log1_result; |
| 99 |
log1_result << "stk::bind_output_streams(\"out>log1\");" << std::endl |
| 100 |
<< "stk::bind_output_streams(\"out>+log2\");" << std::endl; |
| 101 |
|
| 102 |
std::ostringstream log2_result; |
| 103 |
log2_result << "stk::bind_output_streams(\"out>+log2\");" << std::endl |
| 104 |
<< "stk::bind_output_streams(\"out>-log1\");" << std::endl; |
| 105 |
|
| 106 |
ASSERT_EQ((log1_result.str() == log1.str()), true); |
| 107 |
ASSERT_EQ((log2_result.str() == log2.str()), true); |
| 108 |
|
| 109 |
stk::unregister_log_ostream(log1); |
| 110 |
stk::unregister_log_ostream(log2); |
| 111 |
stk::unregister_ostream(out); |
| 112 |
|
| 113 |
ASSERT_EQ(out.rdbuf(), std::cout.rdbuf()); |
| 114 |
} |
| 115 |
|
| 116 |
TEST_F(TestOutputStreams, LogToFile) |
| 117 |
{ |
| 118 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 119 |
|
| 120 |
std::ostream out(std::cout.rdbuf()); |
| 121 |
|
| 122 |
stk::register_ostream(out, "out"); |
| 123 |
|
| 124 |
stk::bind_output_streams("log=\"logfile\" out>log"); |
| 125 |
|
| 126 |
ASSERT_EQ((std::string("logfile") == stk::get_log_path("log")), true); |
| 127 |
|
| 128 |
out << "This is a test" << std::endl; |
| 129 |
|
| 130 |
stk::bind_output_streams("log=\"\""); |
| 131 |
|
| 132 |
stk::unregister_ostream(out); |
| 133 |
|
| 134 |
std::ostringstream log_result; |
| 135 |
log_result << "This is a test"; |
| 136 |
|
| 137 |
std::ifstream log_stream("logfile"); |
| 138 |
std::string log_string; |
| 139 |
getline(log_stream, log_string); |
| 140 |
ASSERT_EQ((log_result.str() == log_string), true); |
| 141 |
unlink("logfile"); |
| 142 |
} |
| 143 |
|
| 144 |
TEST_F(TestOutputStreams, LogViaOutputP0) |
| 145 |
{ |
| 146 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 147 |
|
| 148 |
stk::bind_output_streams("log=\"myfile.log\""); |
| 149 |
stk::set_outputP0(stk::get_log_ostream("log")); |
| 150 |
|
| 151 |
ASSERT_EQ((std::string("myfile.log") == stk::get_log_path("log")), true); |
| 152 |
|
| 153 |
stk::outputP0() << "This is a test" << std::endl; |
| 154 |
|
| 155 |
stk::bind_output_streams("log=\"\""); |
| 156 |
|
| 157 |
std::ostringstream log_result; |
| 158 |
log_result << "This is a test"; |
| 159 |
|
| 160 |
std::ifstream log_stream("myfile.log"); |
| 161 |
std::string log_string; |
| 162 |
getline(log_stream, log_string); |
| 163 |
ASSERT_EQ((log_result.str() == log_string), true); |
| 164 |
} |
| 165 |
|
| 166 |
TEST_F(TestOutputStreams, LogViaSierraOut) |
| 167 |
{ |
| 168 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 169 |
|
| 170 |
if (stk::get_ostream_ostream("out") == NULL) { |
| 171 |
stk::register_ostream(sierra::out(), "out"); |
| 172 |
} |
| 173 |
|
| 174 |
stk::bind_output_streams("log=\"myfile.log\" out>log"); |
| 175 |
|
| 176 |
ASSERT_EQ((std::string("myfile.log") == stk::get_log_path("log")), true); |
| 177 |
|
| 178 |
sierra::out() << "This is a test" << std::endl; |
| 179 |
|
| 180 |
stk::bind_output_streams("log=\"\""); |
| 181 |
|
| 182 |
std::ostringstream log_result; |
| 183 |
log_result << "This is a test"; |
| 184 |
|
| 185 |
std::ifstream log_stream("myfile.log"); |
| 186 |
std::string log_string; |
| 187 |
getline(log_stream, log_string); |
| 188 |
ASSERT_EQ((log_result.str() == log_string), true); |
| 189 |
} |
| 190 |
|
| 191 |
TEST_F(TestOutputStreams, UnregisterOutputBoundAsLog) |
| 192 |
{ |
| 193 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 194 |
|
| 195 |
std::ostringstream default_log; |
| 196 |
std::ostream out(default_log.rdbuf()); |
| 197 |
std::ostream pout(std::cout.rdbuf()); |
| 198 |
|
| 199 |
stk::register_ostream(out, "out"); |
| 200 |
stk::register_ostream(pout, "pout"); |
| 201 |
|
| 202 |
// Constructing the log streams after the registered output stream is not exception safe. |
| 203 |
std::ostringstream log; |
| 204 |
stk::register_log_ostream(log, "log"); |
| 205 |
|
| 206 |
// As a result, this try catch block must be represent to ensure the that unregistration |
| 207 |
// happens correctly. |
| 208 |
try { |
| 209 |
stk::bind_output_streams("out>pout pout>log"); |
| 210 |
|
| 211 |
out << "This is to out" << std::endl; |
| 212 |
pout << "This is to pout" << std::endl; |
| 213 |
|
| 214 |
std::ostringstream log_result; |
| 215 |
log_result << "This is to out" << std::endl |
| 216 |
<< "This is to pout" << std::endl; |
| 217 |
|
| 218 |
ASSERT_EQ((log_result.str() == log.str()), true); |
| 219 |
|
| 220 |
throw std::exception(); |
| 221 |
} |
| 222 |
catch (...) { |
| 223 |
} |
| 224 |
|
| 225 |
stk::unregister_log_ostream(log); |
| 226 |
stk::unregister_ostream(pout); |
| 227 |
stk::unregister_ostream(out); |
| 228 |
|
| 229 |
out << "This is to out" << std::endl; |
| 230 |
|
| 231 |
std::ostringstream log_result; |
| 232 |
log_result << "This is to out" << std::endl; |
| 233 |
ASSERT_EQ((log_result.str() == default_log.str()), true); |
| 234 |
} |
| 235 |
|
| 236 |
TEST_F(TestOutputStreams, RegisterExistingName) |
| 237 |
{ |
| 238 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 239 |
|
| 240 |
std::ostringstream log1; |
| 241 |
std::ostringstream log2; |
| 242 |
|
| 243 |
std::ostream out(std::cout.rdbuf()); |
| 244 |
std::ostream pout(std::cout.rdbuf()); |
| 245 |
|
| 246 |
stk::register_ostream(out, "out"); |
| 247 |
ASSERT_THROW(stk::register_ostream(pout, "out"), std::runtime_error); |
| 248 |
|
| 249 |
ASSERT_EQ(&out, stk::get_ostream_ostream("out")); |
| 250 |
|
| 251 |
stk::register_log_ostream(log1, "log"); |
| 252 |
|
| 253 |
ASSERT_THROW(stk::bind_output_streams("badout>log"), std::runtime_error); |
| 254 |
|
| 255 |
ASSERT_THROW(stk::bind_output_streams("out>badlog"), std::runtime_error); |
| 256 |
|
| 257 |
stk::unregister_log_ostream(log1); |
| 258 |
stk::unregister_ostream(out); |
| 259 |
} |
| 260 |
|