Permalink
Browse files

Simplify syslog module usage

* It seems like lager_syslog must require that either:
  1) syslog:start/0 or syslog:start_link/0 was called
  2) syslog_drv.so is in the PATH environment variable for
     erlang:open_port/2 to find as a port driver
  Both of these options have nothing to do currently with starting
  syslog as an application.  This all seems odd, so I just added a
  load/0 function and an unload/0 function to make it easier to use the
  port driver without relying on an extra Erlang process that sits idle
  or the PATH environment variable's extra entry.
  • Loading branch information...
1 parent 6fb3c72 commit bce08564f78c997f25f47a0e1426c9c6d990e434 @okeuday okeuday committed Feb 6, 2014
Showing with 37 additions and 18 deletions.
  1. +37 −18 src/syslog.erl
View
@@ -42,7 +42,9 @@
close/1,
priority/1,
facility/1,
- openlog_opt/1
+ openlog_opt/1,
+ load/0,
+ unload/0
]).
%% gen_server callbacks
@@ -195,11 +197,10 @@ openlog_opt(perror) -> 20;
openlog_opt(N) when is_integer(N), N >= 1 -> N;
openlog_opt(_) -> erlang:error(badarg).
-%%% gen_server callbacks %%%
+-spec load() ->
+ ok | {error, string()}.
-init([]) ->
- process_flag(trap_exit, true),
- erl_ddll:start(),
+load() ->
PrivDir = case code:priv_dir(?MODULE) of
{error, bad_name} ->
EbinDir = filename:dirname(code:which(?MODULE)),
@@ -208,21 +209,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) ->

0 comments on commit bce0856

Please sign in to comment.