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.

Parameters:

  • identifier (String)

    this value will be passed as SYSLOG_IDENTIFIER to the journal.

  • priority (Integer)

    the log level for events writen to this stream.

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :prefix (Boolean)

    true to enable kernel-style log priority prefixes

Returns:

  • (IO)

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.

Parameters:

  • contents (Hash)

    the set of key-value pairs defining the event.

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.

Parameters:

  • message (String)

    the text to prefix the error message with.

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.

Parameters:

  • level (Integer)

    one of the LOG_* constants defining the severity of the event.

  • message (String)

    the content of the message to write.

Raises:



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

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