@@ -66,6 +66,38 @@ LOG_LEVEL_LIST
6666 log_trace (logging)(" log_trace-test" );
6767 log_debug (logging)(" log_debug-test" );
6868 }
69+
70+ void test_asynclog_drop_messages () {
71+ if (AsyncLogWriter::instance () != nullptr ) {
72+ const size_t sz = 100 ;
73+
74+ // shrink async buffer.
75+ AutoModifyRestore<size_t > saver (AsyncLogBufferSize, sz * 1024 /* in byte*/ );
76+ LogMessage (logging) lm;
77+
78+ // write 100x more messages than its capacity in burst
79+ for (size_t i = 0 ; i < sz * 100 ; ++i) {
80+ lm.debug (" a lot of log..." );
81+ }
82+ lm.flush ();
83+ }
84+ }
85+
86+ // stdout/stderr support
87+ bool write_to_file (const std::string& output) {
88+ FILE* f = fopen (TestLogFileName, " w" );
89+
90+ if (f != NULL ) {
91+ size_t sz = output.size ();
92+ size_t written = fwrite (output.c_str (), sizeof (char ), output.size (), f);
93+
94+ if (written == sz * sizeof (char )) {
95+ return fclose (f) == 0 ;
96+ }
97+ }
98+
99+ return false ;
100+ }
69101};
70102
71103TEST_VM (AsyncLogBufferTest, fifo) {
@@ -198,19 +230,48 @@ TEST_VM_F(AsyncLogTest, logMessage) {
198230
199231TEST_VM_F (AsyncLogTest, droppingMessage) {
200232 set_log_config (TestLogFileName, " logging=debug" );
201- const size_t sz = 100 ;
233+ test_asynclog_drop_messages () ;
202234
235+ AsyncLogWriter::flush ();
203236 if (AsyncLogWriter::instance () != nullptr ) {
204- // shrink async buffer.
205- AutoModifyRestore< size_t > saver (AsyncLogBufferSize, sz * 1024 /* in byte */ );
206- LogMessage (logging) lm;
237+ EXPECT_TRUE ( file_contains_substring (TestLogFileName, " messages dropped due to async logging " ));
238+ }
239+ }
207240
208- // write 100x more messages than its capacity in burst
209- for (size_t i = 0 ; i < sz * 100 ; ++i) {
210- lm.debug (" a lot of log..." );
211- }
212- lm.flush ();
213- AsyncLogWriter::flush ();
241+ TEST_VM_F (AsyncLogTest, stdoutOutput) {
242+ testing::internal::CaptureStdout ();
243+ set_log_config (" stdout" , " logging=debug" );
244+
245+ test_asynclog_ls ();
246+ test_asynclog_drop_messages ();
247+
248+ AsyncLogWriter::flush ();
249+ EXPECT_TRUE (write_to_file (testing::internal::GetCapturedStdout ()));
250+
251+ EXPECT_TRUE (file_contains_substring (TestLogFileName, " LogStreamWithAsyncLogImpl" ));
252+ EXPECT_TRUE (file_contains_substring (TestLogFileName, " logStream msg1-msg2-msg3" ));
253+ EXPECT_TRUE (file_contains_substring (TestLogFileName, " logStream newline" ));
254+
255+ if (AsyncLogWriter::instance () != nullptr ) {
256+ EXPECT_TRUE (file_contains_substring (TestLogFileName, " messages dropped due to async logging" ));
257+ }
258+ }
259+
260+ TEST_VM_F (AsyncLogTest, stderrOutput) {
261+ testing::internal::CaptureStderr ();
262+ set_log_config (" stderr" , " logging=debug" );
263+
264+ test_asynclog_ls ();
265+ test_asynclog_drop_messages ();
266+
267+ AsyncLogWriter::flush ();
268+ EXPECT_TRUE (write_to_file (testing::internal::GetCapturedStderr ()));
269+
270+ EXPECT_TRUE (file_contains_substring (TestLogFileName, " LogStreamWithAsyncLogImpl" ));
271+ EXPECT_TRUE (file_contains_substring (TestLogFileName, " logStream msg1-msg2-msg3" ));
272+ EXPECT_TRUE (file_contains_substring (TestLogFileName, " logStream newline" ));
273+
274+ if (AsyncLogWriter::instance () != nullptr ) {
214275 EXPECT_TRUE (file_contains_substring (TestLogFileName, " messages dropped due to async logging" ));
215276 }
216277}
0 commit comments