Skip to content

Commit

Permalink
make logger interception more robust
Browse files Browse the repository at this point in the history
Fixes #947
  • Loading branch information
lukaszsamson committed Aug 8, 2023
1 parent 3de597b commit 7c03b8d
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/elixir_ls_debugger/lib/debugger/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule ElixirLS.Debugger.CLI do
WireProtocol.intercept_output(&Output.debuggee_out/1, &Output.debuggee_err/1)
Launch.start_mix()

if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
# make sue that debugger modules are in code path
# without starting the app
Mix.ensure_application!(:debugger)
Expand Down
2 changes: 1 addition & 1 deletion apps/elixir_ls_debugger/test/debugger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2331,7 +2331,7 @@ defmodule ElixirLS.Debugger.ServerTest do
Server.receive_packet(server, request(6, "threads", %{}))
assert_receive(response(_, 6, "threads", %{"threads" => threads}), 1_000)

if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
assert Enum.find(threads, &(&1["id"] == thread_id))["name"] ==
"Task.Supervised.noreply/4 #{:erlang.pid_to_list(pid)}"
else
Expand Down
2 changes: 1 addition & 1 deletion apps/elixir_ls_debugger/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Application.put_env(:elixir_ls_debugger, :test_mode, true)
ExUnit.start(exclude: [pending: true])

if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
# make sue that debugger modules are in code path
# without starting the app
Mix.ensure_application!(:debugger)
Expand Down
2 changes: 1 addition & 1 deletion apps/elixir_ls_utils/lib/launch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule ElixirLS.Utils.Launch do
file = ElixirLS.Utils.MixfileHelpers.mix_exs()

if File.regular?(file) do
if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
Mix.ProjectStack.post_config(state_loader: {:cli, List.first(args)})
end

Expand Down
54 changes: 34 additions & 20 deletions apps/language_server/lib/language_server/build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,20 @@ defmodule ElixirLS.LanguageServer.Build do

Mix.Task.clear()

if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
if Logger.Backends.JsonRpc not in :logger.get_handler_ids() do
raise "build without intercepted logger #{inspect(:logger.get_handler_ids())}"
Logger.error("Build without intercepted logger #{inspect(:logger.get_handler_ids())}")

for handler_id <- :logger.get_handler_ids() do
:ok = :logger.remove_handler(handler_id)
end

:ok =
:logger.add_handler(
Logger.Backends.JsonRpc,
Logger.Backends.JsonRpc,
Logger.Backends.JsonRpc.handler_config()
)
end
end

Expand Down Expand Up @@ -168,28 +179,31 @@ defmodule ElixirLS.LanguageServer.Build do
# store log config
logger_config = Application.get_all_env(:logger)

logger_handler_configs =
if Version.match?(System.version(), ">= 1.15.0-dev") do
for handler_id <- :logger.get_handler_ids() do
{:ok, config} = :logger.get_handler_config(handler_id)
try do
Mix.Task.run("loadconfig")
after
# reset log config
Application.put_all_env(logger: logger_config)

if Version.match?(System.version(), ">= 1.15.0") do
# remove all log handlers and restore our
for handler_id <- :logger.get_handler_ids(), handler_id != Logger.Backends.JsonRpc do
:ok = :logger.remove_handler(handler_id)
config
end
end

Mix.Task.run("loadconfig")

# reset log config
Application.put_all_env(logger: logger_config)

if Version.match?(System.version(), ">= 1.15.0-dev") do
for config <- logger_handler_configs do
:ok = :logger.add_handler(config.id, config.module, config)
if Logger.Backends.JsonRpc not in :logger.get_handler_ids() do
:ok =
:logger.add_handler(
Logger.Backends.JsonRpc,
Logger.Backends.JsonRpc,
Logger.Backends.JsonRpc.handler_config()
)
end
end
end

# make sure ANSI is disabled
Application.put_env(:elixir, :ansi_enabled, false)
# make sure ANSI is disabled
Application.put_env(:elixir, :ansi_enabled, false)
end
end

{status, diagnostics}
Expand All @@ -212,7 +226,7 @@ defmodule ElixirLS.LanguageServer.Build do
]

opts =
if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
opts
else
opts ++ ["--all-warnings"]
Expand Down
2 changes: 1 addition & 1 deletion apps/language_server/lib/language_server/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule ElixirLS.LanguageServer.CLI do

# :logger application is already started
# replace console logger with LSP
if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
:ok = :logger.remove_handler(:default)

:ok =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule ElixirLS.LanguageServer.Experimental.CodeMod.Format do
true = Code.ensure_loaded?(Mix.Tasks.Format)

{formatter_function, options} =
if project_dir && Version.match?(System.version(), ">= 1.15.0-dev") do
if project_dir && Version.match?(System.version(), ">= 1.15.0") do
Mix.Tasks.Format.formatter_for_file(path, root: project_dir)
else
Mix.Tasks.Format.formatter_for_file(path)
Expand Down
2 changes: 1 addition & 1 deletion apps/language_server/lib/language_server/source_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ defmodule ElixirLS.LanguageServer.SourceFile do
try do
true = Code.ensure_loaded?(Mix.Tasks.Format)

if project_dir && Version.match?(System.version(), ">= 1.15.0-dev") do
if project_dir && Version.match?(System.version(), ">= 1.15.0") do
{:ok, Mix.Tasks.Format.formatter_for_file(path, root: project_dir)}
else
{:ok, Mix.Tasks.Format.formatter_for_file(path)}
Expand Down
2 changes: 1 addition & 1 deletion apps/language_server/test/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ defmodule ElixirLS.LanguageServer.ServerTest do

initialize(server)

if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
assert_receive notification("textDocument/publishDiagnostics", %{
"uri" => ^error_file,
"diagnostics" => [
Expand Down
2 changes: 1 addition & 1 deletion apps/language_server/test/support/server_test_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule ElixirLS.LanguageServer.Test.ServerTestHelpers do
def replace_logger(packet_capture) do
# :logger application is already started
# replace console logger with LSP
if Version.match?(System.version(), ">= 1.15.0-dev") do
if Version.match?(System.version(), ">= 1.15.0") do
configs =
for handler_id <- :logger.get_handler_ids() do
{:ok, config} = :logger.get_handler_config(handler_id)
Expand Down

0 comments on commit 7c03b8d

Please sign in to comment.