From 14fb2fad862f46fa24ac927ae0fe02a8b28d0697 Mon Sep 17 00:00:00 2001 From: Masatoshi Nishiguchi Date: Fri, 28 Oct 2022 20:31:03 -0400 Subject: [PATCH] Add count_next function (#99) --- lib/ring_logger.ex | 11 +++++++++++ lib/ring_logger/autoclient.ex | 9 +++++++++ lib/ring_logger/client.ex | 16 ++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/lib/ring_logger.ex b/lib/ring_logger.ex index 2c63531..81a5b7e 100644 --- a/lib/ring_logger.ex +++ b/lib/ring_logger.ex @@ -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 diff --git a/lib/ring_logger/autoclient.ex b/lib/ring_logger/autoclient.ex index 0ebde7f..157859e 100644 --- a/lib/ring_logger/autoclient.ex +++ b/lib/ring_logger/autoclient.ex @@ -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. """ diff --git a/lib/ring_logger/client.ex b/lib/ring_logger/client.ex index 2b9d6d3..73f5794 100644 --- a/lib/ring_logger/client.ex +++ b/lib/ring_logger/client.ex @@ -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. """ @@ -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)