153 LogConfiguration::configure_stdout(LogLevel::Off, false, LOG_TAGS(gc));
154 LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(logging));
155 assert_str_eq("all=off", stdoutput->config_string());
156
157 // Restore saved configuration
158 LogConfiguration::parse_log_arguments("stdout", saved_config, NULL, NULL, log.error_stream());
159 os::free(saved_config);
160 }
161
162 class LogMessageTest {
163 private:
164 static LogHandle(logging) _log;
165 static const char* _level_filename[];
166
167 static void test_level_inclusion();
168 static void test_long_message();
169 static void test_message_with_many_lines();
170 static void test_line_order();
171 static void test_prefixing();
172 static void test_scoped_messages();
173
174 public:
175 static void test();
176 };
177
178 const char* LogMessageTest::_level_filename[] = {
179 NULL, // LogLevel::Off
180 #define LOG_LEVEL(name, printname) "multiline-" #printname ".log",
181 LOG_LEVEL_LIST
182 #undef LOG_LEVEL
183 };
184
185 void Test_multiline_logging() {
186 LogMessageTest::test();
187 }
188
189 void LogMessageTest::test() {
190 ResourceMark rm;
191
192 for (int i = 0; i < LogLevel::Count; i++) {
193 char buf[32];
194 // Attempt to remove possibly pre-existing log files
195 remove(_level_filename[i]);
196
197 jio_snprintf(buf, sizeof(buf), "logging=%s", LogLevel::name(static_cast<LogLevelType>(i)));
198 bool success = LogConfiguration::parse_log_arguments(_level_filename[i], buf,
199 NULL, NULL, _log.error_stream());
200 assert(success, "unable to configure logging to file '%s'", _level_filename[i]);
201 }
202
203 test_level_inclusion();
204 test_line_order();
205 test_long_message();
206 test_message_with_many_lines();
207 test_prefixing();
208 test_scoped_messages();
209
210 // Stop logging to the files and remove them.
211 for (int i = 0; i < LogLevel::Count; i++) {
212 LogConfiguration::parse_log_arguments(_level_filename[i], "all=off", NULL, NULL, _log.error_stream());
213 remove(_level_filename[i]);
214 }
215 }
216
217 // Verify that messages with multiple levels are written
218 // to outputs configured for all the corresponding levels
219 void LogMessageTest::test_level_inclusion() {
220 const size_t message_count = 10;
221 LogMessageBuffer msg[message_count];
222
223 struct {
224 int message_number;
225 LogLevelType level;
226 } lines[] = {
227 { 0, LogLevel::Error },
228 { 1, LogLevel::Info },
346 msg.info("test %d", i);
347 }
348 msg.set_prefix(NULL);
349 msg.info("test 3");
350 _log.write(msg);
351
352 const char* expected[] = {
353 "] some prefix: test 0",
354 "] some prefix: test 1",
355 "] some prefix: test 2",
356 "] test 3",
357 NULL
358 };
359 assert(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected), "error in prefixed output");
360 }
361
362 void LogMessageTest::test_scoped_messages() {
363 {
364 LogMessage(logging) msg;
365 msg.info("scoped info");
366 }
367 assert(file_contains_substring(_level_filename[LogLevel::Info], "scoped info"),
368 "missing output from scoped log message");
369 }
370
371
372 static int Test_logconfiguration_subscribe_triggered = 0;
373
374 static void Test_logconfiguration_subscribe_helper() {
375 Test_logconfiguration_subscribe_triggered++;
376 }
377
378 void Test_logconfiguration_subscribe() {
379 ResourceMark rm;
380 LogHandle(logging) log;
381
382 LogConfiguration::register_update_listener(&Test_logconfiguration_subscribe_helper);
383
384 LogConfiguration::parse_log_arguments("stdout", "logging=trace", NULL, NULL, log.error_stream());
385 assert(Test_logconfiguration_subscribe_triggered == 1, "subscription not triggered (1)");
386
387 LogConfiguration::configure_stdout(LogLevel::Debug, true, LOG_TAGS(gc));
388 assert(Test_logconfiguration_subscribe_triggered == 2, "subscription not triggered (2)");
|
153 LogConfiguration::configure_stdout(LogLevel::Off, false, LOG_TAGS(gc));
154 LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(logging));
155 assert_str_eq("all=off", stdoutput->config_string());
156
157 // Restore saved configuration
158 LogConfiguration::parse_log_arguments("stdout", saved_config, NULL, NULL, log.error_stream());
159 os::free(saved_config);
160 }
161
162 class LogMessageTest {
163 private:
164 static LogHandle(logging) _log;
165 static const char* _level_filename[];
166
167 static void test_level_inclusion();
168 static void test_long_message();
169 static void test_message_with_many_lines();
170 static void test_line_order();
171 static void test_prefixing();
172 static void test_scoped_messages();
173 static void test_scoped_flushing();
174 static void test_scoped_reset();
175
176 public:
177 static void test();
178 };
179
180 const char* LogMessageTest::_level_filename[] = {
181 NULL, // LogLevel::Off
182 #define LOG_LEVEL(name, printname) "multiline-" #printname ".log",
183 LOG_LEVEL_LIST
184 #undef LOG_LEVEL
185 };
186
187 void Test_multiline_logging() {
188 LogMessageTest::test();
189 }
190
191 void LogMessageTest::test() {
192 ResourceMark rm;
193
194 for (int i = 0; i < LogLevel::Count; i++) {
195 char buf[32];
196 // Attempt to remove possibly pre-existing log files
197 remove(_level_filename[i]);
198
199 jio_snprintf(buf, sizeof(buf), "logging=%s", LogLevel::name(static_cast<LogLevelType>(i)));
200 bool success = LogConfiguration::parse_log_arguments(_level_filename[i], buf,
201 NULL, NULL, _log.error_stream());
202 assert(success, "unable to configure logging to file '%s'", _level_filename[i]);
203 }
204
205 test_level_inclusion();
206 test_line_order();
207 test_long_message();
208 test_message_with_many_lines();
209 test_prefixing();
210 test_scoped_messages();
211 test_scoped_flushing();
212 test_scoped_reset();
213
214 // Stop logging to the files and remove them.
215 for (int i = 0; i < LogLevel::Count; i++) {
216 LogConfiguration::parse_log_arguments(_level_filename[i], "all=off", NULL, NULL, _log.error_stream());
217 remove(_level_filename[i]);
218 }
219 }
220
221 // Verify that messages with multiple levels are written
222 // to outputs configured for all the corresponding levels
223 void LogMessageTest::test_level_inclusion() {
224 const size_t message_count = 10;
225 LogMessageBuffer msg[message_count];
226
227 struct {
228 int message_number;
229 LogLevelType level;
230 } lines[] = {
231 { 0, LogLevel::Error },
232 { 1, LogLevel::Info },
350 msg.info("test %d", i);
351 }
352 msg.set_prefix(NULL);
353 msg.info("test 3");
354 _log.write(msg);
355
356 const char* expected[] = {
357 "] some prefix: test 0",
358 "] some prefix: test 1",
359 "] some prefix: test 2",
360 "] test 3",
361 NULL
362 };
363 assert(file_contains_substrings_in_order(_level_filename[LogLevel::Trace], expected), "error in prefixed output");
364 }
365
366 void LogMessageTest::test_scoped_messages() {
367 {
368 LogMessage(logging) msg;
369 msg.info("scoped info");
370 msg.warning("scoped warn");
371 assert(!file_contains_substring(_level_filename[LogLevel::Info], "scoped info"),
372 "scoped log message written prematurely");
373 }
374 assert(file_contains_substring(_level_filename[LogLevel::Info], "scoped info"),
375 "missing output from scoped log message");
376 assert(file_contains_substring(_level_filename[LogLevel::Warning], "scoped warn"),
377 "missing output from scoped log message");
378 }
379
380 void LogMessageTest::test_scoped_flushing() {
381 {
382 LogMessage(logging) msg;
383 msg.info("manual flush info");
384 msg.flush();
385 assert(file_contains_substring(_level_filename[LogLevel::Info], "manual flush info"),
386 "missing output from manually flushed scoped log message");
387 }
388 const char* tmp[] = {"manual flush info", "manual flush info", NULL};
389 assert(!file_contains_substrings_in_order(_level_filename[LogLevel::Info], tmp),
390 "log file contains duplicate lines from single scoped log message");
391 }
392
393 void LogMessageTest::test_scoped_reset() {
394 {
395 LogMessage(logging) msg, partial;
396 msg.info("%s", "info reset msg");
397 msg.reset();
398 partial.info("%s", "info reset msg");
399 partial.reset();
400 partial.trace("%s", "trace reset msg");
401 }
402 assert(!file_contains_substring(_level_filename[LogLevel::Info], "info reset msg"),
403 "reset message written anyway");
404 assert(file_contains_substring(_level_filename[LogLevel::Trace], "trace reset msg"),
405 "missing message from partially reset scoped log message");
406 }
407
408
409 static int Test_logconfiguration_subscribe_triggered = 0;
410
411 static void Test_logconfiguration_subscribe_helper() {
412 Test_logconfiguration_subscribe_triggered++;
413 }
414
415 void Test_logconfiguration_subscribe() {
416 ResourceMark rm;
417 LogHandle(logging) log;
418
419 LogConfiguration::register_update_listener(&Test_logconfiguration_subscribe_helper);
420
421 LogConfiguration::parse_log_arguments("stdout", "logging=trace", NULL, NULL, log.error_stream());
422 assert(Test_logconfiguration_subscribe_triggered == 1, "subscription not triggered (1)");
423
424 LogConfiguration::configure_stdout(LogLevel::Debug, true, LOG_TAGS(gc));
425 assert(Test_logconfiguration_subscribe_triggered == 2, "subscription not triggered (2)");
|