Class: Systemd::JournalEntry
- Inherits:
-
Object
- Object
- Systemd::JournalEntry
- Includes:
- Enumerable
- Defined in:
- lib/systemd/journal_entry.rb
Overview
Represents a single entry in the Journal.
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Instance Method Summary collapse
-
#[](key) ⇒ String
Get the value of a given field in the entry, or nil if it doesn’t exist.
-
#catalog(opts = {}) ⇒ String
Returns the catalog message that this Journal Entry references, if any.
-
#catalog? ⇒ Boolean
Returns true if this Journal Entry has an associated catalog message.
-
#each ⇒ Enumerator
Yields each field name and value pair to the provided block.
-
#initialize(entry, context = {}) ⇒ JournalEntry
constructor
Create a new JournalEntry from the given entry hash.
- #inspect ⇒ Object
- #method_missing(m, *args) ⇒ Object
-
#monotonic_timestamp ⇒ Time
Returns the monotonic time (time since boot) that this entry was received by the journal.
-
#realtime_timestamp ⇒ Time
Returns the wall-clock time that this entry was received by the journal.
- #respond_to_missing?(m, include_private = false) ⇒ Boolean
-
#to_h ⇒ Hash
Convert this Entry into a hash.
Constructor Details
#initialize(entry, context = {}) ⇒ JournalEntry
Create a new JournalEntry from the given entry hash. You probably don’t need to construct this yourself; instead instances are returned from Systemd::Journal methods such as Systemd::Journal#current_entry.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/systemd/journal_entry.rb', line 13 def initialize(entry, context = {}) inspect = [] @entry = entry @ctx = context @fields = entry.map do |key, value| name = key.downcase.to_sym define_singleton_method(name) { value } unless respond_to?(name) inspect.push("#{name}: '#{value}'") name end @inspect = inspect.join(", ") end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
43 44 45 46 47 |
# File 'lib/systemd/journal_entry.rb', line 43 def method_missing(m, *args) # not all journal entries will have all fields. don't raise an error # unless the user passed arguments. super unless args.empty? end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
6 7 8 |
# File 'lib/systemd/journal_entry.rb', line 6 def fields @fields end |
Instance Method Details
#[](key) ⇒ String
Get the value of a given field in the entry, or nil if it doesn’t exist
58 59 60 |
# File 'lib/systemd/journal_entry.rb', line 58 def [](key) @entry[key] || @entry[key.to_s.upcase] end |
#catalog(opts = {}) ⇒ String
Returns the catalog message that this Journal Entry references, if any.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/systemd/journal_entry.rb', line 76 def catalog(opts = {}) return nil unless catalog? opts[:replace] = true unless opts.key?(:replace) cat = Systemd::Journal.catalog_for(self[:message_id]) # catalog_for does not do field substitution for us, so we do it here # if requested opts[:replace] ? field_substitute(cat) : cat end |
#catalog? ⇒ Boolean
Returns true if this Journal Entry has an associated catalog message.
90 91 92 |
# File 'lib/systemd/journal_entry.rb', line 90 def catalog? !self[:message_id].nil? end |
#each ⇒ Enumerator
Yields each field name and value pair to the provided block. If no block is given, returns an enumerator.
65 66 67 68 |
# File 'lib/systemd/journal_entry.rb', line 65 def each return to_enum(:each) unless block_given? @entry.each { |key, value| yield [key, value] } end |
#inspect ⇒ Object
101 102 103 |
# File 'lib/systemd/journal_entry.rb', line 101 def inspect format("#<%s:0x%016x %s>", self.class.name, object_id, @inspect) end |
#monotonic_timestamp ⇒ Time
Returns the monotonic time (time since boot) that this entry was received by the journal. This should be associated with a boot_id.
37 38 39 40 |
# File 'lib/systemd/journal_entry.rb', line 37 def return nil unless @ctx.key?(:monotonic_ts) @monotonic_timestamp ||= Time.at(0, @ctx[:monotonic_ts].first) end |
#realtime_timestamp ⇒ Time
Returns the wall-clock time that this entry was received by the journal.
29 30 31 32 |
# File 'lib/systemd/journal_entry.rb', line 29 def return nil unless @ctx.key?(:realtime_ts) @realtime_timestamp ||= Time.at(0, @ctx[:realtime_ts]) end |
#respond_to_missing?(m, include_private = false) ⇒ Boolean
50 51 52 |
# File 'lib/systemd/journal_entry.rb', line 50 def respond_to_missing?(m, include_private = false) fields&.include?(m) || super end |
#to_h ⇒ Hash
Convert this Entry into a hash.
96 97 98 |
# File 'lib/systemd/journal_entry.rb', line 96 def to_h @entry.each_with_object({}) { |(k, v), h| h[k] = v.dup } end |