Skip to content

Commit

Permalink
8167548: [TESTBUG] Logging tests put log files in source tree
Browse files Browse the repository at this point in the history
Create log files in temp directory, instead of cwd.

Reviewed-by: coleenp, dholmes
  • Loading branch information
Harold Seigel committed Feb 20, 2019
1 parent 865c331 commit f4ac0a2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 29 deletions.
9 changes: 9 additions & 0 deletions src/hotspot/share/logging/logFileOutput.cpp
Expand Up @@ -51,6 +51,14 @@ LogFileOutput::LogFileOutput(const char* name)
_file_name = make_file_name(name + strlen(Prefix), _pid_str, _vm_start_time_str);
}

const char* LogFileOutput::cur_log_file_name() {
if (strlen(_archive_name) == 0) {
return _file_name;
} else {
return _archive_name;
}
}

void LogFileOutput::set_file_name_parameters(jlong vm_start_time) {
int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id());
assert(res > 0, "PID buffer too small");
Expand Down Expand Up @@ -234,6 +242,7 @@ bool LogFileOutput::initialize(const char* options, outputStream* errstream) {
_file_count_max_digits = number_of_digits(_file_count - 1);
_archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
_archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
_archive_name[0] = 0;
}

log_trace(logging)("Initializing logging to file '%s' (filecount: %u"
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/logging/logFileOutput.hpp
Expand Up @@ -92,6 +92,7 @@ class LogFileOutput : public LogFileStreamOutput {
return _name;
}

const char* cur_log_file_name();
static const char* const Prefix;
static void set_file_name_parameters(jlong start_time);
};
Expand Down
13 changes: 7 additions & 6 deletions test/hotspot/gtest/logging/logTestFixture.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,11 +33,12 @@
#include "utilities/ostream.hpp"

LogTestFixture::LogTestFixture() : _n_snapshots(0), _configuration_snapshot(NULL) {
// Set up TestLogFileName to include PID, testcase name and test name
int ret = jio_snprintf(_filename, sizeof(_filename), "testlog.pid%d.%s.%s.log",
os::current_process_id(),
::testing::UnitTest::GetInstance()->current_test_info()->test_case_name(),
::testing::UnitTest::GetInstance()->current_test_info()->name());

// Set up TestLogFileName to include temp_dir, PID, testcase name and test name.
const testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
int ret = jio_snprintf(_filename, sizeof(_filename), "%s%stestlog.pid%d.%s.%s.log",
os::get_temp_directory(), os::file_separator(), os::current_process_id(),
test_info->test_case_name(), test_info->name());
EXPECT_GT(ret, 0) << "_filename buffer issue";
TestLogFileName = _filename;

Expand Down
26 changes: 25 additions & 1 deletion test/hotspot/gtest/logging/logTestUtils.inline.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -88,6 +88,30 @@ static inline void init_log_file(const char* filename, const char* options = "")
guarantee(success, "Failed to disable logging to file '%s'", filename);
}

static const char* tmp_dir = os::get_temp_directory();
static const char* file_sep = os::file_separator();

// Prepend filename with the temp directory and pid and return the result as a
// resource allocated string.
static inline char* prepend_temp_dir(const char* filename) {
size_t temp_file_len = strlen(tmp_dir) + strlen(file_sep) + strlen(filename) + 28;
char* temp_file = NEW_RESOURCE_ARRAY(char, temp_file_len);
int ret = jio_snprintf(temp_file, temp_file_len, "%s%spid%d.%s",
tmp_dir, file_sep,
os::current_process_id(), filename);
return temp_file;
}

// Prepend filename with specified prefix and the temp directory and return the
// result as a malloc allocated string. This is used by test_logFileOutput.cpp.
static inline char* prepend_prefix_temp_dir(const char* prefix, const char* filename) {
size_t temp_file_len = strlen(prefix) + strlen(tmp_dir) + strlen(file_sep) + strlen(filename) + 1;
char* temp_file = (char*)os::malloc(temp_file_len, mtLogging);
int ret = jio_snprintf(temp_file, temp_file_len, "%s%s%s%s",
prefix, tmp_dir, file_sep, filename);
return temp_file;
}

// Read a complete line from fp and return it as a resource allocated string.
// Returns NULL on EOF.
static inline char* read_line(FILE* fp) {
Expand Down
15 changes: 7 additions & 8 deletions test/hotspot/gtest/logging/test_logConfiguration.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -403,16 +403,15 @@ TEST_VM_F(LogConfigurationTest, output_name_normalization) {

// Make sure prefixes are ignored when used within quotes
// (this should create a log with "file=" in its filename)
int ret = jio_snprintf(buf, sizeof(buf), "\"file=%s\"", TestLogFileName);
ASSERT_NE(-1, ret);
set_log_config(buf, "logging=trace");
// Note that the filename cannot contain directories because
// it is being prefixed with "file=".
const char* leafFileName = "\"file=leaf_file_name\"";
set_log_config(leafFileName, "logging=trace");
EXPECT_TRUE(is_described("#3: ")) << "prefix within quotes not ignored as it should be";
set_log_config(buf, "all=off");
set_log_config(leafFileName, "all=off");

// Remove the extra log file created
ret = jio_snprintf(buf, sizeof(buf), "file=%s", TestLogFileName);
ASSERT_NE(-1, ret);
delete_file(buf);
delete_file("file=leaf_file_name");
}

static size_t count_occurrences(const char* haystack, const char* needle) {
Expand Down
20 changes: 8 additions & 12 deletions test/hotspot/gtest/logging/test_logFileOutput.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,7 +31,7 @@
#include "utilities/globalDefinitions.hpp"
#include "utilities/ostream.hpp"

static const char* name = "file=testlog.pid%p.%t.log";
static const char* name = prepend_prefix_temp_dir("file=", "testlog.pid%p.%t.log");

// Test parsing a bunch of valid file output options
TEST_VM(LogFileOutput, parse_valid) {
Expand All @@ -46,11 +46,6 @@ TEST_VM(LogFileOutput, parse_valid) {

// Override LogOutput's vm_start time to get predictable file name
LogFileOutput::set_file_name_parameters(0);
char expected_filename[1 * K];
int ret = jio_snprintf(expected_filename, sizeof(expected_filename),
"testlog.pid%d.1970-01-01_01-00-00.log",
os::current_process_id());
ASSERT_GT(ret, 0) << "Buffer too small";

for (size_t i = 0; i < ARRAY_SIZE(valid_options); i++) {
ResourceMark rm;
Expand All @@ -60,8 +55,8 @@ TEST_VM(LogFileOutput, parse_valid) {
EXPECT_STREQ(name, fo.name());
EXPECT_TRUE(fo.initialize(valid_options[i], &ss))
<< "Did not accept valid option(s) '" << valid_options[i] << "': " << ss.as_string();
remove(fo.cur_log_file_name());
}
remove(expected_filename);
}
}

Expand Down Expand Up @@ -105,11 +100,11 @@ TEST_VM(LogFileOutput, filesize_overflow) {
}

TEST_VM(LogFileOutput, startup_rotation) {
ResourceMark rm;
const size_t rotations = 5;
const char* filename = "start-rotate-test";
const char* filename = prepend_temp_dir("start-rotate-test");
char* rotated_file[rotations];

ResourceMark rm;
for (size_t i = 0; i < rotations; i++) {
size_t len = strlen(filename) + 3;
rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
Expand Down Expand Up @@ -142,8 +137,9 @@ TEST_VM(LogFileOutput, startup_rotation) {
}

TEST_VM(LogFileOutput, startup_truncation) {
const char* filename = "start-truncate-test";
const char* archived_filename = "start-truncate-test.0";
ResourceMark rm;
const char* filename = prepend_temp_dir("start-truncate-test");
const char* archived_filename = prepend_temp_dir("start-truncate-test.0");

delete_file(filename);
delete_file(archived_filename);
Expand Down
5 changes: 3 additions & 2 deletions test/hotspot/gtest/logging/test_logTagSetDescriptions.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -48,7 +48,8 @@ TEST_VM(LogTagSetDescriptions, describe) {
}

TEST_VM(LogTagSetDescriptions, command_line_help) {
const char* filename = "logtagset_descriptions";
ResourceMark rm;
const char* filename = prepend_temp_dir("logtagset_descriptions");
FILE* fp = fopen(filename, "w+");
ASSERT_NE((void*)NULL, fp);
fileStream stream(fp);
Expand Down

0 comments on commit f4ac0a2

Please sign in to comment.