lib/rotatedstream.js
function rotate(cb) {
		var retryCount
		var newName

		if (self.writable && didWrite && !rotating) { // we wrote something to the log, and are not in process of rotating
			rotating = true
			retryCount = 0
			newName = getRotatedName()
			fs.stat(newName, doesNewNameExist) // see if the new filename already exist
		} else end()

		function doesNewNameExist(err, stat) {
			if (err instanceof Error && err.code === 'ENOENT') { // we can use newName!
				buffer = [] // start logging to memory instead of the stream
				closeStream('Log rotated to: ' + path.basename(newName) + '\n', renameLog)
			} else {
				if (!err) { // newFile already exists
					if (++retryCount < maxRetry) setTimeout(retry, rotateAttemptWait)
					else end(new Error('Renamed logfile exists'))
				} else end(err) // some other error
			}
		}

		function retry() {
			//** stream.write('retry')
			if (self.writable) {
				newName = getRotatedName()
				fs.stat(newName, doesNewNameExist) // see if the new filename already exist
			} else end()
		}

		function renameLog(err) { // we are in buffer mode, closed  stream
			if (self.writable && !err) fs.rename(self.filename, newName, createWriteStream)
			else flushBuffer(err)
		}

		function createWriteStream(err) {
			if (self.writable && !err)
				if (!(err = openStream()))
					while (buffer.length && self.writable) {
						stream.write(buffer.shift())
					}
			flushBuffer(err)
		}

		function flushBuffer(err) {
			//** stream.write('flushBuffer')
			buffer = null
			rotating = false
			end(err)
		}

		function end(err) {
			if (!err) {
				if (cb) cb()
			} else {
				emitError(err)
				if (cb) cb(err)
			}
		}
	}
function rotate(cb) {
		var retryCount
		var newName

		if (self.writable && didWrite && !rotating) { // we wrote something to the log, and are not in process of rotating
			rotating = true
			retryCount = 0
			newName = getRotatedName()
			fs.stat(newName, doesNewNameExist) // see if the new filename already exist
		} else end()

		function doesNewNameExist(err, stat) {
			if (err instanceof Error && err.code === 'ENOENT') { // we can use newName!
				buffer = [] // start logging to memory instead of the stream
				closeStream('Log rotated to: ' + path.basename(newName) + '\n', renameLog)
			} else {
				if (!err) { // newFile already exists
					if (++retryCount < maxRetry) setTimeout(retry, rotateAttemptWait)
					else end(new Error('Renamed logfile exists'))
				} else end(err) // some other error
			}
		}

		function retry() {
			//** stream.write('retry')
			if (self.writable) {
				newName = getRotatedName()
				fs.stat(newName, doesNewNameExist) // see if the new filename already exist
			} else end()
		}

		function renameLog(err) { // we are in buffer mode, closed  stream
			if (self.writable && !err) fs.rename(self.filename, newName, createWriteStream)
			else flushBuffer(err)
		}

		function createWriteStream(err) {
			if (self.writable && !err)
				if (!(err = openStream()))
					while (buffer.length && self.writable) {
						stream.write(buffer.shift())
					}
			flushBuffer(err)
		}

		function flushBuffer(err) {
			//** stream.write('flushBuffer')
			buffer = null
			rotating = false
			end(err)
		}

		function end(err) {
			if (!err) {
				if (cb) cb()
			} else {
				emitError(err)
				if (cb) cb(err)
			}
		}
	}
function rotate ( cb ) { var retryCount var newName if ( self . writable && didWrite && ! rotating ) { // we wrote something to the log, and are not in process of rotating rotating = true retryCount = 0 newName = getRotatedName ( ) fs . stat ( newName , doesNewNameExist ) // see if the new filename already exist } else end ( ) function doesNewNameExist ( err , stat ) { if ( err instanceof Error && err . code === 'ENOENT' ) { // we can use newName! buffer = [ ] // start logging to memory instead of the stream closeStream ( 'Log rotated to: ' + path . basename ( newName ) + '\n' , renameLog ) } else { if ( ! err ) { // newFile already exists if ( ++ retryCount < maxRetry ) setTimeout ( retry , rotateAttemptWait ) else end ( new Error ( 'Renamed logfile exists' ) ) } else end ( err ) // some other error } } function retry ( ) { //** stream.write('retry') if ( self . writable ) { newName = getRotatedName ( ) fs . stat ( newName , doesNewNameExist ) // see if the new filename already exist } else end ( ) } function renameLog ( err ) { // we are in buffer mode, closed  stream if ( self . writable && ! err ) fs . rename ( self . filename , newName , createWriteStream ) else flushBuffer ( err ) } function createWriteStream ( err ) { if ( self . writable && ! err ) if ( ! ( err = openStream ( ) ) ) while ( buffer . length && self . writable ) { stream . write ( buffer . shift ( ) ) } flushBuffer ( err ) } function flushBuffer ( err ) { //** stream.write('flushBuffer') buffer = null rotating = false end ( err ) } function end ( err ) { if ( ! err ) { if ( cb ) cb ( ) } else { emitError ( err ) if ( cb ) cb ( err ) } } }
rotate the log
rotate the log