dlog 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ coverage
2
+ doc/rdoc
3
+ *log
4
+ pkg
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ load "#{File.dirname __FILE__}/vex/gem.rake"
2
+
3
+ task :default => :test
4
+
5
+ task :test do
6
+ sh "ruby test/test.rb"
7
+ end
8
+
9
+ task :rcov do
10
+ sh "cd test; rcov -o ../coverage -x ruby/.*/gems -x ^test.rb test.rb"
11
+ end
12
+
13
+ task :rdoc do
14
+ sh "rdoc -o doc/rdoc"
15
+ end
16
+
17
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext|
18
+ puts ext
19
+ load ext }
data/gem.yml ADDED
@@ -0,0 +1,10 @@
1
+ version: "0.2"
2
+ summary: "{d,r}log"
3
+ description: |
4
+ A logging library mainly for development purposes.
5
+ homepage: http://github.com/pboy/dlog
6
+ author: pboy
7
+ email: eno-pboy@open-lab.org
8
+ # ignore_pattern:
9
+ # - "tmp/*"
10
+ # - "**/.*"
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ $: << "#{File.dirname(__FILE__)}/lib"
2
+
3
+ require "dlog"
data/lib/dlog.rb ADDED
@@ -0,0 +1,238 @@
1
+ require "logger"
2
+
3
+ module Dlog
4
+ module Color
5
+ extend self
6
+
7
+ def color(id)
8
+ "\e[#{id}m"
9
+ end
10
+
11
+ RED = color 31
12
+ GREEN = color 32
13
+ YELLOW = color 33
14
+ BLUE = color 34
15
+ MAGENTA = color 35
16
+ CYAN = color 36
17
+ DARK_GREY = color "1;30"
18
+ LIGHT_RED = color "1;31"
19
+ LIGHT_GREEN = color "1;32"
20
+ LIGHT_YELLOW = color "1;33"
21
+ LIGHT_BLUE = color "1;34"
22
+ LIGHT_MAGENTA = color "1;35"
23
+ LIGHT_CYAN = color "1;36"
24
+
25
+ CLEAR = color 0
26
+
27
+ def error(msg); "#{LIGHT_RED}#{msg}#{CLEAR}"; end
28
+ def warn(msg); "#{LIGHT_YELLOW}#{msg}#{CLEAR}"; end
29
+ def info(msg); "#{GREEN}#{msg}#{CLEAR}"; end
30
+ def debug(msg); msg; end
31
+ end
32
+
33
+ #
34
+ # The logger device.
35
+ def self.logger=(logger)
36
+ @logger = logger
37
+ end
38
+
39
+ def self.logger
40
+ @logger ||= if defined?(RAILS_DEFAULT_LOGGER)
41
+ RAILS_DEFAULT_LOGGER
42
+ else
43
+ logger = Logger.new(STDERR)
44
+ logger.formatter = StderrFormatter
45
+ logger
46
+ end
47
+ end
48
+
49
+ def self.log(severity, args, source_offset = 1)
50
+ msg = ""
51
+ was_string = true
52
+ args.map do |s|
53
+ msg += was_string ? " " : ", " unless msg.empty?
54
+ msg += ((was_string = s.is_a?(String)) ? s : s.inspect)
55
+ end
56
+
57
+ source = caller[source_offset]
58
+ msg = "#{release? ? rlog_caller(source) : dlog_caller(source)} #{msg}"
59
+ msg = Color.send severity, msg
60
+
61
+ logger = self.logger
62
+ logger.send severity, msg
63
+
64
+ if irb? && !log_to_stderr?
65
+ STDERR.puts msg
66
+ end
67
+
68
+ args.first
69
+ end
70
+
71
+ def self.irb?
72
+ caller.detect do |s| s =~ /irb\/workspace.rb/ end != nil
73
+ end
74
+
75
+ def self.log_to_stderr?
76
+ logdev = logger.instance_variable_get("@logdev")
77
+ logdev.dev == STDERR if logdev.respond_to?(:dev)
78
+ end
79
+
80
+ # Logging formatter
81
+ module Formatter
82
+ # formatter#call is invoked with 4 arguments: severity, time, progname
83
+ # and msg for each log. Bear in mind that time is a Time and msg is an
84
+ # Object that user passed and it could not be a String.
85
+ def self.call(severity, time, progname, msg)
86
+ time.strftime("%Y-%m-%d %H:%M:%S #{severity}: #{msg}")
87
+ end
88
+ end
89
+
90
+ module StderrFormatter
91
+ # formatter#call is invoked with 4 arguments: severity, time, progname
92
+ # and msg for each log. Bear in mind that time is a Time and msg is an
93
+ # Object that user passed and it could not be a String.
94
+ def self.call(severity, time, progname, msg)
95
+ "#{msg}\n"
96
+ end
97
+ end
98
+
99
+ #
100
+ # get a caller description, for release mode
101
+ def self.rlog_caller(source)
102
+ if source =~ /^(.*):(\d+)/
103
+ file, line = $1, $2
104
+ file, line = $1, $2
105
+ if file == "(irb)"
106
+ "[irb]:"
107
+ else
108
+ "[" + File.basename(file).sub(/\.[^\.]*$/, "") + "]:"
109
+ end
110
+ else
111
+ "[log]"
112
+ end
113
+ end
114
+
115
+ #
116
+ # get a caller description, for debug mode
117
+ def self.dlog_caller(source)
118
+ if source =~ /^(.*):(\d+)/
119
+ file, line = $1, $2
120
+ if file == "(irb)"
121
+ "[irb]:"
122
+ else
123
+ file = File.expand_path(file)
124
+
125
+ file.gsub!(ROOT, ".") or
126
+ file.gsub!(HOME, "~/")
127
+
128
+ "#{file}(#{line}):"
129
+ end
130
+ else
131
+ "[dlog]:"
132
+ end
133
+ end
134
+
135
+ #
136
+ # The applications ROOT dir, to shorten the source line
137
+ ROOT = if defined?(RAILS_ROOT)
138
+ RAILS_ROOT
139
+ else
140
+ File.expand_path(Dir.getwd)
141
+ end
142
+
143
+ # The user's HOME dir, to shorten the source line
144
+ HOME = ENV["HOME"] + "/"
145
+
146
+ # -- dlog modi ------------------------------------------------------
147
+
148
+ @@mode = :debug
149
+
150
+ def self.release!(&block); set_mode :release, &block; end
151
+ def self.debug!(&block); set_mode :debug, &block; end
152
+ def self.quiet!(&block); set_mode :quiet, &block; end
153
+
154
+ def self.release?; @@mode == :release; end
155
+ def self.debug?; @@mode == :debug; end
156
+ def self.quiet?; @@mode == :quiet; end
157
+
158
+ def self.set_mode(mode, &block)
159
+ if !block_given?
160
+ old = mode
161
+ else
162
+ old, @@mode = @@mode, mode
163
+ yield
164
+ end
165
+ ensure
166
+ @@mode = old
167
+ end
168
+
169
+ def self.error(*args)
170
+ log :error, args
171
+ end
172
+
173
+ def self.warn(*args)
174
+ log :warn, args
175
+ end
176
+
177
+ def self.info(*args)
178
+ log :info, args
179
+ end
180
+
181
+ def self.debug(*args)
182
+ log :debug, args
183
+ end
184
+
185
+ module Nolog
186
+ extend self
187
+
188
+ def error(*args); args.first; end
189
+ def warn(*args); args.first; end
190
+ def info(*args); args.first; end
191
+ def debug(*args); args.first; end
192
+ end
193
+ end
194
+
195
+ class Object
196
+ def dlog(*args)
197
+ quiet = Dlog.quiet? || Dlog.release?
198
+
199
+ if args.empty?
200
+ quiet ? Dlog::Nolog : Dlog
201
+ else
202
+ Dlog.log :info, args unless quiet
203
+ args.first
204
+ end
205
+ end
206
+
207
+ def rlog(*args)
208
+ quiet = Dlog.quiet? || Dlog.release?
209
+
210
+ if args.empty?
211
+ quiet ? Dlog::Nolog : Dlog
212
+ else
213
+ Dlog.log :warn, args unless quiet
214
+ args.first
215
+ end
216
+ end
217
+
218
+ def benchmark(*args, &block)
219
+ if Dlog.quiet?
220
+ return yield
221
+ end
222
+
223
+ start = Time.now
224
+ r = yield
225
+ args.push ": %3d msecs" % (1000 * (Time.now - start))
226
+ Dlog.log :warn, args
227
+ r
228
+ rescue
229
+ args.push ": exception raised after #{"%3d msecs" % (1000 * (Time.now - start)) }"
230
+ args.push :source => caller[0]
231
+ rlog *args
232
+ raise
233
+ end
234
+
235
+ private
236
+
237
+ def dlogger; nil; end
238
+ end
data/script/console ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require "irb"
3
+ $: << "#{File.dirname(__FILE__)}/../lib"
4
+ require "dlog"
5
+ IRB.start
data/test/test.rb ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ DIRNAME = File.expand_path File.dirname(__FILE__)
3
+ Dir.chdir(DIRNAME)
4
+
5
+ #
6
+ # initialize the gem
7
+ require "../init"
8
+
9
+ dlog "HEY"
10
+ rlog "HEY"
data/vex/gem.rake ADDED
@@ -0,0 +1,36 @@
1
+ def sys(*args)
2
+ STDERR.puts "#{args.join(" ")}"
3
+ system *args
4
+ end
5
+
6
+ namespace :gem do
7
+ task :build => :spec do
8
+ options = []
9
+ sys "gem build .gemspec #{options.join(" ")} && mkdir -p pkg && mv *.gem pkg"
10
+ end
11
+
12
+ task :spec do
13
+ File.open ".gemspec", "w" do |file|
14
+ file.write <<-TXT
15
+ require "vex/gem"
16
+ Gem.spec File.dirname(__FILE__)
17
+ TXT
18
+ end
19
+ end
20
+
21
+ task :install => :build do
22
+ file = Dir.glob("pkg/*.gem").sort.last
23
+ sys "sudo gem install #{file}"
24
+ end
25
+
26
+ task :push => :build do
27
+ file = Dir.glob("pkg/*.gem").sort.last
28
+ puts "To push the gem to gemcutter please run"
29
+ puts
30
+ puts "\tgem push #{file}"
31
+ end
32
+ end
33
+
34
+ desc "Build gem"
35
+ # task :gem => %w(test gem:install)
36
+ task :gem => %w(gem:install gem:push)
data/vex/gem.rb ADDED
@@ -0,0 +1,95 @@
1
+ module Gem
2
+ (class << self; self; end).class_eval do
3
+ def default_attributes
4
+ %w(name version date files executables)
5
+ end
6
+
7
+ def set_root(root)
8
+ @root = File.expand_path(root)
9
+ @name = File.basename(@root)
10
+ @conf ||= YAML.load File.read("#{@root}/gem.yml")
11
+ @attributes ||= (default_attributes + @conf.keys.map(&:to_s)).uniq
12
+ end
13
+
14
+ # attr_reader :root, :conf, :attributes,
15
+ attr_reader :name
16
+
17
+ def attribute(name)
18
+ if @conf.key?(name) && !%w(dependencies).include?(name)
19
+ @conf[name]
20
+ else
21
+ self.send(name)
22
+ end
23
+ end
24
+
25
+ #
26
+ # create a new Gem::Specification object for this gem.
27
+ def spec(root)
28
+ self.set_root root
29
+
30
+ Gem::Specification.new do |s|
31
+ @attributes.each do |attr|
32
+ v = attribute(attr)
33
+ next if v.nil?
34
+
35
+ log attr, v
36
+
37
+ s.send attr + "=", v
38
+ end
39
+
40
+ %w(pre_uninstall post_install).each do |hook|
41
+ next unless File.exists?("#{root}/hooks/#{hook}.rb")
42
+ log hook, "yes"
43
+ Gem.send(hook) {
44
+ load "hooks/#{hook}.rb"
45
+ }
46
+ end
47
+ end
48
+ end
49
+
50
+ def log(attr, v)
51
+ v = case attr
52
+ when "files" then "#{v.length} files"
53
+ else v.inspect
54
+ end
55
+
56
+ STDERR.puts "#{"%20s" % attr}:\t#{v}"
57
+ end
58
+
59
+ def dependencies
60
+ return nil unless @conf["dependencies"]
61
+
62
+ @conf["dependencies"].map do |d|
63
+ Gem::Dependency.new d, ">= 0"
64
+ end
65
+ end
66
+
67
+ def head(file)
68
+ File.open(file) do |f|
69
+ f.readline
70
+ end
71
+ end
72
+
73
+ def executables
74
+ r = Dir.glob("#{@root}/bin/**/*").map do |file|
75
+ next unless head(file) =~ /^#!/
76
+ file[@root.length + 5 .. -1]
77
+ end.compact
78
+
79
+ return nil if r.empty?
80
+ r
81
+ end
82
+
83
+ #
84
+ # get files from git
85
+ def files
86
+ r = `git ls-files`.split("\n")
87
+ end
88
+
89
+ #
90
+ # return the date
91
+ def date
92
+ Date.today.to_s
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dlog
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ version: "0.2"
10
+ platform: ruby
11
+ authors:
12
+ - pboy
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-29 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: |
22
+ A logging library mainly for development purposes.
23
+
24
+ email: eno-pboy@open-lab.org
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Rakefile
34
+ - gem.yml
35
+ - init.rb
36
+ - lib/dlog.rb
37
+ - script/console
38
+ - test/test.rb
39
+ - vex/gem.rake
40
+ - vex/gem.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/pboy/dlog
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: "{d,r}log"
75
+ test_files: []
76
+