class SimpleRotate def initialize(log_file=$0+".log", max_size=1000000, rotate=true, log_format=".", log_mess=true) @log_file = log_file @max_size = max_size @log_format = log_format @rotate = rotate @log_mess = log_mess @rotate = false if @rotate == 0 if File.exist?(@log_file) if over_size(@log_file) ch_name(@log_file) @logf = File.open(@log_file, "w+") else @logf = File.open(@log_file, "a+") end else @logf = File.open(@log_file, "w+") end @size = File.size(@log_file) w("\n----- Start Logs [Date: #{get_time('milestone')}] [PID: #{$$}] -----\n") if @log_mess end def over_size(f) if File.size(f) > @max_size return true else return false end end def ch_name(f) cnt = 1 rename_f = "#{@log_file}#{@log_format}#{cnt}" if File.exist?(rename_f) while File.exist?(rename_f) cnt += 1 rename_f = "#{@log_file}#{@log_format}#{cnt}" end wait_rename = Array.new for nc in 1...cnt break if @rotate == 1 wait_rename << "File.rename('#{@log_file}#{@log_format}#{nc}', '#{@log_file}#{@log_format}#{nc+1}')" if @rotate next if @rotate.class == TrueClass break if @rotate <= nc+1 end end wait_rename.reverse! wait_rename.each{ |do_rename| eval(do_rename) } File.rename(f, "#{@log_file}#{@log_format}1") if @rotate else File.rename(f, rename_f) if @rotate end end def get_time(request) if request == "party" return Time.now.strftime("[%Y/%m/%d %H:%M:%S] ") elsif request == "milestone" return Time.now.strftime("%Y/%m/%d %H:%M:%S") end end def flush e ch_name(@log_file) @logf = File.open(@log_file, "w+") @size = File.size(@log_file) w("\n----- Start Logs [Date: #{get_time('milestone')}] [PID: #{$$}] -----\n") if @log_mess end def w(str, output=false, rec_time=false) if rec_time @logf.puts(get_time('party')+str) puts get_time('party')+str if output else @logf.puts(str) puts str if output end end def e w("----- End Logs [Date: #{get_time('milestone')}] [PID: #{$$}] -----\n") if @log_mess @logf.close end end