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



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/apache-loggen/base.rb', line 325

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)
    writer.flush()

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

  end
  writer.close

end