From 8759b87a29c1318075e277eb35930396e35b1a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 5 Jul 2018 09:01:49 +0200 Subject: Add a logger transport option I had to use the process dictionary to work around the current interface for one log call. You have been warned. --- src/ranch_acceptors_sup.erl | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/ranch_acceptors_sup.erl') diff --git a/src/ranch_acceptors_sup.erl b/src/ranch_acceptors_sup.erl index 9ec4abf..3df78cb 100644 --- a/src/ranch_acceptors_sup.erl +++ b/src/ranch_acceptors_sup.erl @@ -27,12 +27,21 @@ init([Ref, Transport]) -> ConnsSup = ranch_server:get_connections_sup(Ref), TransOpts = ranch_server:get_transport_options(Ref), NumAcceptors = maps:get(num_acceptors, TransOpts, 10), + Logger = maps:get(logger, TransOpts, error_logger), LSocket = case maps:get(socket, TransOpts, undefined) of undefined -> SocketOpts = maps:get(socket_opts, TransOpts, []), + %% We temporarily put the logger in the process dictionary + %% so that it can be used from ranch:filter_options. The + %% interface as it currently is does not allow passing it + %% down otherwise. + put(logger, Logger), case Transport:listen(SocketOpts) of - {ok, Socket} -> Socket; - {error, Reason} -> listen_error(Ref, Transport, SocketOpts, Reason) + {ok, Socket} -> + erase(logger), + Socket; + {error, Reason} -> + listen_error(Ref, Transport, SocketOpts, Reason, Logger) end; Socket -> Socket @@ -41,18 +50,18 @@ init([Ref, Transport]) -> ranch_server:set_addr(Ref, Addr), Procs = [ {{acceptor, self(), N}, {ranch_acceptor, start_link, [ - LSocket, Transport, ConnsSup + LSocket, Transport, Logger, ConnsSup ]}, permanent, brutal_kill, worker, []} || N <- lists:seq(1, NumAcceptors)], {ok, {{one_for_one, 1, 5}, Procs}}. --spec listen_error(any(), module(), any(), atom()) -> no_return(). -listen_error(Ref, Transport, TransOpts0, Reason) -> - TransOpts1 = lists:keyreplace(cert, 1, TransOpts0, {cert, '...'}), - TransOpts = lists:keyreplace(key, 1, TransOpts1, {key, '...'}), - error_logger:error_msg( +-spec listen_error(any(), module(), any(), atom(), module()) -> no_return(). +listen_error(Ref, Transport, SocketOpts0, Reason, Logger) -> + SocketOpts1 = [{cert, '...'}|proplists:delete(cert, SocketOpts0)], + SocketOpts = [{key, '...'}|proplists:delete(key, SocketOpts1)], + ranch:log(error, "Failed to start Ranch listener ~p in ~p:listen(~999999p) for reason ~p (~s)~n", - [Ref, Transport, TransOpts, Reason, format_error(Reason)]), + [Ref, Transport, SocketOpts, Reason, format_error(Reason)], Logger), exit({listen_error, Ref, Reason}). format_error(no_cert) -> -- cgit v1.2.3