|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -38,6 +38,7 @@ const char* const LogFileOutput::FileOpenMode = "a"; |
38 | 38 | const char* const LogFileOutput::PidFilenamePlaceholder = "%p"; |
39 | 39 | const char* const LogFileOutput::TimestampFilenamePlaceholder = "%t"; |
40 | 40 | const char* const LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S"; |
| 41 | +const char* const LogFileOutput::HostnameFilenamePlaceholder = "%hn"; |
41 | 42 | const char* const LogFileOutput::FileSizeOptionKey = "filesize"; |
42 | 43 | const char* const LogFileOutput::FileCountOptionKey = "filecount"; |
43 | 44 | char LogFileOutput::_pid_str[PidBufferSize]; |
@@ -378,81 +379,81 @@ void LogFileOutput::rotate() { |
378 | 379 | char* LogFileOutput::make_file_name(const char* file_name, |
379 | 380 | const char* pid_string, |
380 | 381 | const char* timestamp_string) { |
| 382 | + char hostname_string[HostnameBufferSize]; |
381 | 383 | char* result = nullptr; |
382 | 384 |
|
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. |
384 | 386 | // We will only replace the first occurrence of any placeholder |
385 | 387 | const char* pid = strstr(file_name, PidFilenamePlaceholder); |
386 | 388 | const char* timestamp = strstr(file_name, TimestampFilenamePlaceholder); |
| 389 | + const char* hostname = strstr(file_name, HostnameFilenamePlaceholder); |
387 | 390 |
|
388 | | - if (pid == nullptr && timestamp == nullptr) { |
| 391 | + if (pid == nullptr && timestamp == nullptr && hostname == nullptr) { |
389 | 392 | // We found no place-holders, return the simple filename |
390 | 393 | return os::strdup_check_oom(file_name, mtLogging); |
391 | 394 | } |
392 | 395 |
|
393 | 396 | // 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); |
403 | 398 | 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); |
413 | 401 | } |
414 | | - |
415 | 402 | 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"); |
424 | 410 | } |
| 411 | + result_len -= strlen(HostnameFilenamePlaceholder); |
| 412 | + result_len += strlen(hostname_string); |
425 | 413 | } |
426 | | - |
427 | | - size_t first_len = strlen(first); |
428 | | - size_t second_len = strlen(second); |
429 | | - |
430 | 414 | // 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; |
432 | 415 | result = NEW_C_HEAP_ARRAY(char, result_len + 1, mtLogging); |
433 | 416 |
|
434 | 417 | // Assemble the strings |
435 | 418 | size_t file_name_pos = 0; |
436 | 419 | size_t i = 0; |
437 | 420 | 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 | + } |
454 | 450 | } |
| 451 | + // Else, copy char by char of the original file |
| 452 | + result[i++] = file_name[file_name_pos++]; |
455 | 453 | } |
| 454 | + assert(i == result_len, "should be"); |
| 455 | + assert(file_name[file_name_pos] == '\0', "should be"); |
| 456 | + |
456 | 457 | // Add terminating char |
457 | 458 | result[result_len] = '\0'; |
458 | 459 | return result; |
|
0 commit comments