42 #ifdef HAVE_SYS_WAIT_H
43 # include <sys/wait.h>
59 #include <glog/logging.h>
60 #include <glog/raw_logging.h>
65 #ifdef HAVE_LIB_GFLAGS
66 #include <gflags/gflags.h>
67 using namespace GFLAGS_NAMESPACE;
76 using testing::HasSubstr;
81 using GOOGLE_NAMESPACE::glog_testing::ScopedMockLog;
95 _END_GOOGLE_NAMESPACE_
133 static void CheckFailure(
int a,
int b,
const char* file,
int line,
const char* msg);
178 VLOG(1) <<
"test message";
187 if (
data ==
NULL || *
static_cast<string*
>(
data) !=
"good data") {
192 << setw(4) << 1900 + l.time.year()
193 << setw(2) << 1 + l.time.month()
194 << setw(2) << l.time.day()
196 << setw(2) << l.time.hour() <<
':'
197 << setw(2) << l.time.min() <<
':'
198 << setw(2) << l.time.sec() <<
"."
199 << setw(6) << l.time.usec()
201 << setfill(
' ') << setw(5)
202 << l.thread_id << setfill(
'0')
204 << l.filename <<
':' << l.line_number <<
"]";
207 int main(
int argc,
char **argv) {
208 FLAGS_colorlogtostderr =
false;
209 FLAGS_timestamp_in_logfile_name =
true;
213 setbuf(stderr,
NULL);
226 string prefix_attacher_data =
"good data";
236 #ifdef HAVE_LIB_GMOCK
240 #ifdef HAVE_LIB_GFLAGS
279 fprintf(
stdout,
"PASS\n");
289 for (
int i = 0;
i < 10; ++
i ) {
290 int old_errno =
errno;
292 PLOG_EVERY_N(
ERROR, 2) <<
"Plog every 2, iteration " << COUNTER;
295 LOG_EVERY_N(
ERROR, 3) <<
"Log every 3, iteration " << COUNTER << endl;
296 LOG_EVERY_N(
ERROR, 4) <<
"Log every 4, iteration " << COUNTER << endl;
298 LOG_IF_EVERY_N(
WARNING,
true, 5) <<
"Log if every 5, iteration " << COUNTER;
299 LOG_IF_EVERY_N(
WARNING,
false, 3)
300 <<
"Log if every 3, iteration " << COUNTER;
301 LOG_IF_EVERY_N(
INFO,
true, 1) <<
"Log if every 1, iteration " << COUNTER;
302 LOG_IF_EVERY_N(
ERROR, (
i < 3), 2)
303 <<
"Log if less than 3 every 2, iteration " << COUNTER;
305 LOG_IF(
WARNING,
true) <<
"log_if this";
306 LOG_IF(
WARNING,
false) <<
"don't log_if this";
310 const char const_s[] =
"const array";
313 LOG(
ERROR) <<
string(
"foo") <<
' '<< j <<
' ' << setw(10) << j <<
" "
314 << setw(1) << hex << j;
315 LOG(
INFO) <<
"foo " << std::setw(10) << 1.0;
318 google::LogMessage outer(__FILE__, __LINE__,
GLOG_ERROR);
319 outer.stream() <<
"outer";
346 TEST(DeathNoAllocNewHook, logging) {
351 },
"unexpected new");
356 string huge_str(50000,
'a');
363 RAW_LOG(
INFO,
"%s%s%d%c%f",
foo->c_str(),
"bar ", 10,
' ', 3.4);
366 const char const_s[] =
"const array";
367 RAW_LOG(
INFO,
"%s", const_s);
369 RAW_LOG(
INFO,
"ptr %p",
p);
371 RAW_LOG(
INFO,
"ptr %p",
p);
373 RAW_LOG(
ERROR,
"%s%d%c%010d%s%1x",
foo->c_str(), j,
' ', j,
" ", j);
374 RAW_VLOG(0,
"foo %d", j);
377 RAW_LOG(
INFO,
"foo %d", j);
379 RAW_DLOG(
INFO,
"foo %d", j);
383 RAW_LOG(
WARNING,
"Huge string: %s", huge_str.c_str());
384 RAW_VLOG(0,
"Huge string: %s", huge_str.c_str());
387 RAW_LOG(
INFO,
"log");
388 RAW_VLOG(0,
"vlog 0 on");
389 RAW_VLOG(1,
"vlog 1 off");
390 RAW_VLOG(2,
"vlog 2 off");
391 RAW_VLOG(3,
"vlog 3 off");
393 RAW_LOG(
INFO,
"log");
394 RAW_VLOG(1,
"vlog 1 on");
395 RAW_VLOG(2,
"vlog 2 on");
396 RAW_VLOG(3,
"vlog 3 off");
399 RAW_DCHECK(1 == 2,
" RAW_DCHECK's shouldn't be compiled in normal mode");
402 RAW_CHECK(1 == 1,
"should be ok");
403 RAW_DCHECK(
true,
"should be ok");
410 "Test: v=%d stderrthreshold=%d logtostderr=%d alsologtostderr=%d",
420 RAW_VLOG(-1,
"vlog -1");
421 RAW_VLOG(0,
"vlog 0");
422 RAW_VLOG(1,
"vlog 1");
423 RAW_LOG(
INFO,
"log info");
424 RAW_LOG(
WARNING,
"log warning");
425 RAW_LOG(
ERROR,
"log error");
427 VLOG(-1) <<
"vlog -1";
434 VLOG_IF(-1,
true) <<
"vlog_if -1";
435 VLOG_IF(-1,
false) <<
"don't vlog_if -1";
436 VLOG_IF(0,
true) <<
"vlog_if 0";
437 VLOG_IF(0,
false) <<
"don't vlog_if 0";
438 VLOG_IF(1,
true) <<
"vlog_if 1";
439 VLOG_IF(1,
false) <<
"don't vlog_if 1";
440 LOG_IF(
INFO,
true) <<
"log_if info";
441 LOG_IF(
INFO,
false) <<
"don't log_if info";
442 LOG_IF(
WARNING,
true) <<
"log_if warning";
443 LOG_IF(
WARNING,
false) <<
"don't log_if warning";
444 LOG_IF(
ERROR,
true) <<
"log_if error";
445 LOG_IF(
ERROR,
false) <<
"don't log_if error";
448 c = 1; VLOG_IF(100, c -= 2) <<
"vlog_if 100 expr";
EXPECT_EQ(c, -1);
449 c = 1; VLOG_IF(0, c -= 2) <<
"vlog_if 0 expr";
EXPECT_EQ(c, -1);
450 c = 1; LOG_IF(
INFO, c -= 2) <<
"log_if info expr";
EXPECT_EQ(c, -1);
451 c = 1; LOG_IF(
ERROR, c -= 2) <<
"log_if error expr";
EXPECT_EQ(c, -1);
452 c = 2; VLOG_IF(0, c -= 2) <<
"don't vlog_if 0 expr";
EXPECT_EQ(c, 0);
453 c = 2; LOG_IF(
ERROR, c -= 2) <<
"don't log_if error expr";
EXPECT_EQ(c, 0);
455 c = 3; LOG_IF_EVERY_N(
INFO, c -= 4, 1) <<
"log_if info every 1 expr";
457 c = 3; LOG_IF_EVERY_N(
ERROR, c -= 4, 1) <<
"log_if error every 1 expr";
459 c = 4; LOG_IF_EVERY_N(
ERROR, c -= 4, 3) <<
"don't log_if info every 3 expr";
461 c = 4; LOG_IF_EVERY_N(
ERROR, c -= 4, 3) <<
"don't log_if error every 3 expr";
463 c = 5; VLOG_IF_EVERY_N(0, c -= 4, 1) <<
"vlog_if 0 every 1 expr";
465 c = 5; VLOG_IF_EVERY_N(100, c -= 4, 3) <<
"vlog_if 100 every 3 expr";
467 c = 6; VLOG_IF_EVERY_N(0, c -= 6, 1) <<
"don't vlog_if 0 every 1 expr";
469 c = 6; VLOG_IF_EVERY_N(100, c -= 6, 3) <<
"don't vlog_if 100 every 1 expr";
488 "RAW: Check false failed: failure 1");
490 "RAW: Check 1 == 2 failed: failure 2");
495 vector<string> *no_errors =
NULL;
505 for (
size_t i = 0;
i <
errors.size(); ++
i) {
512 string* no_error =
NULL;
514 LOG_TO_STRING(
INFO, &
error) <<
"LOG_TO_STRING: " <<
"collected info";
516 LOG_TO_STRING(
WARNING, &
error) <<
"LOG_TO_STRING: " <<
"collected warning";
518 LOG_TO_STRING(
ERROR, &
error) <<
"LOG_TO_STRING: " <<
"collected error";
521 LOG_TO_STRING(
INFO, no_error) <<
"LOG_TO_STRING: " <<
"reported info";
522 LOG_TO_STRING(
WARNING, no_error) <<
"LOG_TO_STRING: " <<
"reported warning";
523 LOG_TO_STRING(
ERROR,
NULL) <<
"LOG_TO_STRING: " <<
"reported error";
530 const char* base_filename,
int line,
531 const LogMessageTime &logmsgtime,
532 const char*
message,
size_t message_len) {
534 ToString(
severity, base_filename, line, logmsgtime,
message, message_len));
540 LogSink *no_sink =
NULL;
542 LOG_TO_SINK(&sink,
INFO) <<
"LOG_TO_SINK: " <<
"collected info";
543 LOG_TO_SINK(&sink,
WARNING) <<
"LOG_TO_SINK: " <<
"collected warning";
544 LOG_TO_SINK(&sink,
ERROR) <<
"LOG_TO_SINK: " <<
"collected error";
546 LOG_TO_SINK(no_sink,
INFO) <<
"LOG_TO_SINK: " <<
"reported info";
547 LOG_TO_SINK(no_sink,
WARNING) <<
"LOG_TO_SINK: " <<
"reported warning";
548 LOG_TO_SINK(
NULL,
ERROR) <<
"LOG_TO_SINK: " <<
"reported error";
550 LOG_TO_SINK_BUT_NOT_TO_LOGFILE(&sink,
INFO)
551 <<
"LOG_TO_SINK_BUT_NOT_TO_LOGFILE: " <<
"collected info";
552 LOG_TO_SINK_BUT_NOT_TO_LOGFILE(&sink,
WARNING)
553 <<
"LOG_TO_SINK_BUT_NOT_TO_LOGFILE: " <<
"collected warning";
554 LOG_TO_SINK_BUT_NOT_TO_LOGFILE(&sink,
ERROR)
555 <<
"LOG_TO_SINK_BUT_NOT_TO_LOGFILE: " <<
"collected error";
557 LOG_TO_SINK_BUT_NOT_TO_LOGFILE(no_sink,
INFO)
558 <<
"LOG_TO_SINK_BUT_NOT_TO_LOGFILE: " <<
"thrashed info";
559 LOG_TO_SINK_BUT_NOT_TO_LOGFILE(no_sink,
WARNING)
560 <<
"LOG_TO_SINK_BUT_NOT_TO_LOGFILE: " <<
"thrashed warning";
561 LOG_TO_SINK_BUT_NOT_TO_LOGFILE(
NULL,
ERROR)
562 <<
"LOG_TO_SINK_BUT_NOT_TO_LOGFILE: " <<
"thrashed error";
564 LOG(
INFO) <<
"Captured by LOG_TO_SINK:";
565 for (
size_t i = 0;
i < sink.
errors.size(); ++
i) {
591 #if !defined(GLOG_OS_MACOSX)
605 DCHECK( 1 == 2 ) <<
" DCHECK's shouldn't be compiled in normal mode";
618 int64* ptr = DCHECK_NOTNULL(orig_ptr);
624 CHECK_STREQ(
"this",
"this");
626 CHECK_STRCASEEQ(
"this",
"tHiS");
628 CHECK_STRNE(
"this",
"tHiS");
629 CHECK_STRNE(
"this",
NULL);
630 CHECK_STRCASENE(
"this",
"that");
631 CHECK_STRCASENE(
NULL,
"that");
632 CHECK_STREQ((
string(
"a")+
"b").c_str(),
"ab");
633 CHECK_STREQ(
string(
"test").c_str(),
634 (
string(
"te") +
string(
"st")).c_str());
644 ASSERT_DEATH(CHECK_STREQ((
string(
"a")+
"b").c_str(),
"abc"),
"");
649 void *ptr =
static_cast<void *
>(&t);
650 void *
ref = CHECK_NOTNULL(ptr);
652 CHECK_NOTNULL(
reinterpret_cast<char *
>(ptr));
653 CHECK_NOTNULL(
reinterpret_cast<unsigned char *
>(ptr));
654 CHECK_NOTNULL(
reinterpret_cast<int *
>(ptr));
655 CHECK_NOTNULL(
reinterpret_cast<int64 *
>(ptr));
663 static void GetFiles(
const string& pattern, vector<string>* files) {
665 #if defined(HAVE_GLOB_H)
667 const int r = glob(pattern.c_str(), 0,
NULL, &
g);
668 CHECK((
r == 0) || (
r == GLOB_NOMATCH)) <<
": error matching " << pattern;
669 for (
size_t i = 0;
i <
g.gl_pathc;
i++) {
670 files->push_back(
string(
g.gl_pathv[
i]));
673 #elif defined(GLOG_OS_WINDOWS)
674 WIN32_FIND_DATAA
data;
675 HANDLE handle = FindFirstFileA(pattern.c_str(), &
data);
676 size_t index = pattern.rfind(
'\\');
677 if (
index == string::npos) {
678 LOG(
FATAL) <<
"No directory separator.";
680 const string dirname = pattern.substr(0,
index + 1);
681 if (handle == INVALID_HANDLE_VALUE) {
686 files->push_back(dirname +
data.cFileName);
687 }
while (FindNextFileA(handle, &
data));
688 BOOL result = FindClose(handle);
689 LOG_SYSRESULT(result != 0);
691 # error There is no way to do glob.
697 vector<string> files;
699 for (
size_t i = 0;
i < files.size();
i++) {
705 static void CheckFile(
const string&
name,
const string& expected_string,
const bool checkInFileOrNot =
true) {
706 vector<string> files;
710 FILE* file = fopen(files[0].c_str(),
"r");
711 CHECK(file !=
NULL) <<
": could not open " << files[0];
713 while (fgets(
buf,
sizeof(
buf), file) !=
NULL) {
714 char*
first = strstr(
buf, expected_string.c_str());
717 if (checkInFileOrNot != (
first ==
NULL)) {
723 LOG(
FATAL) <<
"Did " << (checkInFileOrNot?
"not " :
"") <<
"find " << expected_string <<
" in " << files[0];
727 fprintf(stderr,
"==== Test setting log file basename\n");
728 const string dest = FLAGS_test_tmpdir +
"/logging_test_basename";
732 LOG(
INFO) <<
"message to new base";
743 fprintf(stderr,
"==== Test setting log file basename without timestamp and appending properly\n");
744 const string dest = FLAGS_test_tmpdir +
"/logging_test_basename_append_when_no_timestamp";
747 ofstream out(
dest.c_str());
748 out <<
"test preexisting content" << endl;
753 FLAGS_timestamp_in_logfile_name=
false;
755 LOG(
INFO) <<
"message to new base, appending to preexisting file";
757 FLAGS_timestamp_in_logfile_name=
true;
761 CheckFile(
dest,
"message to new base, appending to preexisting file");
770 #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_UNISTD_H) && defined(HAVE_FCNTL)
771 fprintf(stderr,
"==== Test setting log file basename and two processes writing - second should fail\n");
772 const string dest = FLAGS_test_tmpdir +
"/logging_test_basename_two_processes_writing";
776 FLAGS_timestamp_in_logfile_name=
false;
778 LOG(
INFO) <<
"message to new base, parent";
784 LOG(
INFO) <<
"message to new base, child - should only appear on STDERR not on the file";
787 }
else if (pid > 0) {
790 FLAGS_timestamp_in_logfile_name=
true;
793 CheckFile(
dest,
"message to new base, child - should only appear on STDERR not on the file",
false);
802 #ifndef GLOG_OS_WINDOWS
803 fprintf(stderr,
"==== Test setting log file symlink\n");
804 string dest = FLAGS_test_tmpdir +
"/logging_test_symlink";
805 string sym = FLAGS_test_tmpdir +
"/symlinkbase";
811 LOG(
INFO) <<
"message to new symlink";
813 CheckFile(sym,
"message to new symlink");
821 fprintf(stderr,
"==== Test setting log file extension\n");
822 string dest = FLAGS_test_tmpdir +
"/logging_test_extension";
827 LOG(
INFO) <<
"message to new extension";
832 vector<string> filenames;
835 CHECK(strstr(filenames[0].c_str(),
"specialextension") !=
NULL);
858 fprintf(stderr,
"==== Test log wrapper\n");
861 base::Logger* old_logger = base::GetLogger(
GLOG_INFO);
863 LOG(
INFO) <<
"Send to wrapped logger";
867 CHECK(strstr(my_logger.
data.c_str(),
"Send to wrapped logger") !=
NULL);
871 fprintf(stderr,
"==== Test errno preservation\n");
879 size_t dsize,
size_t ksize,
size_t expect) {
881 CHECK_ERR(fd = open(
path, O_RDWR | O_CREAT | O_TRUNC, 0600));
883 const char *discardstr =
"DISCARDME!", *keepstr =
"KEEPME!";
884 const size_t discard_size = strlen(discardstr), keep_size = strlen(keepstr);
888 while (written < dsize) {
889 size_t bytes = min(dsize - written, discard_size);
890 CHECK_ERR(write(fd, discardstr,
bytes));
894 while (written < ksize) {
895 size_t bytes = min(ksize - written, keep_size);
896 CHECK_ERR(write(fd, keepstr,
bytes));
904 CHECK_ERR(fstat(fd, &statbuf));
905 CHECK_EQ(
static_cast<size_t>(statbuf.st_size), expect);
906 CHECK_ERR(lseek(fd, 0, SEEK_SET));
909 const size_t buf_size =
static_cast<size_t>(statbuf.st_size) + 1;
910 char*
buf =
new char[buf_size];
911 memset(
buf, 0, buf_size);
912 CHECK_ERR(read(fd,
buf, buf_size));
916 while (checked < expect) {
917 size_t bytes = min(expect - checked, keep_size);
927 fprintf(stderr,
"==== Test log truncation\n");
928 string path = FLAGS_test_tmpdir +
"/truncatefilecustom";
946 #if !defined(GLOG_OS_MACOSX) && !defined(GLOG_OS_WINDOWS)
948 string linkname =
path +
".link";
949 unlink(linkname.c_str());
950 CHECK_ERR(symlink(
path.c_str(), linkname.c_str()));
955 #if defined(GLOG_OS_LINUX)
958 CHECK_ERR(fd = open(
path.c_str(), O_APPEND | O_WRONLY));
960 snprintf(fdpath,
sizeof(fdpath),
"/proc/self/fd/%d", fd);
969 base::Logger* wrapped_logger) :
970 set_on_destruction_(set_on_destruction),
971 wrapped_logger_(wrapped_logger)
973 *set_on_destruction_ =
false;
976 *set_on_destruction_ =
true;
978 virtual void Write(
bool force_flush,
982 wrapped_logger_->Write(force_flush, timestamp,
message,
length);
984 virtual void Flush() { wrapped_logger_->Flush(); }
992 bool custom_logger_deleted =
false;
1005 bool SafeFNMatch_(
const char* pattern,
size_t patt_len,
1006 const char*
str,
size_t str_len);
1009 _END_GOOGLE_NAMESPACE_
1014 return SafeFNMatch_(pattern.data(), pattern.size() - 3,
1015 str.data(),
str.size() - 5);
1057 RAW_LOG(
INFO,
"Buffering");
1060 RAW_LOG(
INFO,
"Buffered");
1065 RAW_LOG(
INFO,
"Waiting");
1072 RAW_LOG(
INFO,
"Waited");
1079 should_exit_ =
true;
1088 bool HaveWork() {
return !messages_.empty() || should_exit_; }
1094 while (!HaveWork()) {
1099 if (should_exit_ && messages_.empty()) {
1107 RAW_LOG(
INFO,
"Sink got a messages");
1108 string message = messages_.front();
1113 size_t messages_left = messages_.size();
1121 LOG(
INFO) <<
"Have " << messages_left <<
" left";
1140 tid_ = pthread_self();
1152 const char* base_filename,
int line,
1153 const LogMessageTime &logmsgtime,
1154 const char*
message,
size_t message_len) {
1158 if (pthread_equal(tid_, pthread_self())) {
1159 writer_.Buffer(ToString(
severity, base_filename, line,
1160 logmsgtime,
message, message_len));
1166 if (pthread_equal(tid_, pthread_self())) writer_.Wait();
1195 int errcode =
EINTR;
1196 char *msg = strdup(
strerror(errcode));
1197 const size_t buf_size = strlen(msg) + 1;
1198 char *
buf =
new char[buf_size];
1204 #if defined(GLOG_OS_MACOSX) || defined(GLOG_OS_FREEBSD) || defined(GLOG_OS_OPENBSD)
1211 CHECK_STREQ(
buf,
"");
1213 CHECK_STREQ(
buf, msg);
1229 #ifdef HAVE_LIB_GMOCK
1231 TEST(DVLog, Basic) {
1242 DVLOG(1) <<
"debug log";
1252 DVLOG(1) <<
"debug log";
1255 TEST(LogAtLevel, Basic) {
1263 LogAtLevel(
severity,
"function version");
1267 LOG_AT_LEVEL(
severity) <<
"macro" <<
' ' <<
"version";
1270 TEST(TestExitOnDFatal, ToBeOrNotToBe) {
1291 LOG(DFATAL) <<
"This should not be fatal";
1298 #ifdef GTEST_HAS_DEATH_TEST
1300 EXPECT_DEBUG_DEATH({
1301 LOG(DFATAL) <<
"This should be fatal in debug mode";
1302 },
"This should be fatal in debug mode");
1306 #ifdef HAVE_STACKTRACE
1308 static void BacktraceAtHelper() {
1314 static int kBacktraceAtLine = __LINE__ - 2;
1316 TEST(LogBacktraceAt, DoesNotBacktraceWhenDisabled) {
1317 StrictMock<ScopedMockLog> log;
1319 FLAGS_log_backtrace_at =
"";
1324 BacktraceAtHelper();
1327 TEST(LogBacktraceAt, DoesBacktraceAtRightLineWhenEnabled) {
1328 StrictMock<ScopedMockLog> log;
1332 FLAGS_log_backtrace_at = where;
1339 HasSubstr(
"BacktraceAtHelper"),
1341 HasSubstr(
"Backtrace me"))));
1345 BacktraceAtHelper();
1348 #endif // HAVE_STACKTRACE
1350 #endif // HAVE_LIB_GMOCK
1366 CHECK(
buf[0].find(
"OK") != string::npos);
1377 google::LogMessage log_obj(__FILE__, __LINE__);
1379 long int nGmtOff = log_obj.getLogMessageTime().gmtoff();
1381 const long utc_min_offset = -43200;
1382 const long utc_max_offset = 50400;
1383 EXPECT_TRUE( (nGmtOff >= utc_min_offset) && (nGmtOff <= utc_max_offset) );