< prev index next >

src/share/vm/logging/logFileOutput.cpp

Print this page




 138 bool LogFileOutput::initialize(const char* options) {
 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;




 138 bool LogFileOutput::initialize(const char* options) {
 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   rotate(false);





 159 
 160   return written;
 161 }
 162 
 163 void LogFileOutput::archive() {
 164   assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function.");
 165   int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u",
 166                          _file_name, _file_count_max_digits, _current_file);
 167   assert(ret >= 0, "Buffer should always be large enough");
 168 
 169   // Attempt to remove possibly existing archived log file before we rename.
 170   // Don't care if it fails, we really only care about the rename that follows.
 171   remove(_archive_name);
 172 
 173   // Rename the file from ex hotspot.log to hotspot.log.2
 174   if (rename(_file_name, _archive_name) == -1) {
 175     jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n",
 176                 _file_name, _archive_name, strerror(errno));
 177   }
 178 }
 179 
 180 void LogFileOutput::rotate(bool force) {
 181 
 182   if (!should_rotate(force)) {
 183     return;
 184   }
 185 
 186   MutexLockerEx ml(&_rotation_lock, true /* no safepoint check */);
 187 
 188   // Archive the current log file
 189   archive();
 190 
 191   // Open the active log file using the same stream as before
 192   _stream = freopen(_file_name, FileOpenMode, _stream);
 193   if (_stream == NULL) {
 194     jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n",
 195                 _file_name, strerror(errno));
 196     return;
 197   }
 198 
 199   // Reset accumulated size, increase current file counter, and check for file count wrap-around.
 200   _current_size = 0;
 201   _current_file = (_current_file >= _file_count ? 1 : _current_file + 1);
 202 }
 203 
 204 char* LogFileOutput::make_file_name(const char* file_name,
 205                                     const char* pid_string,
 206                                     const char* timestamp_string) {
 207   char* result = NULL;


< prev index next >