< prev index next >

src/share/vm/logging/log.cpp

Print this page
rev 10386 : imported patch 8146879.03

@@ -131,9 +131,112 @@
   LogConfiguration::configure_stdout(LogLevel::Off, false, LOG_TAGS(gc));
   LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(logging));
   assert_str_eq("all=off", stdoutput->config_string());
 
   // Restore saved configuration
+  if (saved_config[strlen(saved_config) - 1] == ',') {
+    saved_config[strlen(saved_config) - 1] = '\0'; // strip trailing comma
+  }
   LogConfiguration::parse_log_arguments("stdout", saved_config, NULL, NULL, log.error_stream());
   os::free(saved_config);
 }
+
+static size_t number_of_lines_with_substring_in_file(const char* filename,
+                                                     const char* substr) {
+  size_t ret = 0;
+  FILE* fp = fopen(filename, "r");
+  assert(fp, "error opening file %s: %s", filename, strerror(errno));
+  char buf[512];
+  while (fgets(buf, sizeof(buf), fp) != NULL) {
+    if (strstr(buf, substr) != NULL) {
+      ret++;
+    }
+  }
+  fclose(fp);
+  return ret;
+}
+
+static bool file_exists(const char* filename) {
+  struct stat st;
+  return os::stat(filename, &st) == 0;
+}
+
+static void delete_file(const char* filename) {
+  if (!file_exists(filename)) {
+    return;
+  }
+  int ret = remove(filename);
+  assert(ret == 0, "failed to remove file '%s': %s", filename, strerror(errno));
+}
+
+static const char* ExpectedLine = "a (hopefully) unique log line for testing";
+
+static void init_file(const char* filename, const char* options = "") {
+  LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options,
+                                        LogHandle(logging)::error_stream());
+  log_debug(logging)("%s", ExpectedLine);
+  LogConfiguration::parse_log_arguments(filename, "all=off", "", "",
+                                        LogHandle(logging)::error_stream());
+}
+
+void Test_log_file_startup_rotation() {
+  ResourceMark rm;
+  const size_t rotations = 5;
+  const char* filename = "start-rotate-test";
+  char* rotated_file[rotations];
+  for (size_t i = 0; i < rotations; i++) {
+    size_t len = strlen(filename) + 3;
+    rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
+    jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);
+    delete_file(rotated_file[i]);
+  };
+
+  delete_file(filename);
+  init_file(filename);
+  assert(file_exists(filename),
+         "configured logging to file '%s' but file was not found", filename);
+
+  // Initialize the same file a bunch more times to trigger rotations
+  for (size_t i = 0; i < rotations; i++) {
+    init_file(filename);
+    assert(file_exists(rotated_file[i]), "existing file was not rotated");
+  }
+
+  // Remove a file and expect its slot to be re-used
+  delete_file(rotated_file[1]);
+  init_file(filename);
+  assert(file_exists(rotated_file[1]), "log file not properly rotated");
+
+  // Clean up after test
+  delete_file(filename);
+  for (size_t i = 0; i < rotations; i++) {
+    delete_file(rotated_file[i]);
+  }
+}
+
+void Test_log_file_startup_truncation() {
+  ResourceMark rm;
+  const char* filename = "start-truncate-test";
+  const char* archived_filename = "start-truncate-test.0";
+
+  delete_file(filename);
+  delete_file(archived_filename);
+
+  // Use the same log file twice and expect it to be overwritten/truncated
+  init_file(filename, "filecount=0");
+  assert(file_exists(filename), "couldn't find log file: %s", filename);
+
+  init_file(filename, "filecount=0");
+  assert(file_exists(filename), "couldn't find log file: %s", filename);
+  assert(!file_exists(archived_filename),
+         "existing log file %s was not properly truncated when filecount was 0",
+         filename);
+
+  // Verify that the file was really truncated and not just appended
+  assert(number_of_lines_with_substring_in_file(filename, ExpectedLine) == 1,
+         "log file %s appended rather than truncated", filename);
+
+  delete_file(filename);
+  delete_file(archived_filename);
+}
+
 #endif // PRODUCT
< prev index next >