Skip to content

Commit 2d90d8a

Browse files
author
José Valim
committed
Remove code duplication on watcher
1 parent a8b2cb9 commit 2d90d8a

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

lib/logger/lib/logger/watcher.ex

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,17 @@ defmodule Logger.Watcher do
5858
@doc false
5959
def init({mod, handler, args, :monitor}) do
6060
ref = Process.monitor(mod)
61+
res = GenEvent.add_handler(mod, handler, args, monitor: true)
62+
do_init(res, mod, handler, ref)
63+
end
64+
65+
def init({mod, handler, args, :link}) do
66+
res = :gen_event.add_sup_handler(mod, handler, args)
67+
do_init(res, mod, handler, nil)
68+
end
6169

62-
case GenEvent.add_handler(mod, handler, args, monitor: true) do
70+
defp do_init(res, mod, handler, ref) do
71+
case res do
6372
:ok ->
6473
{:ok, {mod, handler, ref}}
6574
{:error, :ignore} ->
@@ -73,18 +82,6 @@ defmodule Logger.Watcher do
7382
end
7483
end
7584

76-
def init({mod, handler, args, :link}) do
77-
case :gen_event.add_sup_handler(mod, handler, args) do
78-
:ok ->
79-
{:ok, {mod, handler, nil}}
80-
{:error, :ignore} ->
81-
send(self(), {:gen_event_EXIT, handler, :normal})
82-
{:ok, {mod, handler, nil}}
83-
{:error, reason} ->
84-
{:stop, reason}
85-
end
86-
end
87-
8885
@doc false
8986
def handle_info({:gen_event_EXIT, handler, reason}, {_, handler, _} = state)
9087
when reason in [:normal, :shutdown] do

0 commit comments

Comments
 (0)