anchor.diff:              Dedup2 = dict:store(Key, {0, Now, Now}, Dedup),
               {noreply, State#state{dedup=Dedup2}}
       end;
  -handle_cast({do_log, Entry}, State = #state{recent=Recent}) ->
  -    {noreply, schedule_save(State#state{recent=lists:umerge([Entry], Recent)})};
  -handle_cast({sync, Compressed}, State = #state{recent=Recent}) ->
  +handle_cast({do_log, Entry}, State) ->
  +    {noreply, schedule_save(add_pending(State, Entry))};
  +handle_cast({sync, Compressed}, StateBefore) ->
  +    State = flush_pending(StateBefore),
  +    Recent = State#state.unique_recent,
       case binary_to_term(zlib:uncompress(Compressed)) of
           Recent ->
               {noreply, State};
           Logs ->
               State1 = schedule_save(State),
  -            {noreply, State1#state{recent=lists:sublist(
  -                                            lists:umerge(Recent, Logs),
  -                                            ?RB_SIZE)}}
  -    end.
  -
  -% Not handling any other state.
  -
  -% Nothing special.
  +            {noreply, State1#state{unique_recent=tail_of_length(lists:umerge(Recent, Logs),
  +                                                                ?RB_SIZE)}}
  +    end.
  +
  +%% Not handling any other state.
  +
  +%% Nothing special.
   handle_info(garbage_collect, State) ->
       misc:flush(garbage_collect),
       {noreply, gc(State)};
  -handle_info(sync, State = #state{recent=Recent}) ->
  -    timer:send_after(5000 + random:uniform(55000), sync),
  -    Nodes = ns_node_disco:nodes_actual_other(),
  -    Node = lists:nth(random:uniform(length(Nodes)), Nodes),
  -    gen_server:cast({?MODULE, Node}, {sync, zlib:compress(term_to_binary(Recent))}),
  +handle_info(sync, StateBefore) ->
  +    State = flush_pending(StateBefore),
  +    Recent = State#state.unique_recent,
  +    erlang:send_after(5000 + random:uniform(55000), self(), sync),
  +    case ns_node_disco:nodes_actual_other() of
  +        [] -> ok;
  +        Nodes ->
  +            Node = lists:nth(random:uniform(length(Nodes)), Nodes),
  +            gen_server:cast({?MODULE, Node}, {sync, zlib:compress(term_to_binary(Recent))})
  +    end,
       {noreply, State};
  -handle_info(save, State = #state{filename=Filename, recent=Recent}) ->
  +handle_info(save, StateBefore = #state{filename=Filename}) ->
  +    State = flush_pending(StateBefore),
  +    Recent = State#state.unique_recent,
       Compressed = zlib:compress(term_to_binary(Recent)),
       case file:write_file(Filename, Compressed) of
           ok -> ok;
  
positive.code:                        [?MODULE, Filename, E]),
                       []
               end,
      {ok, #state{recent=Recent, dedup=dict:new(), filename=Filename}}.
  
  % Request for recent items.
  handle_call(recent, _From, State) ->
      {reply, State#state.recent, State}.
  
  % Inbound logging request.
  handle_cast({log, Module, Code, Fmt, Args}, State = #state{dedup=Dedup}) ->
      Now = erlang:now(),
      Key = {Module, Code, Fmt, Args},
  
negative.code: 
  -include_lib("eunit/include/eunit.hrl").
  
  -record(state, {unique_recent,
                  dedup,
                  save_tref,
                  filename,
                  pending_recent = [],
                  pending_length = 0}).
  
  start_link() ->
      gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  