hotspot/src/share/vm/logging/log.cpp
changeset 37245 70413a0266d4
parent 37243 34e1821bde93
child 37247 bdbfe42632cb
equal deleted inserted replaced
37243:34e1821bde93 37245:70413a0266d4
    91   char* _saved_config;
    91   char* _saved_config;
    92   char* _new_output;
    92   char* _new_output;
    93   Log(logging) _log;
    93   Log(logging) _log;
    94  public:
    94  public:
    95   TestLogSavedConfig(const char* apply_output = NULL, const char* apply_setting = NULL) : _new_output(0) {
    95   TestLogSavedConfig(const char* apply_output = NULL, const char* apply_setting = NULL) : _new_output(0) {
       
    96     ResourceMark rm;
    96     _saved_config = os::strdup_check_oom(LogOutput::Stdout->config_string());
    97     _saved_config = os::strdup_check_oom(LogOutput::Stdout->config_string());
    97     bool success = LogConfiguration::parse_log_arguments("stdout", "all=off", NULL, NULL, _log.error_stream());
    98     bool success = LogConfiguration::parse_log_arguments("stdout", "all=off", NULL, NULL, _log.error_stream());
    98     assert(success, "test unable to turn all off");
    99     assert(success, "test unable to turn all off");
    99 
   100 
   100     if (apply_output) {
   101     if (apply_output) {
   103       assert(success, "test unable to apply test log configuration");
   104       assert(success, "test unable to apply test log configuration");
   104     }
   105     }
   105   }
   106   }
   106 
   107 
   107   ~TestLogSavedConfig() {
   108   ~TestLogSavedConfig() {
       
   109     ResourceMark rm;
   108     if (_new_output) {
   110     if (_new_output) {
   109       bool success = LogConfiguration::parse_log_arguments(_new_output, "all=off", NULL, NULL, _log.error_stream());
   111       bool success = LogConfiguration::parse_log_arguments(_new_output, "all=off", NULL, NULL, _log.error_stream());
   110       assert(success, "test unable to turn all off");
   112       assert(success, "test unable to turn all off");
   111       os::free(_new_output);
   113       os::free(_new_output);
   112     }
   114     }
   152 
   154 
   153 void Test_logconfiguration_subscribe() {
   155 void Test_logconfiguration_subscribe() {
   154   ResourceMark rm;
   156   ResourceMark rm;
   155   Log(logging) log;
   157   Log(logging) log;
   156 
   158 
   157   TestLogSavedConfig log_cfg("stdout", "logging+test=trace");
   159   TestLogSavedConfig log_cfg("stdout", "logging*=trace");
   158 
   160 
   159   LogConfiguration::register_update_listener(&Test_logconfiguration_subscribe_helper);
   161   LogConfiguration::register_update_listener(&Test_logconfiguration_subscribe_helper);
   160 
   162 
   161   LogConfiguration::parse_log_arguments("stdout", "logging=trace", NULL, NULL, log.error_stream());
   163   LogConfiguration::parse_log_arguments("stdout", "logging=trace", NULL, NULL, log.error_stream());
   162   assert(Test_logconfiguration_subscribe_triggered == 1, "subscription not triggered (1)");
   164   assert(Test_logconfiguration_subscribe_triggered == 1, "subscription not triggered (1)");
   265       }
   267       }
   266     }
   268     }
   267   }
   269   }
   268 }
   270 }
   269 
   271 
       
   272 #define Test_logtarget_string_literal "First line"
       
   273 
       
   274 
       
   275 static void Test_logtarget_on() {
       
   276   TestLogFile log_file("log_target");
       
   277   TestLogSavedConfig tlsc(log_file.name(), "gc=debug");
       
   278 
       
   279   LogTarget(Debug, gc) log;
       
   280 
       
   281   assert(log.is_enabled(), "assert");
       
   282 
       
   283   // Log the line and expect it to be available in the output file.
       
   284   log.print(Test_logtarget_string_literal);
       
   285 
       
   286   FILE* fp = fopen(log_file.name(), "r");
       
   287   assert(fp != NULL, "File read error");
       
   288 
       
   289   char output[256 /* Large enough buffer */];
       
   290   char* res = fgets(output, sizeof(output), fp);
       
   291   assert(res != NULL, "assert");
       
   292 
       
   293   assert(strstr(output, Test_logtarget_string_literal) != NULL, "log line missing");
       
   294 
       
   295   fclose(fp);
       
   296 }
       
   297 
       
   298 static void Test_logtarget_off() {
       
   299   TestLogFile log_file("log_target");
       
   300   TestLogSavedConfig tlsc(log_file.name(), "gc=info");
       
   301 
       
   302   LogTarget(Debug, gc) log;
       
   303 
       
   304   if (log.is_enabled()) {
       
   305     // The log config could have been redirected gc=debug to a file. If gc=debug
       
   306     // is enabled, we can only test that the LogTarget returns the same value
       
   307     // as the log_is_enabled function. The rest of the test will be ignored.
       
   308     assert(log.is_enabled() == log_is_enabled(Debug, gc), "assert");
       
   309     log_warning(logging)("This test doesn't support runs with -Xlog");
       
   310     return;
       
   311   }
       
   312 
       
   313   // Try to log, but expect this to be filtered out.
       
   314   log.print(Test_logtarget_string_literal);
       
   315 
       
   316   // Log a dummy line so that fgets doesn't return NULL because the file is empty.
       
   317   log_info(gc)("Dummy line");
       
   318 
       
   319   FILE* fp = fopen(log_file.name(), "r");
       
   320   assert(fp != NULL, "File read error");
       
   321 
       
   322   char output[256 /* Large enough buffer */];
       
   323   char* res = fgets(output, sizeof(output), fp);
       
   324   assert(res != NULL, "assert");
       
   325 
       
   326   assert(strstr(output, Test_logtarget_string_literal) == NULL, "log line not missing");
       
   327 
       
   328   fclose(fp);
       
   329 }
       
   330 
       
   331 void Test_logtarget() {
       
   332   Test_logtarget_on();
       
   333   Test_logtarget_off();
       
   334 }
       
   335 
   270 #endif // PRODUCT
   336 #endif // PRODUCT