Class: LogGenerator::MyWriter

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

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ MyWriter

Returns a new instance of MyWriter.



282
283
284
285
286
# File 'lib/apache-loggen/base.rb', line 282

def initialize(filename)
  @filename = filename
  @io = nil
  rotate()
end

Instance Method Details

#closeObject



309
310
311
312
313
# File 'lib/apache-loggen/base.rb', line 309

def close()
  if @filename != nil && @io != nil && !@io.closed? then
    @io.close
  end
end

#flushObject



306
307
308
# File 'lib/apache-loggen/base.rb', line 306

def flush()
  @io.flush()
end

#rotateObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/apache-loggen/base.rb', line 287

def rotate()
  if @filename == nil then
    @io = $stdout
    return nil
  else
    dir = File.dirname(@filename)
    name = File.basename(@filename, '.*') + '.' + Time.now.strftime('%Y-%m-%d_%H%M%S') + File.extname(@filename)
    FileUtils.mkdir_p(dir) unless File.exists?(dir)
    if @io != nil then
      File.rename(@filename, name)
      @io.close
    end
    @io = open(@filename, "a")
    return File.join(dir, name)
  end
end

#write(str) ⇒ Object



303
304
305
# File 'lib/apache-loggen/base.rb', line 303

def write(str)
  return @io.write(str)
end