Update C version to 1.0.6 features

pull/2/merge
Anton Smirnov 8 years ago
parent 9cf5337dde
commit d9c420fe61

@ -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

@ -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')

@ -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;
}

@ -1,3 +1,5 @@
#include <stdbool.h>
#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 <systemd/sd-journal.h>
#else
#warning Compiling dummy version of the gem for non-Linux OS
#include <stdlib.h>
/* use dummy */
#define JOURNALD_NATIVE_SYSTEMD_JOURNAL_DUMMY
#define JOURNALD_NATIVE_SD_JOURNAL_DUMMY true
/* 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 */
};
#warning Compiling dummy version of the gem for non-Linux OS
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

@ -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

@ -0,0 +1,23 @@
/* dummy code to be used in sd_journal.h on non-linux system */
#include <stdlib.h>
/* 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; }
Loading…
Cancel
Save