dump to format

This commit is contained in:
Ery Lee 2015-04-15 20:27:46 +08:00
parent d50507475a
commit 9325c31be6
2 changed files with 46 additions and 48 deletions

View File

@ -20,24 +20,23 @@
%%% SOFTWARE. %%% SOFTWARE.
%%%----------------------------------------------------------------------------- %%%-----------------------------------------------------------------------------
%%% @doc %%% @doc
%%% emqttd message. %%% MQTT Message.
%%% %%%
%%% @end %%% @end
%%%----------------------------------------------------------------------------- %%%-----------------------------------------------------------------------------
-module(emqttd_message). -module(emqtt_message).
-author('feng@emqtt.io'). -author('feng@emqtt.io').
-include("emqttd.hrl"). -include("emqtt.hrl").
-include_lib("emqtt/include/emqtt.hrl"). -include("emqtt_packet.hrl").
-include_lib("emqtt/include/emqtt_packet.hrl").
-export([from_packet/1, to_packet/1]). -export([from_packet/1, to_packet/1]).
-export([set_flag/1, set_flag/2, unset_flag/1, unset_flag/2]). -export([set_flag/1, set_flag/2, unset_flag/1, unset_flag/2]).
-export([dump/1]). -export([format/1]).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% @doc %% @doc
@ -79,8 +78,7 @@ from_packet(#mqtt_packet_connect{will_retain = Retain,
%% %%
%% @end %% @end
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec( to_packet( mqtt_message() ) -> mqtt_packet() ). -spec to_packet(mqtt_message()) -> mqtt_packet().
to_packet(#mqtt_message{msgid = MsgId, to_packet(#mqtt_message{msgid = MsgId,
qos = Qos, qos = Qos,
retain = Retain, retain = Retain,
@ -138,11 +136,11 @@ unset_flag(Flag, Msg) when Flag =:= dup orelse Flag =:= retain -> Msg.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% @doc %% @doc
%% Dump mqtt message. %% Format MQTT Message.
%% %%
%% @end %% @end
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
dump(#mqtt_message{msgid= MsgId, qos = Qos, retain = Retain, dup = Dup, topic = Topic}) -> format(#mqtt_message{msgid=MsgId, qos=Qos, retain=Retain, dup=Dup, topic=Topic}) ->
io_lib:format("Message(MsgId=~p, Qos=~p, Retain=~s, Dup=~s, Topic=~s)", io_lib:format("Message(MsgId=~p, Qos=~p, Retain=~s, Dup=~s, Topic=~s)",
[MsgId, Qos, Retain, Dup, Topic]). [MsgId, Qos, Retain, Dup, Topic]).

View File

@ -20,7 +20,7 @@
%%% SOFTWARE. %%% SOFTWARE.
%%%----------------------------------------------------------------------------- %%%-----------------------------------------------------------------------------
%%% @doc %%% @doc
%%% emqtt packet. %%% MQTT packet utility functions.
%%% %%%
%%% @end %%% @end
%%%----------------------------------------------------------------------------- %%%-----------------------------------------------------------------------------
@ -35,7 +35,7 @@
%% API %% API
-export([protocol_name/1, type_name/1, connack_name/1]). -export([protocol_name/1, type_name/1, connack_name/1]).
-export([dump/1]). -export([format/1]).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% @doc %% @doc
@ -75,15 +75,15 @@ connack_name(?CONNACK_AUTH) -> 'CONNACK_AUTH'.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% @doc %% @doc
%% Dump packet. %% Format packet.
%% %%
%% @end %% @end
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec dump(mqtt_packet()) -> iolist(). -spec format(mqtt_packet()) -> iolist().
dump(#mqtt_packet{header = Header, variable = Variable, payload = Payload}) -> format(#mqtt_packet{header = Header, variable = Variable, payload = Payload}) ->
dump_header(Header, dump_variable(Variable, Payload)). format_header(Header, format_variable(Variable, Payload)).
dump_header(#mqtt_packet_header{type = Type, dup = Dup, qos = QoS, retain = Retain}, S) -> format_header(#mqtt_packet_header{type = Type, dup = Dup, qos = QoS, retain = Retain}, S) ->
S1 = S1 =
if if
S == undefined -> <<>>; S == undefined -> <<>>;
@ -91,28 +91,28 @@ dump_header(#mqtt_packet_header{type = Type, dup = Dup, qos = QoS, retain = Reta
end, end,
io_lib:format("~s(Qos=~p, Retain=~s, Dup=~s~s)", [type_name(Type), QoS, Retain, Dup, S1]). io_lib:format("~s(Qos=~p, Retain=~s, Dup=~s~s)", [type_name(Type), QoS, Retain, Dup, S1]).
dump_variable(undefined, _) -> format_variable(undefined, _) ->
undefined; undefined;
dump_variable(Variable, undefined) -> format_variable(Variable, undefined) ->
dump_variable(Variable); format_variable(Variable);
dump_variable(Variable, Payload) -> format_variable(Variable, Payload) ->
io_lib:format("~s, Payload=~p", [dump_variable(Variable), Payload]). io_lib:format("~s, Payload=~p", [format_variable(Variable), Payload]).
dump_variable(#mqtt_packet_connect{ format_variable(#mqtt_packet_connect{
proto_ver = ProtoVer, proto_ver = ProtoVer,
proto_name = ProtoName, proto_name = ProtoName,
will_retain = WillRetain, will_retain = WillRetain,
will_qos = WillQoS, will_qos = WillQoS,
will_flag = WillFlag, will_flag = WillFlag,
clean_sess = CleanSess, clean_sess = CleanSess,
keep_alive = KeepAlive, keep_alive = KeepAlive,
clientid = ClientId, clientid = ClientId,
will_topic = WillTopic, will_topic = WillTopic,
will_msg = WillMsg, will_msg = WillMsg,
username = Username, username = Username,
password = Password}) -> password = Password}) ->
Format = "ClientId=~s, ProtoName=~s, ProtoVsn=~p, CleanSess=~s, KeepAlive=~p, Username=~s, Password=~s", Format = "ClientId=~s, ProtoName=~s, ProtoVsn=~p, CleanSess=~s, KeepAlive=~p, Username=~s, Password=~s",
Args = [ClientId, ProtoName, ProtoVer, CleanSess, KeepAlive, Username, dump_password(Password)], Args = [ClientId, ProtoName, ProtoVer, CleanSess, KeepAlive, Username, format_password(Password)],
{Format1, Args1} = if {Format1, Args1} = if
WillFlag -> { Format ++ ", Will(Qos=~p, Retain=~s, Topic=~s, Msg=~s)", WillFlag -> { Format ++ ", Will(Qos=~p, Retain=~s, Topic=~s, Msg=~s)",
Args ++ [ WillQoS, WillRetain, WillTopic, WillMsg ] }; Args ++ [ WillQoS, WillRetain, WillTopic, WillMsg ] };
@ -120,37 +120,37 @@ dump_variable(#mqtt_packet_connect{
end, end,
io_lib:format(Format1, Args1); io_lib:format(Format1, Args1);
dump_variable(#mqtt_packet_connack{ack_flags = AckFlags, format_variable(#mqtt_packet_connack{ack_flags = AckFlags,
return_code = ReturnCode } ) -> return_code = ReturnCode}) ->
io_lib:format("AckFlags=~p, RetainCode=~p", [AckFlags, ReturnCode]); io_lib:format("AckFlags=~p, RetainCode=~p", [AckFlags, ReturnCode]);
dump_variable(#mqtt_packet_publish{topic_name = TopicName, format_variable(#mqtt_packet_publish{topic_name = TopicName,
packet_id = PacketId}) -> packet_id = PacketId}) ->
io_lib:format("TopicName=~s, PacketId=~p", [TopicName, PacketId]); io_lib:format("TopicName=~s, PacketId=~p", [TopicName, PacketId]);
dump_variable(#mqtt_packet_puback{packet_id = PacketId}) -> format_variable(#mqtt_packet_puback{packet_id = PacketId}) ->
io_lib:format("PacketId=~p", [PacketId]); io_lib:format("PacketId=~p", [PacketId]);
dump_variable(#mqtt_packet_subscribe{packet_id = PacketId, format_variable(#mqtt_packet_subscribe{packet_id = PacketId,
topic_table = TopicTable}) -> topic_table = TopicTable}) ->
io_lib:format("PacketId=~p, TopicTable=~p", [PacketId, TopicTable]); io_lib:format("PacketId=~p, TopicTable=~p", [PacketId, TopicTable]);
dump_variable(#mqtt_packet_unsubscribe{packet_id = PacketId, format_variable(#mqtt_packet_unsubscribe{packet_id = PacketId,
topics = Topics}) -> topics = Topics}) ->
io_lib:format("PacketId=~p, Topics=~p", [PacketId, Topics]); io_lib:format("PacketId=~p, Topics=~p", [PacketId, Topics]);
dump_variable(#mqtt_packet_suback{packet_id = PacketId, format_variable(#mqtt_packet_suback{packet_id = PacketId,
qos_table = QosTable}) -> qos_table = QosTable}) ->
io_lib:format("PacketId=~p, QosTable=~p", [PacketId, QosTable]); io_lib:format("PacketId=~p, QosTable=~p", [PacketId, QosTable]);
dump_variable(#mqtt_packet_unsuback{packet_id = PacketId}) -> format_variable(#mqtt_packet_unsuback{packet_id = PacketId}) ->
io_lib:format("PacketId=~p", [PacketId]); io_lib:format("PacketId=~p", [PacketId]);
dump_variable(PacketId) when is_integer(PacketId) -> format_variable(PacketId) when is_integer(PacketId) ->
io_lib:format("PacketId=~p", [PacketId]); io_lib:format("PacketId=~p", [PacketId]);
dump_variable(undefined) -> undefined. format_variable(undefined) -> undefined.
dump_password(undefined) -> undefined; format_password(undefined) -> undefined;
dump_password(_) -> <<"******">>. format_password(_Password) -> '******'.