1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 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" 25 #include "logging/log.hpp" 26 #include "logging/logConfiguration.hpp" 27 #include "logging/logFileOutput.hpp" 28 #include "memory/allocation.inline.hpp" 29 #include "runtime/os.inline.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 #include "utilities/defaultStream.hpp" 32 33 const char* LogFileOutput::FileOpenMode = "a"; 34 const char* LogFileOutput::PidFilenamePlaceholder = "%p"; 35 const char* LogFileOutput::TimestampFilenamePlaceholder = "%t"; 36 const char* LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S"; 37 const char* LogFileOutput::FileSizeOptionKey = "filesize"; 38 const char* LogFileOutput::FileCountOptionKey = "filecount"; 39 char LogFileOutput::_pid_str[PidBufferSize]; 40 char LogFileOutput::_vm_start_time_str[StartTimeBufferSize]; 41 42 LogFileOutput::LogFileOutput(const char* name) 43 : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)), 44 _file_name(NULL), _archive_name(NULL), _archive_name_len(0), _current_size(0), 45 _rotate_size(0), _current_file(1), _file_count(0), _rotation_semaphore(1) { 46 _file_name = make_file_name(name, _pid_str, _vm_start_time_str); 47 } 48 49 void LogFileOutput::set_file_name_parameters(jlong vm_start_time) { 50 int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id()); 51 assert(res > 0, "PID buffer too small"); 52 53 struct tm local_time; 54 time_t utc_time = vm_start_time / 1000; 55 os::localtime_pd(&utc_time, &local_time); 56 res = (int)strftime(_vm_start_time_str, sizeof(_vm_start_time_str), TimestampFormat, &local_time); 57 assert(res > 0, "VM start time buffer too small."); 58 } 59 60 LogFileOutput::~LogFileOutput() { 61 if (_stream != NULL) { 62 if (_archive_name != NULL) { 63 archive(); 64 } 65 if (fclose(_stream) != 0) { 66 jio_fprintf(defaultStream::error_stream(), "Could not close log file '%s' (%s).\n", 67 _file_name, strerror(errno)); 68 } 69 } 70 os::free(_archive_name); 71 os::free(_file_name); 72 os::free(const_cast<char*>(_name)); 73 } 74 75 size_t LogFileOutput::parse_value(const char* value_str) { 76 char* end; 77 unsigned long long value = strtoull(value_str, &end, 10); 78 if (!isdigit(*value_str) || end != value_str + strlen(value_str) || value >= SIZE_MAX) { 79 return SIZE_MAX; 80 } 81 return value; 82 } 83 84 bool LogFileOutput::configure_rotation(const char* options) { 85 if (options == NULL || strlen(options) == 0) { 86 return true; 87 } 88 bool success = true; 89 char* opts = os::strdup_check_oom(options, mtLogging); 90 91 char* comma_pos; 92 char* pos = opts; 93 do { 94 comma_pos = strchr(pos, ','); 95 if (comma_pos != NULL) { 96 *comma_pos = '\0'; 97 } 98 99 char* equals_pos = strchr(pos, '='); 100 if (equals_pos == NULL) { 101 success = false; 102 break; 103 } 104 char* key = pos; 105 char* value_str = equals_pos + 1; 106 *equals_pos = '\0'; 107 108 if (strcmp(FileCountOptionKey, key) == 0) { 109 size_t value = parse_value(value_str); 110 if (value == SIZE_MAX || value >= UINT_MAX) { 111 success = false; 112 break; 113 } 114 _file_count = static_cast<uint>(value); 115 _file_count_max_digits = static_cast<uint>(log10(static_cast<double>(_file_count)) + 1); 116 _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits; 117 _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging); 118 } else if (strcmp(FileSizeOptionKey, key) == 0) { 119 size_t value = parse_value(value_str); 120 if (value == SIZE_MAX || value > SIZE_MAX / K) { 121 success = false; 122 break; 123 } 124 _rotate_size = value * K; 125 } else { 126 success = false; 127 break; 128 } 129 pos = comma_pos + 1; 130 } while (comma_pos != NULL); 131 132 os::free(opts); 133 return success; 134 } 135 136 bool LogFileOutput::initialize(const char* options) { 137 if (!configure_rotation(options)) { 138 return false; 139 } 140 141 // Remove any existing log file in order to replace it (rather than append it) 142 remove(_file_name); 143 144 _stream = fopen(_file_name, FileOpenMode); 145 if (_stream == NULL) { 146 log_error(logging)("Could not open log file '%s' (%s).\n", _file_name, strerror(errno)); 147 return false; 148 } 149 return true; 150 } 151 152 int LogFileOutput::write(const LogDecorations& decorations, const char* msg) { 153 if (_stream == NULL) { 154 // An error has occurred with this output, avoid writing to it. 155 return 0; 156 } 157 158 _rotation_semaphore.wait(); 159 int written = LogFileStreamOutput::write(decorations, msg); 160 _current_size += written; 161 162 if (should_rotate()) { 163 rotate(); 164 } 165 _rotation_semaphore.signal(); 166 167 return written; 168 } 169 170 void LogFileOutput::archive() { 171 assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function."); 172 int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u", 173 _file_name, _file_count_max_digits, _current_file); 174 assert(ret >= 0, "Buffer should always be large enough"); 175 176 // Attempt to remove possibly existing archived log file before we rename. 177 // Don't care if it fails, we really only care about the rename that follows. 178 remove(_archive_name); 179 180 // Rename the file from ex hotspot.log to hotspot.log.2 181 if (rename(_file_name, _archive_name) == -1) { 182 jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n", 183 _file_name, _archive_name, strerror(errno)); 184 } 185 } 186 187 void LogFileOutput::force_rotate() { 188 if (_file_count == 0) { 189 // Rotation not possible 190 return; 191 } 192 _rotation_semaphore.wait(); 193 rotate(); 194 _rotation_semaphore.signal(); 195 } 196 197 void LogFileOutput::rotate() { 198 199 if (fclose(_stream)) { 200 jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n", 201 _file_name, strerror(errno)); 202 } 203 204 // Archive the current log file 205 archive(); 206 207 // Open the active log file using the same stream as before 208 _stream = fopen(_file_name, FileOpenMode); 209 if (_stream == NULL) { 210 jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n", 211 _file_name, strerror(errno)); 212 return; 213 } 214 215 // Reset accumulated size, increase current file counter, and check for file count wrap-around. 216 _current_size = 0; 217 _current_file = (_current_file >= _file_count ? 1 : _current_file + 1); 218 } 219 220 char* LogFileOutput::make_file_name(const char* file_name, 221 const char* pid_string, 222 const char* timestamp_string) { 223 char* result = NULL; 224 225 // Lets start finding out if we have any %d and/or %t in the name. 226 // We will only replace the first occurrence of any placeholder 227 const char* pid = strstr(file_name, PidFilenamePlaceholder); 228 const char* timestamp = strstr(file_name, TimestampFilenamePlaceholder); 229 230 if (pid == NULL && timestamp == NULL) { 231 // We found no place-holders, return the simple filename 232 return os::strdup_check_oom(file_name, mtLogging); 233 } 234 235 // At least one of the place-holders were found in the file_name 236 const char* first = ""; 237 size_t first_pos = SIZE_MAX; 238 size_t first_replace_len = 0; 239 240 const char* second = ""; 241 size_t second_pos = SIZE_MAX; 242 size_t second_replace_len = 0; 243 244 // If we found a %p, then setup our variables accordingly 245 if (pid != NULL) { 246 if (timestamp == NULL || pid < timestamp) { 247 first = pid_string; 248 first_pos = pid - file_name; 249 first_replace_len = strlen(PidFilenamePlaceholder); 250 } else { 251 second = pid_string; 252 second_pos = pid - file_name; 253 second_replace_len = strlen(PidFilenamePlaceholder); 254 } 255 } 256 257 if (timestamp != NULL) { 258 if (pid == NULL || timestamp < pid) { 259 first = timestamp_string; 260 first_pos = timestamp - file_name; 261 first_replace_len = strlen(TimestampFilenamePlaceholder); 262 } else { 263 second = timestamp_string; 264 second_pos = timestamp - file_name; 265 second_replace_len = strlen(TimestampFilenamePlaceholder); 266 } 267 } 268 269 size_t first_len = strlen(first); 270 size_t second_len = strlen(second); 271 272 // Allocate the new buffer, size it to hold all we want to put in there +1. 273 size_t result_len = strlen(file_name) + first_len - first_replace_len + second_len - second_replace_len; 274 result = NEW_C_HEAP_ARRAY(char, result_len + 1, mtLogging); 275 276 // Assemble the strings 277 size_t file_name_pos = 0; 278 size_t i = 0; 279 while (i < result_len) { 280 if (file_name_pos == first_pos) { 281 // We are in the range of the first placeholder 282 strcpy(result + i, first); 283 // Bump output buffer position with length of replacing string 284 i += first_len; 285 // Bump source buffer position to skip placeholder 286 file_name_pos += first_replace_len; 287 } else if (file_name_pos == second_pos) { 288 // We are in the range of the second placeholder 289 strcpy(result + i, second); 290 i += second_len; 291 file_name_pos += second_replace_len; 292 } else { 293 // Else, copy char by char of the original file 294 result[i] = file_name[file_name_pos++]; 295 i++; 296 } 297 } 298 // Add terminating char 299 result[result_len] = '\0'; 300 return result; 301 }