2 // Copyright 2002 Google, Inc.
3 // All Rights Reserved.
21 #include "base/commandlineflags.h"
22 #include "glog/logging.h"
23 #include "glog/raw_logging.h"
24 #include "googletest.h"
27 using namespace GOOGLE_NAMESPACE;
29 static void TestLogging(bool check_counts);
30 static void TestRawLogging();
31 static void LogWithLevels(int v, int severity, bool err, bool alsoerr);
32 static void TestLoggingLevels();
33 static void TestLogString();
34 static void TestLogSink();
35 static void TestLogSinkWaitTillSent();
36 static void TestCHECK();
37 static void TestDCHECK();
38 static void TestSTREQ();
39 static void TestBasename();
40 static void TestSymlink();
41 static void TestExtension();
42 static void TestWrapper();
43 static void TestErrno();
44 static void TestTruncate();
47 static void BM_Check1(int n) {
61 static void CheckFailure(int a, int b, const char* file, int line, const char* msg);
62 static void BM_Check3(int n) {
64 if (n < x) CheckFailure(n, x, __FILE__, __LINE__, "n < x");
65 if (n < x) CheckFailure(n, x, __FILE__, __LINE__, "n < x");
66 if (n < x) CheckFailure(n, x, __FILE__, __LINE__, "n < x");
67 if (n < x) CheckFailure(n, x, __FILE__, __LINE__, "n < x");
68 if (n < x) CheckFailure(n, x, __FILE__, __LINE__, "n < x");
69 if (n < x) CheckFailure(n, x, __FILE__, __LINE__, "n < x");
70 if (n < x) CheckFailure(n, x, __FILE__, __LINE__, "n < x");
71 if (n < x) CheckFailure(n, x, __FILE__, __LINE__, "n < x");
76 static void BM_Check2(int n) {
93 static void CheckFailure(int a, int b, const char* file, int line, const char* msg) {
96 int main(int argc, char **argv) {
97 // Test some basics before InitGoogleLogging:
99 LogWithLevels(FLAGS_v, FLAGS_stderrthreshold,
100 FLAGS_logtostderr, FLAGS_alsologtostderr);
101 LogWithLevels(0, 0, 0, 0); // simulate "before global c-tors"
102 const string early_stderr = GetCapturedTestStderr();
104 FLAGS_logtostderr = true;
106 InitGoogleLogging(argv[0]);
108 RunSpecifiedBenchmarks();
110 // so that death tests run before we use threads
111 CHECK_EQ(RUN_ALL_TESTS(), 0);
115 // re-emit early_stderr
116 LogMessage("dummy", LogMessage::kNoLogPrefix, INFO).stream() << early_stderr;
123 TestLogSinkWaitTillSent();
128 // TODO: The golden test portion of this test is very flakey.
130 MungeAndDiffTestStderr(FLAGS_test_srcdir + "/src/logging_unittest.err"));
132 FLAGS_logtostderr = false;
145 void TestLogging(bool check_counts) {
146 int64 base_num_infos = LogMessage::num_messages(INFO);
147 int64 base_num_warning = LogMessage::num_messages(WARNING);
148 int64 base_num_errors = LogMessage::num_messages(ERROR);
150 LOG(INFO) << string("foo ") << "bar " << 10 << ' ' << 3.4;
151 for ( int i = 0; i < 10; ++i ) {
152 int old_errno = errno;
154 PLOG_EVERY_N(ERROR, 2) << "Plog every 2, iteration " << COUNTER;
157 LOG_EVERY_N(ERROR, 3) << "Log every 3, iteration " << COUNTER << endl;
158 LOG_EVERY_N(ERROR, 4) << "Log every 4, iteration " << COUNTER << endl;
160 LOG_IF_EVERY_N(WARNING, true, 5) << "Log if every 5, iteration " << COUNTER;
161 LOG_IF_EVERY_N(WARNING, false, 3)
162 << "Log if every 3, iteration " << COUNTER;
163 LOG_IF_EVERY_N(INFO, true, 1) << "Log if every 1, iteration " << COUNTER;
164 LOG_IF_EVERY_N(ERROR, (i < 3), 2)
165 << "Log if less than 3 every 2, iteration " << COUNTER;
167 LOG_IF(WARNING, true) << "log_if this";
168 LOG_IF(WARNING, false) << "don't log_if this";
172 const char const_s[] = "const array";
173 LOG(INFO) << const_s;
175 LOG(ERROR) << string("foo") << ' '<< j << ' ' << setw(10) << j << " "
176 << setw(1) << hex << j;
178 LogMessage("foo", LogMessage::kNoLogPrefix, INFO).stream() << "no prefix";
181 CHECK_EQ(base_num_infos + 14, LogMessage::num_messages(INFO));
182 CHECK_EQ(base_num_warning + 3, LogMessage::num_messages(WARNING));
183 CHECK_EQ(base_num_errors + 15, LogMessage::num_messages(ERROR));
187 static void NoAllocNewHook() {
188 CHECK(false) << "unexpected new";
193 g_new_hook = &NoAllocNewHook;
200 TEST(DeathNoAllocNewHook, logging) {
201 // tests that NewHook used below works
205 }, "unexpected new");
208 void TestRawLogging() {
209 string* foo = new string("foo ");
210 string huge_str(50000, 'a');
214 // Check that RAW loggging does not use mallocs.
217 RAW_LOG(INFO, "%s%s%d%c%f", foo->c_str(), "bar ", 10, ' ', 3.4);
219 RAW_LOG(WARNING, "%s", s);
220 const char const_s[] = "const array";
221 RAW_LOG(INFO, "%s", const_s);
222 void* p = reinterpret_cast<void*>(0x12345678);
223 RAW_LOG(INFO, "ptr %p", p);
225 RAW_LOG(INFO, "ptr %p", p);
227 RAW_LOG(ERROR, "%s%d%c%010d%s%1x", foo->c_str(), j, ' ', j, " ", j);
228 RAW_VLOG(0, "foo %d", j);
231 RAW_LOG(INFO, "foo %d", j); // so that have same stderr to compare
233 RAW_DLOG(INFO, "foo %d", j); // test RAW_DLOG in debug mode
236 // test how long messages are chopped:
237 RAW_LOG(WARNING, "Huge string: %s", huge_str.c_str());
238 RAW_VLOG(0, "Huge string: %s", huge_str.c_str());
241 RAW_LOG(INFO, "log");
242 RAW_VLOG(0, "vlog 0 on");
243 RAW_VLOG(1, "vlog 1 off");
244 RAW_VLOG(2, "vlog 2 off");
245 RAW_VLOG(3, "vlog 3 off");
247 RAW_LOG(INFO, "log");
248 RAW_VLOG(1, "vlog 1 on");
249 RAW_VLOG(2, "vlog 2 on");
250 RAW_VLOG(3, "vlog 3 off");
253 RAW_DCHECK(1 == 2, " RAW_DCHECK's shouldn't be compiled in normal mode");
256 RAW_CHECK(1 == 1, "should be ok");
257 RAW_DCHECK(true, "should be ok");
262 void LogWithLevels(int v, int severity, bool err, bool alsoerr) {
264 "Test: v=%d stderrthreshold=%d logtostderr=%d alsologtostderr=%d",
265 v, severity, err, alsoerr);
270 FLAGS_stderrthreshold = severity;
271 FLAGS_logtostderr = err;
272 FLAGS_alsologtostderr = alsoerr;
274 RAW_VLOG(-1, "vlog -1");
275 RAW_VLOG(0, "vlog 0");
276 RAW_VLOG(1, "vlog 1");
277 RAW_LOG(INFO, "log info");
278 RAW_LOG(WARNING, "log warning");
279 RAW_LOG(ERROR, "log error");
281 VLOG(-1) << "vlog -1";
284 LOG(INFO) << "log info";
285 LOG(WARNING) << "log warning";
286 LOG(ERROR) << "log error";
288 VLOG_IF(-1, true) << "vlog_if -1";
289 VLOG_IF(-1, false) << "don't vlog_if -1";
290 VLOG_IF(0, true) << "vlog_if 0";
291 VLOG_IF(0, false) << "don't vlog_if 0";
292 VLOG_IF(1, true) << "vlog_if 1";
293 VLOG_IF(1, false) << "don't vlog_if 1";
294 LOG_IF(INFO, true) << "log_if info";
295 LOG_IF(INFO, false) << "don't log_if info";
296 LOG_IF(WARNING, true) << "log_if warning";
297 LOG_IF(WARNING, false) << "don't log_if warning";
298 LOG_IF(ERROR, true) << "log_if error";
299 LOG_IF(ERROR, false) << "don't log_if error";
302 c = 1; VLOG_IF(100, c -= 2) << "vlog_if 100 expr"; EXPECT_EQ(c, -1);
303 c = 1; VLOG_IF(0, c -= 2) << "vlog_if 0 expr"; EXPECT_EQ(c, -1);
304 c = 1; LOG_IF(INFO, c -= 2) << "log_if info expr"; EXPECT_EQ(c, -1);
305 c = 1; LOG_IF(ERROR, c -= 2) << "log_if error expr"; EXPECT_EQ(c, -1);
306 c = 2; VLOG_IF(0, c -= 2) << "don't vlog_if 0 expr"; EXPECT_EQ(c, 0);
307 c = 2; LOG_IF(ERROR, c -= 2) << "don't log_if error expr"; EXPECT_EQ(c, 0);
309 c = 3; LOG_IF_EVERY_N(INFO, c -= 4, 1) << "log_if info every 1 expr";
311 c = 3; LOG_IF_EVERY_N(ERROR, c -= 4, 1) << "log_if error every 1 expr";
313 c = 4; LOG_IF_EVERY_N(ERROR, c -= 4, 3) << "don't log_if info every 3 expr";
315 c = 4; LOG_IF_EVERY_N(ERROR, c -= 4, 3) << "don't log_if error every 3 expr";
317 c = 5; VLOG_IF_EVERY_N(0, c -= 4, 1) << "vlog_if 0 every 1 expr";
319 c = 5; VLOG_IF_EVERY_N(100, c -= 4, 3) << "vlog_if 100 every 3 expr";
321 c = 6; VLOG_IF_EVERY_N(0, c -= 6, 1) << "don't vlog_if 0 every 1 expr";
323 c = 6; VLOG_IF_EVERY_N(100, c -= 6, 3) << "don't vlog_if 100 every 1 expr";
327 void TestLoggingLevels() {
328 LogWithLevels(0, INFO, false, false);
329 LogWithLevels(1, INFO, false, false);
330 LogWithLevels(-1, INFO, false, false);
331 LogWithLevels(0, WARNING, false, false);
332 LogWithLevels(0, ERROR, false, false);
333 LogWithLevels(0, FATAL, false, false);
334 LogWithLevels(0, FATAL, true, false);
335 LogWithLevels(0, FATAL, false, true);
336 LogWithLevels(1, WARNING, false, false);
337 LogWithLevels(1, FATAL, false, true);
340 TEST(DeathRawCHECK, logging) {
341 ASSERT_DEATH(RAW_CHECK(false, "failure 1"),
342 "RAW: Check false failed: failure 1");
343 ASSERT_DEBUG_DEATH(RAW_DCHECK(1 == 2, "failure 2"),
344 "RAW: Check 1 == 2 failed: failure 2");
347 void TestLogString() {
348 vector<string> errors;
349 vector<string> *no_errors = NULL;
351 LOG_STRING(INFO, &errors) << "LOG_STRING: " << "collected info";
352 LOG_STRING(WARNING, &errors) << "LOG_STRING: " << "collected warning";
353 LOG_STRING(ERROR, &errors) << "LOG_STRING: " << "collected error";
355 LOG_STRING(INFO, no_errors) << "LOG_STRING: " << "reported info";
356 LOG_STRING(WARNING, no_errors) << "LOG_STRING: " << "reported warning";
357 LOG_STRING(ERROR, NULL) << "LOG_STRING: " << "reported error";
359 for (int i = 0; i < errors.size(); ++i) {
360 LOG(INFO) << "Captured by LOG_STRING: " << errors[i];
364 class TestLogSinkImpl : public LogSink {
366 vector<string> errors;
367 virtual void send(LogSeverity severity, const char* full_filename,
368 const char* base_filename, int line,
369 const struct tm* tm_time,
370 const char* message, size_t message_len) {
372 ToString(severity, base_filename, line, tm_time, message, message_len));
377 TestLogSinkImpl sink;
378 LogSink *no_sink = NULL;
380 LOG_TO_SINK(&sink, INFO) << "LOG_TO_SINK: " << "collected info";
381 LOG_TO_SINK(&sink, WARNING) << "LOG_TO_SINK: " << "collected warning";
382 LOG_TO_SINK(&sink, ERROR) << "LOG_TO_SINK: " << "collected error";
384 LOG_TO_SINK(no_sink, INFO) << "LOG_TO_SINK: " << "reported info";
385 LOG_TO_SINK(no_sink, WARNING) << "LOG_TO_SINK: " << "reported warning";
386 LOG_TO_SINK(NULL, ERROR) << "LOG_TO_SINK: " << "reported error";
388 LOG(INFO) << "Captured by LOG_TO_SINK:";
389 for (int i = 0; i < sink.errors.size(); ++i) {
390 LogMessage("foo", LogMessage::kNoLogPrefix, INFO).stream()
395 // For testing using CHECK*() on anonymous enums.
402 // Tests using CHECK*() on int values.
413 // Tests using CHECK*() on anonymous enums.
414 // Apple's GCC doesn't like this.
415 #if !defined(OS_MACOSX)
416 CHECK_EQ(CASE_A, CASE_A);
417 CHECK_NE(CASE_A, CASE_B);
418 CHECK_GE(CASE_A, CASE_A);
419 CHECK_GE(CASE_B, CASE_A);
420 CHECK_LE(CASE_A, CASE_A);
421 CHECK_LE(CASE_A, CASE_B);
422 CHECK_GT(CASE_B, CASE_A);
423 CHECK_LT(CASE_A, CASE_B);
429 DCHECK( 1 == 2 ) << " DCHECK's shouldn't be compiled in normal mode";
443 CHECK_STREQ("this", "this");
444 CHECK_STREQ(NULL, NULL);
445 CHECK_STRCASEEQ("this", "tHiS");
446 CHECK_STRCASEEQ(NULL, NULL);
447 CHECK_STRNE("this", "tHiS");
448 CHECK_STRNE("this", NULL);
449 CHECK_STRCASENE("this", "that");
450 CHECK_STRCASENE(NULL, "that");
451 CHECK_STREQ((string("a")+"b").c_str(), "ab");
452 CHECK_STREQ(string("test").c_str(),
453 (string("te") + string("st")).c_str());
456 TEST(DeathSTREQ, logging) {
457 ASSERT_DEATH(CHECK_STREQ(NULL, "this"), "");
458 ASSERT_DEATH(CHECK_STREQ("this", "siht"), "");
459 ASSERT_DEATH(CHECK_STRCASEEQ(NULL, "siht"), "");
460 ASSERT_DEATH(CHECK_STRCASEEQ("this", "siht"), "");
461 ASSERT_DEATH(CHECK_STRNE(NULL, NULL), "");
462 ASSERT_DEATH(CHECK_STRNE("this", "this"), "");
463 ASSERT_DEATH(CHECK_STREQ((string("a")+"b").c_str(), "abc"), "");
466 TEST(CheckNOTNULL, Simple) {
468 void *ptr = static_cast<void *>(&t);
469 void *ref = CHECK_NOTNULL(ptr);
471 CHECK_NOTNULL(reinterpret_cast<char *>(&t));
472 CHECK_NOTNULL(reinterpret_cast<unsigned char *>(&t));
473 CHECK_NOTNULL(reinterpret_cast<int *>(&t));
474 CHECK_NOTNULL(reinterpret_cast<int64 *>(&t));
477 TEST(DeathCheckNN, Simple) {
478 ASSERT_DEATH(CHECK_NOTNULL(static_cast<void *>(NULL)), "");
481 // Get list of file names that match pattern
482 static void GetFiles(const string& pattern, vector<string>* files) {
485 const int r = glob(pattern.c_str(), 0, NULL, &g);
486 CHECK((r == 0) || (r == GLOB_NOMATCH)) << ": error matching " << pattern;
487 for (int i = 0; i < g.gl_pathc; i++) {
488 files->push_back(string(g.gl_pathv[i]));
493 // Delete files patching pattern
494 static void DeleteFiles(const string& pattern) {
495 vector<string> files;
496 GetFiles(pattern, &files);
497 for (int i = 0; i < files.size(); i++) {
498 CHECK(unlink(files[i].c_str()) == 0) << ": " << strerror(errno);
502 static void CheckFile(const string& name, const string& expected_string) {
503 vector<string> files;
504 GetFiles(name + "*", &files);
505 CHECK_EQ(files.size(), 1);
507 FILE* file = fopen(files[0].c_str(), "r");
508 CHECK(file != NULL) << ": could not open " << files[0];
510 while (fgets(buf, sizeof(buf), file) != NULL) {
511 if (strstr(buf, expected_string.c_str()) != NULL) {
517 LOG(FATAL) << "Did not find " << expected_string << " in " << files[0];
520 static void TestBasename() {
521 fprintf(stderr, "==== Test setting log file basename\n");
522 string dest = FLAGS_test_tmpdir + "/logging_test_basename";
523 DeleteFiles(dest + "*");
525 SetLogDestination(INFO, dest.c_str());
526 LOG(INFO) << "message to new base";
528 CheckFile(dest, "message to new base");
530 DeleteFiles(dest + "*");
533 static void TestSymlink() {
534 fprintf(stderr, "==== Test setting log file symlink\n");
535 string dest = FLAGS_test_tmpdir + "/logging_test_symlink";
536 string sym = FLAGS_test_tmpdir + "/symlinkbase";
537 DeleteFiles(dest + "*");
538 DeleteFiles(sym + "*");
540 SetLogSymlink(INFO, "symlinkbase");
541 SetLogDestination(INFO, dest.c_str());
542 LOG(INFO) << "message to new symlink";
544 CheckFile(sym, "message to new symlink");
546 DeleteFiles(dest + "*");
547 DeleteFiles(sym + "*");
550 static void TestExtension() {
551 fprintf(stderr, "==== Test setting log file extension\n");
552 string dest = FLAGS_test_tmpdir + "/logging_test_extension";
553 DeleteFiles(dest + "*");
555 SetLogDestination(INFO, dest.c_str());
556 SetLogFilenameExtension("specialextension");
557 LOG(INFO) << "message to new extension";
559 CheckFile(dest, "message to new extension");
561 // Check that file name ends with extension
562 vector<string> filenames;
563 GetFiles(dest + "*", &filenames);
564 CHECK_EQ(filenames.size(), 1);
565 CHECK(strstr(filenames[0].c_str(), "specialextension") != NULL);
567 DeleteFiles(dest + "*");
570 struct MyLogger : public base::Logger {
573 virtual void Write(bool should_flush,
577 data.append(message, length);
580 virtual void Flush() { }
582 virtual uint32 LogSize() { return data.length(); }
585 static void TestWrapper() {
586 fprintf(stderr, "==== Test log wrapper\n");
589 base::Logger* old_logger = base::GetLogger(INFO);
590 base::SetLogger(INFO, &my_logger);
591 LOG(INFO) << "Send to wrapped logger";
593 base::SetLogger(INFO, old_logger);
595 CHECK(strstr(my_logger.data.c_str(), "Send to wrapped logger") != NULL);
598 static void TestErrno() {
599 fprintf(stderr, "==== Test errno preservation\n");
603 CHECK_EQ(errno, ENOENT);
606 static void TestOneTruncate(const char *path, int64 limit, int64 keep,
607 int64 dsize, int64 ksize, int64 expect) {
609 CHECK_ERR(fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600));
611 const char *discardstr = "DISCARDME!", *keepstr = "KEEPME!";
613 // Fill the file with the requested data; first discard data, then kept data
615 while (written < dsize) {
616 int bytes = min<int64>(dsize - written, strlen(discardstr));
617 CHECK_ERR(write(fd, discardstr, bytes));
621 while (written < ksize) {
622 int bytes = min<int64>(ksize - written, strlen(keepstr));
623 CHECK_ERR(write(fd, keepstr, bytes));
627 TruncateLogFile(path, limit, keep);
629 // File should now be shorter
631 CHECK_ERR(fstat(fd, &statbuf));
632 CHECK_EQ(statbuf.st_size, expect);
633 CHECK_ERR(lseek(fd, 0, SEEK_SET));
635 // File should contain the suffix of the original file
636 char buf[statbuf.st_size + 1];
637 memset(buf, 0, sizeof(buf));
638 CHECK_ERR(read(fd, buf, sizeof(buf)));
642 while (checked < expect) {
643 int bytes = min<int64>(expect - checked, strlen(keepstr));
644 CHECK(!memcmp(p, keepstr, bytes));
650 static void TestTruncate() {
651 fprintf(stderr, "==== Test log truncation\n");
652 string path = FLAGS_test_tmpdir + "/truncatefile";
654 // Test on a small file
655 TestOneTruncate(path.c_str(), 10, 10, 10, 10, 10);
657 // And a big file (multiple blocks to copy)
658 TestOneTruncate(path.c_str(), 2<<20, 4<<10, 3<<20, 4<<10, 4<<10);
660 // Check edge-case limits
661 TestOneTruncate(path.c_str(), 10, 20, 0, 20, 20);
662 TestOneTruncate(path.c_str(), 10, 0, 0, 0, 0);
663 TestOneTruncate(path.c_str(), 10, 50, 0, 10, 10);
664 TestOneTruncate(path.c_str(), 50, 100, 0, 30, 30);
666 // MacOSX 10.4 doesn't fail in this case. Let's just ignore this test.
667 #if !defined(OS_MACOSX)
668 // Through a symlink should fail to truncate
669 string linkname = path + ".link";
670 unlink(linkname.c_str());
671 CHECK_ERR(symlink(path.c_str(), linkname.c_str()));
672 TestOneTruncate(linkname.c_str(), 10, 10, 0, 30, 30);
675 // The /proc/self path makes sense only for linux.
676 #if defined(OS_LINUX)
677 // Through an open fd symlink should work
679 CHECK_ERR(fd = open(path.c_str(), O_APPEND | O_WRONLY));
681 snprintf(fdpath, sizeof(fdpath), "/proc/self/fd/%d", fd);
682 TestOneTruncate(fdpath, 10, 10, 10, 10, 10);
686 _START_GOOGLE_NAMESPACE_
687 extern // in logging.cc
688 bool SafeFNMatch_(const char* pattern, size_t patt_len,
689 const char* str, size_t str_len);
690 _END_GOOGLE_NAMESPACE_
692 static bool WrapSafeFNMatch(string pattern, string str) {
695 return SafeFNMatch_(pattern.data(), pattern.size() - 3,
696 str.data(), str.size() - 5);
699 TEST(SafeFNMatch, logging) {
700 CHECK(WrapSafeFNMatch("foo", "foo"));
701 CHECK(!WrapSafeFNMatch("foo", "bar"));
702 CHECK(!WrapSafeFNMatch("foo", "fo"));
703 CHECK(!WrapSafeFNMatch("foo", "foo2"));
704 CHECK(WrapSafeFNMatch("bar/foo.ext", "bar/foo.ext"));
705 CHECK(WrapSafeFNMatch("*ba*r/fo*o.ext*", "bar/foo.ext"));
706 CHECK(!WrapSafeFNMatch("bar/foo.ext", "bar/baz.ext"));
707 CHECK(!WrapSafeFNMatch("bar/foo.ext", "bar/foo"));
708 CHECK(!WrapSafeFNMatch("bar/foo.ext", "bar/foo.ext.zip"));
709 CHECK(WrapSafeFNMatch("ba?/*.ext", "bar/foo.ext"));
710 CHECK(WrapSafeFNMatch("ba?/*.ext", "baZ/FOO.ext"));
711 CHECK(!WrapSafeFNMatch("ba?/*.ext", "barr/foo.ext"));
712 CHECK(!WrapSafeFNMatch("ba?/*.ext", "bar/foo.ext2"));
713 CHECK(WrapSafeFNMatch("ba?/*", "bar/foo.ext2"));
714 CHECK(WrapSafeFNMatch("ba?/*", "bar/"));
715 CHECK(!WrapSafeFNMatch("ba?/?", "bar/"));
716 CHECK(!WrapSafeFNMatch("ba?/*", "bar"));
719 // TestWaitingLogSink will save messages here
720 // No lock: Accessed only by TestLogSinkWriter thread
721 // and after its demise by its creator.
722 static vector<string> global_messages;
724 // helper for TestWaitingLogSink below.
725 // Thread that does the logic of TestWaitingLogSink
726 // It's free to use LOG() itself.
727 class TestLogSinkWriter : public Thread {
730 TestLogSinkWriter() : should_exit_(false) {
735 // Just buffer it (can't use LOG() here).
736 void Buffer(const string& message) {
738 RAW_LOG(INFO, "Buffering");
739 messages_.push(message);
741 RAW_LOG(INFO, "Buffered");
744 // Wait for the buffer to clear (can't use LOG() here).
746 RAW_LOG(INFO, "Waiting");
750 SleepForMilliseconds(1);
753 RAW_LOG(INFO, "Waited");
757 // Trigger thread exit.
759 MutexLock l(&mutex_);
765 // helpers ---------------
767 // For creating a "Condition".
768 bool NoWork() { return messages_.empty(); }
769 bool HaveWork() { return !messages_.empty() || should_exit_; }
771 // Thread body; CAN use LOG() here!
775 while (!HaveWork()) {
777 SleepForMilliseconds(1);
780 if (should_exit_ && messages_.empty()) {
784 // Give the main thread time to log its message,
785 // so that we get a reliable log capture to compare to golden file.
786 // Same for the other sleep below.
787 SleepForMilliseconds(20);
788 RAW_LOG(INFO, "Sink got a messages"); // only RAW_LOG under mutex_ here
789 string message = messages_.front();
791 // Normally this would be some more real/involved logging logic
792 // where LOG() usage can't be eliminated,
793 // e.g. pushing the message over with an RPC:
794 int messages_left = messages_.size();
796 SleepForMilliseconds(20);
797 // May not use LOG while holding mutex_, because Buffer()
798 // acquires mutex_, and Buffer is called from LOG(),
799 // which has its own internal mutex:
800 // LOG()->LogToSinks()->TestWaitingLogSink::send()->Buffer()
801 LOG(INFO) << "Sink is sending out a message: " << message;
802 LOG(INFO) << "Have " << messages_left << " left";
803 global_messages.push_back(message);
807 // data ---------------
811 queue<string> messages_; // messages to be logged
814 // A log sink that exercises WaitTillSent:
815 // it pushes data to a buffer and wakes up another thread to do the logging
816 // (that other thread can than use LOG() itself),
817 class TestWaitingLogSink : public LogSink {
820 TestWaitingLogSink() {
821 tid_ = pthread_self(); // for thread-specific behavior
824 ~TestWaitingLogSink() {
830 // (re)define LogSink interface
832 virtual void send(LogSeverity severity, const char* full_filename,
833 const char* base_filename, int line,
834 const struct tm* tm_time,
835 const char* message, size_t message_len) {
836 // Push it to Writer thread if we are the original logging thread.
837 // Note: Something like ThreadLocalLogSink is a better choice
838 // to do thread-specific LogSink logic for real.
839 if (pthread_equal(tid_, pthread_self())) {
840 writer_.Buffer(ToString(severity, base_filename, line,
841 tm_time, message, message_len));
844 virtual void WaitTillSent() {
845 // Wait for Writer thread if we are the original logging thread.
846 if (pthread_equal(tid_, pthread_self())) writer_.Wait();
852 TestLogSinkWriter writer_;
855 // Check that LogSink::WaitTillSent can be used in the advertised way.
856 // We also do golden-stderr comparison.
857 static void TestLogSinkWaitTillSent() {
858 { TestWaitingLogSink sink;
859 // Sleeps give the sink threads time to do all their work,
860 // so that we get a reliable log capture to compare to the golden file.
861 LOG(INFO) << "Message 1";
862 SleepForMilliseconds(60);
863 LOG(ERROR) << "Message 2";
864 SleepForMilliseconds(60);
865 LOG(WARNING) << "Message 3";
866 SleepForMilliseconds(60);
868 for (int i = 0; i < global_messages.size(); ++i) {
869 LOG(INFO) << "Sink capture: " << global_messages[i];
871 CHECK_EQ(global_messages.size(), 3);
874 TEST(Strerror, logging) {
876 char *msg = strdup(strerror(errcode));
877 char buf[strlen(msg) + 1];
878 CHECK_EQ(posix_strerror_r(errcode, NULL, 0), -1);
880 CHECK_EQ(posix_strerror_r(errcode, buf, 0), -1);
881 CHECK_EQ(buf[0], 'A');
882 CHECK_EQ(posix_strerror_r(errcode, NULL, sizeof(buf)), -1);
883 #if defined(OS_MACOSX) || defined(OS_FREEBSD)
884 // MacOSX or FreeBSD considers this case is an error since there is
886 CHECK_EQ(posix_strerror_r(errcode, buf, 1), -1);
888 CHECK_EQ(posix_strerror_r(errcode, buf, 1), 0);
890 CHECK_STREQ(buf, "");
891 CHECK_EQ(posix_strerror_r(errcode, buf, sizeof(buf)), 0);
892 CHECK_STREQ(buf, msg);
896 // Simple routines to look at the sizes of generated code for LOG(FATAL) and
897 // CHECK(..) via objdump
899 LOG(FATAL) << "Failed";
901 void MyCheck(bool a, bool b) {
905 struct UserDefinedClass {
906 bool operator==(const UserDefinedClass& rhs) const { return true; }
909 inline ostream& operator<<(ostream& out, const UserDefinedClass& u) {
914 TEST(UserDefinedClass, logging) {
917 LOG_STRING(INFO, &buf) << u;
918 CHECK_EQ(1, buf.size());
919 CHECK(buf[0].find("OK") != string::npos);
921 // We must be able to compile this.