| // Copyright 2023 Google Inc. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // 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 "logger.h" |
| |
| #include <stdio.h> |
| #include <sstream> |
| |
| #include "stdio_redirection.h" |
| #include "test.h" |
| |
| #ifdef _WIN32 |
| #define NULL_FILE "NUL" |
| #else |
| #define NULL_FILE "/dev/null" |
| #endif |
| |
| TEST(Logger, ToStdio) { |
| FILE* f = fopen(NULL_FILE, "wb"); |
| char buffer[10000]; |
| setvbuf(f, buffer, _IOFBF, sizeof(buffer)); |
| |
| Logger logger = Logger::CreateForStdio(f); |
| ASSERT_TRUE(logger.enabled()); |
| EXPECT_TRUE(logger.Log("Raw")); |
| EXPECT_TRUE(logger.Log("Hello %s", "World!")); |
| ASSERT_TRUE(!::memcmp(buffer, "Raw\nHello World!\n", 17)); |
| fclose(f); |
| } |
| |
| TEST(Logger, ToStdostream) { |
| std::ostringstream out; |
| Logger logger = Logger::CreateForStd(out); |
| ASSERT_TRUE(logger.enabled()); |
| EXPECT_TRUE(logger.Log("Raw")); |
| EXPECT_TRUE(logger.Log("Hello %s", "World!")); |
| ASSERT_EQ(out.str(), "Raw\nHello World!\n"); |
| } |
| |
| TEST(Logger, ToString) { |
| std::string out; |
| Logger logger = Logger::CreateForString(&out); |
| ASSERT_TRUE(logger.enabled()); |
| EXPECT_TRUE(logger.Log("Raw")); |
| EXPECT_TRUE(logger.Log("Hello %s", "World!")); |
| ASSERT_EQ(out, "Raw\nHello World!\n"); |
| } |
| |
| TEST(Logger, ProxyToDisabledLogger) { |
| Logger null_logger; |
| ASSERT_FALSE(null_logger.enabled()); |
| Logger logger = Logger::CreateProxyFor(null_logger); |
| ASSERT_FALSE(logger.enabled()); |
| } |
| |
| TEST(Logger, LoggingAnEmptyStringReturnsTrue) { |
| std::string out; |
| Logger str_logger = Logger::CreateForString(&out); |
| EXPECT_TRUE(str_logger.Log("")); |
| EXPECT_EQ(out, "\n"); |
| } |
| |
| TEST(Logger, ProxyToLogger) { |
| std::string out; |
| Logger str_logger = Logger::CreateForString(&out); |
| Logger logger = Logger::CreateProxyFor(str_logger); |
| ASSERT_TRUE(logger.enabled()); |
| EXPECT_TRUE(logger.Log("Raw")); |
| EXPECT_TRUE(logger.Log("Hello %s", "World!")); |
| ASSERT_EQ(out, "Raw\nHello World!\n"); |
| } |
| |
| TEST(Logger, ToTeeLogger) { |
| std::string out1 = "This is 1: "; |
| std::string out2; |
| Logger str1_logger = Logger::CreateForString(&out1); |
| Logger str2_logger = Logger::CreateForString(&out2); |
| Logger logger = Logger::CreateTeeFor(str1_logger, str2_logger); |
| ASSERT_TRUE(logger.enabled()); |
| EXPECT_TRUE(logger.Log("Raw")); |
| EXPECT_TRUE(logger.Log("Hello %s", "World!")); |
| ASSERT_EQ(out1, "This is 1: Raw\nHello World!\n"); |
| ASSERT_EQ(out2, "Raw\nHello World!\n"); |
| |
| out1.clear(); |
| out2.clear(); |
| Logger null_logger; |
| |
| logger = Logger::CreateTeeFor(str1_logger, null_logger); |
| ASSERT_TRUE(logger.enabled()); |
| EXPECT_TRUE(logger.Log("Raw")); |
| EXPECT_TRUE(logger.Log("Hello %s", "World!")); |
| ASSERT_EQ(out1, "Raw\nHello World!\n"); |
| ASSERT_EQ(out2, ""); |
| |
| out1.clear(); |
| logger = Logger::CreateTeeFor(null_logger, str2_logger); |
| ASSERT_TRUE(logger.enabled()); |
| EXPECT_TRUE(logger.Log("Raw")); |
| EXPECT_TRUE(logger.Log("Hello %s", "World!")); |
| ASSERT_EQ(out2, "Raw\nHello World!\n"); |
| ASSERT_EQ(out1, ""); |
| |
| out2.clear(); |
| logger = Logger::CreateTeeFor(null_logger, null_logger); |
| EXPECT_FALSE(logger.enabled()); |
| EXPECT_FALSE(logger.Log("Raw")); |
| EXPECT_FALSE(logger.Log("Hello %s", "World!")); |
| } |
| |
| TEST(Logger, LOGGER_LOG) { |
| bool did_work = false; |
| auto get_string_arg = [&did_work]() -> const char* { |
| did_work = true; |
| return "Hello World"; |
| }; |
| |
| Logger null_logger; |
| ASSERT_FALSE(did_work); |
| // get_string_arg() should not be called here! |
| EXPECT_FALSE(LOGGER_LOG(null_logger, "I say: %s", get_string_arg())); |
| ASSERT_FALSE(did_work); |
| |
| std::string out; |
| Logger str_logger = Logger::CreateForString(&out); |
| // get_string_arg() should be called here! |
| EXPECT_TRUE(LOGGER_LOG(str_logger, "I say: %s", get_string_arg())); |
| ASSERT_TRUE(did_work); |
| ASSERT_EQ(out, "I say: Hello World\n"); |
| } |