Skip to content

Commit

Permalink
Merge pull request #43 from ledbettj/develop
Browse files Browse the repository at this point in the history
release support for opening containers / files.
  • Loading branch information
John Ledbetter committed Apr 9, 2014
2 parents dad3f4d + d996b8c commit 8329796
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: ruby
rvm:
- 2.1.0
- 2.0.0
- 1.9.3
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@ thing.

Obviously you will need to have
[systemd](http://www.freedesktop.org/wiki/Software/systemd/) installed on your
system in order to use the gem.

__systemd 208__:

* `libsystemd-journal`
* `libsystemd-id128`

__systemd 209+__:

* `libsystemd`
system in order to use the gem. Currently we support systemd 208 or higher.

## Usage

Expand Down Expand Up @@ -114,5 +105,3 @@ If you run into problems or have questions, please open an
4. Push to the branch
5. Create new Pull Request, targeting the __develop__ branch.
6. Wipe hands on pants, you're done!

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/ledbettj/systemd-journal/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
39 changes: 34 additions & 5 deletions lib/systemd/journal.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'systemd/journal/version'
require 'systemd/journal/native'
require 'systemd/journal/flags'
require 'systemd/journal/writable'
Expand Down Expand Up @@ -29,22 +30,33 @@ class Journal
# @option opts [Integer] :flags a set of bitwise OR-ed
# {Systemd::Journal::Flags} which control what journal files are opened.
# Defaults to `0`, meaning all journals avaiable to the current user.
# @option opts [String] :path if provided, open the journal files living
# in the provided directory only. Any provided flags will be ignored per
# @option opts [String] :path if provided, open the journal files living
# in the provided directory only. Any provided flags will be ignored
# since sd_journal_open_directory does not currently accept any flags.
# @option opts [Array] :files if provided, open the provided journal files
# only. Any provided flags will be ignored since sd_journal_open_files
# does not currently accept any flags.
# @option opts [String] :container if provided, open the journal files from
# the container with the provided machine name only.
# @example Read only system journal entries
# j = Systemd::Journal.new(flags: Systemd::Journal::Flags::SYSTEM_ONLY)
# @example Directly open a journal directory
# j = Systemd::Journal.new(
# path: '/var/log/journal/5f5777e46c5f4131bd9b71cbed6b9abf'
# )
def initialize(opts = {})
validate_options!(opts)

flags = opts[:flags] || 0
path = opts[:path]
ptr = FFI::MemoryPointer.new(:pointer, 1)

rc = if path
Native.sd_journal_open_directory(ptr, path, 0)
rc = case
when opts[:path]
Native.sd_journal_open_directory(ptr, opts[:path], 0)
when opts[:files]
Native.sd_journal_open_files(ptr, array_to_ptrs(opts[:files]), 0)
when opts[:container]
Native.sd_journal_open_container(ptr, opts[:container], flags)
else
Native.sd_journal_open(ptr, flags)
end
Expand Down Expand Up @@ -194,6 +206,23 @@ def data_threshold=(threshold)

private

def array_to_ptrs(strings)
ptr = FFI::MemoryPointer.new(:pointer, strings.length + 1)
strings.each_with_index do |s, i|
ptr[i].put_pointer(0, FFI::MemoryPointer.from_string(s))
end
ptr[strings.length].put_pointer(0, nil)
ptr
end

def validate_options!(opts)
exclusive = [:path, :files, :container]
provided = (opts.keys & exclusive)
if provided.length > 1
raise ArgumentError.new("#{provided} are conflicting options")
end
end

def self.finalize(ptr)
proc { Native.sd_journal_close(ptr) unless ptr.nil? }
end
Expand Down
3 changes: 3 additions & 0 deletions lib/systemd/journal/native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module Native
attach_function :sd_journal_open_directory, [:pointer, :string, :int], :int
attach_function :sd_journal_close, [:pointer], :void

attach_function :sd_journal_open_files, [:pointer, :pointer, :int], :int
attach_function :sd_journal_open_container, [:pointer, :string, :int], :int

# navigation
attach_function :sd_journal_next, [:pointer], :int
attach_function :sd_journal_next_skip, [:pointer, :uint64], :int
Expand Down
8 changes: 5 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
0
end

Systemd::Journal::Native.stub(:sd_journal_open, &dummy_open)
Systemd::Journal::Native.stub(:sd_journal_open_directory, &dummy_open)
['', '_directory', '_files', '_container'].each do |suffix|
Systemd::Journal::Native.stub(:"sd_journal_open#{suffix}", &dummy_open)
end
Systemd::Journal::Native.stub(:sd_journal_close).and_return(0)

# Raise an exception if any native calls are actually called
Expand All @@ -25,7 +26,8 @@
end

native_calls -= [
:sd_journal_open, :sd_journal_open_directory, :sd_journal_close
:sd_journal_open, :sd_journal_open_directory, :sd_journal_close,
:sd_journal_open_files, :sd_journal_open_container
]

build_err_proc = ->(method_name) do
Expand Down
19 changes: 19 additions & 0 deletions spec/systemd/journal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,29 @@
Systemd::Journal.new(flags: 1234)
end

it 'accepts a files argument to open specific files' do
Systemd::Journal::Native.should_receive(:sd_journal_open_files)
Systemd::Journal.new(files: ['/path/to/journal/1', '/path/to/journal/2'])
end

it 'accepts a machine name to open a container' do
Systemd::Journal::Native.should_receive(:sd_journal_open_container)
Systemd::Journal.new(container: 'bobs-machine')
end

it 'raises a Journal Error if a native call fails' do
Systemd::Journal::Native.should_receive(:sd_journal_open).and_return(-1)
expect { Systemd::Journal.new }.to raise_error(Systemd::JournalError)
end

it 'raises an argument error if conflicting options are passed' do
expect do
Systemd::Journal.new(path: 'p', files: %w{a b})
end.to raise_error(ArgumentError)
expect do
Systemd::Journal.new(container: 'c', files: %w{a b})
end.to raise_error(ArgumentError)
end
end

describe '#move' do
Expand Down

0 comments on commit 8329796

Please sign in to comment.