-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Use Erlang logger handlers in ExUnit.CaptureLog #12408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bbbd19c
Use Erlang logger handlers in ExUnit.CaptureLog
whatyouhide 89874cd
Add handler
whatyouhide f8a7dbb
Add more stuff
whatyouhide 2493347
More
whatyouhide 9c70847
FIXUP
whatyouhide 7a3ee23
Update lib/ex_unit/lib/ex_unit/capture_server.ex
whatyouhide 796214e
No warnings
whatyouhide b58b31c
Update lib/ex_unit/test/ex_unit/capture_log_test.exs
whatyouhide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ defmodule ExUnit.CaptureServer do | |||||||||
@compile {:no_warn_undefined, Logger} | ||||||||||
@timeout :infinity | ||||||||||
@name __MODULE__ | ||||||||||
@ets __MODULE__ | ||||||||||
|
||||||||||
use GenServer | ||||||||||
|
||||||||||
|
@@ -22,8 +23,8 @@ defmodule ExUnit.CaptureServer do | |||||||||
GenServer.call(@name, {:device_capture_off, ref}, @timeout) | ||||||||||
end | ||||||||||
|
||||||||||
def log_capture_on(pid) do | ||||||||||
GenServer.call(@name, {:log_capture_on, pid}, @timeout) | ||||||||||
def log_capture_on(pid, string_io, opts) do | ||||||||||
GenServer.call(@name, {:log_capture_on, pid, string_io, opts}, @timeout) | ||||||||||
end | ||||||||||
|
||||||||||
def log_capture_off(ref) do | ||||||||||
|
@@ -32,7 +33,10 @@ defmodule ExUnit.CaptureServer do | |||||||||
|
||||||||||
## Callbacks | ||||||||||
|
||||||||||
@impl true | ||||||||||
def init(:ok) do | ||||||||||
:ets.new(@name, [:named_table, :public, :set]) | ||||||||||
|
||||||||||
state = %{ | ||||||||||
devices: %{}, | ||||||||||
log_captures: %{}, | ||||||||||
|
@@ -42,6 +46,9 @@ defmodule ExUnit.CaptureServer do | |||||||||
{:ok, state} | ||||||||||
end | ||||||||||
|
||||||||||
@impl true | ||||||||||
def handle_call(call, from, state) | ||||||||||
|
||||||||||
def handle_call({:device_capture_on, name, encoding, input}, {caller, _}, config) do | ||||||||||
capture_device(name, encoding, input, config, caller) | ||||||||||
end | ||||||||||
|
@@ -59,11 +66,17 @@ defmodule ExUnit.CaptureServer do | |||||||||
{:reply, :ok, release_device(ref, config)} | ||||||||||
end | ||||||||||
|
||||||||||
def handle_call({:log_capture_on, pid}, _from, config) do | ||||||||||
def handle_call({:log_capture_on, pid, string_io, opts}, _from, config) do | ||||||||||
ref = Process.monitor(pid) | ||||||||||
refs = Map.put(config.log_captures, ref, true) | ||||||||||
|
||||||||||
{level, opts} = Keyword.pop(opts, :level) | ||||||||||
true = :ets.insert(@ets, {ref, string_io, level || :all}) | ||||||||||
Comment on lines
+73
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
if map_size(refs) == 1 do | ||||||||||
formatter = Logger.default_formatter(opts) | ||||||||||
:ok = :logger.add_handler(@name, __MODULE__, %{formatter: formatter}) | ||||||||||
|
||||||||||
status = | ||||||||||
with {:ok, config} <- :logger.get_handler_config(:default), | ||||||||||
:ok <- :logger.remove_handler(:default) do | ||||||||||
|
@@ -84,6 +97,7 @@ defmodule ExUnit.CaptureServer do | |||||||||
{:reply, :ok, config} | ||||||||||
end | ||||||||||
|
||||||||||
@impl true | ||||||||||
def handle_info({:DOWN, ref, _, _, _}, config) do | ||||||||||
config = remove_log_capture(ref, config) | ||||||||||
config = release_device(ref, config) | ||||||||||
|
@@ -186,21 +200,39 @@ defmodule ExUnit.CaptureServer do | |||||||||
end | ||||||||||
|
||||||||||
defp remove_log_capture(ref, %{log_captures: refs} = config) do | ||||||||||
true = :ets.delete(@ets, ref) | ||||||||||
|
||||||||||
case Map.pop(refs, ref, false) do | ||||||||||
{true, refs} -> | ||||||||||
maybe_add_console(refs, config.log_status) | ||||||||||
maybe_revert_to_default_handler(refs, config.log_status) | ||||||||||
%{config | log_captures: refs} | ||||||||||
|
||||||||||
{false, _refs} -> | ||||||||||
config | ||||||||||
end | ||||||||||
end | ||||||||||
|
||||||||||
defp maybe_add_console(refs, {:ok, %{module: module} = config}) when map_size(refs) == 0 do | ||||||||||
:logger.add_handler(:default, module, config) | ||||||||||
defp maybe_revert_to_default_handler(refs, status) when map_size(refs) == 0 do | ||||||||||
:logger.remove_handler(@name) | ||||||||||
|
||||||||||
with {:ok, %{module: module} = config} <- status do | ||||||||||
:logger.add_handler(:default, module, config) | ||||||||||
end | ||||||||||
end | ||||||||||
|
||||||||||
defp maybe_add_console(_refs, _config) do | ||||||||||
defp maybe_revert_to_default_handler(_refs, _config) do | ||||||||||
:ok | ||||||||||
end | ||||||||||
|
||||||||||
## :logger handler callback. | ||||||||||
|
||||||||||
def log(event, %{} = config) do | ||||||||||
%{formatter: {formatter_mod, formatter_config}} = config | ||||||||||
chardata = formatter_mod.format(event, formatter_config) | ||||||||||
|
||||||||||
for [string_io, level] <- :ets.match(@ets, {:_, :"$1", :"$2"}), | ||||||||||
:logger.compare_levels(event.level, level) in [:gt, :eq] do | ||||||||||
:ok = IO.write(string_io, chardata) | ||||||||||
end | ||||||||||
end | ||||||||||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll be able to get rid of this and only use the ETS table to store these, but I'd rather do one thing at a time and do that in another PR. 🙃