Skip to content

Commit

Permalink
Replaced lager by hut
Browse files Browse the repository at this point in the history
Made same changes as were done for feuerlabs/exometer_core

See related discussions:

* Feuerlabs/exometer_core#57
* Feuerlabs/exometer_core#66
  • Loading branch information
ldr committed Jul 1, 2017
1 parent cd86e74 commit fc9c7c6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 70 deletions.
44 changes: 0 additions & 44 deletions include/log.hrl

This file was deleted.

3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -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, [
Expand Down
51 changes: 26 additions & 25 deletions src/netlink.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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").

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -360,15 +361,15 @@ 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)
end, State, MsgList),
{noreply, State1}
catch
error:_ ->
?error("netlink: handle_info: Crash: ~p",
?log(error, "netlink: handle_info: Crash: ~p",
[erlang:get_stacktrace()]),
{noreply, State}
end;
Expand All @@ -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}.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -591,20 +592,20 @@ 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),
State1 = State#state { request = undefined },
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?)
Expand All @@ -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"
Expand Down

0 comments on commit fc9c7c6

Please sign in to comment.