Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/systemd/id128.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def self.random
module Native
require "ffi"
extend FFI::Library

ffi_lib %w[ libsystemd.so.0 libsystemd.so
libsystemd-id128.so.0 libsystemd-id128.so ]

Expand Down
2 changes: 1 addition & 1 deletion lib/systemd/journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def open_journal(type, ptr, opts, flags)
Native.sd_journal_open_directory(ptr, opts[:path], 0)
when :files, :file
files = Array(opts[type])
@open_target = "file#{files.one? ? "" : "s"}:#{files.join(",")}"
@open_target = "file#{"s" unless files.one?}:#{files.join(",")}"
Native.sd_journal_open_files(ptr, array_to_ptrs(files), 0)
when :container
@open_target = "container:#{opts[:container]}"
Expand Down
3 changes: 2 additions & 1 deletion lib/systemd/journal/native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Native
# rubocop:disable Layout/LineLength
require "ffi"
extend FFI::Library

ffi_lib %w[ libsystemd.so.0 libsystemd.so
libsystemd-journal.so.0 libsystemd-journal.so]

Expand Down Expand Up @@ -78,7 +79,7 @@ def self.open_container?

# writing
attach_function :sd_journal_print, [:int, :string], :int
attach_function :sd_journal_send, [:varargs], :int
attach_function :sd_journal_send, [:string, :varargs], :int
attach_function :sd_journal_perror, [:string], :int
attach_function :sd_journal_stream_fd, [:string, :int, :bool], :int

Expand Down
3 changes: 1 addition & 2 deletions lib/systemd/journal/writable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def message(contents)
[:string, "#{k.to_s.upcase}=#{value}"]
end
# add a null pointer to terminate the varargs
items += [:string, nil]
rc = Native.sd_journal_send(*items)
rc = Native.sd_journal_send(*items[1..], :pointer, nil)
raise JournalError, rc if rc < 0
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/systemd/journal_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(code)
# @private
module LIBC
extend FFI::Library

ffi_lib FFI::Library::LIBC

attach_function :strerror, [:int], :string
Expand Down
39 changes: 37 additions & 2 deletions spec/systemd/journal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,32 @@
describe "message" do
it "escapes percent signs in messages" do
expect(Systemd::Journal::Native).to receive(:sd_journal_send)
.with(:string, "MESSAGE=hello %% world %%", :string, nil)
.with("MESSAGE=hello %% world %%", :string, "CUSTOM_FIELD=test", :pointer, nil)
.and_return(0)

Systemd::Journal.message(message: "hello % world %")
Systemd::Journal.message(message: "hello % world %", custom_field: "test")
end

it "sends message to journal" do
Systemd::Journal.message(message: "test message")
end

it "sends message with priority and custom field to journal" do
Systemd::Journal.message(
message: "test message",
priority: Systemd::Journal::LOG_ERR,
custom_field: "hello world"
)

j = Systemd::Journal.new
j.seek(:tail)
j.move_previous

expect(j.current_entry).to have_attributes(
message: "test message",
custom_field: "hello world",
priority: Systemd::Journal::LOG_ERR.to_s
)
end
end

Expand All @@ -495,5 +517,18 @@

Systemd::Journal.print(Systemd::Journal::LOG_DEBUG, "hello % world %")
end

it "prints to journal" do
Systemd::Journal.print(Systemd::Journal::LOG_DEBUG, "hello % world !")

j = Systemd::Journal.new
j.seek(:tail)
j.move_previous

expect(j.current_entry).to have_attributes(
message: "hello % world !",
priority: Systemd::Journal::LOG_DEBUG.to_s
)
end
end
end