Skip to content

Commit

Permalink
Logger changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed Nov 16, 2023
1 parent 5511c23 commit b3c62d9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 37 deletions.
4 changes: 3 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ config :logger,
handle_sasl_reports: true,
backends: [:console],
truncate: 8000,
format: "$time $metadata[$level] $message\n",
format: "$time $metadata[$level] $message",
metadata: [:request_id]

config :logger, :console, format: "$time $metadata[$level] $levelpad$message\n"

case Mix.env() do
:benchmark ->
System.put_env("SEED", "none")
Expand Down
10 changes: 5 additions & 5 deletions lib/network/edge_v2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ defmodule Network.EdgeV2 do
end

def terminate(reason, state) do
log(state, "Received terminate before init ~p ~p", [reason, state])
log(state, "Received terminate before init #{inspect({reason, state})}")
reason
end

Expand Down Expand Up @@ -488,7 +488,7 @@ defmodule Network.EdgeV2 do
{:reply, :ok, %{state | ports: ports}}

nil ->
log(state, "ignoring response for undefined ref ~p", [ref])
log(state, "ignoring response for undefined ref #{inspect(ref)}")
{:reply, :ok, state}
end
end)
Expand Down Expand Up @@ -549,11 +549,11 @@ defmodule Network.EdgeV2 do
end

nil ->
log(state, "Unhandled message: ~40s~n", [truncate(msg)])
log(state, "Unhandled message: #{truncate(msg)}")
error(400, "that is not rlp")

_ ->
log(state, "Unhandled message: ~40s~n", [truncate(msg)])
log(state, "Unhandled message: #{truncate(msg)}")
error(401, "bad input")
end
end
Expand Down Expand Up @@ -665,7 +665,7 @@ defmodule Network.EdgeV2 do
end

def handle_info(msg, state) do
log(state, "Unhandled info: ~p", [msg])
log(state, "Unhandled info: #{inspect(msg)}")
{:noreply, state}
end

Expand Down
21 changes: 4 additions & 17 deletions lib/network/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ defmodule Network.Handler do
other ->
log(
{node_id, address, port},
"Connection failed in ssl.connect(): ~p",
[other]
"Connection failed in ssl.connect(): #{inspect(other)}"
)

{:stop, :normal, state}
Expand Down Expand Up @@ -109,7 +108,7 @@ defmodule Network.Handler do
{:stop, :normal, state}

{:error, reason} ->
log(nil, "Connection gone away ~p", [reason])
log(nil, "Connection gone away #{inspect(reason)}")
{:stop, :normal, state}
end
end
Expand Down Expand Up @@ -193,22 +192,10 @@ defmodule Network.Handler do
"nil"
end

def log(state, format, args \\ []) do
def log(state, format) do
mod = List.last(Module.split(__MODULE__))
date = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) |> to_string()
:io.format("~s ~s: ~s ~s~n", [date, mod, name(state), format(format, args)])
end

defp format(format, []), do: format

defp format(format, vars) do
string = :io_lib.format(format, vars) |> :erlang.iolist_to_binary()

if byte_size(string) > 180 do
binary_part(string, 0, 180)
else
string
end
:io.format("~s ~s: ~s ~s~n", [date, mod, name(state), format])
end
end
end
Expand Down
19 changes: 10 additions & 9 deletions lib/network/peer_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ defmodule Network.PeerHandler do
end

err ->
log(state, "received invalid blocks: ~180p", [err])
log(state, "received invalid blocks: #{inspect(err)}")
{:noreply, %{state | blocks: nil, random_blocks: 0}}
end
end
Expand Down Expand Up @@ -149,7 +149,7 @@ defmodule Network.PeerHandler do
handle_msg(msg, state)

_ ->
log(state, "expected hello message, but got ~p", [msg])
log(state, "expected hello message, but got #{inspect(msg)}")
{:stop, :normal, state}
end
after
Expand Down Expand Up @@ -191,20 +191,20 @@ defmodule Network.PeerHandler do
end

def handle_info({:ssl_closed, info}, state) do
log(state, "Connection closed by remote. info: ~0p", [info])
log(state, "Connection closed by remote. info: #{inspect(info)}")
{:stop, :normal, state}
end

def handle_info(msg, state) do
log(state, "unhandled info: ~180p", [msg])
log(state, "unhandled info: #{inspect(msg)}")
{:noreply, state}
end

defp handle_msg([@hello, server, genesis_hash], state) do
genesis = Chain.genesis_hash()

if genesis != genesis_hash do
log(state, "wrong genesis: ~p ~p", [Base16.encode(genesis), Base16.encode(genesis_hash)])
log(state, "wrong genesis: #{Base16.encode(genesis)}, #{Base16.encode(genesis_hash)}")
{:stop, :normal, state}
else
state = publish_peak(state)
Expand Down Expand Up @@ -400,12 +400,13 @@ defmodule Network.PeerHandler do
# is this a randomly broadcasted block or a chain re-org?
# assuming reorg after n blocks
if state.random_blocks < 100 do
log(state, "ignoring wrong ordered block [~p]", [state.random_blocks + 1])
log(state, "ignoring wrong ordered block [#{state.random_blocks + 1}]")
{state.random_blocks + 1, blocks}
else
log(state, "restarting sync because of random blocks [~p]", [
state.random_blocks + 1
])
log(
state,
"restarting sync because of random blocks [#{state.random_blocks + 1}]"
)

{:error, :too_many_random_blocks}
end
Expand Down
14 changes: 9 additions & 5 deletions lib/network/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ defmodule Network.Server do
{:valid, state}

other ->
IO.puts("Failed for #{inspect(other)}")
Logger.warning("Check cert failed for #{inspect(other)}")
{:fail, event}
end
end
Expand Down Expand Up @@ -279,20 +279,22 @@ defmodule Network.Server do

{:error, :closed} ->
# Connection abort before handshake
:io.format("~p Anomaly - Connection closed before TLS handshake~n", [state.protocol])
Logger.info("#{state.protocol} Anomaly - Connection closed before TLS handshake")

{:ok, newSocket} ->
spawn_link(fn ->
peername = :ssl.peername(newSocket)

with {:ok, {_address, _port}} <- peername,
{:ok, newSocket2} <- :ssl.handshake(newSocket, 25000) do
{:ok, newSocket2} <- :ssl.handshake(newSocket, 45000) do
worker = start_worker!(state, :init)
:ok = :ssl.controlling_process(newSocket2, worker)
send(worker, {:init, newSocket2})
else
{:error, error} ->
:io.format("~p Handshake error: ~0p ~0p~n", [state.protocol, error, peername])
Logger.warning(
"#{state.protocol} Handshake error: #{inspect(error)} #{inspect(peername)}"
)
end
end)
end
Expand Down Expand Up @@ -339,7 +341,9 @@ defmodule Network.Server do
key: {:ECPrivateKey, Secp256k1.der_encode_private(private, public)},
show_econnreset: true,
nodelay: false,
delay_send: true
delay_send: true,
log_level: :warning,
log_alert: false
] ++
if role == :server do
[fail_if_no_peer_cert: true]
Expand Down

0 comments on commit b3c62d9

Please sign in to comment.