src/hotspot/share/logging/logFileOutput.cpp
author naoto
Tue, 09 Jul 2019 08:05:38 -0700
changeset 55627 9c1885fb2a42
parent 53845 1807da9ad196
permissions -rw-r--r--
8227127: Era designator not displayed correctly using the COMPAT provider Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     1
/*
53369
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     4
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     7
 * published by the Free Software Foundation.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     8
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    13
 * accompanied this code).
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    14
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    18
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    21
 * questions.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    22
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    23
 */
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    24
#include "precompiled.hpp"
47765
b7c7428eaab9 8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents: 47216
diff changeset
    25
#include "jvm.h"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    26
#include "logging/log.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    27
#include "logging/logConfiguration.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    28
#include "logging/logFileOutput.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    29
#include "memory/allocation.inline.hpp"
38195
d317aafcc87c 8153073: UL: Set filesize option with k/m/g
ysuenaga
parents: 38065
diff changeset
    30
#include "runtime/arguments.hpp"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    31
#include "runtime/os.inline.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    32
#include "utilities/globalDefinitions.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    33
#include "utilities/defaultStream.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    34
46672
dc24cb5e50a1 8184309: Buld warnings from GCC 7.1 on Fedora 26
ysuenaga
parents: 46630
diff changeset
    35
const char* const LogFileOutput::Prefix = "file=";
dc24cb5e50a1 8184309: Buld warnings from GCC 7.1 on Fedora 26
ysuenaga
parents: 46630
diff changeset
    36
const char* const LogFileOutput::FileOpenMode = "a";
dc24cb5e50a1 8184309: Buld warnings from GCC 7.1 on Fedora 26
ysuenaga
parents: 46630
diff changeset
    37
const char* const LogFileOutput::PidFilenamePlaceholder = "%p";
dc24cb5e50a1 8184309: Buld warnings from GCC 7.1 on Fedora 26
ysuenaga
parents: 46630
diff changeset
    38
const char* const LogFileOutput::TimestampFilenamePlaceholder = "%t";
dc24cb5e50a1 8184309: Buld warnings from GCC 7.1 on Fedora 26
ysuenaga
parents: 46630
diff changeset
    39
const char* const LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S";
dc24cb5e50a1 8184309: Buld warnings from GCC 7.1 on Fedora 26
ysuenaga
parents: 46630
diff changeset
    40
const char* const LogFileOutput::FileSizeOptionKey = "filesize";
dc24cb5e50a1 8184309: Buld warnings from GCC 7.1 on Fedora 26
ysuenaga
parents: 46630
diff changeset
    41
const char* const LogFileOutput::FileCountOptionKey = "filecount";
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    42
char        LogFileOutput::_pid_str[PidBufferSize];
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    43
char        LogFileOutput::_vm_start_time_str[StartTimeBufferSize];
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    44
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    45
LogFileOutput::LogFileOutput(const char* name)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    46
    : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)),
51334
cc2c79d22508 8208671: Runtime, JFR, Serviceability changes to allow enabling -Wreorder
tschatzl
parents: 49984
diff changeset
    47
      _file_name(NULL), _archive_name(NULL), _current_file(0),
53369
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
    48
      _file_count(DefaultFileCount), _is_default_file_count(true), _archive_name_len(0),
51334
cc2c79d22508 8208671: Runtime, JFR, Serviceability changes to allow enabling -Wreorder
tschatzl
parents: 49984
diff changeset
    49
      _rotate_size(DefaultFileSize), _current_size(0), _rotation_semaphore(1) {
40902
395e1f3ec886 8157948: UL allows same log file with multiple file=
mlarsson
parents: 39707
diff changeset
    50
  assert(strstr(name, Prefix) == name, "invalid output name '%s': missing prefix: %s", name, Prefix);
395e1f3ec886 8157948: UL allows same log file with multiple file=
mlarsson
parents: 39707
diff changeset
    51
  _file_name = make_file_name(name + strlen(Prefix), _pid_str, _vm_start_time_str);
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    52
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    53
53845
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
    54
const char* LogFileOutput::cur_log_file_name() {
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
    55
  if (strlen(_archive_name) == 0) {
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
    56
    return _file_name;
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
    57
  } else {
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
    58
    return _archive_name;
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
    59
  }
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
    60
}
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
    61
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    62
void LogFileOutput::set_file_name_parameters(jlong vm_start_time) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    63
  int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id());
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    64
  assert(res > 0, "PID buffer too small");
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    65
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    66
  struct tm local_time;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    67
  time_t utc_time = vm_start_time / 1000;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    68
  os::localtime_pd(&utc_time, &local_time);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    69
  res = (int)strftime(_vm_start_time_str, sizeof(_vm_start_time_str), TimestampFormat, &local_time);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    70
  assert(res > 0, "VM start time buffer too small.");
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    71
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    72
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    73
LogFileOutput::~LogFileOutput() {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    74
  if (_stream != NULL) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    75
    if (fclose(_stream) != 0) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    76
      jio_fprintf(defaultStream::error_stream(), "Could not close log file '%s' (%s).\n",
37113
5a33bf5089ac 8148425: strerror() function is not thread-safe
stuefe
parents: 35230
diff changeset
    77
                  _file_name, os::strerror(errno));
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    78
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    79
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    80
  os::free(_archive_name);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    81
  os::free(_file_name);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    82
  os::free(const_cast<char*>(_name));
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    83
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    84
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
    85
static size_t parse_value(const char* value_str) {
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    86
  char* end;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    87
  unsigned long long value = strtoull(value_str, &end, 10);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    88
  if (!isdigit(*value_str) || end != value_str + strlen(value_str) || value >= SIZE_MAX) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    89
    return SIZE_MAX;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    90
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    91
  return value;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    92
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    93
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
    94
static bool file_exists(const char* filename) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
    95
  struct stat dummy_stat;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
    96
  return os::stat(filename, &dummy_stat) == 0;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
    97
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
    98
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
    99
static uint number_of_digits(uint number) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   100
  return number < 10 ? 1 : (number < 100 ? 2 : 3);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   101
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   102
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   103
static bool is_regular_file(const char* filename) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   104
  struct stat st;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   105
  int ret = os::stat(filename, &st);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   106
  if (ret != 0) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   107
    return false;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   108
  }
41667
4a349512fde1 8166930: minor cleanups 1) remove reference to ZIP_ReadMappedEntry 2) checking of st_mode
ccheung
parents: 40902
diff changeset
   109
  return (st.st_mode & S_IFMT) == S_IFREG;
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   110
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   111
53369
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   112
static bool is_fifo_file(const char* filename) {
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   113
  struct stat st;
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   114
  int ret = os::stat(filename, &st);
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   115
  if (ret != 0) {
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   116
    return false;
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   117
  }
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   118
  return S_ISFIFO(st.st_mode);
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   119
}
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   120
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   121
// Try to find the next number that should be used for file rotation.
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   122
// Return UINT_MAX on error.
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   123
static uint next_file_number(const char* filename,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   124
                             uint number_of_digits,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   125
                             uint filecount,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   126
                             outputStream* errstream) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   127
  bool found = false;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   128
  uint next_num = 0;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   129
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   130
  // len is filename + dot + digits + null char
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   131
  size_t len = strlen(filename) + number_of_digits + 2;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   132
  char* archive_name = NEW_C_HEAP_ARRAY(char, len, mtLogging);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   133
  char* oldest_name = NEW_C_HEAP_ARRAY(char, len, mtLogging);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   134
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   135
  for (uint i = 0; i < filecount; i++) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   136
    int ret = jio_snprintf(archive_name, len, "%s.%0*u",
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   137
                           filename, number_of_digits, i);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   138
    assert(ret > 0 && static_cast<size_t>(ret) == len - 1,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   139
           "incorrect buffer length calculation");
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   140
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   141
    if (file_exists(archive_name) && !is_regular_file(archive_name)) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   142
      // We've encountered something that's not a regular file among the
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   143
      // possible file rotation targets. Fail immediately to prevent
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   144
      // problems later.
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   145
      errstream->print_cr("Possible rotation target file '%s' already exists "
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   146
                          "but is not a regular file.", archive_name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   147
      next_num = UINT_MAX;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   148
      break;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   149
    }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   150
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   151
    // Stop looking if we find an unused file name
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   152
    if (!file_exists(archive_name)) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   153
      next_num = i;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   154
      found = true;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   155
      break;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   156
    }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   157
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   158
    // Keep track of oldest existing log file
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   159
    if (!found
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   160
        || os::compare_file_modified_times(oldest_name, archive_name) > 0) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   161
      strcpy(oldest_name, archive_name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   162
      next_num = i;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   163
      found = true;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   164
    }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   165
  }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   166
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   167
  FREE_C_HEAP_ARRAY(char, oldest_name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   168
  FREE_C_HEAP_ARRAY(char, archive_name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   169
  return next_num;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   170
}
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   171
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   172
bool LogFileOutput::parse_options(const char* options, outputStream* errstream) {
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   173
  if (options == NULL || strlen(options) == 0) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   174
    return true;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   175
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   176
  bool success = true;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   177
  char* opts = os::strdup_check_oom(options, mtLogging);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   178
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   179
  char* comma_pos;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   180
  char* pos = opts;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   181
  do {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   182
    comma_pos = strchr(pos, ',');
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   183
    if (comma_pos != NULL) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   184
      *comma_pos = '\0';
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   185
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   186
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   187
    char* equals_pos = strchr(pos, '=');
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   188
    if (equals_pos == NULL) {
49180
9637557def32 8190346: improve unified JVM logging help message and warnings
mlarsson
parents: 47765
diff changeset
   189
      errstream->print_cr("Invalid option '%s' for log file output.", pos);
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   190
      success = false;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   191
      break;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   192
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   193
    char* key = pos;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   194
    char* value_str = equals_pos + 1;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   195
    *equals_pos = '\0';
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   196
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   197
    if (strcmp(FileCountOptionKey, key) == 0) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   198
      size_t value = parse_value(value_str);
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   199
      if (value > MaxRotationFileCount) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   200
        errstream->print_cr("Invalid option: %s must be in range [0, %u]",
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   201
                            FileCountOptionKey,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   202
                            MaxRotationFileCount);
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   203
        success = false;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   204
        break;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   205
      }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   206
      _file_count = static_cast<uint>(value);
53369
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   207
      _is_default_file_count = false;
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   208
    } else if (strcmp(FileSizeOptionKey, key) == 0) {
38195
d317aafcc87c 8153073: UL: Set filesize option with k/m/g
ysuenaga
parents: 38065
diff changeset
   209
      julong value;
d317aafcc87c 8153073: UL: Set filesize option with k/m/g
ysuenaga
parents: 38065
diff changeset
   210
      success = Arguments::atojulong(value_str, &value);
d317aafcc87c 8153073: UL: Set filesize option with k/m/g
ysuenaga
parents: 38065
diff changeset
   211
      if (!success || (value > SIZE_MAX)) {
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   212
        errstream->print_cr("Invalid option: %s must be in range [0, "
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 46560
diff changeset
   213
                            SIZE_FORMAT "]", FileSizeOptionKey, (size_t)SIZE_MAX);
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   214
        success = false;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   215
        break;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   216
      }
38195
d317aafcc87c 8153073: UL: Set filesize option with k/m/g
ysuenaga
parents: 38065
diff changeset
   217
      _rotate_size = static_cast<size_t>(value);
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   218
    } else {
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   219
      errstream->print_cr("Invalid option '%s' for log file output.", key);
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   220
      success = false;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   221
      break;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   222
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   223
    pos = comma_pos + 1;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   224
  } while (comma_pos != NULL);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   225
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   226
  os::free(opts);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   227
  return success;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   228
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   229
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   230
bool LogFileOutput::initialize(const char* options, outputStream* errstream) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   231
  if (!parse_options(options, errstream)) {
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   232
    return false;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   233
  }
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   234
53369
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   235
  bool file_exist = file_exists(_file_name);
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   236
  if (file_exist && _is_default_file_count && is_fifo_file(_file_name)) {
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   237
    _file_count = 0; // Prevent file rotation for fifo's such as named pipes.
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   238
  }
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   239
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   240
  if (_file_count > 0) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   241
    // compute digits with filecount - 1 since numbers will start from 0
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   242
    _file_count_max_digits = number_of_digits(_file_count - 1);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   243
    _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   244
    _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
53845
1807da9ad196 8167548: [TESTBUG] Logging tests put log files in source tree
hseigel
parents: 53369
diff changeset
   245
    _archive_name[0] = 0;
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   246
  }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   247
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   248
  log_trace(logging)("Initializing logging to file '%s' (filecount: %u"
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   249
                     ", filesize: " SIZE_FORMAT " KiB).",
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   250
                     _file_name, _file_count, _rotate_size / K);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   251
53369
55cee96fefec 8215699: -Xlog::file cannot be used with named pipe
hseigel
parents: 52892
diff changeset
   252
  if (_file_count > 0 && file_exist) {
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   253
    if (!is_regular_file(_file_name)) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   254
      errstream->print_cr("Unable to log to file %s with log file rotation: "
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   255
                          "%s is not a regular file",
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   256
                          _file_name, _file_name);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   257
      return false;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   258
    }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   259
    _current_file = next_file_number(_file_name,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   260
                                     _file_count_max_digits,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   261
                                     _file_count,
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   262
                                     errstream);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   263
    if (_current_file == UINT_MAX) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   264
      return false;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   265
    }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   266
    log_trace(logging)("Existing log file found, saving it as '%s.%0*u'",
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   267
                       _file_name, _file_count_max_digits, _current_file);
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   268
    archive();
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   269
    increment_file_count();
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   270
  }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   271
49984
aa3afd9bda87 8176717: GC log file handle leaked to child processes
lkorinth
parents: 49180
diff changeset
   272
  _stream = os::fopen(_file_name, FileOpenMode);
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   273
  if (_stream == NULL) {
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   274
    errstream->print_cr("Error opening log file '%s': %s",
52892
442d322bb6d8 8214944: replace strerror by os::strerror
mbaesken
parents: 51334
diff changeset
   275
                        _file_name, os::strerror(errno));
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   276
    return false;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   277
  }
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   278
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   279
  if (_file_count == 0 && is_regular_file(_file_name)) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   280
    log_trace(logging)("Truncating log file");
38065
025c784d9333 8154921: Fix aix after "8146879: Add option for handling existing log files in UL"
goetz
parents: 37465
diff changeset
   281
    os::ftruncate(os::get_fileno(_stream), 0);
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   282
  }
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   283
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   284
  return true;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   285
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   286
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   287
int LogFileOutput::write(const LogDecorations& decorations, const char* msg) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   288
  if (_stream == NULL) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   289
    // An error has occurred with this output, avoid writing to it.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   290
    return 0;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   291
  }
35230
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   292
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   293
  _rotation_semaphore.wait();
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   294
  int written = LogFileStreamOutput::write(decorations, msg);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   295
  _current_size += written;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   296
35230
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   297
  if (should_rotate()) {
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   298
    rotate();
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   299
  }
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   300
  _rotation_semaphore.signal();
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   301
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   302
  return written;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   303
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   304
38263
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   305
int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   306
  if (_stream == NULL) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   307
    // An error has occurred with this output, avoid writing to it.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   308
    return 0;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   309
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   310
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   311
  _rotation_semaphore.wait();
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   312
  int written = LogFileStreamOutput::write(msg_iterator);
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   313
  _current_size += written;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   314
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   315
  if (should_rotate()) {
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   316
    rotate();
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   317
  }
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   318
  _rotation_semaphore.signal();
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   319
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   320
  return written;
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   321
}
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 38195
diff changeset
   322
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   323
void LogFileOutput::archive() {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   324
  assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function.");
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   325
  int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u",
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   326
                         _file_name, _file_count_max_digits, _current_file);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   327
  assert(ret >= 0, "Buffer should always be large enough");
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   328
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   329
  // Attempt to remove possibly existing archived log file before we rename.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   330
  // Don't care if it fails, we really only care about the rename that follows.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   331
  remove(_archive_name);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   332
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   333
  // Rename the file from ex hotspot.log to hotspot.log.2
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   334
  if (rename(_file_name, _archive_name) == -1) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   335
    jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n",
37113
5a33bf5089ac 8148425: strerror() function is not thread-safe
stuefe
parents: 35230
diff changeset
   336
                _file_name, _archive_name, os::strerror(errno));
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   337
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   338
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   339
35230
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   340
void LogFileOutput::force_rotate() {
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   341
  if (_file_count == 0) {
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   342
    // Rotation not possible
34637
9b9298044d23 8140556: Add force rotation option to VM.log jcmd
ysuenaga
parents: 33120
diff changeset
   343
    return;
9b9298044d23 8140556: Add force rotation option to VM.log jcmd
ysuenaga
parents: 33120
diff changeset
   344
  }
35230
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   345
  _rotation_semaphore.wait();
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   346
  rotate();
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   347
  _rotation_semaphore.signal();
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   348
}
34637
9b9298044d23 8140556: Add force rotation option to VM.log jcmd
ysuenaga
parents: 33120
diff changeset
   349
35230
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   350
void LogFileOutput::rotate() {
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   351
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   352
  if (fclose(_stream)) {
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   353
    jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",
37113
5a33bf5089ac 8148425: strerror() function is not thread-safe
stuefe
parents: 35230
diff changeset
   354
                _file_name, os::strerror(errno));
35230
a528ea8203ec 8145294: TestLogRotation.java triggers a race in the UL framework
mlarsson
parents: 34637
diff changeset
   355
  }
34637
9b9298044d23 8140556: Add force rotation option to VM.log jcmd
ysuenaga
parents: 33120
diff changeset
   356
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   357
  // Archive the current log file
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   358
  archive();
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   359
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   360
  // Open the active log file using the same stream as before
49984
aa3afd9bda87 8176717: GC log file handle leaked to child processes
lkorinth
parents: 49180
diff changeset
   361
  _stream = os::fopen(_file_name, FileOpenMode);
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   362
  if (_stream == NULL) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   363
    jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n",
37113
5a33bf5089ac 8148425: strerror() function is not thread-safe
stuefe
parents: 35230
diff changeset
   364
                _file_name, os::strerror(errno));
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   365
    return;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   366
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   367
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   368
  // Reset accumulated size, increase current file counter, and check for file count wrap-around.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   369
  _current_size = 0;
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 37113
diff changeset
   370
  increment_file_count();
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   371
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   372
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   373
char* LogFileOutput::make_file_name(const char* file_name,
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   374
                                    const char* pid_string,
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   375
                                    const char* timestamp_string) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   376
  char* result = NULL;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   377
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   378
  // Lets start finding out if we have any %d and/or %t in the name.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   379
  // We will only replace the first occurrence of any placeholder
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   380
  const char* pid = strstr(file_name, PidFilenamePlaceholder);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   381
  const char* timestamp = strstr(file_name, TimestampFilenamePlaceholder);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   382
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   383
  if (pid == NULL && timestamp == NULL) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   384
    // We found no place-holders, return the simple filename
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   385
    return os::strdup_check_oom(file_name, mtLogging);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   386
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   387
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   388
  // At least one of the place-holders were found in the file_name
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   389
  const char* first = "";
33120
63aca8d49f2a 8137260: fix warning after "8046148: JEP 158: Unified JVM Logging"
goetz
parents: 33097
diff changeset
   390
  size_t first_pos = SIZE_MAX;
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   391
  size_t first_replace_len = 0;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   392
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   393
  const char* second = "";
33120
63aca8d49f2a 8137260: fix warning after "8046148: JEP 158: Unified JVM Logging"
goetz
parents: 33097
diff changeset
   394
  size_t second_pos = SIZE_MAX;
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   395
  size_t second_replace_len = 0;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   396
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   397
  // If we found a %p, then setup our variables accordingly
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   398
  if (pid != NULL) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   399
    if (timestamp == NULL || pid < timestamp) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   400
      first = pid_string;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   401
      first_pos = pid - file_name;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   402
      first_replace_len = strlen(PidFilenamePlaceholder);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   403
    } else {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   404
      second = pid_string;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   405
      second_pos = pid - file_name;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   406
      second_replace_len = strlen(PidFilenamePlaceholder);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   407
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   408
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   409
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   410
  if (timestamp != NULL) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   411
    if (pid == NULL || timestamp < pid) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   412
      first = timestamp_string;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   413
      first_pos = timestamp - file_name;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   414
      first_replace_len = strlen(TimestampFilenamePlaceholder);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   415
    } else {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   416
      second = timestamp_string;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   417
      second_pos = timestamp - file_name;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   418
      second_replace_len = strlen(TimestampFilenamePlaceholder);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   419
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   420
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   421
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   422
  size_t first_len = strlen(first);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   423
  size_t second_len = strlen(second);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   424
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   425
  // Allocate the new buffer, size it to hold all we want to put in there +1.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   426
  size_t result_len =  strlen(file_name) + first_len - first_replace_len + second_len - second_replace_len;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   427
  result = NEW_C_HEAP_ARRAY(char, result_len + 1, mtLogging);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   428
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   429
  // Assemble the strings
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   430
  size_t file_name_pos = 0;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   431
  size_t i = 0;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   432
  while (i < result_len) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   433
    if (file_name_pos == first_pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   434
      // We are in the range of the first placeholder
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   435
      strcpy(result + i, first);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   436
      // Bump output buffer position with length of replacing string
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   437
      i += first_len;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   438
      // Bump source buffer position to skip placeholder
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   439
      file_name_pos += first_replace_len;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   440
    } else if (file_name_pos == second_pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   441
      // We are in the range of the second placeholder
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   442
      strcpy(result + i, second);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   443
      i += second_len;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   444
      file_name_pos += second_replace_len;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   445
    } else {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   446
      // Else, copy char by char of the original file
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   447
      result[i] = file_name[file_name_pos++];
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   448
      i++;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   449
    }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   450
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   451
  // Add terminating char
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   452
  result[result_len] = '\0';
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   453
  return result;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   454
}
39707
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   455
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   456
void LogFileOutput::describe(outputStream *out) {
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   457
  LogOutput::describe(out);
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   458
  out->print(" ");
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   459
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   460
  out->print("filecount=%u,filesize=" SIZE_FORMAT "%s", _file_count,
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   461
             byte_size_in_proper_unit(_rotate_size),
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   462
             proper_unit_for_byte_size(_rotate_size));
4974c8df2532 8153074: UL: Show output option in VM.log jcmd
ysuenaga
parents: 38263
diff changeset
   463
}