diff --git a/README.md b/README.md index b78ac2e..ecc8b7d 100644 --- a/README.md +++ b/README.md @@ -30,13 +30,13 @@ Journald::LOG_DEBUG # debug-level messages ``` systemd-journal uses syslog constants to denote level therefore they are equal to those of the Syslog module, -e.g. ```Journald::LOG_WARNING == Syslog::LOG_WARNING```. +e.g. ```Journald::LOG_WARNING == Syslog::LOG_WARNING```. [See syslog man page for more info](http://man7.org/linux/man-pages/man3/syslog.3.html) ### Methods -Methods of Journald::Native class wrap systemd-journal calls. -[See sd-journal help for more info](http://www.freedesktop.org/software/systemd/man/sd_journal_print.html) +Methods of Journald::Native class wrap systemd-journal calls. +[See sd-journal help for more info](http://www.freedesktop.org/software/systemd/man/sd_journal_print.html) ```ruby Journald::Native.send "MESSAGE=message", "PRIORITY=#{Journald::LOG_WARNING}" @@ -44,9 +44,9 @@ Journald::Native.print Journald::LOG_WARNING, "message" Journald::Native.perror "message" ``` -It is not recommended to use ```print``` and ```perror``` as you may lose ```'\0'``` byte in your string due to -C zero-terminated string format (all zero bytes in the middle will be removed) On the contrary ```send``` uses -binary buffers and does not have this shortcoming. +It is not recommended to use ```print``` and ```perror``` as you may get exception if your string contains +```'\0'``` byte due to C zero-terminated string format. On the contrary ```send``` uses binary buffers and +does not have this shortcoming. ### License diff --git a/ext/journald_native/extconf.rb b/ext/journald_native/extconf.rb index 291e72e..7fc3cf1 100644 --- a/ext/journald_native/extconf.rb +++ b/ext/journald_native/extconf.rb @@ -4,11 +4,12 @@ LIBDIR = RbConfig::CONFIG['libdir'] INCLUDEDIR = RbConfig::CONFIG['includedir'] HEADER_DIRS = [INCLUDEDIR] - -LIB_DIRS = [LIBDIR] +LIB_DIRS = [LIBDIR] dir_config('systemd', HEADER_DIRS, LIB_DIRS) +$CFLAGS = '-std=c99' + def have_funcs have_funcs = true @@ -33,4 +34,3 @@ end create_header create_makefile('journald_native') - diff --git a/ext/journald_native/journald_native.c b/ext/journald_native/journald_native.c index 99568ad..3317323 100644 --- a/ext/journald_native/journald_native.c +++ b/ext/journald_native/journald_native.c @@ -15,9 +15,7 @@ static void jdl_init_methods(); static VALUE jdl_native_print(VALUE self, VALUE priority, VALUE message); static VALUE jdl_native_send(int argc, VALUE* argv, VALUE self); static VALUE jdl_native_perror(VALUE self, VALUE message); - -/* aux */ -static char * jdl_alloc_safe_string(VALUE string); +static VALUE jdl_native_is_dummy(); /* globals */ static VALUE mJournald; @@ -46,6 +44,9 @@ static void jdl_init_constants() rb_define_const(mJournald, "LOG_NOTICE", INT2NUM(LOG_NOTICE)); /* normal but significant condition */ rb_define_const(mJournald, "LOG_INFO", INT2NUM(LOG_INFO)); /* informational */ rb_define_const(mJournald, "LOG_DEBUG", INT2NUM(LOG_DEBUG)); /* debug-level messages */ + + // dummy detection const + rb_define_const(mNative, "IS_DUMMY", jdl_native_is_dummy()); } static void jdl_init_methods() @@ -53,6 +54,9 @@ static void jdl_init_methods() rb_define_singleton_method(mNative, "print", jdl_native_print, 2); rb_define_singleton_method(mNative, "send", jdl_native_send, -1); /* -1 to pass as C array */ rb_define_singleton_method(mNative, "perror", jdl_native_perror, 1); + + // dummy detection method + rb_define_singleton_method(mNative, "dummy?", jdl_native_is_dummy, 0); } static VALUE jdl_native_print(VALUE v_self, VALUE v_priority, VALUE v_message) @@ -61,12 +65,10 @@ static VALUE jdl_native_print(VALUE v_self, VALUE v_priority, VALUE v_message) char *message; priority = NUM2INT(v_priority); - message = jdl_alloc_safe_string(v_message); + message = StringValueCStr(v_message); result = sd_journal_print(priority, "%s", message); - free(message); - return INT2NUM(result); } @@ -102,42 +104,14 @@ static VALUE jdl_native_perror(VALUE v_self, VALUE v_message) int result; char *message; - message = jdl_alloc_safe_string(v_message); + message = StringValueCStr(v_message); result = sd_journal_perror(message); - free(message); - return INT2NUM(result); } -/** - * Remove zeros from string and ensure it's zero-terminated - */ -static char * jdl_alloc_safe_string(VALUE v_string) +static VALUE jdl_native_is_dummy() { - char *str; - size_t len; - - char *newstr, - *ptr; - size_t i; - - /* convert to string */ - StringValue(v_string); - - str = RSTRING_PTR(v_string); - len = RSTRING_LEN(v_string); - - newstr = calloc(len + 1, sizeof(char)); - - for (i = 0, ptr = newstr; i < len; i++) { - if (str[i]) { - *(ptr++) = str[i]; - } - } - - *ptr = '\0'; - - return newstr; + return JOURNALD_NATIVE_SD_JOURNAL_DUMMY ? Qtrue : Qfalse; } diff --git a/ext/journald_native/sd_journal.h b/ext/journald_native/sd_journal.h index 177bd49..81fb4fa 100644 --- a/ext/journald_native/sd_journal.h +++ b/ext/journald_native/sd_journal.h @@ -1,3 +1,5 @@ +#include + #ifdef __cplusplus extern "C" { #endif @@ -7,6 +9,8 @@ extern "C" { #ifdef __linux__ + #define JOURNALD_NATIVE_SD_JOURNAL_DUMMY false + /* do the real stuff */ #include "extconf.h" @@ -29,43 +33,23 @@ extern "C" { #error Required function sd_journal_perror is missing #endif + /* Do not add C line and file to the log messages */ + #define SD_JOURNAL_SUPPRESS_LOCATION /* include systemd-journal headers */ - #include #else + #define JOURNALD_NATIVE_SD_JOURNAL_DUMMY true + #warning Compiling dummy version of the gem for non-Linux OS - #include - - /* use dummy */ - #define JOURNALD_NATIVE_SYSTEMD_JOURNAL_DUMMY - - /* syslog constants */ - #define LOG_EMERG 0 - #define LOG_ALERT 1 - #define LOG_CRIT 2 - #define LOG_ERR 3 - #define LOG_WARNING 4 - #define LOG_NOTICE 5 - #define LOG_INFO 6 - #define LOG_DEBUG 7 - - /* iovec */ - struct iovec { - void *iov_base; /* Starting address */ - size_t iov_len; /* Number of bytes to transfer */ - }; - - int sd_journal_print(int priority, const char *format, ...); - int sd_journal_sendv(const struct iovec *iov, int n); - int sd_journal_perror(const char *message); + #include "sd_journal_dummy.h" #endif -#endif - #ifdef __cplusplus } #endif + +#endif // JOURNALD_NATIVE_SD_JOURNAL_H diff --git a/ext/journald_native/sd_journal_dummy.c b/ext/journald_native/sd_journal_dummy.c deleted file mode 100644 index 306a9f1..0000000 --- a/ext/journald_native/sd_journal_dummy.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "sd_journal.h" - -#ifdef JOURNALD_NATIVE_SYSTEMD_JOURNAL_DUMMY - -int sd_journal_print(int priority, const char *format, ...) { return 0; } -int sd_journal_sendv(const struct iovec *iov, int n) { return 0; } -int sd_journal_perror(const char *message) { return 0; } - -#endif diff --git a/ext/journald_native/sd_journal_dummy.h b/ext/journald_native/sd_journal_dummy.h new file mode 100644 index 0000000..e1e2c1a --- /dev/null +++ b/ext/journald_native/sd_journal_dummy.h @@ -0,0 +1,23 @@ +/* dummy code to be used in sd_journal.h on non-linux system */ + +#include + +/* syslog constants */ +#define LOG_EMERG 0 +#define LOG_ALERT 1 +#define LOG_CRIT 2 +#define LOG_ERR 3 +#define LOG_WARNING 4 +#define LOG_NOTICE 5 +#define LOG_INFO 6 +#define LOG_DEBUG 7 + +/* iovec */ +struct iovec { + void *iov_base; /* Starting address */ + size_t iov_len; /* Number of bytes to transfer */ +}; + +inline int sd_journal_print(int priority, const char *format, ...) { return 0; } +inline int sd_journal_sendv(const struct iovec *iov, int n) { return 0; } +inline int sd_journal_perror(const char *message) { return 0; }