fixed gcc logging failure
[platform/upstream/glog.git] / src / logging_unittest.cc
index cf41583..b8a9a87 100644 (file)
@@ -61,6 +61,7 @@ DECLARE_string(log_backtrace_at);  // logging.cc
 
 #ifdef HAVE_LIB_GFLAGS
 #include <gflags/gflags.h>
+using namespace GFLAGS_NAMESPACE;
 #endif
 
 #ifdef HAVE_LIB_GMOCK
@@ -155,7 +156,8 @@ static void BM_Check2(int n) {
 }
 BENCHMARK(BM_Check2);
 
-static void CheckFailure(int a, int b, const char* file, int line, const char* msg) {
+static void CheckFailure(int, int, const char* /* file */, int /* line */,
+                         const char* /* msg */) {
 }
 
 static void BM_logspeed(int n) {
@@ -173,9 +175,13 @@ static void BM_vlog(int n) {
 BENCHMARK(BM_vlog);
 
 int main(int argc, char **argv) {
+  FLAGS_colorlogtostderr = false;
 #ifdef HAVE_LIB_GFLAGS
   ParseCommandLineFlags(&argc, &argv, true);
 #endif
+  // Make sure stderr is not buffered as stderr seems to be buffered
+  // on recent windows.
+  setbuf(stderr, NULL);
 
   // Test some basics before InitGoogleLogging:
   CaptureTestStderr();
@@ -266,17 +272,24 @@ void TestLogging(bool check_counts) {
   LOG(ERROR) << string("foo") << ' '<< j << ' ' << setw(10) << j << " "
              << setw(1) << hex << j;
 
+  {
+    google::LogMessage outer(__FILE__, __LINE__, google::ERROR);
+    outer.stream() << "outer";
+
+    LOG(ERROR) << "inner";
+  }
+
   LogMessage("foo", LogMessage::kNoLogPrefix, GLOG_INFO).stream() << "no prefix";
 
   if (check_counts) {
     CHECK_EQ(base_num_infos   + 14, LogMessage::num_messages(GLOG_INFO));
     CHECK_EQ(base_num_warning + 3,  LogMessage::num_messages(GLOG_WARNING));
-    CHECK_EQ(base_num_errors  + 15, LogMessage::num_messages(GLOG_ERROR));
+    CHECK_EQ(base_num_errors  + 17, LogMessage::num_messages(GLOG_ERROR));
   }
 }
 
 static void NoAllocNewHook() {
-  CHECK(false) << "unexpected new";
+  LOG(FATAL) << "unexpected new";
 }
 
 struct NewHook {
@@ -471,7 +484,7 @@ void TestLogToString() {
 class TestLogSinkImpl : public LogSink {
  public:
   vector<string> errors;
-  virtual void send(LogSeverity severity, const char* full_filename,
+  virtual void send(LogSeverity severity, const char* /* full_filename */,
                     const char* base_filename, int line,
                     const struct tm* tm_time,
                     const char* message, size_t message_len) {
@@ -610,7 +623,7 @@ static void GetFiles(const string& pattern, vector<string>* files) {
   glob_t g;
   const int r = glob(pattern.c_str(), 0, NULL, &g);
   CHECK((r == 0) || (r == GLOB_NOMATCH)) << ": error matching " << pattern;
-  for (int i = 0; i < g.gl_pathc; i++) {
+  for (size_t i = 0; i < g.gl_pathc; i++) {
     files->push_back(string(g.gl_pathv[i]));
   }
   globfree(&g);
@@ -622,14 +635,15 @@ static void GetFiles(const string& pattern, vector<string>* files) {
     LOG(FATAL) << "No directory separator.";
   }
   const string dirname = pattern.substr(0, index + 1);
-  if (FAILED(handle)) {
+  if (handle == INVALID_HANDLE_VALUE) {
     // Finding no files is OK.
     return;
   }
   do {
     files->push_back(dirname + data.cFileName);
   } while (FindNextFileA(handle, &data));
-  LOG_SYSRESULT(FindClose(handle));
+  BOOL result = FindClose(handle);
+  LOG_SYSRESULT(result);
 #else
 # error There is no way to do glob.
 #endif
@@ -647,7 +661,7 @@ static void DeleteFiles(const string& pattern) {
 static void CheckFile(const string& name, const string& expected_string) {
   vector<string> files;
   GetFiles(name + "*", &files);
-  CHECK_EQ(files.size(), 1);
+  CHECK_EQ(files.size(), 1UL);
 
   FILE* file = fopen(files[0].c_str(), "r");
   CHECK(file != NULL) << ": could not open " << files[0];
@@ -711,7 +725,7 @@ static void TestExtension() {
   // Check that file name ends with extension
   vector<string> filenames;
   GetFiles(dest + "*", &filenames);
-  CHECK_EQ(filenames.size(), 1);
+  CHECK_EQ(filenames.size(), 1UL);
   CHECK(strstr(filenames[0].c_str(), "specialextension") != NULL);
 
   // Release file handle for the destination file to unlock the file in Windows.
@@ -722,8 +736,8 @@ static void TestExtension() {
 struct MyLogger : public base::Logger {
   string data;
 
-  virtual void Write(bool should_flush,
-                     time_t timestamp,
+  virtual void Write(bool /* should_flush */,
+                     time_t /* timestamp */,
                      const char* message,
                      int length) {
     data.append(message, length);
@@ -991,7 +1005,7 @@ class TestWaitingLogSink : public LogSink {
 
   // (re)define LogSink interface
 
-  virtual void send(LogSeverity severity, const char* full_filename,
+  virtual void send(LogSeverity severity, const char* /* full_filename */,
                     const char* base_filename, int line,
                     const struct tm* tm_time,
                     const char* message, size_t message_len) {
@@ -1030,7 +1044,7 @@ static void TestLogSinkWaitTillSent() {
   for (size_t i = 0; i < global_messages.size(); ++i) {
     LOG(INFO) << "Sink capture: " << global_messages[i];
   }
-  CHECK_EQ(global_messages.size(), 3);
+  CHECK_EQ(global_messages.size(), 3UL);
 }
 
 TEST(Strerror, logging) {
@@ -1053,8 +1067,9 @@ TEST(Strerror, logging) {
   CHECK_STREQ(buf, "");
   CHECK_EQ(posix_strerror_r(errcode, buf, buf_size), 0);
   CHECK_STREQ(buf, msg);
-  free(msg);
   delete[] buf;
+  CHECK_EQ(msg, StrError(errcode));
+  free(msg);
 }
 
 // Simple routines to look at the sizes of generated code for LOG(FATAL) and
@@ -1190,10 +1205,10 @@ TEST(LogBacktraceAt, DoesBacktraceAtRightLineWhenEnabled) {
 #endif // HAVE_LIB_GMOCK
 
 struct UserDefinedClass {
-  bool operator==(const UserDefinedClass& rhs) const { return true; }
+  bool operator==(const UserDefinedClass&) const { return true; }
 };
 
-inline ostream& operator<<(ostream& out, const UserDefinedClass& u) {
+inline ostream& operator<<(ostream& out, const UserDefinedClass&) {
   out << "OK";
   return out;
 }
@@ -1202,7 +1217,7 @@ TEST(UserDefinedClass, logging) {
   UserDefinedClass u;
   vector<string> buf;
   LOG_STRING(INFO, &buf) << u;
-  CHECK_EQ(1, buf.size());
+  CHECK_EQ(1UL, buf.size());
   CHECK(buf[0].find("OK") != string::npos);
 
   // We must be able to compile this.