journald-native 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd487e7cc5f8f4afd839c141cda94ba78a2109cc
4
- data.tar.gz: e7b5952a046140f8d0e0141446342b658de604c5
3
+ metadata.gz: 62f14b24b4e9636244fd45956b5aa2804748099f
4
+ data.tar.gz: a373cd433f9a905a0c4e1310a42979b50b6f06cb
5
5
  SHA512:
6
- metadata.gz: 93f5494817ce979b29d2ce630dd4a35675ab685137b61d8556fbb58c816e0d5978c48d4df8c64323baba2fc0feae783af9720790d7565230eea9c58995287f27
7
- data.tar.gz: 0a3e5b415e8b0491c7bad759d7d99767022e6c4c212923b3083b08a41e636bc6aa221c552d3334081b8db79a02ae6539e425fef31d9431458eb3ded4e4d6dd9e
6
+ metadata.gz: 3df15346ea9c39be6bff77310c24e26e03bb23f654d21f15388c8785d09532b88ac47f7cb0ff939dece01535a18d20785790f9faf245044bb9d1258c5509808d
7
+ data.tar.gz: 34326f6563d13d955da83995d16345a2c385d4f2d87fefee4a248087e703c7c61b8dfb05e4df273db880d4638f6a752abccae058f6b143542efc4cf62588d00c
@@ -0,0 +1,7 @@
1
+ cmake_minimum_required(VERSION 2.8.4)
2
+ project(journald_native)
3
+
4
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5
+
6
+ set(SOURCE_FILES extinit.cpp journald_native.cpp ruby_exception_wrapper.cpp)
7
+ add_executable(journald_native ${SOURCE_FILES})
@@ -0,0 +1,78 @@
1
+ /*
2
+ unique_ptr from http://isocpp.org/files/papers/N3656.txt
3
+ index sequence from gcc 4.9 sources
4
+ */
5
+
6
+ #include <cstddef>
7
+ #include <memory>
8
+ #include <type_traits>
9
+ #include <utility>
10
+
11
+ namespace std {
12
+ template<class T> struct _Unique_if {
13
+ typedef unique_ptr<T> _Single_object;
14
+ };
15
+
16
+ template<class T> struct _Unique_if<T[]> {
17
+ typedef unique_ptr<T[]> _Unknown_bound;
18
+ };
19
+
20
+ template<class T, size_t N> struct _Unique_if<T[N]> {
21
+ typedef void _Known_bound;
22
+ };
23
+
24
+ template<class T, class... Args>
25
+ typename _Unique_if<T>::_Single_object
26
+ make_unique(Args&&... args) {
27
+ return unique_ptr<T>(new T(std::forward<Args>(args)...));
28
+ }
29
+
30
+ template<class T>
31
+ typename _Unique_if<T>::_Unknown_bound
32
+ make_unique(size_t n) {
33
+ typedef typename remove_extent<T>::type U;
34
+ return unique_ptr<T>(new U[n]());
35
+ }
36
+
37
+ template<class T, class... Args>
38
+ typename _Unique_if<T>::_Known_bound
39
+ make_unique(Args&&...) = delete;
40
+
41
+ /// Class template integer_sequence
42
+ template<typename _Tp, _Tp... _Idx>
43
+ struct integer_sequence
44
+ {
45
+ typedef _Tp value_type;
46
+ static constexpr size_t size() { return sizeof...(_Idx); }
47
+ };
48
+
49
+ template<typename _Tp, _Tp _Num,
50
+ typename _ISeq = typename _Build_index_tuple<_Num>::__type>
51
+ struct _Make_integer_sequence;
52
+
53
+ template<typename _Tp, _Tp _Num, size_t... _Idx>
54
+ struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>>
55
+ {
56
+ static_assert( _Num >= 0,
57
+ "Cannot make integer sequence of negative length" );
58
+
59
+ typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type;
60
+ };
61
+
62
+ /// Alias template make_integer_sequence
63
+ template<typename _Tp, _Tp _Num>
64
+ using make_integer_sequence
65
+ = typename _Make_integer_sequence<_Tp, _Num>::__type;
66
+
67
+ /// Alias template index_sequence
68
+ template<size_t... _Idx>
69
+ using index_sequence = integer_sequence<size_t, _Idx...>;
70
+
71
+ /// Alias template make_index_sequence
72
+ template<size_t _Num>
73
+ using make_index_sequence = make_integer_sequence<size_t, _Num>;
74
+
75
+ /// Alias template index_sequence_for
76
+ template<typename... _Types>
77
+ using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
78
+ }
@@ -4,11 +4,12 @@ LIBDIR = RbConfig::CONFIG['libdir']
4
4
  INCLUDEDIR = RbConfig::CONFIG['includedir']
5
5
 
6
6
  HEADER_DIRS = [INCLUDEDIR]
7
-
8
- LIB_DIRS = [LIBDIR]
7
+ LIB_DIRS = [LIBDIR]
9
8
 
10
9
  dir_config('systemd', HEADER_DIRS, LIB_DIRS)
11
10
 
11
+ $CPPFLAGS = '-std=c++11'
12
+
12
13
  def have_funcs
13
14
  have_funcs = true
14
15
 
@@ -33,4 +34,3 @@ end
33
34
 
34
35
  create_header
35
36
  create_makefile('journald_native')
36
-
@@ -0,0 +1,7 @@
1
+ #include "journald_native.h"
2
+
3
+ /* initialize ruby extension */
4
+ extern "C" void Init_journald_native()
5
+ {
6
+ journald_native::init_modules();
7
+ }
@@ -0,0 +1,149 @@
1
+ #include "journald_native.h"
2
+ #include "sd_journal.h"
3
+
4
+ #include <memory>
5
+
6
+ #include "ruby_exception_wrapper.h"
7
+
8
+ namespace journald_native {
9
+
10
+ // just a short alias for ruby_raisable_call()
11
+ template <typename Func, typename... Args>
12
+ inline auto r(Func f, Args... args) -> decltype(f(args...))
13
+ {
14
+ return ruby_exception_wrapper::ruby_raisable_call(f, args...);
15
+ }
16
+
17
+ /* initializers */
18
+ inline void init_constants(VALUE module);
19
+ inline void init_methods(VALUE module);
20
+
21
+ /* methods */
22
+ VALUE native_print(VALUE self, VALUE priority, VALUE message);
23
+ VALUE native_send(int argc, VALUE* argv, VALUE self);
24
+ VALUE native_perror(VALUE self, VALUE message);
25
+ inline VALUE native_print_impl(VALUE v_self, VALUE v_priority, VALUE v_message);
26
+ inline VALUE native_send_impl(int argc, VALUE* argv, VALUE self);
27
+ inline VALUE native_perror_impl(VALUE v_self, VALUE v_message);
28
+
29
+ /* aux */
30
+ std::string create_safe_string(VALUE string); // throws ruby exceptions
31
+
32
+ /* initializers */
33
+ void init_modules()
34
+ {
35
+ // no nontrivial destructors during initialization, no need for ruby catch
36
+
37
+ VALUE mJournald = rb_define_module("Journald");
38
+ VALUE mNative = rb_define_module_under(mJournald, "Native");
39
+
40
+ init_constants(mJournald); // add constants to Journald
41
+ init_methods(mNative); // add methods to Journald::Native
42
+ }
43
+
44
+ inline void init_constants(VALUE module)
45
+ {
46
+ rb_define_const(module, "LOG_EMERG", INT2NUM(LOG_EMERG)); /* system is unusable */
47
+ rb_define_const(module, "LOG_ALERT", INT2NUM(LOG_ALERT)); /* action must be taken immediately */
48
+ rb_define_const(module, "LOG_CRIT", INT2NUM(LOG_CRIT)); /* critical conditions */
49
+ rb_define_const(module, "LOG_ERR", INT2NUM(LOG_ERR)); /* error conditions */
50
+ rb_define_const(module, "LOG_WARNING", INT2NUM(LOG_WARNING)); /* warning conditions */
51
+ rb_define_const(module, "LOG_NOTICE", INT2NUM(LOG_NOTICE)); /* normal but significant condition */
52
+ rb_define_const(module, "LOG_INFO", INT2NUM(LOG_INFO)); /* informational */
53
+ rb_define_const(module, "LOG_DEBUG", INT2NUM(LOG_DEBUG)); /* debug-level messages */
54
+ }
55
+
56
+ inline void init_methods(VALUE module)
57
+ {
58
+ rb_define_singleton_method(module, "print", RUBY_METHOD_FUNC(native_print), 2);
59
+ rb_define_singleton_method(module, "send", RUBY_METHOD_FUNC(native_send), -1); /* -1 to pass as C array */
60
+ rb_define_singleton_method(module, "perror", RUBY_METHOD_FUNC(native_perror), 1);
61
+ }
62
+
63
+ VALUE native_print(VALUE v_self, VALUE v_priority, VALUE v_message)
64
+ {
65
+ try {
66
+ return native_print_impl(v_self, v_priority, v_message);
67
+ } catch(ruby_exception_wrapper::RbWrappedException &e) {
68
+ rb_exc_raise(e.getRubyException());
69
+ }
70
+ }
71
+
72
+ VALUE native_send(int argc, VALUE* argv, VALUE v_self)
73
+ {
74
+ try {
75
+ return native_send_impl(argc, argv, v_self);
76
+ } catch(ruby_exception_wrapper::RbWrappedException &e) {
77
+ rb_exc_raise(e.getRubyException());
78
+ }
79
+ }
80
+
81
+ VALUE native_perror(VALUE v_self, VALUE v_message)
82
+ {
83
+ try {
84
+ return native_perror_impl(v_self, v_message);
85
+ } catch(ruby_exception_wrapper::RbWrappedException &e) {
86
+ rb_exc_raise(e.getRubyException());
87
+ }
88
+ }
89
+
90
+ /* methods */
91
+ inline VALUE native_print_impl(VALUE v_self, VALUE v_priority, VALUE v_message)
92
+ {
93
+ int priority = NUM2INT(v_priority);
94
+ auto message = create_safe_string(v_message); // ruby exception here
95
+
96
+ int result = sd_journal_print(priority, "%s", message.c_str());
97
+
98
+ return INT2NUM(result);
99
+ }
100
+
101
+ inline VALUE native_send_impl(int argc, VALUE* argv, VALUE v_self)
102
+ {
103
+ auto msgs = std::make_unique<iovec[]>((size_t)argc);
104
+
105
+ for (int i = 0; i < argc; i++) {
106
+ VALUE v = r(rb_string_value, &argv[i]);
107
+
108
+ msgs[i].iov_base = (char *)RSTRING_PTR(v);
109
+ msgs[i].iov_len = (size_t)RSTRING_LEN(v);
110
+ }
111
+
112
+ int result = sd_journal_sendv(msgs.get(), argc);
113
+
114
+ return INT2NUM(result);
115
+ }
116
+
117
+ inline VALUE native_perror_impl(VALUE v_self, VALUE v_message)
118
+ {
119
+ auto message = create_safe_string(v_message); // ruby exception here
120
+
121
+ int result = sd_journal_perror(message.c_str());
122
+
123
+ return INT2NUM(result);
124
+ }
125
+
126
+ /**
127
+ * Remove zeros from the string
128
+ */
129
+ std::string create_safe_string(VALUE v_string)
130
+ {
131
+ /* convert to string */
132
+ r(rb_string_value, &v_string);
133
+
134
+ char* str = (char *)RSTRING_PTR(v_string);
135
+ size_t len = (size_t)RSTRING_LEN(v_string);
136
+
137
+ std::string safe_str;
138
+ safe_str.reserve(len);
139
+
140
+ for (size_t i = 0; i < len; i++) {
141
+ if (str[i]) {
142
+ safe_str += str[i];
143
+ }
144
+ }
145
+
146
+ return safe_str;
147
+ }
148
+
149
+ }
@@ -0,0 +1,12 @@
1
+ #ifndef JOURNALD_NATIVE_JOURNALD_NATIVE_H
2
+ #define JOURNALD_NATIVE_JOURNALD_NATIVE_H
3
+
4
+ #include <string>
5
+ #include <ruby.h>
6
+
7
+ namespace journald_native {
8
+ /* initializers */
9
+ void init_modules();
10
+ }
11
+
12
+ #endif // JOURNALD_NATIVE_JOURNALD_NATIVE_H
@@ -0,0 +1,16 @@
1
+ #include "ruby_exception_wrapper.h"
2
+
3
+ namespace ruby_exception_wrapper {
4
+
5
+ // callback for rb_rescue2 to catch ruby exception and wrap it by RbWrappedException
6
+ VALUE rethrow_as_cpp(VALUE put_exception_here_ptr, VALUE exception)
7
+ {
8
+ // cannot actually throw here, just pass the exception
9
+ VALUE* excptr = reinterpret_cast<VALUE*>(put_exception_here_ptr);
10
+
11
+ *excptr = exception;
12
+
13
+ return exception;
14
+ }
15
+
16
+ }
@@ -0,0 +1,91 @@
1
+ #ifndef RUBY_EXCEPTION_WRAPPER_H
2
+ #define RUBY_EXCEPTION_WRAPPER_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include <functional>
7
+ #include <type_traits>
8
+ #include <utility>
9
+
10
+ #include "cpp14shiv.h" // make this c++14 code c++11 compatible; remove for c++14
11
+
12
+ namespace ruby_exception_wrapper {
13
+
14
+ class RbWrappedException: public std::exception {
15
+ VALUE ruby_exception;
16
+ public:
17
+ RbWrappedException(VALUE e): ruby_exception(e) {};
18
+ VALUE getRubyException() { return ruby_exception; }
19
+ };
20
+
21
+ // callback for rb_rescue2 to catch ruby exception and wrap it by RbWrappedException
22
+ VALUE rethrow_as_cpp(VALUE put_exception_here_ptr, VALUE exception);
23
+
24
+ namespace {
25
+ // do real call of function from template for func with params
26
+ template <typename FuncPointer, typename... Args>
27
+ inline VALUE call_wrapper_tuple_impl(FuncPointer& fp, Args... args)
28
+ {
29
+ // only pointers and VALUE permitted
30
+ // VALUE guaranteed to be able to contain pointer
31
+ return reinterpret_cast<VALUE>(fp(args...));
32
+ }
33
+
34
+ // unpack params to do real call
35
+ template <typename CallTuple, std::size_t... Is>
36
+ inline VALUE call_wrapper_tuple_unpack(CallTuple& call_tuple, std::index_sequence<Is...>)
37
+ {
38
+ return call_wrapper_tuple_impl(std::get<Is>(call_tuple)...);
39
+ }
40
+
41
+ // callback for rb_rescue2 for no exceptions
42
+ template <typename CallTuple>
43
+ VALUE call_wrapper_tuple(VALUE v_ct) {
44
+ CallTuple call_tuple = std::move(*reinterpret_cast<CallTuple*>(v_ct));
45
+
46
+ return call_wrapper_tuple_unpack(call_tuple, std::make_index_sequence<std::tuple_size<CallTuple>::value> {});
47
+ };
48
+
49
+ // safely call function
50
+ template <typename Func, typename... Args>
51
+ inline auto do_raisable_call(Func f, Args... args) -> decltype(f(args...))
52
+ {
53
+ typedef std::function<typename std::remove_pointer<Func>::type> FuncPointer;
54
+ typedef std::tuple<FuncPointer, Args...> CallTuple;
55
+
56
+ FuncPointer fp = f;
57
+
58
+ CallTuple call_tuple = std::make_tuple(fp, args...);
59
+
60
+ VALUE exception = 0; // get raised exception if any
61
+
62
+ VALUE result = rb_rescue2(
63
+ RUBY_METHOD_FUNC(call_wrapper_tuple<CallTuple>), reinterpret_cast<VALUE>(&call_tuple),
64
+ RUBY_METHOD_FUNC(rethrow_as_cpp), reinterpret_cast<VALUE>(&exception),
65
+ rb_eException, Qfalse
66
+ );
67
+
68
+ if (exception) { // nonzero here if rescue called unless some pervert throws Qfalse
69
+ throw RbWrappedException(exception);
70
+ }
71
+
72
+ return reinterpret_cast<decltype(f(args...))>(result);
73
+ }
74
+ }
75
+
76
+ template <typename Func, typename... Args>
77
+ inline auto ruby_raisable_call(Func f, Args... args) -> decltype(f(args...))
78
+ {
79
+ static_assert(
80
+ std::is_same<decltype(f(args...)), VALUE>::value || std::is_pointer<decltype(f(args...))>::value,
81
+ "Only for funcs returning VALUE or pointer"
82
+ );
83
+
84
+ auto result = do_raisable_call(f, args...);
85
+
86
+ return result;
87
+ }
88
+
89
+ }
90
+
91
+ #endif
@@ -1,71 +1,41 @@
1
- #ifdef __cplusplus
2
- extern "C" {
3
- #endif
4
-
5
1
  #ifndef JOURNALD_NATIVE_SD_JOURNAL_H
6
- #define JOURNALD_NATIVE_SD_JOURNAL_H
7
-
8
- #ifdef __linux__
9
-
10
- /* do the real stuff */
11
-
12
- #include "extconf.h"
13
-
14
- /* check for extconf results */
15
-
16
- #ifndef HAVE_SYSTEMD_SD_JOURNAL_H
17
- #error Cannot include <systemd/sd-journal.h>. Please use linux version with systemd-journal installed
18
- #endif
2
+ #define JOURNALD_NATIVE_SD_JOURNAL_H
19
3
 
20
- #ifndef HAVE_SD_JOURNAL_PRINT
21
- #error Required function sd_journal_print is missing
22
- #endif
4
+ #ifdef __linux__
23
5
 
24
- #ifndef HAVE_SD_JOURNAL_SENDV
25
- #error Required function sd_journal_sendv is missing
26
- #endif
6
+ /* do the real stuff */
27
7
 
28
- #ifndef HAVE_SD_JOURNAL_PERROR
29
- #error Required function sd_journal_perror is missing
30
- #endif
8
+ #include "extconf.h"
31
9
 
32
- /* include systemd-journal headers */
10
+ /* check for extconf results */
33
11
 
34
- #include <systemd/sd-journal.h>
35
-
36
- #else
12
+ #ifndef HAVE_SYSTEMD_SD_JOURNAL_H
13
+ #error Cannot include <systemd/sd-journal.h>. Please use linux version with systemd-journal installed
14
+ #endif
37
15
 
38
- #warning Compiling dummy version of the gem for non-Linux OS
16
+ #ifndef HAVE_SD_JOURNAL_PRINT
17
+ #error Required function sd_journal_print is missing
18
+ #endif
39
19
 
40
- #include <stdlib.h>
20
+ #ifndef HAVE_SD_JOURNAL_SENDV
21
+ #error Required function sd_journal_sendv is missing
22
+ #endif
41
23
 
42
- /* use dummy */
43
- #define JOURNALD_NATIVE_SYSTEMD_JOURNAL_DUMMY
24
+ #ifndef HAVE_SD_JOURNAL_PERROR
25
+ #error Required function sd_journal_perror is missing
26
+ #endif
44
27
 
45
- /* syslog constants */
46
- #define LOG_EMERG 0
47
- #define LOG_ALERT 1
48
- #define LOG_CRIT 2
49
- #define LOG_ERR 3
50
- #define LOG_WARNING 4
51
- #define LOG_NOTICE 5
52
- #define LOG_INFO 6
53
- #define LOG_DEBUG 7
28
+ /* Do not add C line and file to the log messages */
29
+ #define SD_JOURNAL_SUPPRESS_LOCATION
30
+ /* include systemd-journal headers */
31
+ #include <systemd/sd-journal.h>
54
32
 
55
- /* iovec */
56
- struct iovec {
57
- void *iov_base; /* Starting address */
58
- size_t iov_len; /* Number of bytes to transfer */
59
- };
33
+ #else
60
34
 
61
- int sd_journal_print(int priority, const char *format, ...);
62
- int sd_journal_sendv(const struct iovec *iov, int n);
63
- int sd_journal_perror(const char *message);
35
+ #warning Compiling dummy version of the gem for non-Linux OS
64
36
 
65
- #endif
37
+ #include "sd_journal_dummy.h"
66
38
 
67
39
  #endif
68
40
 
69
- #ifdef __cplusplus
70
- }
71
- #endif
41
+ #endif // JOURNALD_NATIVE_SD_JOURNAL_H
@@ -0,0 +1,23 @@
1
+ /* dummy code to be used in sd_journal.h on non-linux system */
2
+
3
+ #include <stdlib.h>
4
+
5
+ /* syslog constants */
6
+ #define LOG_EMERG 0
7
+ #define LOG_ALERT 1
8
+ #define LOG_CRIT 2
9
+ #define LOG_ERR 3
10
+ #define LOG_WARNING 4
11
+ #define LOG_NOTICE 5
12
+ #define LOG_INFO 6
13
+ #define LOG_DEBUG 7
14
+
15
+ /* iovec */
16
+ struct iovec {
17
+ void *iov_base; /* Starting address */
18
+ size_t iov_len; /* Number of bytes to transfer */
19
+ };
20
+
21
+ inline int sd_journal_print(int priority, const char *format, ...) { return 0; }
22
+ inline int sd_journal_sendv(const struct iovec *iov, int n) { return 0; }
23
+ inline int sd_journal_perror(const char *message) { return 0; }
@@ -1,5 +1,5 @@
1
1
  module Journald
2
2
  module Native
3
- VERSION = '1.0.3'
3
+ VERSION = '1.0.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: journald-native
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Smirnov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-05 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,10 +65,16 @@ files:
65
65
  - LICENSE.txt
66
66
  - README.md
67
67
  - Rakefile
68
+ - ext/journald_native/CMakeLists.txt
69
+ - ext/journald_native/cpp14shiv.h
68
70
  - ext/journald_native/extconf.rb
69
- - ext/journald_native/journald_native.c
71
+ - ext/journald_native/extinit.cpp
72
+ - ext/journald_native/journald_native.cpp
73
+ - ext/journald_native/journald_native.h
74
+ - ext/journald_native/ruby_exception_wrapper.cpp
75
+ - ext/journald_native/ruby_exception_wrapper.h
70
76
  - ext/journald_native/sd_journal.h
71
- - ext/journald_native/sd_journal_dummy.c
77
+ - ext/journald_native/sd_journal_dummy.h
72
78
  - journald-native.gemspec
73
79
  - lib/journald/native.rb
74
80
  - lib/journald/native/version.rb
@@ -92,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
98
  version: '0'
93
99
  requirements: []
94
100
  rubyforge_project:
95
- rubygems_version: 2.4.3
101
+ rubygems_version: 2.2.2
96
102
  signing_key:
97
103
  specification_version: 4
98
104
  summary: systemd-journal logging native lib wrapper
@@ -1,143 +0,0 @@
1
- /* Do not add C line and file to the log messages */
2
- #define SD_JOURNAL_SUPPRESS_LOCATION
3
-
4
- #include <ruby.h>
5
- #include "sd_journal.h"
6
-
7
- void Init_journald_native();
8
-
9
- /* initializers */
10
- static void jdl_init_modules();
11
- static void jdl_init_constants();
12
- static void jdl_init_methods();
13
-
14
- /* methods */
15
- static VALUE jdl_native_print(VALUE self, VALUE priority, VALUE message);
16
- static VALUE jdl_native_send(int argc, VALUE* argv, VALUE self);
17
- static VALUE jdl_native_perror(VALUE self, VALUE message);
18
-
19
- /* aux */
20
- static char * jdl_alloc_safe_string(VALUE string);
21
-
22
- /* globals */
23
- static VALUE mJournald;
24
- static VALUE mNative;
25
-
26
- void Init_journald_native()
27
- {
28
- jdl_init_modules();
29
- jdl_init_constants();
30
- jdl_init_methods();
31
- }
32
-
33
- static void jdl_init_modules()
34
- {
35
- mJournald = rb_define_module("Journald");
36
- mNative = rb_define_module_under(mJournald, "Native");
37
- }
38
-
39
- static void jdl_init_constants()
40
- {
41
- rb_define_const(mJournald, "LOG_EMERG", INT2NUM(LOG_EMERG)); /* system is unusable */
42
- rb_define_const(mJournald, "LOG_ALERT", INT2NUM(LOG_ALERT)); /* action must be taken immediately */
43
- rb_define_const(mJournald, "LOG_CRIT", INT2NUM(LOG_CRIT)); /* critical conditions */
44
- rb_define_const(mJournald, "LOG_ERR", INT2NUM(LOG_ERR)); /* error conditions */
45
- rb_define_const(mJournald, "LOG_WARNING", INT2NUM(LOG_WARNING)); /* warning conditions */
46
- rb_define_const(mJournald, "LOG_NOTICE", INT2NUM(LOG_NOTICE)); /* normal but significant condition */
47
- rb_define_const(mJournald, "LOG_INFO", INT2NUM(LOG_INFO)); /* informational */
48
- rb_define_const(mJournald, "LOG_DEBUG", INT2NUM(LOG_DEBUG)); /* debug-level messages */
49
- }
50
-
51
- static void jdl_init_methods()
52
- {
53
- rb_define_singleton_method(mNative, "print", jdl_native_print, 2);
54
- rb_define_singleton_method(mNative, "send", jdl_native_send, -1); /* -1 to pass as C array */
55
- rb_define_singleton_method(mNative, "perror", jdl_native_perror, 1);
56
- }
57
-
58
- static VALUE jdl_native_print(VALUE v_self, VALUE v_priority, VALUE v_message)
59
- {
60
- int priority, result;
61
- char *message;
62
-
63
- priority = NUM2INT(v_priority);
64
- message = jdl_alloc_safe_string(v_message);
65
-
66
- result = sd_journal_print(priority, "%s", message);
67
-
68
- free(message);
69
-
70
- return INT2NUM(result);
71
- }
72
-
73
- static VALUE jdl_native_send(int argc, VALUE* argv, VALUE self)
74
- {
75
- struct iovec *msgs;
76
- int i;
77
- int result;
78
-
79
- /* first check everything is a string / convertable to string */
80
- for (i = 0; i < argc; i++) {
81
- StringValue(argv[i]); /* you may get a ruby exception here */
82
- }
83
-
84
- /* allocate memory after all checks to avoid possible memory leak */
85
- msgs = calloc(argc, sizeof(struct iovec));
86
-
87
- for (i = 0; i < argc; i++) {
88
- VALUE v = argv[i];
89
- msgs[i].iov_base = RSTRING_PTR(v);
90
- msgs[i].iov_len = RSTRING_LEN(v);
91
- }
92
-
93
- result = sd_journal_sendv(msgs, argc);
94
-
95
- free(msgs);
96
-
97
- return INT2NUM(result);
98
- }
99
-
100
- static VALUE jdl_native_perror(VALUE v_self, VALUE v_message)
101
- {
102
- int result;
103
- char *message;
104
-
105
- message = jdl_alloc_safe_string(v_message);
106
-
107
- result = sd_journal_perror(message);
108
-
109
- free(message);
110
-
111
- return INT2NUM(result);
112
- }
113
-
114
- /**
115
- * Remove zeros from string and ensure it's zero-terminated
116
- */
117
- static char * jdl_alloc_safe_string(VALUE v_string)
118
- {
119
- char *str;
120
- size_t len;
121
-
122
- char *newstr,
123
- *ptr;
124
- size_t i;
125
-
126
- /* convert to string */
127
- StringValue(v_string);
128
-
129
- str = RSTRING_PTR(v_string);
130
- len = RSTRING_LEN(v_string);
131
-
132
- newstr = calloc(len + 1, sizeof(char));
133
-
134
- for (i = 0, ptr = newstr; i < len; i++) {
135
- if (str[i]) {
136
- *(ptr++) = str[i];
137
- }
138
- }
139
-
140
- *ptr = '\0';
141
-
142
- return newstr;
143
- }
@@ -1,9 +0,0 @@
1
- #include "sd_journal.h"
2
-
3
- #ifdef JOURNALD_NATIVE_SYSTEMD_JOURNAL_DUMMY
4
-
5
- int sd_journal_print(int priority, const char *format, ...) { return 0; }
6
- int sd_journal_sendv(const struct iovec *iov, int n) { return 0; }
7
- int sd_journal_perror(const char *message) { return 0; }
8
-
9
- #endif