Module: Libvirt::Util
- Extended by:
- Loggable::ClassMethods
- Defined in:
- lib/libvirt/util.rb
Constant Summary collapse
- UNIT_TO_BYTES =
{ b: 1, bytes: 1, KB: 1_000, KiB: 1_024, k: 1_024, MB: 1_000_000, M: 1_048_576, MiB: 1_048_576, GB: 1_000_000_000, G: 1_073_741_824, GiB: 1_073_741_824, TB: 1_000_000_000_000, T: 1_099_511_627_776, TiB: 1_099_511_627_776 }.freeze
- UUID_STRING_BUFLEN =
RFC4122
0x80
Class Method Summary collapse
- .define_finalizer(object, &block) ⇒ Object
- .library_path ⇒ Object
- .log(severity, prog = nil, &block) ⇒ Object
- .logger ⇒ Object
- .logger=(value) ⇒ Object
-
.parse_enum(enum, value) ⇒ Array
Event_id, event_id_sym.
-
.parse_flags(flags, enum, default: 0x0) ⇒ Integer
Bitwise OR integer flags calculation for C language.
-
.parse_memory(value, unit) ⇒ Integer
Memory in bytes.
- .parse_version(version_number) ⇒ Object
Methods included from Loggable::ClassMethods
Class Method Details
.define_finalizer(object, &block) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/libvirt/util.rb', line 111 def define_finalizer(object, &block) free = ->(obj_id) do obj_id_hex = "0x#{obj_id.to_s(16)}" ptr = object.to_ptr ptr_hex = "0x#{ptr.address.to_s(16)}" klass = object.class dbg { "Finalize #{klass} object_id=#{obj_id_hex}, pointer=#{ptr_hex}" } return if ptr.null? cl_result = block.call(ptr) err { "Couldn't close #{klass} object_id=#{obj_id_hex}, pointer=#{ptr_hex}" } if cl_result.negative? end ObjectSpace.define_finalizer(object, free) end |
.library_path ⇒ Object
44 45 46 |
# File 'lib/libvirt/util.rb', line 44 def library_path %w[libvirt libvirt.so.0] end |
.log(severity, prog = nil, &block) ⇒ Object
38 39 40 41 42 |
# File 'lib/libvirt/util.rb', line 38 def log(severity, prog = nil, &block) return if logger.nil? logger.public_send(severity, prog, &block) end |
.logger ⇒ Object
30 31 32 |
# File 'lib/libvirt/util.rb', line 30 def logger @logger if defined?(@logger) end |
.logger=(value) ⇒ Object
34 35 36 |
# File 'lib/libvirt/util.rb', line 34 def logger=(value) @logger = value end |
.parse_enum(enum, value) ⇒ Array
Returns event_id, event_id_sym.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/libvirt/util.rb', line 60 def parse_enum(enum, value) if value.is_a?(Symbol) raise ArgumentError, 'invalid enum value' unless enum.symbols.include?(value) return [enum.find(value), value] end raise ArgumentError, 'invalid enum value' unless enum.symbol_map.values.include?(value) [value, enum.symbol_map[value]] end |
.parse_flags(flags, enum, default: 0x0) ⇒ Integer
Bitwise OR integer flags calculation for C language.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/libvirt/util.rb', line 85 def parse_flags(flags, enum, default: 0x0) flags = default if flags.nil? flags = enum[flags] if flags.is_a?(Symbol) return flags if flags.is_a?(Integer) result = 0x0 flags = flags.select { |_, v| v }.keys if flags.is_a?(Hash) raise ArgumentError, 'flags must be an Integer or a Hash or an Array' unless flags.is_a?(Array) flags.each do |key| result |= enum[key.to_s.upcase.to_sym] end result end |
.parse_memory(value, unit) ⇒ Integer
Returns memory in bytes.
105 106 107 108 109 |
# File 'lib/libvirt/util.rb', line 105 def parse_memory(value, unit) unit ||= 'bytes' multiplier = UNIT_TO_BYTES.fetch(unit.to_sym) Integer(value) * multiplier end |
.parse_version(version_number) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/libvirt/util.rb', line 49 def parse_version(version_number) major = version_number / 1_000_000 minor = (version_number - major * 1_000_000) / 1_000 release = version_number - major * 1_000_000 - minor * 1_000 "#{major}.#{minor}.#{release}" end |