journald-native 1.0.1 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +18 -15
- data/ext/journald_native/CMakeLists.txt +9 -0
- data/ext/journald_native/cpp14shiv.h +78 -0
- data/ext/journald_native/extconf.rb +22 -6
- data/ext/journald_native/extinit.cpp +7 -0
- data/ext/journald_native/journald_native.cpp +100 -0
- data/ext/journald_native/journald_native.h +12 -0
- data/ext/journald_native/ruby_exception_wrapper.cpp +16 -0
- data/ext/journald_native/ruby_exception_wrapper.h +105 -0
- data/ext/journald_native/sd_journal.h +45 -0
- data/ext/journald_native/sd_journal_dummy.h +23 -0
- data/lib/journald/native/version.rb +1 -1
- metadata +21 -14
- data/ext/journald_native/journald_native.c +0 -139
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7074ca755c39b71b382f63b7befe21e0dc7e18ec
|
4
|
+
data.tar.gz: 585840efef3b5ee2b50f97e830ecf21df0ec811b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e19cdf3224ca356c47319c5dd6897cc364a8f4d87a1f9c7d9319a31be19be2b4298cbc1b869024e8f0846f48115947dc296afb9d809de368bc16ae18434cdb2a
|
7
|
+
data.tar.gz: 214828671762b128ae1875fffce35e8a3db25217db47e6bca8d4a127750c3ad1ca55d9c8999c623829dce3a8b998ffe70d2fe62395b3dedf75b3479d3487532a
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
# journald-native
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/journald-native.svg)](http://badge.fury.io/rb/journald-native)
|
4
|
+
[![Reference Status](https://www.versioneye.com/ruby/journald-native/reference_badge.svg)](https://www.versioneye.com/ruby/journald-native/references)
|
5
|
+
|
3
6
|
A systemd-journal native logging lib wrapper.
|
4
7
|
[See sd-journal help for more info](http://www.freedesktop.org/software/systemd/man/sd_journal_print.html)
|
5
8
|
|
6
9
|
## Usage
|
7
10
|
|
8
11
|
```ruby
|
9
|
-
|
12
|
+
require 'journald/native'
|
10
13
|
```
|
11
14
|
|
12
15
|
### Constants
|
@@ -16,14 +19,14 @@ Constants are used to denote a log level
|
|
16
19
|
Available constants:
|
17
20
|
|
18
21
|
```ruby
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
Journald::LOG_EMERG # system is unusable
|
23
|
+
Journald::LOG_ALERT # action must be taken immediately
|
24
|
+
Journald::LOG_CRIT # critical conditions
|
25
|
+
Journald::LOG_ERR # error conditions
|
26
|
+
Journald::LOG_WARNING # warning conditions
|
27
|
+
Journald::LOG_NOTICE # normal but significant condition
|
28
|
+
Journald::LOG_INFO # informational
|
29
|
+
Journald::LOG_DEBUG # debug-level messages
|
27
30
|
```
|
28
31
|
|
29
32
|
systemd-journal uses syslog constants to denote level therefore they are equal to those of the Syslog module,
|
@@ -36,14 +39,14 @@ Methods of Journald::Native class wrap systemd-journal calls.
|
|
36
39
|
[See sd-journal help for more info](http://www.freedesktop.org/software/systemd/man/sd_journal_print.html)
|
37
40
|
|
38
41
|
```ruby
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
Journald::Native.send "MESSAGE=message", "PRIORITY=#{Journald::LOG_WARNING}"
|
43
|
+
Journald::Native.print Journald::LOG_WARNING, "message"
|
44
|
+
Journald::Native.perror "message"
|
42
45
|
```
|
43
46
|
|
44
|
-
It is not recommended to use ```print``` and ```perror``` as you may
|
45
|
-
C zero-terminated string format
|
46
|
-
|
47
|
+
It is not recommended to use ```print``` and ```perror``` as you may get exception if your string contains
|
48
|
+
```'\0'``` byte due to C zero-terminated string format. On the contrary ```send``` uses binary buffers and
|
49
|
+
does not have this shortcoming.
|
47
50
|
|
48
51
|
### License
|
49
52
|
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# dummy cmake file for CLion
|
2
|
+
|
3
|
+
cmake_minimum_required(VERSION 2.8.4)
|
4
|
+
project(journald_native)
|
5
|
+
|
6
|
+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
7
|
+
|
8
|
+
set(SOURCE_FILES extinit.cpp journald_native.cpp ruby_exception_wrapper.cpp)
|
9
|
+
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,17 +4,33 @@ 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
|
+
|
13
|
+
def have_funcs
|
14
|
+
have_funcs = true
|
15
|
+
|
16
|
+
# check functions. redefine const list in sd_journal.h if changed
|
17
|
+
%w(sd_journal_print sd_journal_sendv sd_journal_perror).each do |func|
|
18
|
+
have_funcs &&= have_func(func)
|
19
|
+
end
|
20
|
+
|
21
|
+
have_funcs
|
22
|
+
end
|
23
|
+
|
12
24
|
# check headers
|
13
|
-
|
25
|
+
have_header('systemd/sd-journal.h')
|
26
|
+
|
27
|
+
# first try to find funcs in systemd
|
28
|
+
have_library('systemd')
|
14
29
|
|
15
|
-
|
16
|
-
|
17
|
-
|
30
|
+
unless have_funcs
|
31
|
+
have_library('systemd-journal') # try to fall back to systemd-journal if older systemd
|
32
|
+
have_funcs
|
18
33
|
end
|
19
34
|
|
35
|
+
create_header
|
20
36
|
create_makefile('journald_native')
|
@@ -0,0 +1,100 @@
|
|
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
|
+
namespace {
|
11
|
+
|
12
|
+
/* aux */
|
13
|
+
// just a short alias for ruby_raisable_call()
|
14
|
+
template <typename Func, typename... Args>
|
15
|
+
inline auto r(Func f, Args... args) -> decltype(f(args...))
|
16
|
+
{
|
17
|
+
return ruby_exception_wrapper::ruby_raisable_call(f, args...);
|
18
|
+
}
|
19
|
+
|
20
|
+
/* methods */
|
21
|
+
inline VALUE native_print(VALUE v_self, VALUE v_priority, VALUE v_message)
|
22
|
+
{
|
23
|
+
int priority = NUM2INT(v_priority);
|
24
|
+
std::string message = r(rb_string_value_cstr, &v_message);
|
25
|
+
|
26
|
+
int result = sd_journal_print(priority, "%s", message.c_str());
|
27
|
+
|
28
|
+
return INT2NUM(result);
|
29
|
+
}
|
30
|
+
|
31
|
+
inline VALUE native_send(int argc, VALUE* argv, VALUE v_self)
|
32
|
+
{
|
33
|
+
auto msgs = std::make_unique<iovec[]>((size_t)argc);
|
34
|
+
|
35
|
+
for (int i = 0; i < argc; i++) {
|
36
|
+
VALUE v = r(rb_string_value, &argv[i]);
|
37
|
+
|
38
|
+
msgs[i].iov_base = (char *)RSTRING_PTR(v);
|
39
|
+
msgs[i].iov_len = (size_t)RSTRING_LEN(v);
|
40
|
+
}
|
41
|
+
|
42
|
+
int result = sd_journal_sendv(msgs.get(), argc);
|
43
|
+
|
44
|
+
return INT2NUM(result);
|
45
|
+
}
|
46
|
+
|
47
|
+
inline VALUE native_perror(VALUE v_self, VALUE v_message)
|
48
|
+
{
|
49
|
+
std::string message = r(rb_string_value_cstr, &v_message);
|
50
|
+
|
51
|
+
int result = sd_journal_perror(message.c_str());
|
52
|
+
|
53
|
+
return INT2NUM(result);
|
54
|
+
}
|
55
|
+
|
56
|
+
VALUE is_dummy(VALUE v_self = Qnil)
|
57
|
+
{
|
58
|
+
constexpr VALUE dummy = JOURNALD_NATIVE_SD_JOURNAL_DUMMY ? Qtrue : Qfalse;
|
59
|
+
return dummy;
|
60
|
+
}
|
61
|
+
|
62
|
+
/* initializers */
|
63
|
+
inline void init_journald(VALUE module)
|
64
|
+
{
|
65
|
+
rb_define_const(module, "LOG_EMERG", INT2NUM(LOG_EMERG)); /* system is unusable */
|
66
|
+
rb_define_const(module, "LOG_ALERT", INT2NUM(LOG_ALERT)); /* action must be taken immediately */
|
67
|
+
rb_define_const(module, "LOG_CRIT", INT2NUM(LOG_CRIT)); /* critical conditions */
|
68
|
+
rb_define_const(module, "LOG_ERR", INT2NUM(LOG_ERR)); /* error conditions */
|
69
|
+
rb_define_const(module, "LOG_WARNING", INT2NUM(LOG_WARNING)); /* warning conditions */
|
70
|
+
rb_define_const(module, "LOG_NOTICE", INT2NUM(LOG_NOTICE)); /* normal but significant condition */
|
71
|
+
rb_define_const(module, "LOG_INFO", INT2NUM(LOG_INFO)); /* informational */
|
72
|
+
rb_define_const(module, "LOG_DEBUG", INT2NUM(LOG_DEBUG)); /* debug-level messages */
|
73
|
+
}
|
74
|
+
|
75
|
+
inline void init_native(VALUE module)
|
76
|
+
{
|
77
|
+
// methods
|
78
|
+
rb_define_singleton_method(module, "print", RXW_WRAPPED_METHOD_FUNC(native_print, VALUE, VALUE, VALUE), 2);
|
79
|
+
rb_define_singleton_method(module, "send", RXW_WRAPPED_METHOD_FUNC(native_send, int, VALUE*, VALUE), -1); /* -1 to pass as C array */
|
80
|
+
rb_define_singleton_method(module, "perror", RXW_WRAPPED_METHOD_FUNC(native_perror, VALUE, VALUE), 1);
|
81
|
+
|
82
|
+
// dummy detection
|
83
|
+
rb_define_const(module, "IS_DUMMY", is_dummy());
|
84
|
+
rb_define_singleton_method(module, "dummy?", RUBY_METHOD_FUNC(is_dummy), 0);
|
85
|
+
}
|
86
|
+
|
87
|
+
} // private namespace
|
88
|
+
|
89
|
+
void init_modules()
|
90
|
+
{
|
91
|
+
// no nontrivial destructors during initialization, no need for ruby catch
|
92
|
+
|
93
|
+
VALUE mJournald = rb_define_module("Journald");
|
94
|
+
VALUE mNative = rb_define_module_under(mJournald, "Native");
|
95
|
+
|
96
|
+
init_journald(mJournald); // add constants to Journald
|
97
|
+
init_native(mNative); // add methods to Journald::Native
|
98
|
+
}
|
99
|
+
|
100
|
+
}
|
@@ -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,105 @@
|
|
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
|
+
// wrap functions, prototype required, i.e. RXW_WRAPPED_METHOD_FUNC(native_send, int, VALUE*, VALUE)
|
13
|
+
// functions can be inline
|
14
|
+
#define RXW_WRAPPED_METHOD_FUNC(method, ...) ((VALUE (*)(ANYARGS))::ruby_exception_wrapper::method_wrapper_call<decltype(&method), method, __VA_ARGS__>)
|
15
|
+
|
16
|
+
namespace ruby_exception_wrapper {
|
17
|
+
|
18
|
+
class RbWrappedException: public std::exception {
|
19
|
+
VALUE ruby_exception;
|
20
|
+
public:
|
21
|
+
RbWrappedException(VALUE e): ruby_exception(e) {};
|
22
|
+
VALUE getRubyException() { return ruby_exception; }
|
23
|
+
};
|
24
|
+
|
25
|
+
// callback for rb_rescue2 to catch ruby exception and wrap it by RbWrappedException
|
26
|
+
VALUE rethrow_as_cpp(VALUE put_exception_here_ptr, VALUE exception);
|
27
|
+
|
28
|
+
namespace {
|
29
|
+
// do real call of function from template
|
30
|
+
template <typename FuncPointer, typename... Args>
|
31
|
+
inline VALUE call_wrapper_tuple_impl(FuncPointer& fp, Args... args)
|
32
|
+
{
|
33
|
+
// only pointers and VALUE permitted
|
34
|
+
// VALUE guaranteed to be able to contain pointer
|
35
|
+
return reinterpret_cast<VALUE>(fp(args...));
|
36
|
+
}
|
37
|
+
|
38
|
+
// unpack params to do real call
|
39
|
+
template <typename CallTuple, std::size_t... Is>
|
40
|
+
inline VALUE call_wrapper_tuple_unpack(CallTuple& call_tuple, std::index_sequence<Is...>)
|
41
|
+
{
|
42
|
+
return call_wrapper_tuple_impl(std::get<Is>(call_tuple)...);
|
43
|
+
}
|
44
|
+
|
45
|
+
// callback for rb_rescue2 for no exceptions
|
46
|
+
template <typename CallTuple>
|
47
|
+
VALUE call_wrapper_tuple(VALUE v_ct) {
|
48
|
+
CallTuple call_tuple = std::move(*reinterpret_cast<CallTuple*>(v_ct));
|
49
|
+
|
50
|
+
return call_wrapper_tuple_unpack(call_tuple, std::make_index_sequence<std::tuple_size<CallTuple>::value> {});
|
51
|
+
};
|
52
|
+
|
53
|
+
// safely call function
|
54
|
+
template <typename Func, typename... Args>
|
55
|
+
inline auto do_raisable_call(Func f, Args... args) -> decltype(f(args...))
|
56
|
+
{
|
57
|
+
typedef std::function<typename std::remove_pointer<Func>::type> FuncPointer;
|
58
|
+
typedef std::tuple<FuncPointer, Args...> CallTuple;
|
59
|
+
|
60
|
+
FuncPointer fp = f;
|
61
|
+
|
62
|
+
CallTuple call_tuple = std::make_tuple(fp, args...);
|
63
|
+
|
64
|
+
VALUE exception = 0; // get raised exception if any
|
65
|
+
|
66
|
+
VALUE result = rb_rescue2(
|
67
|
+
RUBY_METHOD_FUNC(call_wrapper_tuple<CallTuple>), reinterpret_cast<VALUE>(&call_tuple),
|
68
|
+
RUBY_METHOD_FUNC(rethrow_as_cpp), reinterpret_cast<VALUE>(&exception),
|
69
|
+
rb_eException, Qfalse
|
70
|
+
);
|
71
|
+
|
72
|
+
if (exception) { // nonzero here if rescue called unless some pervert throws Qfalse
|
73
|
+
throw RbWrappedException(exception);
|
74
|
+
}
|
75
|
+
|
76
|
+
return reinterpret_cast<decltype(f(args...))>(result);
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
template <typename Func, typename... Args>
|
81
|
+
inline auto ruby_raisable_call(Func f, Args... args) -> decltype(f(args...))
|
82
|
+
{
|
83
|
+
static_assert(
|
84
|
+
std::is_same<decltype(f(args...)), VALUE>::value || std::is_pointer<decltype(f(args...))>::value,
|
85
|
+
"Only for funcs returning VALUE or pointer"
|
86
|
+
);
|
87
|
+
|
88
|
+
auto result = do_raisable_call(f, args...);
|
89
|
+
|
90
|
+
return result;
|
91
|
+
}
|
92
|
+
|
93
|
+
template <typename Func, Func func, typename... Args>
|
94
|
+
VALUE method_wrapper_call(Args... args)
|
95
|
+
{
|
96
|
+
try {
|
97
|
+
return func(args...);
|
98
|
+
} catch(RbWrappedException &e) {
|
99
|
+
rb_exc_raise(e.getRubyException());
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
#endif
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#ifndef JOURNALD_NATIVE_SD_JOURNAL_H
|
2
|
+
#define JOURNALD_NATIVE_SD_JOURNAL_H
|
3
|
+
|
4
|
+
#ifdef __linux__
|
5
|
+
|
6
|
+
#define JOURNALD_NATIVE_SD_JOURNAL_DUMMY false
|
7
|
+
|
8
|
+
/* do the real stuff */
|
9
|
+
|
10
|
+
#include "extconf.h"
|
11
|
+
|
12
|
+
/* check for extconf results */
|
13
|
+
|
14
|
+
#ifndef HAVE_SYSTEMD_SD_JOURNAL_H
|
15
|
+
#error Cannot include <systemd/sd-journal.h>. Please use linux version with systemd-journal installed
|
16
|
+
#endif
|
17
|
+
|
18
|
+
#ifndef HAVE_SD_JOURNAL_PRINT
|
19
|
+
#error Required function sd_journal_print is missing
|
20
|
+
#endif
|
21
|
+
|
22
|
+
#ifndef HAVE_SD_JOURNAL_SENDV
|
23
|
+
#error Required function sd_journal_sendv is missing
|
24
|
+
#endif
|
25
|
+
|
26
|
+
#ifndef HAVE_SD_JOURNAL_PERROR
|
27
|
+
#error Required function sd_journal_perror is missing
|
28
|
+
#endif
|
29
|
+
|
30
|
+
/* Do not add C line and file to the log messages */
|
31
|
+
#define SD_JOURNAL_SUPPRESS_LOCATION
|
32
|
+
/* include systemd-journal headers */
|
33
|
+
#include <systemd/sd-journal.h>
|
34
|
+
|
35
|
+
#else
|
36
|
+
|
37
|
+
#define JOURNALD_NATIVE_SD_JOURNAL_DUMMY true
|
38
|
+
|
39
|
+
#warning Compiling dummy version of the gem for non-Linux OS
|
40
|
+
|
41
|
+
#include "sd_journal_dummy.h"
|
42
|
+
|
43
|
+
#endif
|
44
|
+
|
45
|
+
#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; }
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: journald-native
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Smirnov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake-compiler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description:
|
@@ -60,13 +60,21 @@ extensions:
|
|
60
60
|
- ext/journald_native/extconf.rb
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
-
- .gitignore
|
63
|
+
- ".gitignore"
|
64
64
|
- Gemfile
|
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/
|
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
|
76
|
+
- ext/journald_native/sd_journal.h
|
77
|
+
- ext/journald_native/sd_journal_dummy.h
|
70
78
|
- journald-native.gemspec
|
71
79
|
- lib/journald/native.rb
|
72
80
|
- lib/journald/native/version.rb
|
@@ -80,19 +88,18 @@ require_paths:
|
|
80
88
|
- lib
|
81
89
|
required_ruby_version: !ruby/object:Gem::Requirement
|
82
90
|
requirements:
|
83
|
-
- -
|
91
|
+
- - ">="
|
84
92
|
- !ruby/object:Gem::Version
|
85
93
|
version: 1.9.2
|
86
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
95
|
requirements:
|
88
|
-
- -
|
96
|
+
- - ">="
|
89
97
|
- !ruby/object:Gem::Version
|
90
98
|
version: '0'
|
91
99
|
requirements: []
|
92
100
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.
|
101
|
+
rubygems_version: 2.4.3
|
94
102
|
signing_key:
|
95
103
|
specification_version: 4
|
96
104
|
summary: systemd-journal logging native lib wrapper
|
97
105
|
test_files: []
|
98
|
-
has_rdoc:
|
@@ -1,139 +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 <systemd/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
|
-
//const char * fmt = "%s";
|
76
|
-
struct iovec *msgs;
|
77
|
-
size_t i;
|
78
|
-
int result;
|
79
|
-
|
80
|
-
msgs = calloc(argc, sizeof(struct iovec));
|
81
|
-
|
82
|
-
for (i = 0; i < argc; i++) {
|
83
|
-
VALUE v = argv[i];
|
84
|
-
StringValue(v);
|
85
|
-
msgs[i].iov_base = RSTRING_PTR(v);
|
86
|
-
msgs[i].iov_len = RSTRING_LEN(v);
|
87
|
-
}
|
88
|
-
|
89
|
-
result = sd_journal_sendv(msgs, argc);
|
90
|
-
|
91
|
-
free(msgs);
|
92
|
-
|
93
|
-
return INT2NUM(result);
|
94
|
-
}
|
95
|
-
|
96
|
-
static VALUE jdl_native_perror(VALUE v_self, VALUE v_message)
|
97
|
-
{
|
98
|
-
int result;
|
99
|
-
char *message;
|
100
|
-
|
101
|
-
message = jdl_alloc_safe_string(v_message);
|
102
|
-
|
103
|
-
result = sd_journal_perror(message);
|
104
|
-
|
105
|
-
free(message);
|
106
|
-
|
107
|
-
return INT2NUM(result);
|
108
|
-
}
|
109
|
-
|
110
|
-
/**
|
111
|
-
* Remove zeros from string and ensure it's zero-terminated
|
112
|
-
*/
|
113
|
-
static char * jdl_alloc_safe_string(VALUE v_string)
|
114
|
-
{
|
115
|
-
char *str;
|
116
|
-
size_t len;
|
117
|
-
|
118
|
-
char *newstr,
|
119
|
-
*ptr;
|
120
|
-
size_t i;
|
121
|
-
|
122
|
-
/* convert tos sring */
|
123
|
-
StringValue(v_string);
|
124
|
-
|
125
|
-
str = RSTRING_PTR(v_string);
|
126
|
-
len = RSTRING_LEN(v_string);
|
127
|
-
|
128
|
-
newstr = calloc(len + 1, sizeof(char));
|
129
|
-
|
130
|
-
for (i = 0, ptr = newstr; i < len; i++) {
|
131
|
-
if (str[i]) {
|
132
|
-
*(ptr++) = str[i];
|
133
|
-
}
|
134
|
-
}
|
135
|
-
|
136
|
-
*ptr = '\0';
|
137
|
-
|
138
|
-
return newstr;
|
139
|
-
}
|