< prev index next >

test/native/logging/test_logMessageTest.cpp

Print this page
rev 12483 : 8172098: A lot of gtests uses TEST instead of TEST_VM
Reviewed-by: duke


  48   for (int i = 0; i < LogLevel::Count; i++) {
  49     char buf[32];
  50     // Attempt to remove possibly pre-existing log files
  51     remove(_level_filename[i]);
  52 
  53     jio_snprintf(buf, sizeof(buf), "logging=%s", LogLevel::name(static_cast<LogLevelType>(i)));
  54     set_log_config(_level_filename[i], buf);
  55   }
  56 }
  57 
  58 LogMessageTest::~LogMessageTest() {
  59   // Stop logging to the files and remove them.
  60   for (int i = 0; i < LogLevel::Count; i++) {
  61     set_log_config(_level_filename[i], "all=off");
  62     remove(_level_filename[i]);
  63   }
  64 }
  65 
  66 // Verify that messages with multiple levels are written
  67 // to outputs configured for all the corresponding levels
  68 TEST_F(LogMessageTest, level_inclusion) {
  69   const size_t message_count = 10;
  70   LogMessageBuffer msg[message_count];
  71 
  72   struct {
  73     int message_number;
  74     LogLevelType level;
  75   } lines[] = {
  76     { 0, LogLevel::Error },
  77     { 1, LogLevel::Info },
  78     { 2, LogLevel::Info }, { 2, LogLevel::Debug },
  79     { 3, LogLevel::Info }, { 3, LogLevel::Warning },
  80     { 4, LogLevel::Debug }, { 4, LogLevel::Warning },
  81     { 5, LogLevel::Trace }, { 5, LogLevel::Debug },
  82     { 6, LogLevel::Warning }, { 6, LogLevel::Error },
  83     { 7, LogLevel::Trace }, { 7, LogLevel::Info }, { 7, LogLevel::Debug },
  84     { 8, LogLevel::Trace }, { 8, LogLevel::Debug }, { 8, LogLevel::Info },
  85     { 8, LogLevel::Warning }, { 8, LogLevel::Error},
  86     { 9, LogLevel::Trace }
  87   };
  88 


 102     _log.write(msg[i]);
 103   }
 104 
 105   // Verify that lines are written to the expected log files
 106   for (size_t i = 0; i < ARRAY_SIZE(lines); i++) {
 107     char expected[256];
 108     jio_snprintf(expected, sizeof(expected), "msg[%d]: %s",
 109                  lines[i].message_number, LogLevel::name(lines[i].level));
 110     for (int level = lines[i].level; level > 0; level--) {
 111       EXPECT_TRUE(file_contains_substring(_level_filename[level], expected))
 112         << "line #" << i << " missing from log file " << _level_filename[level];
 113     }
 114     for (int level = lines[i].level + 1; level < LogLevel::Count; level++) {
 115       EXPECT_FALSE(file_contains_substring(_level_filename[level], expected))
 116         << "line #" << i << " erroneously included in log file " << _level_filename[level];
 117     }
 118   }
 119 }
 120 
 121 // Verify that messages are logged in the order they are added to the log message
 122 TEST_F(LogMessageTest, line_order) {
 123   LogMessageBuffer msg;
 124   msg.info("info line").error("error line").trace("trace line")
 125       .error("another error").warning("warning line").debug("debug line");
 126   _log.write(msg);
 127 
 128   const char* expected[] = { "info line", "error line", "trace line",
 129                              "another error", "warning line", "debug line", NULL };
 130   EXPECT_TRUE(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected))
 131     << "output missing or in incorrect order";
 132 }
 133 
 134 TEST_F(LogMessageTest, long_message) {
 135   // Write 10K bytes worth of log data
 136   LogMessageBuffer msg;
 137   const size_t size = 10 * K;
 138   const char* start_marker = "#start#";
 139   const char* end_marker = "#the end#";
 140   char* data = NEW_C_HEAP_ARRAY(char, size, mtLogging);
 141 
 142   // fill buffer with start_marker...some data...end_marker
 143   sprintf(data, "%s", start_marker);
 144   for (size_t i = strlen(start_marker); i < size; i++) {
 145     data[i] = '0' + (i % 10);
 146   }
 147   sprintf(data + size - strlen(end_marker) - 1, "%s", end_marker);
 148 
 149   msg.trace("%s", data); // Adds a newline, making the message exactly 10K in length.
 150   _log.write(msg);
 151 
 152   const char* expected[] = { start_marker, "0123456789", end_marker, NULL };
 153   EXPECT_TRUE(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected))
 154     << "unable to print long line";
 155   FREE_C_HEAP_ARRAY(char, data);
 156 }
 157 
 158 TEST_F(LogMessageTest, message_with_many_lines) {
 159   const size_t lines = 100;
 160   const size_t line_length = 16;
 161 
 162   LogMessageBuffer msg;
 163   for (size_t i = 0; i < lines; i++) {
 164     msg.info("Line #" SIZE_FORMAT, i);
 165   }
 166   _log.write(msg);
 167 
 168   char expected_lines_data[lines][line_length];
 169   const char* expected_lines[lines + 1];
 170   for (size_t i = 0; i < lines; i++) {
 171     jio_snprintf(&expected_lines_data[i][0], line_length, "Line #" SIZE_FORMAT, i);
 172     expected_lines[i] = expected_lines_data[i];
 173   }
 174   expected_lines[lines] = NULL;
 175 
 176   EXPECT_TRUE(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected_lines))
 177     << "couldn't find all lines in multiline message";
 178 }
 179 
 180 static size_t dummy_prefixer(char* buf, size_t len) {
 181   static int i = 0;
 182   const char* prefix = "some prefix: ";
 183   const size_t prefix_len = strlen(prefix);
 184   if (len < prefix_len) {
 185     return prefix_len;
 186   }
 187   jio_snprintf(buf, len, "%s", prefix);
 188   return prefix_len;
 189 }
 190 
 191 TEST_F(LogMessageTest, prefixing) {
 192   LogMessageBuffer msg;
 193   msg.set_prefix(dummy_prefixer);
 194   for (int i = 0; i < 3; i++) {
 195     msg.info("test %d", i);
 196   }
 197   msg.set_prefix(NULL);
 198   msg.info("test 3");
 199   _log.write(msg);
 200 
 201   const char* expected[] = {
 202     "] some prefix: test 0",
 203     "] some prefix: test 1",
 204     "] some prefix: test 2",
 205     "] test 3",
 206     NULL
 207   };
 208   EXPECT_TRUE(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected))
 209     << "error in prefixed output";
 210 }
 211 
 212 TEST_F(LogMessageTest, scoped_messages) {
 213   {
 214     LogMessage(logging) msg;
 215     msg.info("scoped info");
 216     msg.warning("scoped warn");
 217     EXPECT_FALSE(file_contains_substring(_level_filename[LogLevel::Info], "scoped info"))
 218       << "scoped log message written prematurely";
 219   }
 220   EXPECT_TRUE(file_contains_substring(_level_filename[LogLevel::Info], "scoped info"))
 221     << "missing output from scoped log message";
 222   EXPECT_TRUE(file_contains_substring(_level_filename[LogLevel::Warning], "scoped warn"))
 223     << "missing output from scoped log message";
 224 }
 225 
 226 TEST_F(LogMessageTest, scoped_flushing) {
 227   {
 228     LogMessage(logging) msg;
 229     msg.info("manual flush info");
 230     msg.flush();
 231     EXPECT_TRUE(file_contains_substring(_level_filename[LogLevel::Info], "manual flush info"))
 232       << "missing output from manually flushed scoped log message";
 233   }
 234   const char* tmp[] = {"manual flush info", "manual flush info", NULL};
 235   EXPECT_FALSE(file_contains_substrings_in_order(_level_filename[LogLevel::Info], tmp))
 236     << "log file contains duplicate lines from single scoped log message";
 237 }
 238 
 239 TEST_F(LogMessageTest, scoped_reset) {
 240   {
 241     LogMessage(logging) msg, partial;
 242     msg.info("%s", "info reset msg");
 243     msg.reset();
 244     partial.info("%s", "info reset msg");
 245     partial.reset();
 246     partial.trace("%s", "trace reset msg");
 247   }
 248   EXPECT_FALSE(file_contains_substring(_level_filename[LogLevel::Info], "info reset msg"))
 249     << "reset message written anyway";
 250   EXPECT_TRUE(file_contains_substring(_level_filename[LogLevel::Trace], "trace reset msg"))
 251     << "missing message from partially reset scoped log message";
 252 }


  48   for (int i = 0; i < LogLevel::Count; i++) {
  49     char buf[32];
  50     // Attempt to remove possibly pre-existing log files
  51     remove(_level_filename[i]);
  52 
  53     jio_snprintf(buf, sizeof(buf), "logging=%s", LogLevel::name(static_cast<LogLevelType>(i)));
  54     set_log_config(_level_filename[i], buf);
  55   }
  56 }
  57 
  58 LogMessageTest::~LogMessageTest() {
  59   // Stop logging to the files and remove them.
  60   for (int i = 0; i < LogLevel::Count; i++) {
  61     set_log_config(_level_filename[i], "all=off");
  62     remove(_level_filename[i]);
  63   }
  64 }
  65 
  66 // Verify that messages with multiple levels are written
  67 // to outputs configured for all the corresponding levels
  68 TEST_VM_F(LogMessageTest, level_inclusion) {
  69   const size_t message_count = 10;
  70   LogMessageBuffer msg[message_count];
  71 
  72   struct {
  73     int message_number;
  74     LogLevelType level;
  75   } lines[] = {
  76     { 0, LogLevel::Error },
  77     { 1, LogLevel::Info },
  78     { 2, LogLevel::Info }, { 2, LogLevel::Debug },
  79     { 3, LogLevel::Info }, { 3, LogLevel::Warning },
  80     { 4, LogLevel::Debug }, { 4, LogLevel::Warning },
  81     { 5, LogLevel::Trace }, { 5, LogLevel::Debug },
  82     { 6, LogLevel::Warning }, { 6, LogLevel::Error },
  83     { 7, LogLevel::Trace }, { 7, LogLevel::Info }, { 7, LogLevel::Debug },
  84     { 8, LogLevel::Trace }, { 8, LogLevel::Debug }, { 8, LogLevel::Info },
  85     { 8, LogLevel::Warning }, { 8, LogLevel::Error},
  86     { 9, LogLevel::Trace }
  87   };
  88 


 102     _log.write(msg[i]);
 103   }
 104 
 105   // Verify that lines are written to the expected log files
 106   for (size_t i = 0; i < ARRAY_SIZE(lines); i++) {
 107     char expected[256];
 108     jio_snprintf(expected, sizeof(expected), "msg[%d]: %s",
 109                  lines[i].message_number, LogLevel::name(lines[i].level));
 110     for (int level = lines[i].level; level > 0; level--) {
 111       EXPECT_TRUE(file_contains_substring(_level_filename[level], expected))
 112         << "line #" << i << " missing from log file " << _level_filename[level];
 113     }
 114     for (int level = lines[i].level + 1; level < LogLevel::Count; level++) {
 115       EXPECT_FALSE(file_contains_substring(_level_filename[level], expected))
 116         << "line #" << i << " erroneously included in log file " << _level_filename[level];
 117     }
 118   }
 119 }
 120 
 121 // Verify that messages are logged in the order they are added to the log message
 122 TEST_VM_F(LogMessageTest, line_order) {
 123   LogMessageBuffer msg;
 124   msg.info("info line").error("error line").trace("trace line")
 125       .error("another error").warning("warning line").debug("debug line");
 126   _log.write(msg);
 127 
 128   const char* expected[] = { "info line", "error line", "trace line",
 129                              "another error", "warning line", "debug line", NULL };
 130   EXPECT_TRUE(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected))
 131     << "output missing or in incorrect order";
 132 }
 133 
 134 TEST_VM_F(LogMessageTest, long_message) {
 135   // Write 10K bytes worth of log data
 136   LogMessageBuffer msg;
 137   const size_t size = 10 * K;
 138   const char* start_marker = "#start#";
 139   const char* end_marker = "#the end#";
 140   char* data = NEW_C_HEAP_ARRAY(char, size, mtLogging);
 141 
 142   // fill buffer with start_marker...some data...end_marker
 143   sprintf(data, "%s", start_marker);
 144   for (size_t i = strlen(start_marker); i < size; i++) {
 145     data[i] = '0' + (i % 10);
 146   }
 147   sprintf(data + size - strlen(end_marker) - 1, "%s", end_marker);
 148 
 149   msg.trace("%s", data); // Adds a newline, making the message exactly 10K in length.
 150   _log.write(msg);
 151 
 152   const char* expected[] = { start_marker, "0123456789", end_marker, NULL };
 153   EXPECT_TRUE(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected))
 154     << "unable to print long line";
 155   FREE_C_HEAP_ARRAY(char, data);
 156 }
 157 
 158 TEST_VM_F(LogMessageTest, message_with_many_lines) {
 159   const size_t lines = 100;
 160   const size_t line_length = 16;
 161 
 162   LogMessageBuffer msg;
 163   for (size_t i = 0; i < lines; i++) {
 164     msg.info("Line #" SIZE_FORMAT, i);
 165   }
 166   _log.write(msg);
 167 
 168   char expected_lines_data[lines][line_length];
 169   const char* expected_lines[lines + 1];
 170   for (size_t i = 0; i < lines; i++) {
 171     jio_snprintf(&expected_lines_data[i][0], line_length, "Line #" SIZE_FORMAT, i);
 172     expected_lines[i] = expected_lines_data[i];
 173   }
 174   expected_lines[lines] = NULL;
 175 
 176   EXPECT_TRUE(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected_lines))
 177     << "couldn't find all lines in multiline message";
 178 }
 179 
 180 static size_t dummy_prefixer(char* buf, size_t len) {
 181   static int i = 0;
 182   const char* prefix = "some prefix: ";
 183   const size_t prefix_len = strlen(prefix);
 184   if (len < prefix_len) {
 185     return prefix_len;
 186   }
 187   jio_snprintf(buf, len, "%s", prefix);
 188   return prefix_len;
 189 }
 190 
 191 TEST_VM_F(LogMessageTest, prefixing) {
 192   LogMessageBuffer msg;
 193   msg.set_prefix(dummy_prefixer);
 194   for (int i = 0; i < 3; i++) {
 195     msg.info("test %d", i);
 196   }
 197   msg.set_prefix(NULL);
 198   msg.info("test 3");
 199   _log.write(msg);
 200 
 201   const char* expected[] = {
 202     "] some prefix: test 0",
 203     "] some prefix: test 1",
 204     "] some prefix: test 2",
 205     "] test 3",
 206     NULL
 207   };
 208   EXPECT_TRUE(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected))
 209     << "error in prefixed output";
 210 }
 211 
 212 TEST_VM_F(LogMessageTest, scoped_messages) {
 213   {
 214     LogMessage(logging) msg;
 215     msg.info("scoped info");
 216     msg.warning("scoped warn");
 217     EXPECT_FALSE(file_contains_substring(_level_filename[LogLevel::Info], "scoped info"))
 218       << "scoped log message written prematurely";
 219   }
 220   EXPECT_TRUE(file_contains_substring(_level_filename[LogLevel::Info], "scoped info"))
 221     << "missing output from scoped log message";
 222   EXPECT_TRUE(file_contains_substring(_level_filename[LogLevel::Warning], "scoped warn"))
 223     << "missing output from scoped log message";
 224 }
 225 
 226 TEST_VM_F(LogMessageTest, scoped_flushing) {
 227   {
 228     LogMessage(logging) msg;
 229     msg.info("manual flush info");
 230     msg.flush();
 231     EXPECT_TRUE(file_contains_substring(_level_filename[LogLevel::Info], "manual flush info"))
 232       << "missing output from manually flushed scoped log message";
 233   }
 234   const char* tmp[] = {"manual flush info", "manual flush info", NULL};
 235   EXPECT_FALSE(file_contains_substrings_in_order(_level_filename[LogLevel::Info], tmp))
 236     << "log file contains duplicate lines from single scoped log message";
 237 }
 238 
 239 TEST_VM_F(LogMessageTest, scoped_reset) {
 240   {
 241     LogMessage(logging) msg, partial;
 242     msg.info("%s", "info reset msg");
 243     msg.reset();
 244     partial.info("%s", "info reset msg");
 245     partial.reset();
 246     partial.trace("%s", "trace reset msg");
 247   }
 248   EXPECT_FALSE(file_contains_substring(_level_filename[LogLevel::Info], "info reset msg"))
 249     << "reset message written anyway";
 250   EXPECT_TRUE(file_contains_substring(_level_filename[LogLevel::Trace], "trace reset msg"))
 251     << "missing message from partially reset scoped log message";
 252 }
< prev index next >