Skip to content

Commit f3db279

Browse files
fbredberJesperIRL
authored andcommitted
8327410: Add hostname option for UL file names
Reviewed-by: jsjolen, dholmes
1 parent 21867c9 commit f3db279

File tree

4 files changed

+60
-57
lines changed

4 files changed

+60
-57
lines changed

src/hotspot/share/logging/logConfiguration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -614,7 +614,7 @@ void LogConfiguration::print_command_line_help(outputStream* out) {
614614
out->print_cr("Available log outputs:");
615615
out->print_cr(" stdout/stderr");
616616
out->print_cr(" file=<filename>");
617-
out->print_cr(" If the filename contains %%p and/or %%t, they will expand to the JVM's PID and startup timestamp, respectively.");
617+
out->print_cr(" If the filename contains %%p, %%t and/or %%hn, they will expand to the JVM's PID, startup timestamp and host name, respectively.");
618618
out->cr();
619619

620620
out->print_cr("Available log output options:");

src/hotspot/share/logging/logFileOutput.cpp

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,7 @@ const char* const LogFileOutput::FileOpenMode = "a";
3838
const char* const LogFileOutput::PidFilenamePlaceholder = "%p";
3939
const char* const LogFileOutput::TimestampFilenamePlaceholder = "%t";
4040
const char* const LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S";
41+
const char* const LogFileOutput::HostnameFilenamePlaceholder = "%hn";
4142
const char* const LogFileOutput::FileSizeOptionKey = "filesize";
4243
const char* const LogFileOutput::FileCountOptionKey = "filecount";
4344
char LogFileOutput::_pid_str[PidBufferSize];
@@ -378,81 +379,81 @@ void LogFileOutput::rotate() {
378379
char* LogFileOutput::make_file_name(const char* file_name,
379380
const char* pid_string,
380381
const char* timestamp_string) {
382+
char hostname_string[HostnameBufferSize];
381383
char* result = nullptr;
382384

383-
// Lets start finding out if we have any %d and/or %t in the name.
385+
// Lets start finding out if we have any %p, %t and/or %hn in the name.
384386
// We will only replace the first occurrence of any placeholder
385387
const char* pid = strstr(file_name, PidFilenamePlaceholder);
386388
const char* timestamp = strstr(file_name, TimestampFilenamePlaceholder);
389+
const char* hostname = strstr(file_name, HostnameFilenamePlaceholder);
387390

388-
if (pid == nullptr && timestamp == nullptr) {
391+
if (pid == nullptr && timestamp == nullptr && hostname == nullptr) {
389392
// We found no place-holders, return the simple filename
390393
return os::strdup_check_oom(file_name, mtLogging);
391394
}
392395

393396
// At least one of the place-holders were found in the file_name
394-
const char* first = "";
395-
size_t first_pos = SIZE_MAX;
396-
size_t first_replace_len = 0;
397-
398-
const char* second = "";
399-
size_t second_pos = SIZE_MAX;
400-
size_t second_replace_len = 0;
401-
402-
// If we found a %p, then setup our variables accordingly
397+
size_t result_len = strlen(file_name);
403398
if (pid != nullptr) {
404-
if (timestamp == nullptr || pid < timestamp) {
405-
first = pid_string;
406-
first_pos = pid - file_name;
407-
first_replace_len = strlen(PidFilenamePlaceholder);
408-
} else {
409-
second = pid_string;
410-
second_pos = pid - file_name;
411-
second_replace_len = strlen(PidFilenamePlaceholder);
412-
}
399+
result_len -= strlen(PidFilenamePlaceholder);
400+
result_len += strlen(pid_string);
413401
}
414-
415402
if (timestamp != nullptr) {
416-
if (pid == nullptr || timestamp < pid) {
417-
first = timestamp_string;
418-
first_pos = timestamp - file_name;
419-
first_replace_len = strlen(TimestampFilenamePlaceholder);
420-
} else {
421-
second = timestamp_string;
422-
second_pos = timestamp - file_name;
423-
second_replace_len = strlen(TimestampFilenamePlaceholder);
403+
result_len -= strlen(TimestampFilenamePlaceholder);
404+
result_len += strlen(timestamp_string);
405+
}
406+
if (hostname != nullptr) {
407+
if (!os::get_host_name(hostname_string, sizeof(hostname_string))) {
408+
int res = jio_snprintf(hostname_string, sizeof(hostname_string), "unknown-host");
409+
assert(res > 0, "Hostname buffer too small");
424410
}
411+
result_len -= strlen(HostnameFilenamePlaceholder);
412+
result_len += strlen(hostname_string);
425413
}
426-
427-
size_t first_len = strlen(first);
428-
size_t second_len = strlen(second);
429-
430414
// Allocate the new buffer, size it to hold all we want to put in there +1.
431-
size_t result_len = strlen(file_name) + first_len - first_replace_len + second_len - second_replace_len;
432415
result = NEW_C_HEAP_ARRAY(char, result_len + 1, mtLogging);
433416

434417
// Assemble the strings
435418
size_t file_name_pos = 0;
436419
size_t i = 0;
437420
while (i < result_len) {
438-
if (file_name_pos == first_pos) {
439-
// We are in the range of the first placeholder
440-
strcpy(result + i, first);
441-
// Bump output buffer position with length of replacing string
442-
i += first_len;
443-
// Bump source buffer position to skip placeholder
444-
file_name_pos += first_replace_len;
445-
} else if (file_name_pos == second_pos) {
446-
// We are in the range of the second placeholder
447-
strcpy(result + i, second);
448-
i += second_len;
449-
file_name_pos += second_replace_len;
450-
} else {
451-
// Else, copy char by char of the original file
452-
result[i] = file_name[file_name_pos++];
453-
i++;
421+
if (file_name[file_name_pos] == '%') {
422+
// Replace the first occurrence of any placeholder
423+
if (pid != nullptr && strncmp(&file_name[file_name_pos],
424+
PidFilenamePlaceholder,
425+
strlen(PidFilenamePlaceholder)) == 0) {
426+
strcpy(result + i, pid_string);
427+
i += strlen(pid_string);
428+
file_name_pos += strlen(PidFilenamePlaceholder);
429+
pid = nullptr;
430+
continue;
431+
}
432+
if (timestamp != nullptr && strncmp(&file_name[file_name_pos],
433+
TimestampFilenamePlaceholder,
434+
strlen(TimestampFilenamePlaceholder)) == 0) {
435+
strcpy(result + i, timestamp_string);
436+
i += strlen(timestamp_string);
437+
file_name_pos += strlen(TimestampFilenamePlaceholder);
438+
timestamp = nullptr;
439+
continue;
440+
}
441+
if (hostname != nullptr && strncmp(&file_name[file_name_pos],
442+
HostnameFilenamePlaceholder,
443+
strlen(HostnameFilenamePlaceholder)) == 0) {
444+
strcpy(result + i, hostname_string);
445+
i += strlen(hostname_string);
446+
file_name_pos += strlen(HostnameFilenamePlaceholder);
447+
hostname = nullptr;
448+
continue;
449+
}
454450
}
451+
// Else, copy char by char of the original file
452+
result[i++] = file_name[file_name_pos++];
455453
}
454+
assert(i == result_len, "should be");
455+
assert(file_name[file_name_pos] == '\0', "should be");
456+
456457
// Add terminating char
457458
result[result_len] = '\0';
458459
return result;

src/hotspot/share/logging/logFileOutput.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,10 +39,12 @@ class LogFileOutput : public LogFileStreamOutput {
3939
static const char* const PidFilenamePlaceholder;
4040
static const char* const TimestampFilenamePlaceholder;
4141
static const char* const TimestampFormat;
42+
static const char* const HostnameFilenamePlaceholder;
4243
static const size_t DefaultFileCount = 5;
4344
static const size_t DefaultFileSize = 20 * M;
4445
static const size_t StartTimeBufferSize = 20;
4546
static const size_t PidBufferSize = 21;
47+
static const size_t HostnameBufferSize = 512;
4648
static const uint MaxRotationFileCount = 1000;
4749
static char _pid_str[PidBufferSize];
4850
static char _vm_start_time_str[StartTimeBufferSize];

src/java.base/share/man/java.1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4308,9 +4308,9 @@ The \f[V]-Xlog\f[R] option supports the following types of outputs:
43084308
.IP \[bu] 2
43094309
\f[V]file=\f[R]\f[I]filename\f[R] --- Sends output to text file(s).
43104310
.PP
4311-
When using \f[V]file=\f[R]\f[I]filename\f[R], specifying \f[V]%p\f[R]
4312-
and/or \f[V]%t\f[R] in the file name expands to the JVM\[aq]s PID and
4313-
startup timestamp, respectively.
4311+
When using \f[V]file=\f[R]\f[I]filename\f[R], specifying \f[V]%p\f[R],
4312+
\f[V]%t\f[R] and/or \f[V]%hn\f[R] in the file name expands to the
4313+
JVM\[aq]s PID, startup timestamp and host name, respectively.
43144314
You can also configure text files to handle file rotation based on file
43154315
size and a number of files to rotate.
43164316
For example, to rotate the log file every 10 MB and keep 5 files in

0 commit comments

Comments
 (0)