Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit bf7db6a376c69ac6b98424fd87f6d12b61b7b07d @ngerakines committed Mar 17, 2010
Showing with 161 additions and 0 deletions.
  1. +6 −0 Emakefile
  2. +36 −0 Makefile
  3. +9 −0 README.markdown
  4. +69 −0 c_src/syslog.c
  5. +41 −0 src/syslog.erl
@@ -0,0 +1,6 @@
+{["src/*"],
+ [{i, "include"},
+ {outdir, "ebin"},
+ debug_info]
+}.
+
@@ -0,0 +1,36 @@
+
+ERL=erl
+APP=syslog
+
+CC=gcc
+
+#Mac OS X: use "-m64" for a 64-bit erlang
+ARCH=-m64
+
+# By default, use the system crypt(3), which is DES only
+FLAGS=$(ARCH) -O3 -fPIC -shared
+
+# Linux
+#FLAGS=-fPIC -shared -lcrypt
+
+ERL_ROOT=/usr/local/lib/erlang
+CFLAGS=-g -Wall
+
+
+all: dir erl nif
+
+dir:
+ -@mkdir -p priv ebin
+
+erl:
+ @$(ERL) -noinput +B \
+ -eval 'case make:all() of up_to_date -> halt(0); error -> halt(1) end.'
+
+nif:
+ (cd c_src && \
+ $(CC) -g -Wall $(FLAGS) -o ../priv/$(APP).so \
+ $(APP).c -I $(ERL_ROOT)/usr/include/ )
+
+clean:
+ @rm -fv ebin/*.beam priv/$(APP).so c_src/*.a c_src/*.o
+
@@ -0,0 +1,9 @@
+
+A small nif to write to the local syslog daemon inspired by the project [JacobVorreuter/erlang_syslog](http://github.com/JacobVorreuter/erlang_syslog). Patches wanted.
+
+TODO:
+
+ * openlog options
+ * tests
+
+
@@ -0,0 +1,69 @@
+/*
+Copyright (c) 2010 Nick Gerakines <nick at gerakines dot net>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "erl_nif.h"
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <err.h>
+#include <sys/errno.h>
+#include <syslog.h>
+
+#define MAXBUFLEN 1024
+
+static ERL_NIF_TERM nif_open(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
+ char facil[MAXBUFLEN];
+ if (enif_get_string(env, argv[0], facil, sizeof(facil), ERL_NIF_LATIN1) < 1) {
+ return enif_make_badarg(env);
+ }
+ openlog(facil, LOG_CONS | LOG_PID, 0);
+ return enif_make_atom(env, "ok");
+}
+
+static ERL_NIF_TERM nif_write(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
+ char message[MAXBUFLEN];
+ int log_level;
+ if (!enif_get_int(env, argv[0], &log_level)) {
+ return enif_make_badarg(env);
+ }
+ if (enif_get_string(env, argv[1], message, sizeof(message), ERL_NIF_LATIN1) < 1) {
+ return enif_make_badarg(env);
+ }
+ syslog(log_level, message);
+ return enif_make_atom(env, "ok");
+}
+
+static ERL_NIF_TERM nif_close(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
+ closelog();
+ return enif_make_atom(env, "ok");
+}
+
+static ErlNifFunc nif_funcs[] = {
+ {"open", 1, nif_open},
+ {"write", 2, nif_write},
+ {"close", 0, nif_close}
+};
+
+ERL_NIF_INIT(syslog, nif_funcs, NULL, NULL, NULL, NULL)
+
@@ -0,0 +1,41 @@
+%% Copyright (c) 2010 Nick Gerakines <nick at gerakines dot net>
+%%
+%% Permission is hereby granted, free of charge, to any person
+%% obtaining a copy of this software and associated documentation
+%% files (the "Software"), to deal in the Software without
+%% restriction, including without limitation the rights to use,
+%% copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the
+%% Software is furnished to do so, subject to the following
+%% conditions:
+%%
+%% The above copyright notice and this permission notice shall be
+%% included in all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+%% 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]).
+-on_load(on_load/0).
+
+on_load() ->
+ Lib = filename:join([
+ filename:dirname(code:which(?MODULE)),
+ "..",
+ "priv",
+ ?MODULE
+ ]),
+ erlang:load_nif(Lib, 0).
+
+open(_) -> erlang:error(not_implemented).
+
+write(_,_) -> erlang:error(not_implemented).
+
+close() -> erlang:error(not_implemented).
+

0 comments on commit bf7db6a

Please sign in to comment.