1 #include <map>
2 
3 #include <gtest/gtest.h>
4 #include <xgboost/logging.h>
5 
6 namespace xgboost {
7 
TEST(Logging,Basic)8 TEST(Logging, Basic) {
9   std::map<std::string, std::string> args {};
10   std::string output;
11 
12   args["verbosity"] = "0";  // silent
13   ConsoleLogger::Configure({args.cbegin(), args.cend()});
14   testing::internal::CaptureStderr();
15   LOG(DEBUG) << "Test silent.";
16   output = testing::internal::GetCapturedStderr();
17   ASSERT_EQ(output.length(), 0);
18 
19   args["verbosity"] = "3";  // debug
20   ConsoleLogger::Configure({args.cbegin(), args.cend()});
21 
22   testing::internal::CaptureStderr();
23   LOG(WARNING) << "Test Log Warning.";
24   output = testing::internal::GetCapturedStderr();
25   ASSERT_NE(output.find("WARNING"), std::string::npos);
26 
27   testing::internal::CaptureStderr();
28   LOG(INFO) << "Test Log Info.";
29   output = testing::internal::GetCapturedStderr();
30   ASSERT_NE(output.find("Test Log Info"), std::string::npos);
31 
32   testing::internal::CaptureStderr();
33   LOG(DEBUG) << "Test Log Debug.";
34   output = testing::internal::GetCapturedStderr();
35   ASSERT_NE(output.find("DEBUG"), std::string::npos);
36 
37   args["verbosity"] = "1";  // warning
38   ConsoleLogger::Configure({args.cbegin(), args.cend()});
39   testing::internal::CaptureStderr();
40   LOG(INFO) << "INFO should not be displayed when set to warning.";
41   output = testing::internal::GetCapturedStderr();
42   ASSERT_EQ(output.size(), 0);
43 
44   testing::internal::CaptureStderr();
45   LOG(CONSOLE) << "Test Log Console";  // ignore global setting.
46   output = testing::internal::GetCapturedStderr();
47   ASSERT_NE(output.find("Test Log Console"), std::string::npos);
48 
49   args["verbosity"] = "2";  // restore
50   ConsoleLogger::Configure({args.cbegin(), args.cend()});
51 }
52 
53 }  // namespace xgboost
54