diff options
author | ccalvarin <ccalvarin@google.com> | 2018-03-21 15:32:30 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-03-21 15:33:51 -0700 |
commit | 9eea0f9a98c6bec344284bfecc7c91c9d8dd7715 (patch) | |
tree | 1b3ca4173ff215bfa0651b7614cbfd056ed71cb1 /src/test/cpp | |
parent | b12892e0ca50a436f1e629ec4391ee4068623dc6 (diff) |
Update the client's skeleton logging framework to use it for --client_debug.
We are still unable to turn this on to write to files, but there are currently 2 logging systems in use in the client: the inactive one, and the print-to-stderr option triggered by --client_debug. Combine these, so we can use the same logging format for both.
Also combine it with the VerboseLogging functionality - it was not documented anywhere and seems redundant.
RELNOTES: None.
PiperOrigin-RevId: 189979051
Diffstat (limited to 'src/test/cpp')
-rw-r--r-- | src/test/cpp/util/logging_test.cc | 182 |
1 files changed, 161 insertions, 21 deletions
diff --git a/src/test/cpp/util/logging_test.cc b/src/test/cpp/util/logging_test.cc index 3288bdf0a2..5779030c68 100644 --- a/src/test/cpp/util/logging_test.cc +++ b/src/test/cpp/util/logging_test.cc @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +#include <fstream> #include <iostream> #include <memory> #include <string> @@ -21,14 +22,18 @@ #include "gtest/gtest.h" namespace blaze_util { +class LoggingTest : public ::testing::Test { + protected: + void SetUp() { + // Set the value of $TMP first, because CaptureStderr retrieves a temp + // directory path and on Windows, the corresponding function (GetTempPathA) + // reads $TMP. + blaze::SetEnv("TMP", blaze::GetEnv("TEST_TMPDIR")); + } +}; TEST(LoggingTest, BazelLogHandlerDumpsToCerrAtFail) { - // Set the value of $TMP first, because CaptureStderr retrieves a temp - // directory path and on Windows, the corresponding function (GetTempPathA) - // reads $TMP. - blaze::SetEnv("TMP", blaze::GetEnv("TEST_TMPDIR")); - // Set up logging now that $TMP is set, and be prepared to capture stderr at - // destruction. + // Set up logging and be prepared to capture stderr at destruction. testing::internal::CaptureStderr(); std::unique_ptr<blaze_util::BazelLogHandler> handler( new blaze_util::BazelLogHandler()); @@ -39,14 +44,19 @@ TEST(LoggingTest, BazelLogHandlerDumpsToCerrAtFail) { BAZEL_LOG(INFO) << teststring; // Check that stderr isn't getting anything yet. - std::string nothing = testing::internal::GetCapturedStderr(); - ASSERT_TRUE(nothing.find(teststring) == std::string::npos); + std::string stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos) + << "stderr unexpectedly contains the log message, before log output was " + "set. Stderr contained: " + << stderr_output; testing::internal::CaptureStderr(); // Destruct the log handler and get the stderr remains. blaze_util::SetLogHandler(nullptr); - std::string output = testing::internal::GetCapturedStderr(); - ASSERT_TRUE(output.find(teststring) != std::string::npos); + stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find(teststring) != std::string::npos) + << "stderr does not contain the expected message. Stderr contained: " + << stderr_output; } TEST(LoggingTest, LogLevelNamesMatch) { @@ -56,26 +66,156 @@ TEST(LoggingTest, LogLevelNamesMatch) { EXPECT_STREQ("FATAL", LogLevelName(LOGLEVEL_FATAL)); } +TEST(LoggingTest, BazelLogDoesNotDumpToStderrIfOuputStreamSetToNull) { + // Set up logging and be prepared to capture stderr at destruction. + testing::internal::CaptureStderr(); + std::unique_ptr<blaze_util::BazelLogHandler> handler( + new blaze_util::BazelLogHandler()); + blaze_util::SetLogHandler(std::move(handler)); + + // Log something. + std::string teststring = "test that the log message is lost."; + BAZEL_LOG(INFO) << teststring; + blaze_util::SetLoggingOutputStream(nullptr); + + // Destruct the log handler and check if stderr got anything. + blaze_util::SetLogHandler(nullptr); + std::string stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos) + << "stderr unexpectedly contains the log message, even though log output " + "was explicitly set to null. Stderr contained: " + << stderr_output; +} + +TEST(LoggingTest, DirectLogsToBufferStreamWorks) { + // Set up logging and be prepared to capture stderr at destruction. + testing::internal::CaptureStderr(); + std::unique_ptr<blaze_util::BazelLogHandler> handler( + new blaze_util::BazelLogHandler()); + blaze_util::SetLogHandler(std::move(handler)); + + // Ask that the logs get output to a string buffer (keep a ptr to it so we can + // check its contents) + std::unique_ptr<std::stringstream> stringbuf(new std::stringstream()); + std::stringstream* stringbuf_ptr = stringbuf.get(); + blaze_util::SetLoggingOutputStream(std::move(stringbuf)); + + std::string teststring = "testing log getting directed to a stringbuffer."; + BAZEL_LOG(INFO) << teststring; + + // Check that output went to the buffer. + std::string output(stringbuf_ptr->str()); + EXPECT_TRUE(output.find(teststring) != std::string::npos) + << "log output is missing the log message, the output is: " << output; + + // Check that the output never went to stderr. + std::string stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos) + << "stderr unexpectedly contains the log message that should have gone " + "to the specified string buffer. Stderr contained: " + << stderr_output; +} + +TEST(LoggingTest, BufferedLogsSentToSpecifiedStream) { + // Set up logging and be prepared to capture stderr at destruction. + testing::internal::CaptureStderr(); + std::unique_ptr<blaze_util::BazelLogHandler> handler( + new blaze_util::BazelLogHandler()); + blaze_util::SetLogHandler(std::move(handler)); + + std::string teststring = + "test sending logs to the buffer before setting the output stream"; + BAZEL_LOG(INFO) << teststring; + + // Check that stderr isn't getting anything yet. + std::string stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos) + << "stderr unexpectedly contains the log message, before log output was " + "set. Stderr contained: " + << stderr_output; + testing::internal::CaptureStderr(); + + // Ask that the logs get output to a string buffer (keep a ptr to it so we can + // check its contents) + std::unique_ptr<std::stringstream> stringbuf(new std::stringstream()); + std::stringstream* stringbuf_ptr = stringbuf.get(); + blaze_util::SetLoggingOutputStream(std::move(stringbuf)); + + // Check that the buffered logs were sent. + std::string output(stringbuf_ptr->str()); + EXPECT_TRUE(output.find(teststring) != std::string::npos) + << "log output is missing the log message, the output is: " << output; + + // Check that the output did not go to stderr. + stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos) + << "stderr unexpectedly contains the log message that should have gone " + "to the specified string buffer. Stderr contained: " + << stderr_output; +} + +TEST(LoggingTest, DirectLogsToCerrWorks) { + // Set up logging and be prepared to capture stderr at destruction. + testing::internal::CaptureStderr(); + std::unique_ptr<blaze_util::BazelLogHandler> handler( + new blaze_util::BazelLogHandler()); + blaze_util::SetLogHandler(std::move(handler)); + + // Ask that the logs get output to stderr + blaze_util::SetLoggingOutputStreamToStderr(); + + // Log something. + std::string teststring = "test that the log messages get directed to cerr"; + BAZEL_LOG(INFO) << teststring; + + // Cause the logs to be flushed, and capture them. + blaze_util::SetLogHandler(nullptr); + std::string stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find(teststring) != std::string::npos) + << "stderr does not contain the expected log message. Stderr contained: " + << stderr_output; +} + +TEST(LoggingTest, BufferedLogsGetDirectedToCerr) { + // Set up logging and be prepared to capture stderr at destruction. + testing::internal::CaptureStderr(); + std::unique_ptr<blaze_util::BazelLogHandler> handler( + new blaze_util::BazelLogHandler()); + blaze_util::SetLogHandler(std::move(handler)); + + // Log something before telling the loghandler where to send it. + std::string teststring = "test that this message gets directed to cerr"; + BAZEL_LOG(INFO) << teststring; + + // Ask that the logs get output to stderr + blaze_util::SetLoggingOutputStreamToStderr(); + + // Cause the logs to be flushed, and capture them. + blaze_util::SetLogHandler(nullptr); + std::string stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find(teststring) != std::string::npos) + << "stderr does not contain the expected log message. Stderr contained: " + << stderr_output; +} + TEST(LoggingTest, ImpossibleFile) { - // Set the value of $TMP first, because CaptureStderr retrieves a temp - // directory path and on Windows, the corresponding function (GetTempPathA) - // reads $TMP. - blaze::SetEnv("TMP", blaze::GetEnv("TEST_TMPDIR")); - // Set up logging now that $TMP is set, and be prepared to capture stderr at - // destruction. + // Set up logging and be prepared to capture stderr at destruction. testing::internal::CaptureStderr(); std::unique_ptr<blaze_util::BazelLogHandler> handler( new blaze_util::BazelLogHandler()); blaze_util::SetLogHandler(std::move(handler)); // Deliberately try to log to an impossible location, check that we error out. - blaze_util::SetLogfileDirectory("/this/doesnt/exist"); + std::unique_ptr<std::ofstream> bad_logfile_stream_( + new std::ofstream("/this/doesnt/exist.log", std::fstream::out)); + blaze_util::SetLoggingOutputStream(std::move(bad_logfile_stream_)); // Cause the logs to be flushed, and capture them. blaze_util::SetLogHandler(nullptr); - std::string output = testing::internal::GetCapturedStderr(); - ASSERT_TRUE(output.find("ERROR") != std::string::npos); - ASSERT_TRUE(output.find("/this/doesnt/exist") != std::string::npos); + std::string stderr_output = testing::internal::GetCapturedStderr(); + EXPECT_TRUE(stderr_output.find("ERROR") != std::string::npos); + EXPECT_TRUE(stderr_output.find("Provided stream failed") != std::string::npos) + << "stderr does not contain the expected error. Stderr contained: " + << stderr_output; } - } // namespace blaze_util |