Class: Locd::Newsyslog::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/locd/newsyslog.rb

Overview

Lil' class that holds values for a newsyslog conf file entry.

Constant Summary collapse

WHEN_NEVER =

Value for the when field when we want to never rotate based on time.

Returns:

  • (String)
'*'
NO_FLAGS =

Conf file value for flags where there are none

Returns:

  • (String)
'-'
FIELD_SEP =
"  "

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_path:, pid_path:, owner: nil, group: nil, mode: '644', count: 7, size: 100, when_: '*', flags: [], signal: 'HUP') ⇒ Entry

Instantiate a new Entry.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/locd/newsyslog.rb', line 128

def initialize  log_path:,
                pid_path:,
                owner: nil,
                group: nil,
                mode: '644',
                count: 7,
                size: 100, # 100 KB max size
                when_: '*', # '$D0',  # rotate every day at midnight
                flags: [],
                signal: 'HUP'
  @log_path = log_path
  @pid_path = pid_path
  @owner = owner
  @group = group
  @mode = self.class.mode_string_for mode
  @count = t.pos_int.check count
  @size = t.pos_int.check size
  @when_ = when_
  @flags = flags
  @sig_num = self.class.sig_num_for signal
  
  render
end

Instance Attribute Details

#countObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def count
  @count
end

#flagsObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def flags
  @flags
end

#groupObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def group
  @group
end

#log_pathObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def log_path
  @log_path
end

#modeObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def mode
  @mode
end

#ownerObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def owner
  @owner
end

#pid_pathObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def pid_path
  @pid_path
end

#sig_numObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def sig_num
  @sig_num
end

#sizeObject (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def size
  @size
end

#when_Object (readonly)

Attributes



113
114
115
# File 'lib/locd/newsyslog.rb', line 113

def when_
  @when_
end

Class Method Details

.mode_string_for(mode) ⇒ return_type

TODO:

Document normalize_mode method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/locd/newsyslog.rb', line 80

def self.mode_string_for mode
  # t.match mode,
  #   0000..0777, mode.to_s( 8 ),
  #   /\A[0-7]{3}\z/, mode
  
  case mode
  when 0000..0777
    mode.to_s 8
  when /\A[0-7]{3}\z/
    mode
  else
    raise ArgumentError,
      "Bad mode: #{ mode.inspect }, need String or Fixnum: '644'; 0644"
  end
end

.sig_num_for(signal) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/locd/newsyslog.rb', line 97

def self.sig_num_for signal
  case signal
  when String
    Signal.list.fetch signal
  when Fixnum
    signal
  else
    raise ArgumentError,
      "Bad signal: #{ signal.inspect }, need String or Fixnum: 'HUP'; 1"
  end
end

Instance Method Details

#renderObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/locd/newsyslog.rb', line 170

def render
  @render ||= begin
    fields = [
      log_path,
      "#{ owner }:#{ group }",
      mode,
      count,
      size,
      when_,
      render_flags,
    ]
    
    if pid_path
      fields << pid_path
      fields << sig_num
    end
    
    fields.map( &:to_s ).join FIELD_SEP
  end
end

#render_flagsObject



161
162
163
164
165
166
167
# File 'lib/locd/newsyslog.rb', line 161

def render_flags
  if flags.empty?
    NO_FLAGS
  else
    flags.join
  end
end

#sig_nameObject

Instance Methods



156
157
158
# File 'lib/locd/newsyslog.rb', line 156

def sig_name
  Signal.signame @sig_num
end