Module: Systemd::Journal::Writable::ClassMethods

Defined in:
lib/systemd/journal/writable.rb

Overview

methods in this module will be available as class methods on

{Systemd::Journal}

Instance Method Summary collapse

Instance Method Details

#log_stream(identifier, priority, opts = {}) ⇒ IO

Creates a new IO stream which writes newline-seperated messages to the journal.

Options Hash (opts):

  • :prefix (Boolean)

    true to enable kernel-style log priority prefixes

Raises:


45
46
47
48
49
50
51
52
53
54
# File 'lib/systemd/journal/writable.rb', line 45

def log_stream(identifier, priority, opts = {})
  fd = Native.sd_journal_stream_fd(
    identifier,
    priority,
    !opts[:prefix].nil?
  )
  raise JournalError, fd if fd < 0

  IO.new(fd, File::WRONLY, encoding: Encoding::UTF_8)
end

#message(contents) ⇒ Object

write an event to the systemd journal.

Raises:


75
76
77
78
79
80
81
82
83
84
# File 'lib/systemd/journal/writable.rb', line 75

def message(contents)
  items = contents.flat_map do |k, v|
    value = v.to_s.gsub('%', '%%')
    [:string, "#{k.to_s.upcase}=#{value}"]
  end
  # add a null pointer to terminate the varargs
  items += [:string, nil]
  rc = Native.sd_journal_send(*items)
  raise JournalError, rc if rc < 0
end

#perror(message) ⇒ Object

write the value of the c errno constant to the systemd journal in the style of the perror() function.

Raises:


59
60
61
62
# File 'lib/systemd/journal/writable.rb', line 59

def perror(message)
  rc = Native.sd_journal_perror(message)
  raise JournalError, rc if rc < 0
end

write a simple message to the systemd journal.

Raises:


68
69
70
71
# File 'lib/systemd/journal/writable.rb', line 68

def print(level, message)
  rc = Native.sd_journal_print(level, message)
  raise JournalError, rc if rc < 0
end