From 869ca2018031c7b4bd46fa2dd28ad3ece59fc20e Mon Sep 17 00:00:00 2001 From: Michael Santos Date: Thu, 18 Mar 2010 17:03:18 -0400 Subject: [PATCH] Allow setting facility and options in openlog. Modify openlog to an arity of 3. Provide some functions for retrieving syslog constants. --- c_src/syslog.c | 12 ++++++++---- src/syslog.erl | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/c_src/syslog.c b/c_src/syslog.c index 96a27e8..80aab8a 100644 --- a/c_src/syslog.c +++ b/c_src/syslog.c @@ -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"); } @@ -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} }; diff --git a/src/syslog.erl b/src/syslog.erl index 5da23af..3d90699 100644 --- a/src/syslog.erl +++ b/src/syslog.erl @@ -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() -> @@ -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.