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