Parcourir la source

refactor: unify all io:format calls to use the same macro

Zaiming Shi il y a 4 ans
Parent
commit
b10fb4e95d

+ 4 - 0
apps/emqx/include/logger.hrl

@@ -61,4 +61,8 @@
         logger:log(Level, Data, #{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
                                  , line => ?LINE})).
 
+%% print to 'user' group leader
+-define(ULOG(Fmt, Args), io:format(user, Fmt, Args)).
+-define(ELOG(Fmt, Args), io:format(standard_error, Fmt, Args)).
+
 -endif.

+ 2 - 2
apps/emqx/src/emqx.erl

@@ -76,8 +76,8 @@ set_debug_secret(PathToSecretFile) ->
                 catch _ : _ -> error({badfile, PathToSecretFile})
                 end;
             {error, Reason} ->
-                io:format("Failed to read debug_info encryption key file ~s: ~p~n",
-                          [PathToSecretFile, Reason]),
+                ?ULOG("Failed to read debug_info encryption key file ~s: ~p~n",
+                      [PathToSecretFile, Reason]),
                 error(Reason)
         end,
     F = fun(init) -> ok;

+ 6 - 5
apps/emqx/src/emqx_app.erl

@@ -27,6 +27,8 @@
         ]).
 
 -include("emqx.hrl").
+-include("emqx_release.hrl").
+-include("logger.hrl").
 
 -define(APP, emqx).
 
@@ -37,7 +39,6 @@
                      , ?MOD_DELAYED_SHARD
                      ]).
 
--include("emqx_release.hrl").
 
 %%--------------------------------------------------------------------
 %% Application callbacks
@@ -113,17 +114,17 @@ start_ce_modules() ->
 print_otp_version_warning() -> ok.
 -else.
 print_otp_version_warning() ->
-    io:format("WARNING: Running on Erlang/OTP version ~p. Recommended: 23~n",
-              [?OTP_RELEASE]).
+    ?ULOG("WARNING: Running on Erlang/OTP version ~p. Recommended: 23~n",
+          [?OTP_RELEASE]).
 -endif. % OTP_RELEASE
 
 -ifndef(TEST).
 
 print_banner() ->
-    io:format("Starting ~s on node ~s~n", [?APP, node()]).
+    ?ULOG("Starting ~s on node ~s~n", [?APP, node()]).
 
 print_vsn() ->
-    io:format("~s ~s is running now!~n", [get_description(), get_release()]).
+    ?ULOG("~s ~s is running now!~n", [get_description(), get_release()]).
 
 -else. % TEST
 

+ 4 - 4
apps/emqx/src/emqx_listeners.erl

@@ -18,6 +18,7 @@
 -module(emqx_listeners).
 
 -include("emqx_mqtt.hrl").
+-include("logger.hrl").
 
 %% APIs
 -export([ list/0
@@ -108,14 +109,13 @@ start_listener(ZoneName, ListenerName, #{type := Type, bind := Bind} = Conf) ->
         {error, {already_started, Pid}} ->
             {error, {already_started, Pid}};
         {error, Reason} ->
-            io:format(standard_error, "Failed to start ~s listener ~s on ~s: ~0p~n",
-                      [Type, listener_id(ZoneName, ListenerName), format(Bind), Reason]),
+            ?ELOG("Failed to start ~s listener ~s on ~s: ~0p~n",
+                  [Type, listener_id(ZoneName, ListenerName), format(Bind), Reason]),
             error(Reason)
     end.
 
 -ifndef(TEST).
-console_print(Fmt, Args) ->
-    io:format(Fmt, Args).
+console_print(Fmt, Args) -> ?ULOG(Fmt, Args).
 -else.
 console_print(_Fmt, _Args) -> ok.
 -endif.

+ 0 - 4
apps/emqx/src/emqx_rule_actions_trans.erl

@@ -18,18 +18,14 @@ trans([Form | AST], ResAST) ->
   trans(AST, [Form | ResAST]).
 
 trans_func_clauses("on_action_create_" ++ _ = _FuncName , Clauses) ->
-  %io:format("~n[[transing function: ~p]]~n", [_FuncName]),
-  %io:format("~n-----old clauses:~n", []), merl:print(Clauses),
   NewClauses = [
     begin
       Bindings = lists:flatten(get_vars(Args) ++ get_vars(Body, lefth)),
       Body2 = append_to_result(Bindings, Body),
       {clause, LineNo, Args, Guards, Body2}
     end || {clause, LineNo, Args, Guards, Body} <- Clauses],
-  %io:format("~n-----new clauses: ~n"), merl:print(NewClauses),
   NewClauses;
 trans_func_clauses(_FuncName, Clauses) ->
-  %io:format("~n[[discarding function: ~p]]~n", [_FuncName]),
   Clauses.
 
 get_vars(Exprs) ->

+ 3 - 2
apps/emqx_dashboard/src/emqx_dashboard.erl

@@ -27,6 +27,7 @@
 %% Authorization
 -export([authorize_appid/1]).
 
+-include_lib("emqx/include/logger.hrl").
 
 -define(BASE_PATH, "/api/v5").
 
@@ -67,7 +68,7 @@ start_listener({Proto, Port, Options}) ->
         dispatch => Dispatch},
     MinirestOptions = maps:merge(Minirest, RanchOptions),
     {ok, _} = minirest:start(listener_name(Proto), MinirestOptions),
-    io:format("Start ~p listener on ~p successfully.~n", [listener_name(Proto), Port]).
+    ?ULOG("Start ~p listener on ~p successfully.~n", [listener_name(Proto), Port]).
 
 apps() ->
     [App || {App, _, _} <- application:loaded_applications(),
@@ -90,7 +91,7 @@ ranch_opts(Port, Options0) ->
     maps:from_list([{port, Port} | Options]).
 
 stop_listener({Proto, Port, _}) ->
-    io:format("Stop dashboard listener on ~s successfully.~n",[format(Port)]),
+    ?ULOG("Stop dashboard listener on ~s successfully.~n", [format(Port)]),
     minirest:stop(listener_name(Proto)).
 
 listeners() ->

+ 9 - 10
apps/emqx_gateway/src/coap/emqx_coap_impl.erl

@@ -31,6 +31,8 @@
         , on_insta_destroy/3
         ]).
 
+-include_lib("emqx/include/logger.hrl").
+
 -dialyzer({nowarn_function, [load/0]}).
 
 %%--------------------------------------------------------------------
@@ -103,13 +105,12 @@ start_listener(InstaId, Ctx, ResourceMod, {Type, ListenOn, SocketOpts, Cfg}) ->
     Cfg2 = Cfg#{resource => ResourceMod},
     case start_listener(InstaId, Ctx, Type, ListenOn, SocketOpts, Cfg2) of
         {ok, Pid} ->
-            io:format("Start coap ~s:~s listener on ~s successfully.~n",
-                      [InstaId, Type, ListenOnStr]),
+            ?ULOG("Start coap ~s:~s listener on ~s successfully.~n",
+                  [InstaId, Type, ListenOnStr]),
             Pid;
         {error, Reason} ->
-            io:format(standard_error,
-                      "Failed to start coap ~s:~s listener on ~s: ~0p~n",
-                      [InstaId, Type, ListenOnStr, Reason]),
+            ?ELOG("Failed to start coap ~s:~s listener on ~s: ~0p~n",
+                  [InstaId, Type, ListenOnStr, Reason]),
             throw({badconf, Reason})
     end.
 
@@ -136,13 +137,11 @@ stop_listener(InstaId, {Type, ListenOn, SocketOpts, Cfg}) ->
     StopRet = stop_listener(InstaId, Type, ListenOn, SocketOpts, Cfg),
     ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
     case StopRet of
-        ok -> io:format("Stop coap ~s:~s listener on ~s successfully.~n",
+        ok -> ?ULOG("Stop coap ~s:~s listener on ~s successfully.~n",
                         [InstaId, Type, ListenOnStr]);
         {error, Reason} ->
-            io:format(standard_error,
-                      "Failed to stop coap ~s:~s listener on ~s: ~0p~n",
-                      [InstaId, Type, ListenOnStr, Reason]
-                     )
+            ?ELOG("Failed to stop coap ~s:~s listener on ~s: ~0p~n",
+                  [InstaId, Type, ListenOnStr, Reason])
     end,
     StopRet.
 

+ 10 - 12
apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl

@@ -32,6 +32,8 @@
         , on_insta_destroy/3
         ]).
 
+-include_lib("emqx/include/logger.hrl").
+
 %%--------------------------------------------------------------------
 %% APIs
 %%--------------------------------------------------------------------
@@ -64,8 +66,7 @@ start_grpc_server(InstaId, Options = #{bind := ListenOn}) ->
                       [{ssl_options, SslOpts}]
               end,
     _ = grpc:start_server(InstaId, ListenOn, Services, SvrOptions),
-    io:format("Start ~s gRPC server on ~p successfully.~n",
-               [InstaId, ListenOn]).
+    ?ULOG("Start ~s gRPC server on ~p successfully.~n", [InstaId, ListenOn]).
 
 start_grpc_client_channel(InstaId, Options = #{address := UriStr}) ->
     UriMap = uri_string:parse(UriStr),
@@ -146,13 +147,12 @@ start_listener(InstaId, Ctx, {Type, ListenOn, SocketOpts, Cfg}) ->
     ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
     case start_listener(InstaId, Ctx, Type, ListenOn, SocketOpts, Cfg) of
         {ok, Pid} ->
-            io:format("Start exproto ~s:~s listener on ~s successfully.~n",
+            ?ULOG("Start exproto ~s:~s listener on ~s successfully.~n",
                       [InstaId, Type, ListenOnStr]),
             Pid;
         {error, Reason} ->
-            io:format(standard_error,
-                      "Failed to start exproto ~s:~s listener on ~s: ~0p~n",
-                      [InstaId, Type, ListenOnStr, Reason]),
+            ?ELOG("Failed to start exproto ~s:~s listener on ~s: ~0p~n",
+                  [InstaId, Type, ListenOnStr, Reason]),
             throw({badconf, Reason})
     end.
 
@@ -204,13 +204,11 @@ stop_listener(InstaId, {Type, ListenOn, SocketOpts, Cfg}) ->
     StopRet = stop_listener(InstaId, Type, ListenOn, SocketOpts, Cfg),
     ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
     case StopRet of
-        ok -> io:format("Stop exproto ~s:~s listener on ~s successfully.~n",
-                        [InstaId, Type, ListenOnStr]);
+        ok -> ?ULOG("Stop exproto ~s:~s listener on ~s successfully.~n",
+                    [InstaId, Type, ListenOnStr]);
         {error, Reason} ->
-            io:format(standard_error,
-                      "Failed to stop exproto ~s:~s listener on ~s: ~0p~n",
-                      [InstaId, Type, ListenOnStr, Reason]
-                     )
+            ?ELOG("Failed to stop exproto ~s:~s listener on ~s: ~0p~n",
+                  [InstaId, Type, ListenOnStr, Reason])
     end,
     StopRet.
 

+ 9 - 10
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_coap_server.erl

@@ -28,8 +28,7 @@
         , stop_listener/2
         ]).
 
--define(LOG(Level, Format, Args),
-    logger:Level("LwM2M: " ++ Format, Args)).
+-include_lib("emqx/include/logger.hrl").
 
 start(Envs) ->
     start_listeners(Envs).
@@ -50,11 +49,11 @@ stop_listeners(Envs) ->
 start_listener({Proto, ListenOn, Opts}) ->
     case start_listener(Proto, ListenOn, Opts) of
         {ok, _Pid} ->
-            io:format("Start lwm2m:~s listener on ~s successfully.~n",
-                      [Proto, format(ListenOn)]);
+            ?ULOG("Start lwm2m:~s listener on ~s successfully.~n",
+                  [Proto, format(ListenOn)]);
         {error, Reason} ->
-            io:format(standard_error, "Failed to start lwm2m:~s listener on ~s: ~0p~n",
-                      [Proto, format(ListenOn), Reason]),
+            ?ELOG("Failed to start lwm2m:~s listener on ~s: ~0p~n",
+                  [Proto, format(ListenOn), Reason]),
             error(Reason)
     end.
 
@@ -66,11 +65,11 @@ start_listener(dtls, ListenOn, Opts) ->
 stop_listener({Proto, ListenOn, _Opts}) ->
     Ret = stop_listener(Proto, ListenOn),
     case Ret of
-        ok -> io:format("Stop lwm2m:~s listener on ~s successfully.~n",
-                        [Proto, format(ListenOn)]);
+        ok -> ?ULOG("Stop lwm2m:~s listener on ~s successfully.~n",
+                    [Proto, format(ListenOn)]);
         {error, Reason} ->
-            io:format(standard_error, "Failed to stop lwm2m:~s listener on ~s: ~0p~n",
-                      [Proto, format(ListenOn), Reason])
+            ?ELOG("Failed to stop lwm2m:~s listener on ~s: ~0p~n",
+                  [Proto, format(ListenOn), Reason])
     end,
     Ret.
 

+ 10 - 11
apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl

@@ -32,6 +32,8 @@
         , on_insta_destroy/3
         ]).
 
+-include_lib("emqx/include/logger.hrl").
+
 %%--------------------------------------------------------------------
 %% APIs
 %%--------------------------------------------------------------------
@@ -113,13 +115,12 @@ start_listener(InstaId, Ctx, {Type, ListenOn, SocketOpts, Cfg}) ->
     ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
     case start_listener(InstaId, Ctx, Type, ListenOn, SocketOpts, Cfg) of
         {ok, Pid} ->
-            io:format("Start mqttsn ~s:~s listener on ~s successfully.~n",
-                      [InstaId, Type, ListenOnStr]),
+            ?ULOG("Start mqttsn ~s:~s listener on ~s successfully.~n",
+                  [InstaId, Type, ListenOnStr]),
             Pid;
         {error, Reason} ->
-            io:format(standard_error,
-                      "Failed to start mqttsn ~s:~s listener on ~s: ~0p~n",
-                      [InstaId, Type, ListenOnStr, Reason]),
+            ?ELOG("Failed to start mqttsn ~s:~s listener on ~s: ~0p~n",
+                  [InstaId, Type, ListenOnStr, Reason]),
             throw({badconf, Reason})
     end.
 
@@ -150,13 +151,11 @@ stop_listener(InstaId, {Type, ListenOn, SocketOpts, Cfg}) ->
     StopRet = stop_listener(InstaId, Type, ListenOn, SocketOpts, Cfg),
     ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
     case StopRet of
-        ok -> io:format("Stop mqttsn ~s:~s listener on ~s successfully.~n",
-                        [InstaId, Type, ListenOnStr]);
+        ok -> ?ULOG("Stop mqttsn ~s:~s listener on ~s successfully.~n",
+                    [InstaId, Type, ListenOnStr]);
         {error, Reason} ->
-            io:format(standard_error,
-                      "Failed to stop mqttsn ~s:~s listener on ~s: ~0p~n",
-                      [InstaId, Type, ListenOnStr, Reason]
-                     )
+            ?ELOG("Failed to stop mqttsn ~s:~s listener on ~s: ~0p~n",
+                  [InstaId, Type, ListenOnStr, Reason])
     end,
     StopRet.
 

+ 11 - 13
apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl

@@ -16,8 +16,6 @@
 
 -module(emqx_stomp_impl).
 
--include_lib("emqx_gateway/include/emqx_gateway.hrl").
-
 -behavior(emqx_gateway_impl).
 
 %% APIs
@@ -31,6 +29,9 @@
         , on_insta_destroy/3
         ]).
 
+-include_lib("emqx_gateway/include/emqx_gateway.hrl").
+-include_lib("emqx/include/logger.hrl").
+
 %%--------------------------------------------------------------------
 %% APIs
 %%--------------------------------------------------------------------
@@ -97,13 +98,12 @@ start_listener(InstaId, Ctx, {Type, ListenOn, SocketOpts, Cfg}) ->
     ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
     case start_listener(InstaId, Ctx, Type, ListenOn, SocketOpts, Cfg) of
         {ok, Pid} ->
-            io:format("Start stomp ~s:~s listener on ~s successfully.~n",
-                      [InstaId, Type, ListenOnStr]),
+            ?ULOG("Start stomp ~s:~s listener on ~s successfully.~n",
+                  [InstaId, Type, ListenOnStr]),
             Pid;
         {error, Reason} ->
-            io:format(standard_error,
-                      "Failed to start stomp ~s:~s listener on ~s: ~0p~n",
-                      [InstaId, Type, ListenOnStr, Reason]),
+            ?ELOG("Failed to start stomp ~s:~s listener on ~s: ~0p~n",
+                  [InstaId, Type, ListenOnStr, Reason]),
             throw({badconf, Reason})
     end.
 
@@ -134,13 +134,11 @@ stop_listener(InstaId, {Type, ListenOn, SocketOpts, Cfg}) ->
     StopRet = stop_listener(InstaId, Type, ListenOn, SocketOpts, Cfg),
     ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
     case StopRet of
-        ok -> io:format("Stop stomp ~s:~s listener on ~s successfully.~n",
-                        [InstaId, Type, ListenOnStr]);
+        ok -> ?ULOG("Stop stomp ~s:~s listener on ~s successfully.~n",
+                    [InstaId, Type, ListenOnStr]);
         {error, Reason} ->
-            io:format(standard_error,
-                      "Failed to stop stomp ~s:~s listener on ~s: ~0p~n",
-                      [InstaId, Type, ListenOnStr, Reason]
-                     )
+            ?ELOG("Failed to stop stomp ~s:~s listener on ~s: ~0p~n",
+                  [InstaId, Type, ListenOnStr, Reason])
     end,
     StopRet.
 

+ 3 - 2
apps/emqx_management/src/emqx_mgmt_http.erl

@@ -24,6 +24,7 @@
 -export([authorize_appid/1]).
 
 -include_lib("emqx/include/emqx.hrl").
+-include_lib("emqx/include/logger.hrl").
 
 -define(APP, emqx_management).
 
@@ -63,7 +64,7 @@ start_listener({Proto, Port, Options}) ->
         swagger_global_spec => GlobalSpec},
     MinirestOptions = maps:merge(Minirest, RanchOptions),
     {ok, _} = minirest:start(listener_name(Proto), MinirestOptions),
-    io:format("Start ~p listener on ~p successfully.~n", [listener_name(Proto), Port]).
+    ?ULOG("Start ~p listener on ~p successfully.~n", [listener_name(Proto), Port]).
 
 ranch_opts(Port, Options0) ->
     Options = lists:foldl(
@@ -79,7 +80,7 @@ ranch_opts(Port, Options0) ->
     maps:from_list([{port, Port} | Options]).
 
 stop_listener({Proto, Port, _}) ->
-    io:format("Stop http:management listener on ~s successfully.~n",[format(Port)]),
+    ?ULOG("Stop http:management listener on ~s successfully.~n",[format(Port)]),
     minirest:stop(listener_name(Proto)).
 
 listeners() ->

+ 0 - 1
apps/emqx_resource/src/emqx_resource_transform.erl

@@ -59,7 +59,6 @@ form(Mod, Form) ->
         ?Q("-module('@_').") ->
             {[Form], fix_spec_attrs(), fix_spec_funcs(Mod)};
         _ ->
-            %io:format("---other form: ~p~n", [Form]),
             {[Form], [], []}
     end.
 

+ 4 - 4
apps/emqx_rule_engine/src/emqx_rule_actions.erl

@@ -119,10 +119,10 @@ on_action_create_inspect(Id, Params) ->
 
 -spec on_action_inspect(selected_data(), env_vars()) -> any().
 on_action_inspect(Selected, Envs) ->
-    io:format("[inspect]~n"
-              "\tSelected Data: ~p~n"
-              "\tEnvs: ~p~n"
-              "\tAction Init Params: ~p~n", [Selected, Envs, ?bound_v('Params', Envs)]),
+    ?ULOG("[inspect]~n"
+          "\tSelected Data: ~p~n"
+          "\tEnvs: ~p~n"
+          "\tAction Init Params: ~p~n", [Selected, Envs, ?bound_v('Params', Envs)]),
     emqx_rule_metrics:inc_actions_success(?bound_v('Id', Envs)).
 
 

+ 8 - 8
apps/emqx_rule_engine/src/emqx_rule_registry.erl

@@ -150,14 +150,14 @@ mnesia(copy) ->
     ok = ekka_mnesia:copy_table(?RES_TYPE_TAB, ram_copies).
 
 dump() ->
-    io:format("Rules: ~p~n"
-              "ActionInstParams: ~p~n"
-              "Resources: ~p~n"
-              "ResourceParams: ~p~n",
-            [ets:tab2list(?RULE_TAB),
-             ets:tab2list(?ACTION_INST_PARAMS_TAB),
-             ets:tab2list(?RES_TAB),
-             ets:tab2list(?RES_PARAMS_TAB)]).
+    ?ULOG("Rules: ~p~n"
+          "ActionInstParams: ~p~n"
+          "Resources: ~p~n"
+          "ResourceParams: ~p~n",
+          [ets:tab2list(?RULE_TAB),
+           ets:tab2list(?ACTION_INST_PARAMS_TAB),
+           ets:tab2list(?RES_TAB),
+           ets:tab2list(?RES_PARAMS_TAB)]).
 
 %%------------------------------------------------------------------------------
 %% Start the registry