loba 0.2.0
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.
- checksums.yaml +7 -0
- data/.codeclimate.yml +21 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.rubocop.yml +1168 -0
- data/.travis.yml +9 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +171 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/loba/version.rb +4 -0
- data/lib/loba.rb +203 -0
- data/loba.gemspec +30 -0
- data/readme/environments.md +0 -0
- data/readme/ts.md +29 -0
- data/readme/val.md +103 -0
- data/readme/zulu.png +0 -0
- metadata +134 -0
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at richard@newmanworks.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Richard Newman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
[](https://gemnasium.com/rdnewman/loba)
|
2
|
+
[](https://travis-ci.org/rdnewman/loba)
|
3
|
+
[](https://codeclimate.com/github/rdnewman/loba)
|
4
|
+
[](https://codeclimate.com/github/rdnewman/loba/coverage)
|
5
|
+
[](http://inch-ci.org/github/rdnewman/loba)
|
6
|
+
[](https://hakiri.io/github/rdnewman/loba/master)
|
7
|
+
|
8
|
+
# Loba
|
9
|
+
|
10
|
+

|
11
|
+
|
12
|
+
Easy tracing for debugging: handy methods for adding trace lines to output or Rails logs.
|
13
|
+
|
14
|
+
(Installation is pretty much what you'd expect for a gem, but read Environment Notes below first.)
|
15
|
+
|
16
|
+
## Overview
|
17
|
+
|
18
|
+
There are two kinds of questions I usually want to answer when trying to diagnose code behavior:
|
19
|
+
|
20
|
+
1. Is this spot of code being reached (or is it reached in the order I think it is)?
|
21
|
+
1. What is the value of this variable?
|
22
|
+
|
23
|
+
Loba statements are intended to be terse to minimize typing.
|
24
|
+
|
25
|
+
Loba statements are intended to be minimally invasive and atomic. They should not have any (much) more impact than a regular `puts` or `Rails.logger.debug` statement.
|
26
|
+
|
27
|
+
Loba statements are expected to be removed when you're done with them. No point in cluttering up production code.
|
28
|
+
|
29
|
+
Loba will check for presence of Rails. If it's there, it'll write to `Rails.logger.debug`. If not, it'll write to STDOUT (i.e., `puts`). Loba will work equally well with or without Rails.
|
30
|
+
|
31
|
+
Loba uses the [colorize gem](https://rubygems.org/gems/colorize) to help make trace statements more visible.
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
My advice is to align Loba statements to the far left in your source code (a la `=begin` or `=end`) so they're easy to see and remove when you're done.
|
36
|
+
|
37
|
+
#### Timestamp notices: `Loba.ts`
|
38
|
+
|
39
|
+
Outputs a timestamped notice, useful for quick traces to see the code path and easier than, say, [Kernel#set_trace_func](http://ruby-doc.org/core-2.2.3/Kernel.html#method-i-set_trace_func). Also does a simple elapsed time check since the previous timestamp notice to help with quick, minimalist profiling.
|
40
|
+
|
41
|
+
For example,
|
42
|
+
|
43
|
+
```
|
44
|
+
[TIMESTAMP] #=0002, diff=93.478016, at=1451444972.970602 (in=/home/usracct/src/myapp/app/models/target.rb:55:in `some_calculation')
|
45
|
+
```
|
46
|
+
|
47
|
+
To invoke,
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
Loba.ts # no arguments, generally (see Environment Notes below)
|
51
|
+
```
|
52
|
+
|
53
|
+
You can read [more detail](readme/ts.md) on this command.
|
54
|
+
|
55
|
+
#### Variable or method return inspection: `Loba.val`
|
56
|
+
|
57
|
+
Inserts line to Rails.logger.debug (or to STDOUT if Rails.logger not available) showing value with method and class identification
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
Loba.val :var_sym # the :var_sym argument is the variable or method name given as a symbol
|
61
|
+
```
|
62
|
+
|
63
|
+
For example,
|
64
|
+
|
65
|
+
```
|
66
|
+
[Target.some_calculation] my_var: 54 (in /home/usracct/src/myapp/app/models/target.rb:55:in `some_calculation')
|
67
|
+
```
|
68
|
+
|
69
|
+
You can read [more detail](readme/val.md) on this command.
|
70
|
+
|
71
|
+
#### Snippet example
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
require 'loba' # not generally necessary in Rails projects
|
75
|
+
class HelloWorld
|
76
|
+
def initialize
|
77
|
+
@x = 42
|
78
|
+
Loba.ts # see? it's easier to see what to remove later
|
79
|
+
@y = "Charlie"
|
80
|
+
end
|
81
|
+
|
82
|
+
def hello
|
83
|
+
Loba.val :@x
|
84
|
+
puts "Hello, #{@y}" if @x == 42
|
85
|
+
Loba.ts
|
86
|
+
end
|
87
|
+
end
|
88
|
+
HelloWorld.new.hello
|
89
|
+
```
|
90
|
+
|
91
|
+
Output:
|
92
|
+
|
93
|
+
```
|
94
|
+
[TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411 (in=/home/usracct/src/lobademo/hello_world.rb:4:in 'initialize'
|
95
|
+
[HelloWorld#hello] @x: 42 (in /home/richard/src/loba/spec/hello_world.rb:9:in `hello')
|
96
|
+
Hello, Charlie
|
97
|
+
[TIMESTAMP] #=0002, diff=0.000720, at=1451615389.506132 (in=/home/usracct/src/lobademo/hello_world.rb:11:in 'hello'
|
98
|
+
```
|
99
|
+
|
100
|
+
## Environment Notes
|
101
|
+
|
102
|
+
This section is only relevant in Rails environments.
|
103
|
+
|
104
|
+
The expectation is that Loba statements are just for development or test trace statements. Generally, it's a bad idea to leave diagnostic code in Rails production; still, it can happen. And, occasionally, it can be useful to have trace statements in production too if you have an issue that is difficult to reproduce.
|
105
|
+
|
106
|
+
`Loba.ts` and `Loba.val` try to protect against timestamp or value notice requests being accidentally left in the code by checking for the Rails environment Loba is being invoked under. If in production, `Loba.ts` and `Loba.val` will normally just return immediately without rendering anything to help minimize any impact on production code. However, that behavior can be overridden with a `true` as an additional last argument to output a notice even when in the production environment. In general, this should be done sparingly if at all.
|
107
|
+
|
108
|
+
These considerations also have an impact on how you install the Loba gem when using `bundler`. If you only install the gem for :development and :test, then any Loba statements left in the code when it goes to production will cause an error because the statements wouldn't be recognized. That's perhaps a Good Thing, if you never want them left in.
|
109
|
+
|
110
|
+
If you simply install the gem for all environments, then Loba will be available in production, but you may not notice as easily if some Loba calls are unintentionally left in. Of course, if you want those statements to work in production, then you should install the gem for all environments.
|
111
|
+
|
112
|
+
The following is the code example snippet but always logging even in Rails production environments:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
class HelloWorld
|
116
|
+
def initialize
|
117
|
+
@x = 42
|
118
|
+
Loba.ts(true)
|
119
|
+
@y = "Charlie"
|
120
|
+
end
|
121
|
+
|
122
|
+
def hello
|
123
|
+
Loba.val :@x, nil, true
|
124
|
+
puts "Hello, #{@y}" if @x == 42
|
125
|
+
Loba.ts true
|
126
|
+
end
|
127
|
+
end
|
128
|
+
HelloWorld.new.hello
|
129
|
+
```
|
130
|
+
|
131
|
+
## Installation
|
132
|
+
|
133
|
+
See above Environment Notes if using with Rails.
|
134
|
+
|
135
|
+
Add this line to your application's Gemfile:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
group :development, :test do
|
139
|
+
gem 'loba', require: false, github: 'rdnewman/loba' # until I publish it on RubyGems
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
or for all environments:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
gem 'loba', require: false, github: 'rdnewman/loba' # until I publish it on RubyGems
|
147
|
+
```
|
148
|
+
|
149
|
+
|
150
|
+
And then execute:
|
151
|
+
|
152
|
+
$ bundle
|
153
|
+
|
154
|
+
Or install it yourself as:
|
155
|
+
|
156
|
+
$ gem install loba
|
157
|
+
|
158
|
+
## Development
|
159
|
+
|
160
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
161
|
+
|
162
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
163
|
+
|
164
|
+
## Contributing
|
165
|
+
|
166
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rdnewman/loba. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
167
|
+
|
168
|
+
|
169
|
+
## License
|
170
|
+
|
171
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "loba"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/loba/version.rb
ADDED
data/lib/loba.rb
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
require 'loba/version'
|
2
|
+
require 'singleton'
|
3
|
+
require 'binding_of_caller'
|
4
|
+
require 'colorize'
|
5
|
+
|
6
|
+
# Loba module for quick tracing of Ruby and Rails.
|
7
|
+
# If a Rails application, will use Rails.logger.debug.
|
8
|
+
# If not a Rails application, will use STDOUT.
|
9
|
+
module Loba
|
10
|
+
|
11
|
+
# Outputs a timestamped notice, useful for quick traces to see the code path.
|
12
|
+
# Also does a simple elapsed time check since the previous timestamp notice to help with quick, minimalist profiling.
|
13
|
+
# @param production_is_ok [Boolean] true if this timestamp notice is enabled when running in :production environment
|
14
|
+
# @return [NilClass] nil
|
15
|
+
# @example Basic use
|
16
|
+
# def hello
|
17
|
+
# Loba.ts
|
18
|
+
# end
|
19
|
+
# #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
|
20
|
+
def ts(production_is_ok = false)
|
21
|
+
if Internal::Platform.logging_ok?(production_is_ok)
|
22
|
+
@loba_logger ||= Internal::Platform.logger
|
23
|
+
@loba_timer ||= Internal::TimeKeeper.instance
|
24
|
+
|
25
|
+
begin
|
26
|
+
@loba_timer.timenum += 1
|
27
|
+
timenow = Time.now()
|
28
|
+
stamptag = '%04d'%(@loba_timer.timenum)
|
29
|
+
timemark = '%.6f'%(timenow.round(6).to_f)
|
30
|
+
timechg = '%.6f'%(timenow - @loba_timer.timewas)
|
31
|
+
@loba_logger.call "[TIMESTAMP]".black.on_light_black +
|
32
|
+
" #=".yellow +
|
33
|
+
"#{stamptag}" +
|
34
|
+
", diff=".yellow +
|
35
|
+
"#{timechg}" +
|
36
|
+
", at=".yellow +
|
37
|
+
"#{timemark}" +
|
38
|
+
" \t(in=#{caller[0]})".light_black
|
39
|
+
@loba_timer.timewas = timenow
|
40
|
+
rescue StandardError => e
|
41
|
+
@loba_logger.call "[TIMESTAMP] #=FAIL, in=#{caller[0]}, err=#{e}".colorize(:red)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
module_function :ts
|
47
|
+
|
48
|
+
# Outputs a value notice showing value of provided argument including method and class identification
|
49
|
+
# @param argument [various] the value to be evaluated and shown; if given as a Symbol, a label based on the argument will proceed the value the argument refers to
|
50
|
+
# @param label [String] an optional, explicit label to be used instead of attempting to infer from the argument
|
51
|
+
# @return [NilClass] nil
|
52
|
+
# @example Using Symbol as argument
|
53
|
+
# class HelloWorld
|
54
|
+
# def hello(name)
|
55
|
+
# Loba.val :name # best to put Loba statement to far left for easy removal when done
|
56
|
+
# puts "Hello, #{name}!"
|
57
|
+
# end
|
58
|
+
# end
|
59
|
+
# HelloWorld.new.hello("Charlie")
|
60
|
+
# #=> [HelloWorld#hello] name: Charlie (at /path/to/file/hello_world.rb:3:in `hello')
|
61
|
+
# #=> Hello, Charlie!
|
62
|
+
# @example Using non-Symbol as argument
|
63
|
+
# class HelloWorld
|
64
|
+
# def hello(name)
|
65
|
+
# Loba.val name
|
66
|
+
# puts "Hello, #{name}!"
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
# HelloWorld.new.hello("Charlie")
|
70
|
+
# #=> [HelloWorld#hello] Charlie (at /path/to/file/hello_world.rb:3:in `hello')
|
71
|
+
# #=> Hello, Charlie!
|
72
|
+
# @example Using non-Symbol as argument with a label
|
73
|
+
# class HelloWorld
|
74
|
+
# def hello(name)
|
75
|
+
# Loba.val name, "Name:"
|
76
|
+
# puts "Hello, #{name}!"
|
77
|
+
# end
|
78
|
+
# end
|
79
|
+
# HelloWorld.new.hello("Charlie")
|
80
|
+
# #=> [HelloWorld#hello] Name: Charlie (at /path/to/file/hello_world.rb:3:in `hello')
|
81
|
+
# #=> Hello, Charlie!
|
82
|
+
def val(argument = :nil, label = nil, production_is_ok = false)
|
83
|
+
if Internal::Platform.logging_ok?(production_is_ok)
|
84
|
+
depth = 0
|
85
|
+
@loba_logger ||= Internal::Platform.logger
|
86
|
+
|
87
|
+
tag = Internal.calling_tag(depth+1)
|
88
|
+
name = argument.is_a?(Symbol) ? "#{argument}:" : nil
|
89
|
+
|
90
|
+
text = if label.nil?
|
91
|
+
name
|
92
|
+
else
|
93
|
+
label.strip!
|
94
|
+
label += ':' unless label[-1] == ':'
|
95
|
+
end
|
96
|
+
|
97
|
+
result = if argument.is_a?(Symbol)
|
98
|
+
binding.of_caller(depth+1).eval(argument.to_s)
|
99
|
+
else
|
100
|
+
argument
|
101
|
+
end
|
102
|
+
|
103
|
+
source_line = Internal.calling_source_line(depth+1)
|
104
|
+
|
105
|
+
@loba_logger.call "#{tag} ".green +
|
106
|
+
"#{text.nil? ? '' : "#{text}"} ".light_green +
|
107
|
+
"#{result.nil? ? '-nil-' : result}" +
|
108
|
+
" \t(in #{source_line})".light_black
|
109
|
+
end
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
module_function :val
|
113
|
+
|
114
|
+
|
115
|
+
module Internal
|
116
|
+
|
117
|
+
class << self
|
118
|
+
LOBA_CLASS_NAME = 'self.class.name'
|
119
|
+
def calling_class_name(depth = 0)
|
120
|
+
m = binding.of_caller(depth+1).eval(LOBA_CLASS_NAME)
|
121
|
+
if m.nil? || m.empty?
|
122
|
+
'<anonymous class>'
|
123
|
+
elsif m == 'Class'
|
124
|
+
binding.of_caller(depth+1).eval('self.name')
|
125
|
+
else
|
126
|
+
m
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
LOBA_METHOD_NAME = 'self.send(:__method__)'
|
131
|
+
def calling_method_name(depth = 0)
|
132
|
+
m = binding.of_caller(depth+1).eval(LOBA_METHOD_NAME)
|
133
|
+
(m.nil? || m.empty?) ? '<anonymous method>' : m
|
134
|
+
end
|
135
|
+
|
136
|
+
def calling_method_type(depth = 0)
|
137
|
+
if binding.of_caller(depth+1).eval('self.class.name') == 'Class'
|
138
|
+
:class
|
139
|
+
else
|
140
|
+
:instance
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def calling_line_number(depth = 0)
|
145
|
+
binding.of_caller(depth+1).eval('__LINE__')
|
146
|
+
end
|
147
|
+
|
148
|
+
def calling_source_line(depth = 0)
|
149
|
+
caller[depth]
|
150
|
+
end
|
151
|
+
|
152
|
+
def calling_tag(depth = 0)
|
153
|
+
delim = {class: '.', instance: '#'}
|
154
|
+
"[#{calling_class_name(depth+1)}#{delim[calling_method_type(depth+1)]}#{calling_method_name(depth+1)}]"
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# Internal class for tracking time stamps; should not be used directly
|
159
|
+
# @!attribute [rw] timewas
|
160
|
+
# Previous timestamped Time value
|
161
|
+
# @!attribute [rw] timenum
|
162
|
+
# Count of timestamping occurances so far
|
163
|
+
class TimeKeeper
|
164
|
+
include Singleton
|
165
|
+
attr_accessor :timewas, :timenum
|
166
|
+
def initialize
|
167
|
+
@timewas, @timenum = Time.now, 0
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# Internal class for managing logging across Rails and non-Rails applications
|
172
|
+
class Platform
|
173
|
+
class << self
|
174
|
+
# Returns true if Rails appears to be available
|
175
|
+
def rails?
|
176
|
+
defined?(Rails)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Returns true if logging is to be allowed
|
180
|
+
def logging_ok?(force_true = false)
|
181
|
+
return true if force_true
|
182
|
+
return true unless rails?
|
183
|
+
begin
|
184
|
+
!Rails.env.production?
|
185
|
+
rescue
|
186
|
+
true # not Rails production if Rails isn't recognized
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# Returns a logging mechanism appropriate for the application
|
191
|
+
def logger
|
192
|
+
if (rails? && Rails.logger.present?)
|
193
|
+
->(arg){Rails.logger.debug arg}
|
194
|
+
else
|
195
|
+
->(arg){puts arg}
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
end # module Internal
|
202
|
+
|
203
|
+
end # module Loba
|
data/loba.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'loba/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "loba"
|
8
|
+
spec.version = Loba::VERSION
|
9
|
+
spec.platform = Gem::Platform::RUBY
|
10
|
+
spec.authors = ["Richard Newman"]
|
11
|
+
spec.email = ["richard@newmanworks.com"]
|
12
|
+
|
13
|
+
spec.summary = %q{Loba: Easy tracing for debugging.}
|
14
|
+
spec.description = %q{Handy methods for adding trace lines to output or Rails logs.}
|
15
|
+
spec.homepage = "https://github.com/rdnewman/loba"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
spec.required_ruby_version = '>= 2.1.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec-rails", "~> 3.0"
|
27
|
+
|
28
|
+
spec.add_dependency "binding_of_caller", "~> 0.7"
|
29
|
+
spec.add_dependency "colorize", "~> 0.7"
|
30
|
+
end
|
File without changes
|
data/readme/ts.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#### Timestamp notices: `Loba.ts`
|
2
|
+
Outputs a timestamped notice, useful for quick traces to see the code path and easier than, say, [Kernel#set_trace_func](http://ruby-doc.org/core-2.2.3/Kernel.html#method-i-set_trace_func).
|
3
|
+
Also does a simple elapsed time check since the previous timestamp notice to help with quick, minimalist profiling.
|
4
|
+
|
5
|
+
For example,
|
6
|
+
|
7
|
+
```
|
8
|
+
[TIMESTAMP] #=0002, diff=93.478016, at=1451444972.970602 (in=/home/usracct/src/myapp/app/models/target.rb:55:in `some_calculation')
|
9
|
+
```
|
10
|
+
|
11
|
+
To invoke,
|
12
|
+
|
13
|
+
```
|
14
|
+
Loba.ts # no arguments
|
15
|
+
```
|
16
|
+
|
17
|
+
The resulting notice output format is
|
18
|
+
|
19
|
+
```
|
20
|
+
[TIMESTAMP] #=nnnn, diff=ss.ssssss, at=tttttttttt.tttttt (in=/path/to/code/somecode.rb:LL:in 'some_method')
|
21
|
+
```
|
22
|
+
|
23
|
+
where
|
24
|
+
* `nnn` ("#=") is a sequential numbering (1, 2, 3, ...) of timestamp notices,
|
25
|
+
* `ss.ssssss` ("diff=") is number of seconds since the last timestamp notice was output (i.e., relative time),
|
26
|
+
* `tttttttttt.tttttt` ("at=") is Time.now (as seconds) (i.e., absolute time),
|
27
|
+
* `/path/to/code/somecode.rb` ("in=") is the source code file that invoked `Loba.ts`,
|
28
|
+
* `LL` ("in=...:") is the line number of the source code file that invoked `Loba.ts`, and
|
29
|
+
* `some_method`is the method in which `Loba.ts` was invoked.
|
data/readme/val.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
#### Value notices: `Loba.val`
|
2
|
+
|
3
|
+
Inserts line to Rails.logger.debug (or to STDOUT if Rails.logger not available) showing value with method and class identification
|
4
|
+
|
5
|
+
```
|
6
|
+
Loba.val :var_sym # the :var_sym argument is the variable or method name given as a symbol (see below)
|
7
|
+
```
|
8
|
+
|
9
|
+
or
|
10
|
+
|
11
|
+
```
|
12
|
+
Loba.val some_identifier # directly give a variable or method name instead of a symbol (see below)
|
13
|
+
```
|
14
|
+
|
15
|
+
or
|
16
|
+
|
17
|
+
```
|
18
|
+
Loba.val some_identifier "My label:" # same as direct variable, but allows a custom label
|
19
|
+
```
|
20
|
+
|
21
|
+
Will produce a notice similar to the following:
|
22
|
+
|
23
|
+
```
|
24
|
+
[Target.some_calculation] my_var: 54 (in /home/usracct/src/myapp/app/models/target.rb:55:in `some_calculation')
|
25
|
+
```
|
26
|
+
|
27
|
+
###### Example 1: Using simple Symbol as argument
|
28
|
+
```ruby
|
29
|
+
class HelloWorld
|
30
|
+
def hello(name)
|
31
|
+
Loba.val :name # best to put Loba statement to far left for easy removal when done
|
32
|
+
puts "Hello, #{name}!"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
HelloWorld.new.hello("Charlie")
|
36
|
+
#=> [HelloWorld#hello] name: Charlie (in /path/to/file/hello_world.rb:3:in `hello')
|
37
|
+
#=> Hello, Charlie!
|
38
|
+
```
|
39
|
+
|
40
|
+
###### Example 2: Using more complex Symbol as argument
|
41
|
+
```ruby
|
42
|
+
class HelloWorld
|
43
|
+
def hello(name)
|
44
|
+
myHash = {somename: name}
|
45
|
+
# Loba.val :myHash[name] won't work directly, but...
|
46
|
+
Loba.val "myHash[name]".to_sym # will work -- just express the name as a String and cast to a Symbol
|
47
|
+
puts "Hello, #{name}!"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
HelloWorld.new.hello("Charlie")
|
51
|
+
#=> [HelloWorld#hello] myHash[name]: Charlie (in /path/to/file/hello_world.rb:5:in `hello')
|
52
|
+
#=> Hello, Charlie!
|
53
|
+
```
|
54
|
+
|
55
|
+
###### Example 3: Using a non-Symbol as argument without a label
|
56
|
+
```ruby
|
57
|
+
class HelloWorld
|
58
|
+
def hello(name)
|
59
|
+
Loba.val name
|
60
|
+
puts "Hello, #{name}!"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
HelloWorld.new.hello("Charlie")
|
64
|
+
#=> [HelloWorld#hello] Charlie (in /path/to/file/hello_world.rb:3:in `hello')
|
65
|
+
#=> Hello, Charlie!
|
66
|
+
```
|
67
|
+
|
68
|
+
###### Example 4: Using a non-Symbol as argument with a label
|
69
|
+
```ruby
|
70
|
+
class HelloWorld
|
71
|
+
def hello(name)
|
72
|
+
Loba.val name, "Name:"
|
73
|
+
puts "Hello, #{name}!"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
HelloWorld.new.hello("Charlie")
|
77
|
+
#=> [HelloWorld#hello] Name: Charlie (in /path/to/file/hello_world.rb:3:in `hello')
|
78
|
+
#=> Hello, Charlie!
|
79
|
+
```
|
80
|
+
|
81
|
+
##### Notice format:
|
82
|
+
|
83
|
+
The resulting notice output format is
|
84
|
+
|
85
|
+
```
|
86
|
+
[ccccc.mmmmm] vvvvv: rrrrr (in /path/to/code/somecode.rb:LL:in 'some_method')
|
87
|
+
```
|
88
|
+
|
89
|
+
where
|
90
|
+
* `ccccc` is the name of the class from where `Loba.val` was invoked,
|
91
|
+
* `mmmmm` is the name of the method from where `Loba.val` was invoked,
|
92
|
+
* `vvvvv` is generally the name of the variable for which `Loba.val` is inspecting, or any custom label given,
|
93
|
+
* `rrrrr` is the result of inspecting what `Loba.val` was invoked against,
|
94
|
+
* `/path/to/code/somecode.rb` is the source code file that invoked `Loba.val`,
|
95
|
+
* `LL` is the line number of the source code file that invoked `Loba.val`, and
|
96
|
+
* `some_method`is the method in which `Loba.val` was invoked.
|
97
|
+
|
98
|
+
|
99
|
+
Notes:
|
100
|
+
* `ccccc`: Ruby supports anonymous classes (e.g., `= Class.new`). If an anonymous class, "<anonymous class>" will be output here.
|
101
|
+
* `mmmmm`: Ruby supports anonymous methods, procs, and lambdas. If an anonymous method, et al, "<anonymous method>" will be output here.
|
102
|
+
* `vvvvv`: This depends on the argument being provided: if a symbol, then this field will use that symbol to determine the name and present it here. If not, nothing will appear for this field.
|
103
|
+
* `rrrrr`: The value of the variable given to `Loba.val`. `inspect` may be used.
|
data/readme/zulu.png
ADDED
Binary file
|