anchor.diff:  
   %% internal functions
   log_msg(Msg, #state{buffer = Buffer,
  -                    buffer_size = BufferSize,
  -                    batch_size = BatchSize} = State) ->
  +                    buffer_size = BufferSize} = State) ->
       NewBuffer = <<Buffer/binary, Msg/binary>>,
       NewBufferSize = BufferSize + byte_size(Msg),
   
       NewState = State#state{buffer = NewBuffer,
                              buffer_size = NewBufferSize},
   
  -    case NewBufferSize >= BatchSize of
  +    maybe_flush_buffer(NewState).
  +
  +maybe_flush_buffer(#state{buffer_size = BufferSize,
  +                          batch_size = BatchSize} = State) ->
  +    case BufferSize >= BatchSize of
           true ->
  -            flush_buffer(NewState);
  +            flush_buffer(State);
           false ->
  -            maybe_arm_flush_timer(NewState)
  -    end.
  -
  -flush_buffer(State) ->
  -    NewState = do_flush_buffer(State),
  -    maybe_rotate_files(NewState).
  -
  -do_flush_buffer(#state{file = File,
  -                       file_size = FileSize,
  -                       buffer = Buffer,
  -                       buffer_size = BufferSize} = State) ->
  -    case BufferSize =:= 0 of
  -        true ->
  -            State;
  -        false ->
  -            ok = file:write(File, Buffer),
  -            NewState = State#state{file_size = FileSize + BufferSize,
  -                                   buffer = <<>>,
  -                                   buffer_size = 0},
  -            cancel_flush_timer(NewState)
  -    end.
  +            maybe_arm_flush_timer(State)
  +    end.
  +
  +flush_buffer(#state{worker = Worker,
  +                    buffer = Buffer,
  +                    buffer_size = BufferSize} = State) ->
  +    Worker ! {write, Buffer, BufferSize},
  +    cancel_flush_timer(State#state{buffer = <<>>,
  +                                   buffer_size = 0}).
   
   maybe_arm_flush_timer(#state{flush_timer = undefined,
                                batch_timeout = Timeout} = State) ->
  
positive.code: cancel_flush_timer(State) ->
      State.
  
  sync(State) ->
      NewState = flush_buffer(State),
      file:datasync(NewState#state.file),
      NewState.
  
  rotate_files(#state{path = Path,
                      rotation_num_files = NumFiles,
                      rotation_compress = Compress}) ->
      Suffix = case Compress of
                   true ->
                       ".gz";
  
negative.code:         false ->
              ok
      end.
  
  spawn_worker(WorkerState) ->
      proc_lib:spawn_link(
        fun () ->
                worker_init(WorkerState)
        end).
  
  worker_init(#worker_state{rotation_check_interval = RotCheckInterval} = State0) ->
      case RotCheckInterval > 0 of
          true ->
              erlang:send_after(RotCheckInterval, self(), check_file);
          false ->
              ok
      end,
      worker_loop(open_log_file(State0)).
  
  worker_loop(State) ->
      NewState =
          receive
              {write, Data0, DataSize0} ->
                  {Data, DataSize} = receive_more_writes(Data0, DataSize0),
                  write_data(Data, DataSize, State);
              check_file ->
                  erlang:send_after(State#worker_state.rotation_check_interval,
                                    self(), check_file),
                  check_log_file(State);
              {'$gen_call', From, nop} ->
                  gen_server:reply(From, ok),
                  State;
              {'$gen_call', From, sync} ->
                  file:datasync(State#worker_state.file),
                  gen_server:reply(From, ok),
                  State;
              Msg ->
                  exit({unexpected_msg, Msg})
          end,
  
      worker_loop(NewState).
  
  receive_more_writes(Data, DataSize) ->
      receive
          {write, MoreData, MoreDataSize} ->
              receive_more_writes(<<Data/binary, MoreData/binary>>,
                                  DataSize + MoreDataSize)
      after
          0 ->
              {Data, DataSize}
      end.
  
  write_data(Data, DataSize,
             #worker_state{file = File,
                           file_size = FileSize} = State) ->
      ok = file:write(File, Data),
      NewState = State#worker_state{file_size = FileSize + DataSize},
      maybe_rotate_files(NewState).
  