Skip to content

Commit

Permalink
Allow setting facility and options in openlog.
Browse files Browse the repository at this point in the history
Modify openlog to an arity of 3. Provide some functions for retrieving
syslog constants.
  • Loading branch information
msantos committed Mar 18, 2010
1 parent 38d82e2 commit 869ca20
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
12 changes: 8 additions & 4 deletions c_src/syslog.c
Expand Up @@ -32,14 +32,18 @@ THE SOFTWARE.

#define MAXBUFLEN 1024

char facil[MAXBUFLEN];
const char ident[MAXBUFLEN];


static ERL_NIF_TERM nif_open(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
if (enif_get_string(env, argv[0], facil, sizeof(facil), ERL_NIF_LATIN1) < 1) {
int option;
int facility;
if ( (enif_get_string(env, argv[0], (char *)ident, sizeof(ident), ERL_NIF_LATIN1) < 1) ||
!enif_get_int(env, argv[1], &option) ||
!enif_get_int(env, argv[2], &facility)) {
return enif_make_badarg(env);
}
openlog(facil, LOG_CONS | LOG_PID, 0);
openlog(ident, option, facility);
return enif_make_atom(env, "ok");
}

Expand All @@ -62,7 +66,7 @@ static ERL_NIF_TERM nif_close(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[
}

static ErlNifFunc nif_funcs[] = {
{"open", 1, nif_open},
{"open", 3, nif_open},
{"write", 2, nif_write},
{"close", 0, nif_close}
};
Expand Down
49 changes: 47 additions & 2 deletions src/syslog.erl
Expand Up @@ -21,7 +21,8 @@
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
%% OTHER DEALINGS IN THE SOFTWARE.
-module(syslog).
-export([open/1, write/2, close/0]).
-export([open/1, open/3, write/2, close/0,
option/1, facility/1, level/1]).
-on_load(on_load/0).

on_load() ->
Expand All @@ -33,9 +34,53 @@ on_load() ->
]),
erlang:load_nif(Lib, 0).

open(_) -> erlang:error(not_implemented).
open(Ident) ->
open(Ident, option(pid) bxor option(cons), level(info)).

open(_,_,_) -> erlang:error(not_implemented).

write(_,_) -> erlang:error(not_implemented).

close() -> erlang:error(not_implemented).


% options for openlog()
option(pid) -> 16#01; % log the pid with each message
option(cons) -> 16#02; % log on the console if errors in sending
option(odelay) -> 16#04; % delay open until first syslog() (default)
option(ndelay) -> 16#08; % don't delay open
option(nowait) -> 16#10; % don't wait for console forks: DEPRECATED
option(perror) -> 16#20. % log to stderr as well

%% specify the type of program logging the message
facility(kern) -> 0 bsl 3; % kernel messages
facility(user) -> 1 bsl 3; % random user-level messages
facility(mail) -> 2 bsl 3; % mail system
facility(daemon) -> 3 bsl 3; % system daemons
facility(auth) -> 4 bsl 3; % security/authorization messages
facility(syslog) -> 5 bsl 3; % messages generated internally by syslogd
facility(lpr) -> 6 bsl 3; % line printer subsystem
facility(news) -> 7 bsl 3; % network news subsystem
facility(uucp) -> 8 bsl 3; % UUCP subsystem
facility(cron) -> 9 bsl 3; % clock daemon
facility(authpriv) -> 10 bsl 3; % security/authorization messages (private)
facility(ftp) -> 11 bsl 3; % ftp daemon

facility(local0) -> 16 bsl 3; % reserved for local use
facility(local1) -> 17 bsl 3; % reserved for local use
facility(local2) -> 18 bsl 3; % reserved for local use
facility(local3) -> 19 bsl 3; % reserved for local use
facility(local4) -> 20 bsl 3; % reserved for local use
facility(local5) -> 21 bsl 3; % reserved for local use
facility(local6) -> 22 bsl 3; % reserved for local use
facility(local7) -> 23 bsl 3. % reserved for local use

%% importance of message
level(emerg) -> 0;
level(alert) -> 1;
level(crit) -> 2;
level(err) -> 3;
level(warning) -> 4;
level(notice) -> 5;
level(info) -> 6;
level(debug) -> 7.

0 comments on commit 869ca20

Please sign in to comment.