Skip to content

Commit

Permalink
Add count_next function (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnishiguchi authored Oct 29, 2022
1 parent f1bbcce commit 14fb2fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/ring_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ defmodule RingLogger do
@spec next([client_option]) :: :ok | {:error, term()}
defdelegate next(opts \\ []), to: Autoclient

@doc """
Count the next messages in the log.
Options include:
* Options from `attach/1`
* `:pager` - a function for printing log messages to the console. Defaults to `IO.binwrite/2`.
"""
@spec count_next([client_option]) :: non_neg_integer() | {:error, term()}
defdelegate count_next(opts \\ []), to: Autoclient

@doc """
Save the contents of the log to the specified path
Expand Down
9 changes: 9 additions & 0 deletions lib/ring_logger/autoclient.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ defmodule RingLogger.Autoclient do
do: Client.next(pid, opts)
end

@doc """
Print the log message count since the previous time this was called.
"""
def count_next(opts \\ []) do
with :ok <- check_server_started(),
pid <- maybe_create_client(opts),
do: Client.count_next(pid)
end

@doc """
Print the most recent log messages.
"""
Expand Down
16 changes: 16 additions & 0 deletions lib/ring_logger/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ defmodule RingLogger.Client do
pager.(io, to_print)
end

@doc """
Count the next set of the messages in the log.
"""
@spec count_next(GenServer.server()) :: non_neg_integer() | {:error, term()}
def count_next(client_pid) do
GenServer.call(client_pid, :count_next)
end

@doc """
Reset the index into the log for `tail/1` to the oldest entry.
"""
Expand Down Expand Up @@ -226,6 +234,14 @@ defmodule RingLogger.Client do
end
end

def handle_call(:count_next, _from, state) do
count =
Server.get(state.index, 0)
|> Enum.count(&should_print?(&1, state))

{:reply, count, state}
end

def handle_call({:tail, n}, _from, state) do
to_return =
Server.tail(n)
Expand Down

0 comments on commit 14fb2fa

Please sign in to comment.