Module: Systemd::Journal::Navigable

Included in:
Systemd::Journal
Defined in:
lib/systemd/journal/navigable.rb

Instance Method Summary collapse

Instance Method Details

#cursorString

returns a string representing the current read position. This string can be passed to #seek or #cursor?.

Returns:

  • (String)

    a cursor token.



7
8
9
10
11
12
13
14
# File 'lib/systemd/journal/navigable.rb', line 7

def cursor
  out_ptr = FFI::MemoryPointer.new(:pointer, 1)
  if (rc = Native.sd_journal_get_cursor(@ptr, out_ptr)) < 0
    raise JournalError, rc
  end

  Journal.read_and_free_outstr(out_ptr.read_pointer)
end

#cursor?(c) ⇒ Boolean

Check if the read position is currently at the entry represented by the provided cursor value. provided cursor, False otherwise.

Parameters:

  • c (String)

    a cursor token returned from #cursor.

Returns:

  • (Boolean)

    True if current entry is the one represented by the



21
22
23
24
25
26
27
# File 'lib/systemd/journal/navigable.rb', line 21

def cursor?(c)
  if (rc = Native.sd_journal_test_cursor(@ptr, c)) < 0
    raise JournalError, rc
  end

  rc > 0
end

#move(offset = 1) ⇒ Integer

Move the read pointer by ‘offset` entries.

Parameters:

  • offset (Integer) (defaults to: 1)

    how many entries to move the read pointer by. If this value is positive, the read pointer moves forward. Otherwise, it moves backwards. Defaults to moving forward one entry.

Returns:

  • (Integer)

    number of entries the read pointer actually moved.



34
35
36
# File 'lib/systemd/journal/navigable.rb', line 34

def move(offset = 1)
  offset > 0 ? move_next_skip(offset) : move_previous_skip(-offset)
end

#move_nextBoolean

Move the read pointer to the next entry in the journal.

Returns:

  • (Boolean)

    True if moving to the next entry was successful.

  • (Boolean)

    False if unable to move to the next entry, indicating that the pointer has reached the end of the journal.

Raises:



42
43
44
45
46
# File 'lib/systemd/journal/navigable.rb', line 42

def move_next
  rc = Native.sd_journal_next(@ptr)
  raise JournalError, rc if rc < 0
  rc > 0
end

#move_next_skip(amount) ⇒ Integer

Move the read pointer forward by ‘amount` entries.

Returns:

  • (Integer)

    actual number of entries by which the read pointer moved. If this number is less than the requested amount, the read pointer has reached the end of the journal.

Raises:



52
53
54
55
56
# File 'lib/systemd/journal/navigable.rb', line 52

def move_next_skip(amount)
  rc = Native.sd_journal_next_skip(@ptr, amount)
  raise JournalError, rc if rc < 0
  rc
end

#move_previousBoolean

Move the read pointer to the previous entry in the journal.

Returns:

  • (Boolean)

    True if moving to the previous entry was successful.

  • (Boolean)

    False if unable to move to the previous entry, indicating that the pointer has reached the beginning of the journal.

Raises:



62
63
64
65
66
# File 'lib/systemd/journal/navigable.rb', line 62

def move_previous
  rc = Native.sd_journal_previous(@ptr)
  raise JournalError, rc if rc < 0
  rc > 0
end

#move_previous_skip(amount) ⇒ Integer

Move the read pointer backwards by ‘amount` entries.

Returns:

  • (Integer)

    actual number of entries by which the read pointer was moved. If this number is less than the requested amount, the read pointer has reached the beginning of the journal.

Raises:



72
73
74
75
76
# File 'lib/systemd/journal/navigable.rb', line 72

def move_previous_skip(amount)
  rc = Native.sd_journal_previous_skip(@ptr, amount)
  raise JournalError, rc if rc < 0
  rc
end

#seek(where) ⇒ True

Seek to a position in the journal. Note: after seeking, you must call #move_next or #move_previous

before you can call {#read_field} or {#current_entry}.
When calling `seek(:tail)` the read pointer is positioned _after_
the last entry in the journal -- thus you should use `move_previous`.
Otherwise, use `move_next`.

Examples:

Read last journal entry

j = Systemd::Joural.new
j.seek(:tail)
j.move_previous
puts j.current_entry

Parameters:

  • whence (Symbol, Time)

    one of :head, :tail, or a Time instance. ‘:head` (or `:start`) will seek to the beginning of the journal. `:tail` (or `:end`) will seek to the end of the journal. When a `Time` is provided, seek to the journal entry logged closest to that time. When a String is provided, assume it is a cursor from #cursor and seek to that entry.

Returns:

  • (True)

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/systemd/journal/navigable.rb', line 97

def seek(where)
  rc = case
       when [:head, :start].include?(where)
         Native.sd_journal_seek_head(@ptr)
       when [:tail, :end].include?(where)
         Native.sd_journal_seek_tail(@ptr)
       when where.is_a?(Time)
         Native.sd_journal_seek_realtime_usec(
           @ptr,
           where.to_i * 1_000_000
         )
       when where.is_a?(String)
         Native.sd_journal_seek_cursor(@ptr, where)
       else
         raise ArgumentError, "Unknown seek type: #{where.class}"
       end

  raise JournalError, rc if rc < 0

  true
end