fixeria has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/erlang/osmo-s1gw/+/37216?usp=email )


Change subject: logging: use ?LOG macro instead of calling logger:level()
......................................................................

logging: use ?LOG macro instead of calling logger:level()

The difference between using the macros and the exported functions
is that macros add location (originator) information to the metadata,
and perform lazy evaluation by wrapping the logger call in a case
statement, so it is only evaluated if the log level of the event
passes the primary log level check.

https://www.erlang.org/doc/apps/kernel/logger_chapter.html#logger-api

Change-Id: I3b6e4296f736f9c87d2706ef5fb75146fd9f1239
---
M src/s1ap_proxy.erl
M src/sctp_client.erl
M src/sctp_proxy.erl
M src/sctp_server.erl
4 files changed, 63 insertions(+), 41 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/erlang/osmo-s1gw 
refs/changes/16/37216/1

diff --git a/src/s1ap_proxy.erl b/src/s1ap_proxy.erl
index d50d765..82cc21b 100644
--- a/src/s1ap_proxy.erl
+++ b/src/s1ap_proxy.erl
@@ -38,6 +38,8 @@
          encode_pdu/1,
          decode_pdu/1]).

+-include_lib("kernel/include/logger.hrl").
+
 -include("S1AP-PDU-Descriptions.hrl").
 -include("S1AP-PDU-Contents.hrl").
 -include("S1AP-Containers.hrl").
@@ -52,10 +54,10 @@
 handle_pdu(Data) when is_binary(Data) ->
     case decode_pdu(Data) of
         {ok, Pdu} ->
-            logger:info("S1AP PDU: ~p", [Pdu]),
+            ?LOG(info, "S1AP PDU: ~p", [Pdu]),
             handle_pdu(Data, Pdu);
         {error, {asn1, Error}} ->
-            logger:error("S1AP PDU decoding failed: ~p", [Error]),
+            ?LOG(error, "S1AP PDU decoding failed: ~p", [Error]),
             Data
     end.

@@ -86,7 +88,7 @@
         {ok, NewData} ->
             NewData;
         {error, {asn1, Error}} ->
-            logger:error("S1AP PDU encoding failed: ~p", [Error]),
+            ?LOG(error, "S1AP PDU encoding failed: ~p", [Error]),
             Data
     end.

@@ -95,7 +97,7 @@
 handle_pdu(Data, {Outcome = initiatingMessage,
                   #'InitiatingMessage'{procedureCode = ?'id-E-RABSetup',
                                        value = Content} = Pdu}) ->
-    logger:debug("Patching E-RAB SETUP REQUEST"),
+    ?LOG(debug, "Patching E-RAB SETUP REQUEST"),
     IEs = handle_ies(Content#'E-RABSetupRequest'.protocolIEs,
                      ?'id-E-RABToBeSetupListBearerSUReq'),
     NewContent = Content#'E-RABSetupRequest'{protocolIEs = IEs},
@@ -105,7 +107,7 @@
 handle_pdu(Data, {Outcome = successfulOutcome,
                   #'SuccessfulOutcome'{procedureCode = ?'id-E-RABSetup',
                                        value = Content} = Pdu}) ->
-    logger:debug("Patching E-RAB SETUP RESPONSE"),
+    ?LOG(debug, "Patching E-RAB SETUP RESPONSE"),
     IEs = handle_ies(Content#'E-RABSetupResponse'.protocolIEs,
                      ?'id-E-RABSetupListBearerSURes'),
     NewContent = Content#'E-RABSetupResponse'{protocolIEs = IEs},
@@ -117,7 +119,7 @@
 handle_pdu(Data, {Outcome = initiatingMessage,
                   #'InitiatingMessage'{procedureCode = 
?'id-E-RABModificationIndication',
                                        value = Content} = Pdu}) ->
-    logger:debug("Patching E-RAB MODIFICATION INDICATION"),
+    ?LOG(debug, "Patching E-RAB MODIFICATION INDICATION"),
     IEs = Content#'E-RABModificationIndication'.protocolIEs,
     %% E-RAB to be Modified List
     IEs1 = handle_ies(IEs, ?'id-E-RABToBeModifiedListBearerModInd'),
@@ -130,7 +132,7 @@
 handle_pdu(Data, {Outcome = initiatingMessage,
                   #'InitiatingMessage'{procedureCode = 
?'id-InitialContextSetup',
                                        value = Content} = Pdu}) ->
-    logger:debug("Patching INITIAL CONTEXT SETUP REQUEST"),
+    ?LOG(debug, "Patching INITIAL CONTEXT SETUP REQUEST"),
     IEs = handle_ies(Content#'InitialContextSetupRequest'.protocolIEs,
                      ?'id-E-RABToBeSetupListCtxtSUReq'),
     NewContent = Content#'InitialContextSetupRequest'{protocolIEs = IEs},
@@ -140,7 +142,7 @@
 handle_pdu(Data, {Outcome = successfulOutcome,
                   #'SuccessfulOutcome'{procedureCode = 
?'id-InitialContextSetup',
                                        value = Content} = Pdu}) ->
-    logger:debug("Patching INITIAL CONTEXT SETUP RESPONSE"),
+    ?LOG(debug, "Patching INITIAL CONTEXT SETUP RESPONSE"),
     IEs = handle_ies(Content#'InitialContextSetupResponse'.protocolIEs,
                      ?'id-E-RABSetupListCtxtSURes'),
     NewContent = Content#'InitialContextSetupResponse'{protocolIEs = IEs},
@@ -234,7 +236,7 @@

 %% Catch-all variant, which should not be called normally
 handle_ie(#'ProtocolIE-Field'{value = Content} = IE) ->
-    logger:error("[BUG] Unhandled S1AP IE: ~p", [IE]),
+    ?LOG(error, "[BUG] Unhandled S1AP IE: ~p", [IE]),
     Content.


diff --git a/src/sctp_client.erl b/src/sctp_client.erl
index 320d4da..06ee31d 100644
--- a/src/sctp_client.erl
+++ b/src/sctp_client.erl
@@ -40,6 +40,7 @@
          send_data/2,
          disconnect/1]).

+-include_lib("kernel/include/logger.hrl").
 -include_lib("kernel/include/inet.hrl").
 -include_lib("kernel/include/inet_sctp.hrl").

diff --git a/src/sctp_proxy.erl b/src/sctp_proxy.erl
index 29cc425..00aef16 100644
--- a/src/sctp_proxy.erl
+++ b/src/sctp_proxy.erl
@@ -45,6 +45,7 @@
          send_data/2,
          shutdown/1]).

+-include_lib("kernel/include/logger.hrl").
 -include_lib("kernel/include/inet.hrl").
 -include_lib("kernel/include/inet_sctp.hrl").

@@ -83,7 +84,7 @@
 %% CONNECTING state
 connecting(enter, OldState,
            #{mme_addr := MmeAddr, mmr_port := MmePort} = S) ->
-    logger:info("State change: ~p -> ~p", [OldState, ?FUNCTION_NAME]),
+    ?LOG(info, "State change: ~p -> ~p", [OldState, ?FUNCTION_NAME]),
     %% Initiate connection establishment with the MME
     {ok, Sock} = sctp_client:connect(MmeAddr, MmePort),
     {keep_state, S#{sock => Sock},
@@ -104,29 +105,29 @@
                                           assoc_id = Aid}}}, S) ->
     case ConnState of
         comm_up ->
-            logger:notice("MME connection (id=~p, ~p:~p) established",
-                          [Aid, MmeAddr, MmePort]),
+            ?LOG(notice, "MME connection (id=~p, ~p:~p) established",
+                 [Aid, MmeAddr, MmePort]),
             {next_state, connected, S#{mme_aid => Aid}};
         _ ->
-            logger:notice("MME connection establishment failed: ~p", 
[ConnState]),
+            ?LOG(notice, "MME connection establishment failed: ~p", 
[ConnState]),
             {stop, {shutdown, conn_est_fail}}
     end;

 %% Catch-all for other kinds of SCTP events
 connecting(info, {sctp, _Socket, MmeAddr, MmePort,
                   {AncData, Data}}, S) ->
-    logger:debug("Unhandled SCTP event (~p:~p): ~p, ~p",
-                 [MmeAddr, MmePort, AncData, Data]),
+    ?LOG(debug, "Unhandled SCTP event (~p:~p): ~p, ~p",
+         [MmeAddr, MmePort, AncData, Data]),
     {keep_state, S};

 connecting(Event, EventData, S) ->
-    logger:error("Unexpected event ~p: ~p", [Event, EventData]),
+    ?LOG(error, "Unexpected event ~p: ~p", [Event, EventData]),
     {keep_state, S}.


 %% CONNECTED state
 connected(enter, OldState, S) ->
-    logger:info("State change: ~p -> ~p", [OldState, ?FUNCTION_NAME]),
+    ?LOG(info, "State change: ~p -> ~p", [OldState, ?FUNCTION_NAME]),
     %% Send pending eNB -> MME messages (if any)
     ok = sctp_send_pending(S),
     {keep_state, maps:remove(tx_queue, S)};
@@ -142,32 +143,32 @@
                                          assoc_id = Aid}}}, S) ->
     case ConnState of
         comm_up ->
-            logger:notice("MME connection (id=~p, ~p:~p) is already 
established?!?",
-                          [Aid, MmeAddr, MmePort]),
+            ?LOG(notice, "MME connection (id=~p, ~p:~p) is already 
established?!?",
+                 [Aid, MmeAddr, MmePort]),
             {keep_state, S};
         _ ->
-            logger:notice("MME connection state: ~p", [ConnState]),
+            ?LOG(notice, "MME connection state: ~p", [ConnState]),
             {stop, {shutdown, conn_fail}}
     end;

 %% Handle an #sctp_sndrcvinfo event (MME -> eNB data)
 connected(info, {sctp, _Socket, MmeAddr, MmePort,
                  {[#sctp_sndrcvinfo{assoc_id = Aid}], Data}}, S) ->
-    logger:debug("MME connection (id=~p, ~p:~p) -> eNB: ~p",
-                [Aid, MmeAddr, MmePort, Data]),
+    ?LOG(debug, "MME connection (id=~p, ~p:~p) -> eNB: ~p",
+         [Aid, MmeAddr, MmePort, Data]),
     sctp_server:send_data(maps:get(enb_aid, S), handle_pdu(Data)),
     {keep_state, S};

 %% Catch-all for other kinds of SCTP events
 connected(info, {sctp, _Socket, MmeAddr, MmePort,
                  {AncData, Data}}, S) ->
-    logger:debug("Unhandled SCTP event (~p:~p): ~p, ~p",
-                 [MmeAddr, MmePort, AncData, Data]),
+    ?LOG(debug, "Unhandled SCTP event (~p:~p): ~p, ~p",
+         [MmeAddr, MmePort, AncData, Data]),
     {keep_state, S};

 %% Catch-all handler for this state
 connected(Event, EventData, S) ->
-    logger:error("Unexpected event ~p: ~p", [Event, EventData]),
+    ?LOG(error, "Unexpected event ~p: ~p", [Event, EventData]),
     {keep_state, S}.


@@ -177,7 +178,7 @@


 terminate(Reason, State, S) ->
-    logger:notice("Terminating in state ~p, reason ~p", [State, Reason]),
+    ?LOG(notice, "Terminating in state ~p, reason ~p", [State, Reason]),
     case S of
         #{sock := Sock, mme_aid := Aid} ->
             sctp_client:disconnect({Sock, Aid}),
@@ -199,7 +200,7 @@
         NewData -> NewData
     catch
         Exception:Reason ->
-            logger:error("An exception occurred: ~p, ~p", [Exception, Reason]),
+            ?LOG(error, "An exception occurred: ~p, ~p", [Exception, Reason]),
             Data %% proxy as-is
     end.

diff --git a/src/sctp_server.erl b/src/sctp_server.erl
index e93dd9f..f4cb425 100644
--- a/src/sctp_server.erl
+++ b/src/sctp_server.erl
@@ -44,6 +44,7 @@
          send_data/2,
          shutdown/0]).

+-include_lib("kernel/include/logger.hrl").
 -include_lib("kernel/include/inet.hrl").
 -include_lib("kernel/include/inet_sctp.hrl").

@@ -85,7 +86,7 @@
                                 {type, seqpacket},
                                 {reuseaddr, true},
                                 {active, true}]),
-    logger:info("SCTP server listening on ~w:~w", [BindAddr, BindPort]),
+    ?LOG(info, "SCTP server listening on ~w:~w", [BindAddr, BindPort]),
     ok = gen_sctp:listen(Sock, true),
     {ok, #server_state{sock = Sock,
                        clients = dict:new(),
@@ -119,7 +120,7 @@
 %% Handle termination events of the child processes
 handle_info({'EXIT', Pid, Reason},
             #server_state{sock = Sock, clients = Clients} = State) ->
-    logger:debug("Child process ~p terminated with reason ~p", [Pid, Reason]),
+    ?LOG(debug, "Child process ~p terminated with reason ~p", [Pid, Reason]),
     case client_find(State, Pid) of
         {ok, {Aid, _Client}} ->
             %% gracefully close the eNB connection
@@ -137,7 +138,7 @@


 terminate(Reason, State) ->
-    logger:notice("Terminating, reason ~p", [Reason]),
+    ?LOG(notice, "Terminating, reason ~p", [Reason]),
     close_conns(State),
     gen_sctp:close(State#server_state.sock),
     ok.
@@ -152,18 +153,18 @@
                                      assoc_id = Aid}}) ->
     case ConnState of
         comm_up ->
-            logger:notice("eNB connection (id=~p, ~p:~p) established", [Aid, 
FromAddr, FromPort]),
+            ?LOG(notice, "eNB connection (id=~p, ~p:~p) established", [Aid, 
FromAddr, FromPort]),
             Clients = client_add(State#server_state.clients, Aid, FromAddr, 
FromPort,
                                  State#server_state.mme_addr_port);
         shutdown_comp ->
-            logger:notice("eNB connection (id=~p, ~p:~p) closed", [Aid, 
FromAddr, FromPort]),
+            ?LOG(notice, "eNB connection (id=~p, ~p:~p) closed", [Aid, 
FromAddr, FromPort]),
             Clients = client_del(State#server_state.clients, Aid);
         comm_lost ->
-            logger:notice("eNB connection (id=~p, ~p:~p) lost", [Aid, 
FromAddr, FromPort]),
+            ?LOG(notice, "eNB connection (id=~p, ~p:~p) lost", [Aid, FromAddr, 
FromPort]),
             Clients = client_del(State#server_state.clients, Aid);
         _ ->
-            logger:notice("eNB connection (id=~p, ~p:~p) state ~p",
-                          [Aid, FromAddr, FromPort, ConnState]),
+            ?LOG(notice, "eNB connection (id=~p, ~p:~p) state ~p",
+                 [Aid, FromAddr, FromPort, ConnState]),
             Clients = State#server_state.clients
     end,
     State#server_state{clients = Clients};
@@ -171,20 +172,20 @@
 %% Handle an #sctp_sndrcvinfo event (incoming data)
 sctp_recv(State, {FromAddr, FromPort,
                   [#sctp_sndrcvinfo{assoc_id = Aid}], Data}) ->
-    logger:debug("eNB connection (id=~p, ~p:~p) -> MME: ~p", [Aid, FromAddr, 
FromPort, Data]),
+    ?LOG(debug, "eNB connection (id=~p, ~p:~p) -> MME: ~p", [Aid, FromAddr, 
FromPort, Data]),
     case dict:find(Aid, State#server_state.clients) of
         {ok, #client_state{pid = Pid}} ->
             sctp_proxy:send_data(Pid, Data);
         error ->
-            logger:error("eNB connection (id=~p, ~p:~p) is not known to us?!?",
-                         [Aid, FromAddr, FromPort])
+            ?LOG(error, "eNB connection (id=~p, ~p:~p) is not known to us?!?",
+                 [Aid, FromAddr, FromPort])
     end,
     State;

 %% Catch-all for other kinds of SCTP events
 sctp_recv(State, {FromAddr, FromPort, AncData, Data}) ->
-    logger:debug("Unhandled SCTP event (~p:~p): ~p, ~p",
-                 [FromAddr, FromPort, AncData, Data]),
+    ?LOG(debug, "Unhandled SCTP event (~p:~p): ~p, ~p",
+         [FromAddr, FromPort, AncData, Data]),
     State.


@@ -228,7 +229,7 @@
 
 close_conns(Sock, [{Aid, Client} | Clients]) ->
     {FromAddr, FromPort} = Client#client_state.addr_port,
-    logger:notice("Terminating eNB connection (id=~p, ~p:~p)", [Aid, FromAddr, 
FromPort]),
+    ?LOG(notice, "Terminating eNB connection (id=~p, ~p:~p)", [Aid, FromAddr, 
FromPort]),
     %% request to terminate an MME connection
     sctp_proxy:shutdown(Client#client_state.pid),
     %% gracefully close an eNB connection

--
To view, visit https://gerrit.osmocom.org/c/erlang/osmo-s1gw/+/37216?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: erlang/osmo-s1gw
Gerrit-Branch: master
Gerrit-Change-Id: I3b6e4296f736f9c87d2706ef5fb75146fd9f1239
Gerrit-Change-Number: 37216
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <[email protected]>
Gerrit-MessageType: newchange

Reply via email to