3333#include " utilities/globalDefinitions.hpp"
3434#include " utilities/defaultStream.hpp"
3535
36+ class RotationLocker : public StackObj {
37+ Semaphore& _sem;
38+
39+ public:
40+ RotationLocker (Semaphore& sem) : _sem(sem) {
41+ sem.wait ();
42+ }
43+
44+ ~RotationLocker () {
45+ _sem.signal ();
46+ }
47+ };
48+
3649const char * const LogFileOutput::Prefix = " file=" ;
3750const char * const LogFileOutput::FileOpenMode = " a" ;
3851const char * const LogFileOutput::PidFilenamePlaceholder = " %p" ;
@@ -286,7 +299,7 @@ bool LogFileOutput::initialize(const char* options, outputStream* errstream) {
286299}
287300
288301int LogFileOutput::write_blocking (const LogDecorations& decorations, const char * msg) {
289- _rotation_semaphore. wait ( );
302+ RotationLocker lock (_rotation_semaphore );
290303 if (_stream == NULL ) {
291304 // An error has occurred with this output, avoid writing to it.
292305 return 0 ;
@@ -300,7 +313,6 @@ int LogFileOutput::write_blocking(const LogDecorations& decorations, const char*
300313 rotate ();
301314 }
302315 }
303- _rotation_semaphore.signal ();
304316
305317 return written;
306318}
@@ -332,7 +344,7 @@ int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
332344 return 0 ;
333345 }
334346
335- _rotation_semaphore. wait ( );
347+ RotationLocker lock (_rotation_semaphore );
336348 int written = LogFileStreamOutput::write (msg_iterator);
337349 if (written > 0 ) {
338350 _current_size += written;
@@ -341,7 +353,6 @@ int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
341353 rotate ();
342354 }
343355 }
344- _rotation_semaphore.signal ();
345356
346357 return written;
347358}
@@ -368,13 +379,11 @@ void LogFileOutput::force_rotate() {
368379 // Rotation not possible
369380 return ;
370381 }
371- _rotation_semaphore. wait ( );
382+ RotationLocker lock (_rotation_semaphore );
372383 rotate ();
373- _rotation_semaphore.signal ();
374384}
375385
376386void LogFileOutput::rotate () {
377-
378387 if (fclose (_stream)) {
379388 jio_fprintf (defaultStream::error_stream (), " Error closing file '%s' during log rotation (%s).\n " ,
380389 _file_name, os::strerror (errno));
0 commit comments