Permalink
Browse files

Merge pull request #15 from okeuday/master

Simplify syslog module usage
  • Loading branch information...
2 parents 14aa307 + 92d4a86 commit 91c892fea2836f8c504b6126dd8da7f7da99a61b @Vagabond committed Feb 13, 2014
Showing with 53 additions and 31 deletions.
  1. +53 −31 src/syslog.erl
View
@@ -42,7 +42,10 @@
close/1,
priority/1,
facility/1,
- openlog_opt/1
+ openlog_opt/1,
+ openlog_opts/1,
+ load/0,
+ unload/0
]).
%% gen_server callbacks
@@ -99,7 +102,7 @@ stop() ->
open(Ident, Logopt, Facility) ->
Log = erlang:open_port({spawn, ?DRV_NAME}, [binary]),
- Args = term_to_binary({Ident, logopt(Logopt), facility(Facility)}),
+ Args = term_to_binary({Ident, openlog_opts(Logopt), facility(Facility)}),
try erlang:port_control(Log, ?SYSLOGDRV_OPEN, Args) of
<<>> ->
{ok, Log};
@@ -195,11 +198,20 @@ openlog_opt(perror) -> 20;
openlog_opt(N) when is_integer(N), N >= 1 -> N;
openlog_opt(_) -> erlang:error(badarg).
-%%% gen_server callbacks %%%
+-spec openlog_opts(N :: list(openlog_opt() | pos_integer()) |
+ openlog_opt() | pos_integer()) ->
+ pos_integer().
-init([]) ->
- process_flag(trap_exit, true),
- erl_ddll:start(),
+openlog_opts([Queue]) -> openlog_opt(Queue);
+openlog_opts([Tail|Queue]) ->
+ openlog_opt(Tail) bor openlog_opts(Queue);
+openlog_opts([]) -> 0;
+openlog_opts(N) -> openlog_opt(N).
+
+-spec load() ->
+ ok | {error, string()}.
+
+load() ->
PrivDir = case code:priv_dir(?MODULE) of
{error, bad_name} ->
EbinDir = filename:dirname(code:which(?MODULE)),
@@ -208,21 +220,39 @@ init([]) ->
Path ->
Path
end,
- LoadResult = case erl_ddll:load_driver(PrivDir, ?DRV_NAME) of
- ok -> ok;
- {error, already_loaded} -> ok;
- {error, LoadError} ->
- LoadErrorStr = erl_ddll:format_error(LoadError),
- ErrStr = lists:flatten(
- io_lib:format("could not load driver ~s: ~p",
- [?DRV_NAME, LoadErrorStr])),
- {stop, ErrStr}
- end,
- case LoadResult of
+ case erl_ddll:load_driver(PrivDir, ?DRV_NAME) of
+ ok -> ok;
+ {error, already_loaded} -> ok;
+ {error, LoadError} ->
+ LoadErrorStr = erl_ddll:format_error(LoadError),
+ ErrStr = lists:flatten(
+ io_lib:format("could not load driver ~s: ~p",
+ [?DRV_NAME, LoadErrorStr])),
+ {error, ErrStr}
+ end.
+
+-spec unload() ->
+ ok | {error, string()}.
+
+unload() ->
+ case erl_ddll:unload_driver(?DRV_NAME) of
+ ok -> ok;
+ {error, UnloadError} ->
+ UnloadErrorStr = erl_ddll:format_error(UnloadError),
+ ErrStr = lists:flatten(
+ io_lib:format("could not unload driver ~s: ~p",
+ [?DRV_NAME, UnloadErrorStr])),
+ {error, ErrStr}
+ end.
+
+%%% gen_server callbacks %%%
+
+init([]) ->
+ case load() of
ok ->
{ok, #state{}};
- Error ->
- Error
+ {error, Reason} ->
+ {stop, Reason}
end.
handle_call(_Msg, _From, State) ->
@@ -244,21 +274,13 @@ code_change(_, State, _) ->
%%% internal functions %%%
-
-logopt([Queue]) -> openlog_opt(Queue);
-logopt([Tail|Queue]) ->
- openlog_opt(Tail) bor logopt(Queue);
-logopt([]) -> 0;
-logopt(N) -> openlog_opt(N).
-
-
-ifdef(TEST).
-logopt_test() ->
- 11 = logopt([1,2,8]),
- 1 = logopt(pid),
+openlog_opts_test() ->
+ 11 = openlog_opts([1,2,8]),
+ 1 = openlog_opts(pid),
try
- foo = logopt(foo)
+ foo = openlog_opts(foo)
catch
error:badarg ->
ok;

0 comments on commit 91c892f

Please sign in to comment.