Permalink
Browse files

Merge pull request #14 from okeuday/master

Improve syslog module
  • Loading branch information...
2 parents 918c9b4 + 6fb3c72 commit 14aa307f733b3cf10cd5b203917cc791ea2ec2d9 @Vagabond committed Feb 5, 2014
Showing with 104 additions and 50 deletions.
  1. +104 −50 src/syslog.erl
View
@@ -39,7 +39,10 @@
open/3,
log/3,
log/4,
- close/1
+ close/1,
+ priority/1,
+ facility/1,
+ openlog_opt/1
]).
%% gen_server callbacks
@@ -58,15 +61,42 @@
-record(state, {}).
+-type priority() :: emerg | alert | crit | err |
+ warning | notice | info | debug | non_neg_integer().
+-type facility() :: kern | user | mail | daemon | auth | syslog |
+ lpr | news | uucp | cron | authpriv | ftp |
+ netinfo | remoteauth | install | ras |
+ local0 | local1 | local2 | local3 |
+ local4 | local5 | local6 | local7 | non_neg_integer().
+-type openlog_opt() :: pid | cons | odelay | ndelay | perror | pos_integer().
+-export_type([priority/0, facility/0, openlog_opt/0]).
+
+%%% API %%%
+
+-spec start() ->
+ {ok, pid()} | ignore | {error, any()}.
+
start() ->
gen_server:start({local, ?MODULE}, ?MODULE, [], []).
+-spec start_link() ->
+ {ok, pid()} | ignore | {error, any()}.
+
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+-spec stop() ->
+ ok.
+
stop() ->
gen_server:cast(?MODULE, stop).
+-spec open(Ident :: string(),
+ Logopt :: list(openlog_opt()),
+ Facility :: facility()) ->
+ {ok, port()} |
+ {error, any()}.
+
open(Ident, Logopt, Facility) ->
Log = erlang:open_port({spawn, ?DRV_NAME}, [binary]),
Args = term_to_binary({Ident, logopt(Logopt), facility(Facility)}),
@@ -80,23 +110,92 @@ open(Ident, Logopt, Facility) ->
{error, Reason}
end.
+-spec log(Log :: port(),
+ Priority :: priority(),
+ Message :: iolist()) ->
+ ok.
+
log(_Log, _Priority, []) ->
ok;
log(Log, Priority, Message) ->
- NumPri = priorities(Priority),
+ NumPri = priority(Priority),
%% encode the priority value as a 4-byte integer in network order, and
%% add a 0 byte to the end of the command data to act as a NUL character
true = erlang:port_command(Log, [<<NumPri:32/big>>, Message, <<0:8>>]),
ok.
+
+-spec log(Log :: port(),
+ Priority :: priority(),
+ FormatStr :: string(),
+ FormatArgs :: list()) ->
+ ok.
+
log(Log, Priority, FormatStr, FormatArgs) ->
log(Log, Priority, io_lib:format(FormatStr, FormatArgs)).
+-spec close(Log :: port()) ->
+ ok.
+
close(Log) ->
true = erlang:port_close(Log),
ok.
+-spec priority(N :: priority() | non_neg_integer()) ->
+ non_neg_integer().
-%%% API %%%
+priority(emerg) -> 0;
+priority(alert) -> 1;
+priority(crit) -> 2;
+priority(err) -> 3;
+priority(warning) -> 4;
+priority(notice) -> 5;
+priority(info) -> 6;
+priority(debug) -> 7;
+priority(N) when is_integer(N), N >= 0 -> N;
+priority(_) -> erlang:error(badarg).
+
+-spec facility(N :: facility() | non_neg_integer()) ->
+ non_neg_integer().
+
+facility(kern) -> 0;
+facility(user) -> 8;
+facility(mail) -> 16;
+facility(daemon) -> 24;
+facility(auth) -> 32;
+facility(syslog) -> 40;
+facility(lpr) -> 48;
+facility(news) -> 56;
+facility(uucp) -> 64;
+facility(cron) -> 72;
+facility(authpriv) -> 80;
+facility(ftp) -> 88;
+facility(netinfo) -> 96;
+facility(remoteauth)-> 104;
+facility(install) -> 112;
+facility(ras) -> 120;
+facility(local0) -> 16 * 8;
+facility(local1) -> 17 * 8;
+facility(local2) -> 18 * 8;
+facility(local3) -> 19 * 8;
+facility(local4) -> 20 * 8;
+facility(local5) -> 21 * 8;
+facility(local6) -> 22 * 8;
+facility(local7) -> 23 * 8;
+facility(N) when is_integer(N), N >= 0 -> N;
+facility(_) -> erlang:error(badarg).
+
+-spec openlog_opt(N :: openlog_opt() | pos_integer()) ->
+ pos_integer().
+
+openlog_opt(pid) -> 1;
+openlog_opt(cons) -> 2;
+openlog_opt(odelay) -> 4;
+openlog_opt(ndelay) -> 8;
+openlog_opt(perror) -> 20;
+openlog_opt(N) when is_integer(N), N >= 1 -> N;
+openlog_opt(_) -> erlang:error(badarg).
+
+%%% gen_server callbacks %%%
init([]) ->
process_flag(trap_exit, true),
@@ -140,56 +239,11 @@ handle_info(_Info, State) ->
terminate(_Reason, _State) ->
ok.
-code_change(_, _, _) ->
- ok.
+code_change(_, State, _) ->
+ {ok, State}.
%%% internal functions %%%
-priorities(emerg) -> 0;
-priorities(alert) -> 1;
-priorities(crit) -> 2;
-priorities(err) -> 3;
-priorities(warning) -> 4;
-priorities(notice) -> 5;
-priorities(info) -> 6;
-priorities(debug) -> 7;
-priorities(N) when is_integer(N) -> N;
-priorities(_) -> erlang:error(badarg).
-
-facility(kern) -> 0;
-facility(user) -> 8;
-facility(mail) -> 16;
-facility(daemon) -> 24;
-facility(auth) -> 32;
-facility(syslog) -> 40;
-facility(lpr) -> 48;
-facility(news) -> 56;
-facility(uucp) -> 64;
-facility(cron) -> 72;
-facility(authpriv) -> 80;
-facility(ftp) -> 88;
-facility(netinfo) -> 96;
-facility(remoteauth)-> 104;
-facility(install) -> 112;
-facility(ras) -> 120;
-facility(local0) -> 16 * 8;
-facility(local1) -> 17 * 8;
-facility(local2) -> 18 * 8;
-facility(local3) -> 19 * 8;
-facility(local4) -> 20 * 8;
-facility(local5) -> 21 * 8;
-facility(local6) -> 22 * 8;
-facility(local7) -> 23 * 8;
-facility(N) when is_integer(N) -> N;
-facility(_) -> erlang:error(badarg).
-
-openlog_opt(pid) -> 1;
-openlog_opt(cons) -> 2;
-openlog_opt(odelay) -> 4;
-openlog_opt(ndelay) -> 8;
-openlog_opt(perror) -> 20;
-openlog_opt(N) when is_integer(N) -> N;
-openlog_opt(_) -> erlang:error(badarg).
logopt([Queue]) -> openlog_opt(Queue);
logopt([Tail|Queue]) ->

0 comments on commit 14aa307

Please sign in to comment.