diff --git a/include/log.hrl b/include/log.hrl deleted file mode 100644 index 270f06c..0000000 --- a/include/log.hrl +++ /dev/null @@ -1,44 +0,0 @@ -%% -%% Log macros -%% --ifndef(__LOG_HRL__). --define(__LOG_HRL__, true). - --compile({parse_transform, lager_transform}). - -%% Lager logging levels -%% debug, info, notice, warning, error, critical, alert, emergency, none. - --define(debug(Fmt), lager:debug(Fmt)). --define(debug(Fmt, Args), lager:debug(Fmt, Args)). --define(debug(Attrs, Fmt, Args), lager:debug(Attrs, Fmt, Args)). - --define(info(Fmt), lager:info(Fmt)). --define(info(Fmt, Args), lager:info(Fmt, Args)). --define(info(Attrs, Fmt, Args), lager:info(Attrs, Fmt, Args)). - --define(notice(Fmt), lager:notice(Fmt)). --define(notice(Fmt, Args), lager:notice(Fmt, Args)). --define(notice(Attrs, Fmt, Args), lager:notice(Attrs, Fmt, Args)). - --define(warning(Fmt), lager:warning(Fmt)). --define(warning(Fmt, Args), lager:warning(Fmt, Args)). --define(warning(Attrs, Fmt, Args), lager:warning(Attrs, Fmt, Args)). - --define(error(Fmt), lager:error(Fmt)). --define(error(Fmt, Args), lager:error(Fmt, Args)). --define(error(Attrs, Fmt, Args), lager:error(Attrs, Fmt, Args)). - --define(critical(Fmt), lager:critical(Fmt)). --define(critical(Fmt, Args), lager:critical(Fmt, Args)). --define(critical(Attrs, Fmt, Args), lager:critical(Attrs, Fmt, Args)). - --define(alert(Fmt), lager:alert(Fmt)). --define(alert(Fmt, Args), lager:alert(Fmt, Args)). --define(alert(Attrs, Fmt, Args), lager:alert(Attrs, Fmt, Args)). - --define(emergency(Fmt), lager:emergency(Fmt)). --define(emergency(Fmt, Args), lager:emergency(Fmt, Args)). --define(emergency(Attrs, Fmt, Args), lager:emergency(Attrs, Fmt, Args)). - --endif. diff --git a/rebar.config b/rebar.config index dd2f13a..0084211 100644 --- a/rebar.config +++ b/rebar.config @@ -1,6 +1,7 @@ %% -*- erlang -*- %% Config file for netlink-application -{deps, [{lager, ".*", {git, "git://github.com/basho/lager.git", {tag,"3.2.4"}}}]}. +{deps, [{hut, ".*", {git, "git://github.com/tolbrino/hut.git", "v1.2.0"}}]}. + {erl_opts, [debug_info, fail_on_warning]}. {port_env, [ diff --git a/src/netlink.erl b/src/netlink.erl index bd08477..445b5c4 100644 --- a/src/netlink.erl +++ b/src/netlink.erl @@ -31,7 +31,8 @@ -export([invalidate/2]). -export([get_root/2, get_match/3, get/4]). --include("log.hrl"). +-include_lib("hut/include/hut.hrl"). + -include("netlink.hrl"). -include("netl_codec.hrl"). @@ -210,10 +211,10 @@ init_drv(Opts, State) -> {ok,Rcvbuf} = update_rcvbuf(Port, ?MIN_RCVBUF), {ok,Sndbuf} = update_sndbuf(Port, ?MIN_SNDBUF), - ?info("Rcvbuf: ~w, Sndbuf: ~w", [Rcvbuf, Sndbuf]), + ?log(info, "Rcvbuf: ~w, Sndbuf: ~w", [Rcvbuf, Sndbuf]), {ok,Sizes} = netlink_drv:get_sizeof(Port), - ?info("Sizes: ~w", [Sizes]), + ?log(info, "Sizes: ~w", [Sizes]), ok = netlink_drv:add_membership(Port, ?RTNLGRP_LINK), ok = netlink_drv:add_membership(Port, ?RTNLGRP_IPV4_IFADDR), @@ -318,7 +319,7 @@ handle_call({invalidate,Name,Fields},_From,State) -> end; handle_call(Req={get,_What,_Fam,_Flags,_Attrs}, From, State) -> - ?debug("handle_call: GET: ~p", [Req]), + ?log(debug, "handle_call: GET: ~p", [Req]), State1 = enq_request(Req, From, State), State2 = dispatch_command(State1), {noreply, State2}; @@ -360,7 +361,7 @@ handle_info(_Info={nl_data,Port,Data},State) when Port =:= State#state.port -> State1 = lists:foldl( fun(Msg,StateI) -> - ?debug("handle_info: msg=~p", [Msg]), + ?log(debug, "handle_info: msg=~p", [Msg]), _Hdr = Msg#nlmsg.hdr, MsgData = Msg#nlmsg.data, handle_nlmsg(MsgData, StateI) @@ -368,7 +369,7 @@ handle_info(_Info={nl_data,Port,Data},State) when Port =:= State#state.port -> {noreply, State1} catch error:_ -> - ?error("netlink: handle_info: Crash: ~p", + ?log(error, "netlink: handle_info: Crash: ~p", [erlang:get_stacktrace()]), {noreply, State} end; @@ -378,34 +379,34 @@ handle_info({'DOWN',Ref,process,Pid,Reason}, State) -> false -> {noreply,State}; {value,_S,SubList} -> - ?debug("subscription from pid ~p deleted reason=~p", + ?log(debug, "subscription from pid ~p deleted reason=~p", [Pid, Reason]), {noreply,State#state { sub_list=SubList }} end; handle_info({timeout,Tmr,request_timeout}, State) -> R = State#state.request, if R#request.tmr =:= Tmr -> - ?debug("Timeout: ref current", []), + ?log(debug, "Timeout: ref current", []), gen_server:reply(R#request.from, {error,timeout}), State1 = State#state { request = undefined }, {noreply, dispatch_command(State1)}; true -> case lists:keytake(Tmr, #request.tmr, State#state.request_queue) of false -> - ?debug("Timeout: ref not found", []), + ?log(debug, "Timeout: ref not found", []), {noreply, State}; {value,#request { from = From},Q} -> - ?debug("Timeout: ref in queue", []), + ?log(debug, "Timeout: ref in queue", []), gen_server:reply(From, {error,timeout}), State1 = State#state { request_queue = Q }, {noreply,dispatch_command(State1)} end end; handle_info({Tag, Reply}, State) when is_reference(Tag) -> - ?debug("INFO: SELF Reply=~p", [Reply]), + ?log(debug, "INFO: SELF Reply=~p", [Reply]), {noreply, State}; handle_info(_Info, State) -> - ?debug("INFO: ~p", [_Info]), + ?log(debug, "INFO: ~p", [_Info]), {noreply, State}. %%-------------------------------------------------------------------- @@ -451,12 +452,12 @@ dispatch_command(State) when State#state.request =:= undefined -> [R=#request { call = {get,What,Fam,Flags,Attrs} } | Q ] -> R1 = update_timer(R), State1 = State#state { request_queue = Q, request = R1 }, - ?debug("dispatch_command: ~p", [R1]), + ?log(debug, "dispatch_command: ~p", [R1]), get_command(What,Fam,Flags,Attrs,State1); [R=#request { call = noop } | Q ] -> R1 = update_timer(R), State1 = State#state { request_queue = Q, request = R1 }, - ?debug("dispatch_command: ~p", [R1]), + ?log(debug, "dispatch_command: ~p", [R1]), State1; %% let it timeout [] -> State @@ -517,7 +518,7 @@ get_command(addr,Fam,Flags,Attrs,State) -> handle_nlmsg(RTM=#newlink{family=_Fam,index=Index,flags=Fs,change=Cs, attributes=As}, State) -> - ?debug("RTM = ~p", [RTM]), + ?log(debug, "RTM = ~p", [RTM]), Name = proplists:get_value(ifname, As, ""), As1 = [{index,Index},{flags,Fs},{change,Cs}|As], case lists:keytake(Index, #link.index, State#state.link_list) of @@ -533,12 +534,12 @@ handle_nlmsg(RTM=#newlink{family=_Fam,index=Index,flags=Fs,change=Cs, end; handle_nlmsg(RTM=#dellink{family=_Fam,index=Index,flags=_Fs,change=_Cs, attributes=As}, State) -> - ?debug("RTM = ~p\n", [RTM]), + ?log(debug, "RTM = ~p\n", [RTM]), Name = proplists:get_value(ifname, As, ""), %% does this delete the link? case lists:keytake(Index, #link.index, State#state.link_list) of false -> - ?warning("Warning link index=~w not found", [Index]), + ?log(warning, "Warning link index=~w not found", [Index]), State; {value,L,Ls} -> As1 = dict:to_list(L#link.attr), @@ -549,14 +550,14 @@ handle_nlmsg(RTM=#newaddr { family=Fam, prefixlen=Prefixlen, flags=Flags, scope=Scope, index=Index, attributes=As }, State) -> - ?debug("RTM = ~p", [RTM]), + ?log(debug, "RTM = ~p", [RTM]), Addr = proplists:get_value(address, As, {}), Name = proplists:get_value(label, As, ""), As1 = [{family,Fam},{prefixlen,Prefixlen},{flags,Flags}, {scope,Scope},{index,Index} | As], case lists:keymember(Index, #link.index, State#state.link_list) of false -> - ?warning("link index ~p does not exist", [Index]); + ?log(warning, "link index ~p does not exist", [Index]); true -> ok end, @@ -574,12 +575,12 @@ handle_nlmsg(RTM=#newaddr { family=Fam, prefixlen=Prefixlen, handle_nlmsg(RTM=#deladdr { family=_Fam, index=_Index, attributes=As }, State) -> - ?debug("RTM = ~p", [RTM]), + ?log(debug, "RTM = ~p", [RTM]), Addr = proplists:get_value(address, As, {}), Name = proplists:get_value(label, As, ""), case lists:keytake(Addr, #addr.addr, State#state.addr_list) of false -> - ?warning("Warning addr=~s not found", [Addr]), + ?log(warning, "Warning addr=~s not found", [Addr]), State; {value,Y,Ys} -> As1 = dict:to_list(Y#addr.attr), @@ -591,7 +592,7 @@ handle_nlmsg(#done { }, State) -> undefined -> dispatch_command(State); #request { tmr = Tmr, from = From, reply = Reply } -> - ?debug("handle_nlmsg: DONE: ~p", + ?log(debug, "handle_nlmsg: DONE: ~p", [State#state.request]), erlang:cancel_timer(Tmr), gen_server:reply(From, Reply), @@ -599,12 +600,12 @@ handle_nlmsg(#done { }, State) -> dispatch_command(State1) end; handle_nlmsg(Err=#error { errno=Err }, State) -> - ?debug("handle_nlmsg: ERROR: ~p", [State#state.request]), + ?log(debug, "handle_nlmsg: ERROR: ~p", [State#state.request]), case State#state.request of undefined -> dispatch_command(State); #request { tmr = Tmr, from = From } -> - ?debug("handle_nlmsg: DONE: ~p", + ?log(debug, "handle_nlmsg: DONE: ~p", [State#state.request]), erlang:cancel_timer(Tmr), %% fixme: convert errno to posix error (netlink.inc?) @@ -614,7 +615,7 @@ handle_nlmsg(Err=#error { errno=Err }, State) -> end; handle_nlmsg(RTM, State) -> - ?debug("netlink: handle_nlmsg, ignore ~p", [RTM]), + ?log(debug, "netlink: handle_nlmsg, ignore ~p", [RTM]), State. %% update attributes form interface "Name"