/* * Copyright (c) 2015, 2021, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/ #include"precompiled.hpp" #include"jvm.h" #include"logging/log.hpp" #include"logging/logAsyncWriter.hpp" #include"logging/logConfiguration.hpp" #include"logging/logFileOutput.hpp" #include"memory/allocation.inline.hpp" #include"runtime/arguments.hpp" #include"runtime/os.hpp" #include"utilities/defaultStream.hpp" #include"utilities/globalDefinitions.hpp"
staticbool is_regular_file(constchar* filename) { struct stat st; int ret = os::stat(filename, &st); if (ret != 0) { returnfalse;
} return (st.st_mode & S_IFMT) == S_IFREG;
}
staticbool is_fifo_file(constchar* filename) { struct stat st; int ret = os::stat(filename, &st); if (ret != 0) { returnfalse;
} return S_ISFIFO(st.st_mode);
}
// Try to find the next number that should be used for file rotation. // Return UINT_MAX on error. static uint next_file_number(constchar* filename,
uint number_of_digits,
uint filecount,
outputStream* errstream) { bool found = false;
uint next_num = 0;
for (uint i = 0; i < filecount; i++) { int ret = jio_snprintf(archive_name, len, "%s.%0*u",
filename, number_of_digits, i);
assert(ret > 0 && static_cast<size_t>(ret) == len - 1, "incorrect buffer length calculation");
if (os::file_exists(archive_name) && !is_regular_file(archive_name)) { // We've encountered something that's not a regular file among the // possible file rotation targets. Fail immediately to prevent // problems later.
errstream->print_cr("Possible rotation target file '%s' already exists " "but is not a regular file.", archive_name);
next_num = UINT_MAX; break;
}
// Stop looking if we find an unused file name if (!os::file_exists(archive_name)) {
next_num = i;
found = true; break;
}
// Keep track of oldest existing log file if (!found
|| os::compare_file_modified_times(oldest_name, archive_name) > 0) {
strcpy(oldest_name, archive_name);
next_num = i;
found = true;
}
}
bool file_exist = os::file_exists(_file_name); if (file_exist && _is_default_file_count && is_fifo_file(_file_name)) {
_file_count = 0; // Prevent file rotation for fifo's such as named pipes.
}
if (_file_count > 0) { // compute digits with filecount - 1 since numbers will start from 0
_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;
}
int LogFileOutput::write_blocking(const LogDecorations& decorations, constchar* msg) {
RotationLocker lock(_rotation_semaphore); if (_stream == NULL) { // An error has occurred with this output, avoid writing to it. return 0;
}
int written = write_internal(decorations, msg); // Need to flush to the filesystem before should_rotate()
written = flush() ? written : -1; if (written > 0) {
_current_size += written;
if (should_rotate()) {
rotate();
}
}
return written;
}
int LogFileOutput::write(const LogDecorations& decorations, constchar* msg) { if (_stream == NULL) { // An error has occurred with this output, avoid writing to it. return 0;
}
int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) { if (_stream == NULL) { // An error has occurred with this output, avoid writing to it. return 0;
}
RotationLocker lock(_rotation_semaphore); int written = LogFileStreamOutput::write(msg_iterator); if (written > 0) {
_current_size += written;
if (should_rotate()) {
rotate();
}
}
return written;
}
void LogFileOutput::archive() {
assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function."); int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u",
_file_name, _file_count_max_digits, _current_file);
assert(ret >= 0, "Buffer should always be large enough");
// Attempt to remove possibly existing archived log file before we rename. // Don't care if it fails, we really only care about the rename that follows.
remove(_archive_name);
// Rename the file from ex hotspot.log to hotspot.log.2 if (rename(_file_name, _archive_name) == -1) {
jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n",
_file_name, _archive_name, os::strerror(errno));
}
}
void LogFileOutput::force_rotate() { if (_file_count == 0) { // Rotation not possible return;
}
void LogFileOutput::rotate() { if (fclose(_stream)) {
jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",
_file_name, os::strerror(errno));
}
// Archive the current log file
archive();
// Open the active log file using the same stream as before
_stream = os::fopen(_file_name, FileOpenMode); if (_stream == NULL) {
jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n",
_file_name, os::strerror(errno)); return;
}
// Reset accumulated size, increase current file counter, and check for file count wrap-around.
_current_size = 0;
increment_file_count();
}
// Lets start finding out if we have any %d and/or %t in the name. // We will only replace the first occurrence of any placeholder constchar* pid = strstr(file_name, PidFilenamePlaceholder); constchar* timestamp = strstr(file_name, TimestampFilenamePlaceholder);
if (pid == NULL && timestamp == NULL) { // We found no place-holders, return the simple filename return os::strdup_check_oom(file_name, mtLogging);
}
// At least one of the place-holders were found in the file_name constchar* first = "";
size_t first_pos = SIZE_MAX;
size_t first_replace_len = 0;
// Allocate the new buffer, size it to hold all we want to put in there +1.
size_t result_len = strlen(file_name) + first_len - first_replace_len + second_len - second_replace_len;
result = NEW_C_HEAP_ARRAY(char, result_len + 1, mtLogging);
// Assemble the strings
size_t file_name_pos = 0;
size_t i = 0; while (i < result_len) { if (file_name_pos == first_pos) { // We are in the range of the first placeholder
strcpy(result + i, first); // Bump output buffer position with length of replacing string
i += first_len; // Bump source buffer position to skip placeholder
file_name_pos += first_replace_len;
} elseif (file_name_pos == second_pos) { // We are in the range of the second placeholder
strcpy(result + i, second);
i += second_len;
file_name_pos += second_replace_len;
} else { // Else, copy char by char of the original file
result[i] = file_name[file_name_pos++];
i++;
}
} // Add terminating char
result[result_len] = '\0'; return result;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.