simple_rotate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gemtest ADDED
File without changes
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2011-12-16
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/simple_rotate.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_helper.rb
11
+ test/test_simple_rotate.rb
12
+ lib/simple_rotate/simple_rotate.rb
13
+ README.jpn
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on simple_rotate, see http://simple_rotate.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.jpn ADDED
@@ -0,0 +1,76 @@
1
+ □ Description
2
+
3
+ Ruby 1.8.7 動作確認。1.9でもいけるんじゃないかにゃ。
4
+
5
+
6
+ □ Class Method
7
+
8
+   new([log_file_name [,max_size [,rotate [,log_format, [log_message]]]]])
9
+
10
+     SimpleRotate オブジェクトを生成します。
11
+     4つのオプションは任意です。
12
+
13
+     log_file_name にはログを保存するファイル名を指定します。
14
+     指定しないと「実行ファイル名.log」という名前でファイルができます。
15
+     既に同じ名前のファイルがある場合は、そのファイルに追記されます。
16
+
17
+     max_size でログファイルの最大サイズを指定します。
18
+     このサイズを超えた場合、rotate を false にしていない限り、新しいファイルにログを書きます。
19
+     その際、古いファイルはリネームされます。
20
+     log_file_name.1、log_file_name.2、log_file_name.3、log_file_name.4 という具合に、
21
+     古いログファイルほど古い数字が記されます。
22
+     ただし、new で既存のログファイルのサイズを評価しますので、
23
+     new の時点でログファイルのサイズが 999KB だった場合は e までのログは既存のファイルに吐きます。
24
+     従ってログファイルのサイズが max_size を超えることはあります。
25
+     デフォルトは 1MBです。
26
+
27
+     rotate は古いログファイルの世代交代をする回数を指定します。
28
+     例えば rotate に 4 を設定すると、古いログファイルは
29
+     log_file_name.1、log_file_name.2、log_file_name.3、log_file_name.4 の4世代まで作られます。
30
+     この場合、新しいログファイルを入れると全部で5世代でローテーションされます。
31
+     この値を true に設定すると世代交代を行わず、無限にログファイルを残します。
32
+     長期間放置しておくとディスク領域を圧迫する可能性がありますので、
33
+     手ごろな回数で世代交代することをおすすめします。
34
+     false にすると、log_file_name.1、log_file_name.2 ... のような古いログは残さず、
35
+     max_size に到達した時点でログファイルは一度フラッシュされ、また同じログファイルにログを記録します。
36
+     デフォルトは true です。
37
+
38
+     log_format は古いログファイル(log_file_namexxxxxN)の xxxxx を指定できます。
39
+     デフォルトは `.' です。
40
+
41
+     log_message はログの開始時の終了時に日付とPIDを記録するかどうかの設定です。
42
+     true は記録、false は記録しないです。
43
+     デフォルトは true です。
44
+
45
+
46
+ □ Method
47
+
48
+   w(string [,true|false ,[true|false]])
49
+     string をログファイルに記録します。
50
+     デフォルトでは標準出力には出力しませんが、
51
+     第二引数に true を与える標準出力にも出力するようになります。
52
+     第三引数に true を与えると w を実行するごとに日付や時間を記録します。
53
+
54
+   e
55
+     ログの記録を終了します。
56
+
57
+   flush
58
+     ログファイルをフラッシュします。
59
+     フラッシュされた時点で、max_size に達していないログファイルでも
60
+     強制的にローテーションさせることができます。
61
+     クーロンと組み合わせることで一日ごとにログをローテーションさせる、
62
+     といった使い方ができます。
63
+
64
+
65
+ □ Example
66
+
67
+ require 'optparse'
68
+ opt = OptionParser.new
69
+ log = "logfile"
70
+ opt.on('--log val'){|v| log = v }
71
+ opt.parse!(ARGV)
72
+
73
+ logf = SimpleRotate.new(log)
74
+ logf.w("ここに残したいログを!")
75
+ logf.w("こんな感じで!")
76
+ logf.e
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = simple_rotate
2
+
3
+ == DESCRIPTION:
4
+
5
+ [Class Method]
6
+ new([log_file_name [,max_size [,rotate [,log_format, [log_message]]]]])
7
+
8
+ [Method]
9
+ w(string [,true|false ,[true|false]])
10
+
11
+ e
12
+
13
+ flush
14
+
15
+ == FEATURES/PROBLEMS:
16
+
17
+ tested only with Ruby 1.8.7.
18
+
19
+ == SYNOPSIS:
20
+
21
+ to rotate the ruby's log file.
22
+
23
+ == REQUIREMENTS:
24
+
25
+ == INSTALL:
26
+
27
+ sudo gem install simple_lotate
28
+
29
+ == LICENSE:
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2011 nyanko
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining
36
+ a copy of this software and associated documentation files (the
37
+ 'Software'), to deal in the Software without restriction, including
38
+ without limitation the rights to use, copy, modify, merge, publish,
39
+ distribute, sublicense, and/or sell copies of the Software, and to
40
+ permit persons to whom the Software is furnished to do so, subject to
41
+ the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be
44
+ included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
47
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
49
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
50
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
51
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
52
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/simple_rotate'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'simple_rotate' do
14
+ self.developer 'nyanko', 'hotta_1234@yahoo.co.jp'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module SimpleRotateVersionInfo
5
+ VERSION = '0.0.1'
6
+ end
7
+
8
+ require 'simple_rotate/simple_rotate'
@@ -0,0 +1,94 @@
1
+ class SimpleRotate
2
+
3
+ def initialize(log_file=$0+".log", max_size=1000000, rotate=true, log_format=".", log_mess=true)
4
+ @log_file = log_file
5
+ @max_size = max_size
6
+ @log_format = log_format
7
+ @rotate = rotate
8
+ @log_mess = log_mess
9
+ @rotate = false if @rotate == 0
10
+
11
+ if File.exist?(@log_file)
12
+ if over_size(@log_file)
13
+ ch_name(@log_file)
14
+ @logf = File.open(@log_file, "w+")
15
+ else
16
+ @logf = File.open(@log_file, "a+")
17
+ end
18
+ else
19
+ @logf = File.open(@log_file, "w+")
20
+ end
21
+ @size = File.size(@log_file)
22
+ w("\n----- Start Logs [Date: #{get_time('milestone')}] [PID: #{$$}] -----\n") if @log_mess
23
+ end
24
+
25
+ def over_size(f)
26
+ if File.size(f) > @max_size
27
+ return true
28
+ else
29
+ return false
30
+ end
31
+ end
32
+
33
+ def ch_name(f)
34
+ cnt = 1
35
+ rename_f = "#{@log_file}#{@log_format}#{cnt}"
36
+
37
+ if File.exist?(rename_f)
38
+ while File.exist?(rename_f)
39
+ cnt += 1
40
+ rename_f = "#{@log_file}#{@log_format}#{cnt}"
41
+ end
42
+ wait_rename = Array.new
43
+ for nc in 1...cnt
44
+ break if @rotate == 1
45
+ wait_rename << "File.rename('#{@log_file}#{@log_format}#{nc}', '#{@log_file}#{@log_format}#{nc+1}')"
46
+ if @rotate
47
+ next if @rotate.class == TrueClass
48
+ break if @rotate <= nc+1
49
+ end
50
+ end
51
+
52
+ wait_rename.reverse!
53
+ wait_rename.each{ |do_rename|
54
+ eval(do_rename)
55
+ }
56
+ File.rename(f, "#{@log_file}#{@log_format}1") if @rotate
57
+
58
+ else
59
+ File.rename(f, rename_f) if @rotate
60
+ end
61
+ end
62
+
63
+ def get_time(request)
64
+ if request == "party"
65
+ return Time.now.strftime("[%Y/%m/%d %H:%M:%S] ")
66
+ elsif request == "milestone"
67
+ return Time.now.strftime("%Y/%m/%d %H:%M:%S")
68
+ end
69
+ end
70
+
71
+ def flush
72
+ e
73
+ ch_name(@log_file)
74
+ @logf = File.open(@log_file, "w+")
75
+ @size = File.size(@log_file)
76
+ w("\n----- Start Logs [Date: #{get_time('milestone')}] [PID: #{$$}] -----\n") if @log_mess
77
+ end
78
+
79
+ def w(str, output=false, rec_time=false)
80
+ if rec_time
81
+ @logf.puts(get_time('party')+str)
82
+ puts get_time('party')+str if output
83
+ else
84
+ @logf.puts(str)
85
+ puts str if output
86
+ end
87
+ end
88
+
89
+ def e
90
+ w("----- End Logs [Date: #{get_time('milestone')}] [PID: #{$$}] -----\n") if @log_mess
91
+ @logf.close
92
+ end
93
+
94
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/simple_rotate.rb'}"
9
+ puts "Loading simple_rotate gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/simple_rotate'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestSimpleRotate < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_rotate
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - nyanko
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-19 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hoe
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 27
29
+ segments:
30
+ - 2
31
+ - 12
32
+ version: "2.12"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rdoc
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 19
44
+ segments:
45
+ - 3
46
+ - 10
47
+ version: "3.10"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: |-
51
+ [Class Method]
52
+ new([log_file_name [,max_size [,rotate [,log_format, [log_message]]]]])
53
+
54
+ [Method]
55
+ w(string [,true|false ,[true|false]])
56
+
57
+ e
58
+
59
+ flush
60
+ email:
61
+ - hotta_1234@yahoo.co.jp
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - History.txt
68
+ - Manifest.txt
69
+ - PostInstall.txt
70
+ files:
71
+ - History.txt
72
+ - Manifest.txt
73
+ - PostInstall.txt
74
+ - README.rdoc
75
+ - Rakefile
76
+ - lib/simple_rotate.rb
77
+ - script/console
78
+ - script/destroy
79
+ - script/generate
80
+ - test/test_helper.rb
81
+ - test/test_simple_rotate.rb
82
+ - lib/simple_rotate/simple_rotate.rb
83
+ - README.jpn
84
+ - .gemtest
85
+ homepage:
86
+ licenses: []
87
+
88
+ post_install_message: PostInstall.txt
89
+ rdoc_options:
90
+ - --main
91
+ - README.rdoc
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project: simple_rotate
115
+ rubygems_version: 1.8.12
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: "[Class Method] new([log_file_name [,max_size [,rotate [,log_format, [log_message]]]]]) [Method] w(string [,true|false ,[true|false]]) e flush"
119
+ test_files:
120
+ - test/test_helper.rb
121
+ - test/test_simple_rotate.rb