Class: LogGenerator::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/apache-loggen/base.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  :limit => 0,
  :rate => 0,
  :rotate => 0,
  :progress => false,
  :json => false,
  :filename => nil,
}

Class Method Summary collapse

Class Method Details

.execute(conf = {}, gen_obj = nil, &block) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/apache-loggen/base.rb', line 315

def self.execute(conf={}, gen_obj=nil, &block)
  
  config = DEFAULT_CONFIG.merge(conf)
  writer = MyWriter.new(config[:filename])
  gen_kick = gen_obj && gen_obj.is_a?(Base)

  # 実行
  last_rotate = Time.now.to_i
  Executors.exec(config) do | context |

    if config[:rotate] > 0 && (last_rotate + config[:rotate]) <= Time.now.to_i then
      rotated_file = writer.rotate()
      if config[:progress] then
        $stderr.write "\rfile rotate. rename to #{rotated_file}\n"
      end
      last_rotate = Time.now.to_i
    end
    
    # レコード生成
    record = gen_obj.generate(context, config) if gen_kick
    record = block.call(context, config, record) if block

    # 出力
    writer.write(record)

    not (config[:limit] > 0 && config[:limit] <= context[:total_count])

  end
  writer.close

end