Skip to content

Commit

Permalink
Support strings being passed to grep
Browse files Browse the repository at this point in the history
This also returns a nicer error if a Regex or String isn't passed.
  • Loading branch information
mobileoverlord committed Nov 14, 2018
1 parent 429f75a commit 7535625
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/ring_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ defmodule RingLogger do
* Options from `attach/1`
* `:pager` - a function for printing log messages to the console. Defaults to `IO.binwrite/2`.
"""
@spec grep(Regex.t(), [client_option]) :: :ok | {:error, term()}
defdelegate grep(regex, opts \\ []), to: Autoclient
@spec grep(Regex.t() | String.t(), [client_option]) :: :ok | {:error, term()}
defdelegate grep(regex_or_string, opts \\ []), to: Autoclient

@doc """
Helper method for formatting log messages per the current client's
Expand Down
4 changes: 2 additions & 2 deletions lib/ring_logger/autoclient.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ defmodule RingLogger.Autoclient do
@doc """
Run a regular expression on each entry in the log and print out the matchers.
"""
def grep(regex, opts \\ []) do
def grep(regex_or_string, opts \\ []) do
with :ok <- check_server_started(),
pid <- maybe_create_client(opts),
do: Client.grep(pid, regex, opts)
do: Client.grep(pid, regex_or_string, opts)
end

@doc """
Expand Down
16 changes: 14 additions & 2 deletions lib/ring_logger/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,26 @@ defmodule RingLogger.Client do
* `:pager` - an optional 2-arity function that takes an IO device and what to print
"""
@spec grep(GenServer.server(), Regex.t(), keyword()) :: :ok | {:error, term()}
def grep(client_pid, regex, opts \\ []) do
@spec grep(GenServer.server(), String.t() | Regex.t(), keyword()) :: :ok | {:error, term()}
def grep(client_pid, regex_or_string, opts \\ [])

def grep(client_pid, regex_string, opts) when is_binary(regex_string) do
with {:ok, regex} <- Regex.compile(regex_string) do
grep(client_pid, regex, opts)
end
end

def grep(client_pid, %Regex{} = regex, opts) do
{io, to_print} = GenServer.call(client_pid, {:grep, regex})

pager = Keyword.get(opts, :pager, &IO.binwrite/2)
pager.(io, to_print)
end

def grep(_client_pid, _regex, _opts) do
{:error, :invalid_regex}
end

def init(config) do
state = %State{
io: Keyword.get(config, :io, :stdio),
Expand Down
16 changes: 16 additions & 0 deletions test/ring_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ defmodule RingLoggerTest do
assert message =~ "[debug] Hello, world"
end

test "can grep using a string", %{io: io} do
:ok = RingLogger.attach(io: io)

io
|> handshake_log(:debug, "Hello")
|> handshake_log(:debug, "World")

RingLogger.grep("H..lo", io: io)
assert_receive {:io, message}
assert message =~ "[debug] Hello"
end

test "invalid regex returns error", %{io: io} do
assert {:error, _} = RingLogger.grep(5, io: io)
end

test "can next the log", %{io: io} do
:ok = RingLogger.attach(io: io)
handshake_log(io, :debug, "Hello")
Expand Down

0 comments on commit 7535625

Please sign in to comment.