139 if (!configure_rotation(options)) { 140 return false; 141 } 142 _stream = fopen(_file_name, FileOpenMode); 143 if (_stream == NULL) { 144 log_error(logging)("Could not open log file '%s' (%s).\n", _file_name, strerror(errno)); 145 return false; 146 } 147 return true; 148 } 149 150 int LogFileOutput::write(const LogDecorations& decorations, const char* msg) { 151 if (_stream == NULL) { 152 // An error has occurred with this output, avoid writing to it. 153 return 0; 154 } 155 int written = LogFileStreamOutput::write(decorations, msg); 156 _current_size += written; 157 158 if (should_rotate()) { 159 MutexLockerEx ml(&_rotation_lock, true /* no safepoint check */); 160 if (should_rotate()) { 161 rotate(); 162 } 163 } 164 165 return written; 166 } 167 168 void LogFileOutput::archive() { 169 assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function."); 170 int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u", 171 _file_name, _file_count_max_digits, _current_file); 172 assert(ret >= 0, "Buffer should always be large enough"); 173 174 // Attempt to remove possibly existing archived log file before we rename. 175 // Don't care if it fails, we really only care about the rename that follows. 176 remove(_archive_name); 177 178 // Rename the file from ex hotspot.log to hotspot.log.2 179 if (rename(_file_name, _archive_name) == -1) { 180 jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n", 181 _file_name, _archive_name, strerror(errno)); 182 } 183 } 184 185 void LogFileOutput::rotate() { 186 // Archive the current log file 187 archive(); 188 189 // Open the active log file using the same stream as before 190 _stream = freopen(_file_name, FileOpenMode, _stream); 191 if (_stream == NULL) { 192 jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n", 193 _file_name, strerror(errno)); 194 return; 195 } 196 197 // Reset accumulated size, increase current file counter, and check for file count wrap-around. 198 _current_size = 0; 199 _current_file = (_current_file >= _file_count ? 1 : _current_file + 1); 200 } 201 202 char* LogFileOutput::make_file_name(const char* file_name, 203 const char* pid_string, 204 const char* timestamp_string) { 205 char* result = NULL; | 139 if (!configure_rotation(options)) { 140 return false; 141 } 142 _stream = fopen(_file_name, FileOpenMode); 143 if (_stream == NULL) { 144 log_error(logging)("Could not open log file '%s' (%s).\n", _file_name, strerror(errno)); 145 return false; 146 } 147 return true; 148 } 149 150 int LogFileOutput::write(const LogDecorations& decorations, const char* msg) { 151 if (_stream == NULL) { 152 // An error has occurred with this output, avoid writing to it. 153 return 0; 154 } 155 int written = LogFileStreamOutput::write(decorations, msg); 156 _current_size += written; 157 158 if (should_rotate()) { 159 rotate(); 160 } 161 162 return written; 163 } 164 165 void LogFileOutput::archive() { 166 assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function."); 167 int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u", 168 _file_name, _file_count_max_digits, _current_file); 169 assert(ret >= 0, "Buffer should always be large enough"); 170 171 // Attempt to remove possibly existing archived log file before we rename. 172 // Don't care if it fails, we really only care about the rename that follows. 173 remove(_archive_name); 174 175 // Rename the file from ex hotspot.log to hotspot.log.2 176 if (rename(_file_name, _archive_name) == -1) { 177 jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n", 178 _file_name, _archive_name, strerror(errno)); 179 } 180 } 181 182 void LogFileOutput::rotate() { 183 MutexLockerEx ml(&_rotation_lock, true /* no safepoint check */); 184 185 // Archive the current log file 186 archive(); 187 188 // Open the active log file using the same stream as before 189 _stream = freopen(_file_name, FileOpenMode, _stream); 190 if (_stream == NULL) { 191 jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n", 192 _file_name, strerror(errno)); 193 return; 194 } 195 196 // Reset accumulated size, increase current file counter, and check for file count wrap-around. 197 _current_size = 0; 198 _current_file = (_current_file >= _file_count ? 1 : _current_file + 1); 199 } 200 201 char* LogFileOutput::make_file_name(const char* file_name, 202 const char* pid_string, 203 const char* timestamp_string) { 204 char* result = NULL; |