def _check_need_new_logfile(self): """if need new log file""" if os.path.exists(self._loglist_switch) and \ (not self._loglist_switching): # start to switch log.info('{0} loglist start to switch'.format(self._name)) self._loglist_switching = True self._loglist_stream.write('NEED_SWITCH_LOCALFILE\n') self._loglist_stream.flush() os.fsync(self._loglist_stream) self._loglist_stream.close() if not os.path.exists(self._logfile_listnew): try: exfile.mk_newnode(self._logfile_listnew) # pylint: disable=W0703 except Exception as err: log.error('switch loglist file failed:{0}'.format(err)) return False log.info('{0} loglist file {1} switched'.format( self._name, self._logfile_list) ) os.rename(self._logfile_list, self._logfile_listold) os.rename(self._logfile_listnew, self._logfile_list) self._loglist_stream = open(self._logfile_list, 'a') self._loglist_switching = False if self._current_filesize >= self._max_log_file_size: # log.info('serilizer file needs moving to a new one') last_logid = self._writestream.name.split('.')[-1] newname = os.path.normpath('{0}/done.{1}'.format( os.path.dirname(self._writestream.name), last_logid )) log.info( 'finish one log file, logid, range:{0}-{1}'.format( last_logid, self._logid - 1 ) ) os.rename(self._writestream.name, newname) self._stream_close() self._loglist_stream.write('{0}\n'.format(newname)) self._loglist_stream.flush() os.fsync(self._loglist_stream.fileno()) self._current_filesize = 0 fname = self._get_next_logfile(self._logid) if not self._stream_wbopen(fname): return False return True
def open4write(self, truncate_last_failure=True): """ open4write :raise Exception: if encounter any IOError, will raise IOError(errmsg) """ try: if not os.path.exists(self._logfile_list): exfile.mk_newnode(self._logfile_list) self._loglist_stream = open(self._logfile_list, 'a') except Exception as err: log.error('cannot create loglist, raise IOError') raise IOError('cannot create loglist, {0}'.format(err)) log.info( '{0} try to recover from last ' 'write if there is any need, truncate_last_failure:{1}'.format( self._name, truncate_last_failure)) self._recover_from_lastwriting(truncate_last_failure)
def switch_logfilelist(self): """switch logfile to logfile.old""" log.info('serializer ({0}) to swtich loglist'.format(self._name)) if not os.path.exists(self._loglist_switch): exfile.mk_newnode(self._loglist_switch)