def tail_log_file(filename, offset, nlines, callback=None):
 def seek_file(filehandle, offset, nlines, callback):
  'step backwards in chunks and stop only when we have enough lines'
  lines_found = []
  buffer_size = 4098
  block_count = (-1)
  while (len(lines_found) < (offset + nlines)):
   try:
    filehandle.seek((block_count * buffer_size), os.SEEK_END)
   except IOError:
    filehandle.seek(0)
    lines_found = filehandle.readlines()
    break
   lines_found = filehandle.readlines()
   block_count -= 1
  lines_found = lines_found[((- nlines) - offset):((- offset) if offset else None)]
  if callback:
   callback(lines_found)
  else:
   return lines_found
 def errback(failure):
  'Catching errors to normal log'
  log_trace()
 filehandle = _open_log_file(filename)
 if filehandle:
  if callback:
   return deferToThread(seek_file, filehandle, offset, nlines, callback).addErrback(errback)
  else:
   return seek_file(filehandle, offset, nlines, callback)

def tail_log_file ( filename , offset , nlines , callback = None ) : def seek_file ( filehandle , offset , nlines , callback ) : lines_found = [ ] buffer_size = 4098 block_count = ( - 1 ) while ( len ( lines_found ) < ( offset + nlines ) ) : try : filehandle . seek ( ( block_count * buffer_size ) , os . SEEK_END ) except IOError : filehandle . seek ( 0 ) lines_found = filehandle . readlines ( ) break lines_found = filehandle . readlines ( ) block_count -= 1 lines_found = lines_found [ ( ( - nlines ) - offset ) : ( ( - offset ) if offset else None ) ] if callback : callback ( lines_found ) else : return lines_found def errback ( failure ) : log_trace ( ) filehandle = _open_log_file ( filename ) if filehandle : if callback : return deferToThread ( seek_file , filehandle , offset , nlines , callback ) . addErrback ( errback ) else : return seek_file ( filehandle , offset , nlines , callback )
return the tail of the log file .