loba 1.2.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -2
- data/LICENSE +1 -1
- data/README.md +36 -26
- data/SECURITY.md +25 -0
- data/lib/loba/error.rb +18 -0
- data/lib/loba/internal/platform/formatter.rb +43 -0
- data/lib/loba/internal/platform/within_rails.rb +22 -0
- data/lib/loba/internal/platform.rb +18 -24
- data/lib/loba/internal/settings.rb +143 -0
- data/lib/loba/internal/time_keeper.rb +9 -0
- data/lib/loba/internal/value/value_helper.rb +6 -6
- data/lib/loba/internal/value.rb +3 -3
- data/lib/loba/internal.rb +29 -1
- data/lib/loba/timestamp.rb +80 -0
- data/lib/loba/value.rb +104 -0
- data/lib/loba/version.rb +2 -1
- data/lib/loba.rb +5 -118
- metadata +17 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d53014bd42826b2fee7d16d06ce19a345522ecfd3eb105b5b286f1bd5348e13
|
4
|
+
data.tar.gz: 2d6258dd046d5899469ebdba37606cc4da74b35ae027e87151d237bf83050137
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c109e14fdbf2d6a6271e92f640cf11a214d92e434938509e8f2ad015a2c6286798b22c1cced8defce4f588394e078e098cc4fa4ef3db5ea07f6a3f21f58141c
|
7
|
+
data.tar.gz: 797fef5afe7e868135070abbd950c6981cdcb63720d0891aa377d62f3086b885c0dfdf9d62a5776ef0034e1ad591f4e05c7b042c73392ce2c85f18a6c8bb4d0f
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# Changelog
|
2
|
+
|
2
3
|
All notable changes to this project will be documented in this file.
|
3
4
|
|
4
5
|
The format is based on [Keep a Changelog (1.0.0)](https://keepachangelog.com/en/1.0.0/),
|
@@ -6,6 +7,31 @@ and, as of version 0.3.0 and later, this project adheres to [Semantic Versioning
|
|
6
7
|
|
7
8
|
## [Unreleased]
|
8
9
|
|
10
|
+
## [2.1.0] - 2024-09-09
|
11
|
+
### Added
|
12
|
+
- Added `logger` option for accepting a logger to write to instead of default behavior
|
13
|
+
- Added `logdev` option to direct logging in non-Rails environments
|
14
|
+
- Added `out` option to allow turning off console (`puts`) output
|
15
|
+
|
16
|
+
### Changed
|
17
|
+
- Changed so `log` option will allow logging in non-Rails environments
|
18
|
+
- Changed so `log` option will be defaulted to `true` when `out` options is set to `false`
|
19
|
+
- Updated documentation for new and changed options and to improve completeness
|
20
|
+
- Refreshed gems and dependencies
|
21
|
+
|
22
|
+
## [2.0.0] - 2023-07-07
|
23
|
+
### Added
|
24
|
+
- `Loba.ts`: `log` option to allow logging to Rails.logger (ignored if unavailable)
|
25
|
+
- `Loba.val`: `log` option to allow logging to Rails.logger (ignored if unavailable)
|
26
|
+
- SECURITY.md to specify security policy
|
27
|
+
|
28
|
+
### Changed
|
29
|
+
- Dropped support for Ruby prior to 3.0.6
|
30
|
+
- Changed output to always write to `$stdout` (regardless whether Rails is present)
|
31
|
+
- Changed to only write to Rails.logger (when present) when `log` option is set to `true`
|
32
|
+
- Updated YARD documentation for improved completeness
|
33
|
+
- Refreshed gems and dependencies
|
34
|
+
|
9
35
|
## [1.2.1] - 2021-09-05
|
10
36
|
### Added
|
11
37
|
- Optional specs for developers to check Loba performance (no surprising issues found)
|
@@ -13,7 +39,7 @@ and, as of version 0.3.0 and later, this project adheres to [Semantic Versioning
|
|
13
39
|
### Changed
|
14
40
|
- Updated README reference links
|
15
41
|
- Updated to support Ruby 2.2.2 as minimum (retreated from 2.5 for broader support)
|
16
|
-
- Refactored to
|
42
|
+
- Refactored to separate stripping quotes from .inspect-generated strings
|
17
43
|
|
18
44
|
## [1.2.0] - 2021-09-05 [YANKED]
|
19
45
|
|
@@ -78,7 +104,9 @@ and, as of version 0.3.0 and later, this project adheres to [Semantic Versioning
|
|
78
104
|
### Added
|
79
105
|
- Initial implementation
|
80
106
|
|
81
|
-
[Unreleased]: https://github.com/rdnewman/loba/compare/
|
107
|
+
[Unreleased]: https://github.com/rdnewman/loba/compare/v2.1.0...HEAD
|
108
|
+
[2.1.0]: https://github.com/rdnewman/loba/compare/v2.0.0...v2.1.0
|
109
|
+
[2.0.0]: https://github.com/rdnewman/loba/compare/v1.2.1...v2.0.0
|
82
110
|
[1.2.1]: https://github.com/rdnewman/loba/compare/v1.2.0...v1.2.1
|
83
111
|
[1.2.0]: https://github.com/rdnewman/loba/compare/v1.1.0...v1.2.0
|
84
112
|
[1.1.0]: https://github.com/rdnewman/loba/compare/v1.0.0...v1.1.0
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,13 @@
|
|
1
|
+
# Loba
|
2
|
+
|
1
3
|
[![Gem Version](https://badge.fury.io/rb/loba.svg)](https://badge.fury.io/rb/loba)
|
2
|
-
[![Build Status](https://
|
4
|
+
[![Build Status](https://github.com/rdnewman/loba/actions/workflows/ruby.yml/badge.svg?branch=main)](https://github.com/rdnewman/loba/actions?query=branch%3Amain++)
|
3
5
|
[![Code Climate](https://codeclimate.com/github/rdnewman/loba/badges/gpa.svg)](https://codeclimate.com/github/rdnewman/loba)
|
4
6
|
[![Test Coverage](https://codeclimate.com/github/rdnewman/loba/badges/coverage.svg)](https://codeclimate.com/github/rdnewman/loba/coverage)
|
5
|
-
[![Inline docs](http://inch-ci.org/github/rdnewman/loba.svg?branch=master)](http://inch-ci.org/github/rdnewman/loba)
|
6
|
-
[![security](https://hakiri.io/github/rdnewman/loba/main.svg)](https://hakiri.io/github/rdnewman/loba/main)
|
7
|
-
|
8
|
-
# Loba
|
9
7
|
|
10
8
|
![Loba is "write" in Zulu](readme/zulu.png)
|
11
9
|
|
12
|
-
Easy tracing for debugging: handy methods for adding trace lines to output or
|
10
|
+
Easy tracing for debugging: handy methods for adding trace lines to output (or logs).
|
13
11
|
|
14
12
|
(Installation is pretty much what you'd expect for a gem, but read Environment Notes below first.)
|
15
13
|
|
@@ -17,22 +15,24 @@ Easy tracing for debugging: handy methods for adding trace lines to output or Ra
|
|
17
15
|
|
18
16
|
There are two kinds of questions I usually want to answer when trying to diagnose code behavior:
|
19
17
|
|
20
|
-
1.
|
21
|
-
1.
|
18
|
+
1. Is this spot of code being reached (or is it reached in the order I think it is)?
|
19
|
+
1. What is the value of this variable?
|
22
20
|
|
23
21
|
Loba statements are intended to be terse to minimize typing.
|
24
22
|
|
25
|
-
Loba statements are intended to be minimally invasive and atomic. They should
|
23
|
+
Loba statements are intended to be minimally invasive and atomic. They should have negligible impact over regular `puts` or logging statements.
|
26
24
|
|
27
25
|
Loba statements are expected to be removed when you're done with them. No point in cluttering up production code.
|
28
26
|
|
29
|
-
Loba will
|
27
|
+
Loba will generally always write to `$stdout` (i.e., `puts`). In addition to `$stdout`, Loba can also be made to write to a log (:debug). Writing to `$stdout` can be overridden.
|
28
|
+
|
29
|
+
Loba will work equally well with or without Rails. If Rails is present and writing to a log is wanted, it will use the Rails log (e.g., `Rails.logger.debug`).
|
30
30
|
|
31
31
|
Loba uses the [rainbow gem](https://rubygems.org/gems/rainbow) to help make trace statements more visible.
|
32
32
|
|
33
33
|
## Usage
|
34
34
|
|
35
|
-
|
35
|
+
Aligning Loba statements to the far left in your source code (a la `=begin` or `=end`) will make them easier to see and remove when you're done. You may though find this somewhat annoying to do if your editor auto-indents.
|
36
36
|
|
37
37
|
#### Timestamp notices: `Loba.ts`
|
38
38
|
|
@@ -40,7 +40,7 @@ Outputs a timestamped notice, useful for quick traces to see the code path and e
|
|
40
40
|
|
41
41
|
For example,
|
42
42
|
|
43
|
-
```
|
43
|
+
```text
|
44
44
|
[TIMESTAMP] #=0002, diff=93.478016, at=1451444972.970602 (in=/home/usracct/src/myapp/app/models/target.rb:55:in `some_calculation')
|
45
45
|
```
|
46
46
|
|
@@ -56,7 +56,7 @@ You can read [more detail](readme/ts.md) on this command.
|
|
56
56
|
|
57
57
|
#### Variable or method return inspection: `Loba.val`
|
58
58
|
|
59
|
-
|
59
|
+
Writes line to `$stdout` (or optionally to a log, `logger.debug`) showing value with method and class identification.
|
60
60
|
|
61
61
|
```ruby
|
62
62
|
Loba.val :var_sym # the :var_sym argument is the variable or method name given as a symbol
|
@@ -64,12 +64,22 @@ Loba.val :var_sym # the :var_sym argument is the variable or method name given
|
|
64
64
|
|
65
65
|
For example,
|
66
66
|
|
67
|
-
```
|
67
|
+
```text
|
68
68
|
[Target.some_calculation] my_var: 54 (in /home/usracct/src/myapp/app/models/target.rb:55:in `some_calculation')
|
69
69
|
```
|
70
70
|
|
71
71
|
You can read [more detail](readme/val.md) on this command.
|
72
72
|
|
73
|
+
#### Output and logging options
|
74
|
+
|
75
|
+
There are several options to control where Loba writes its output:
|
76
|
+
* `out`: controls whether `puts` to `$stdout` occurs (default: `true`)
|
77
|
+
* `log`: controls whether logging occurs (default: `false`)
|
78
|
+
* `logger`: controls which logger is used
|
79
|
+
* `logdev`: in non-Rails environments, another way to direct where logging occurs
|
80
|
+
|
81
|
+
You can read [more detail](readme/log.md) about how to use these options.
|
82
|
+
|
73
83
|
#### Snippet example
|
74
84
|
|
75
85
|
```ruby
|
@@ -92,7 +102,7 @@ HelloWorld.new.hello
|
|
92
102
|
|
93
103
|
Output:
|
94
104
|
|
95
|
-
```
|
105
|
+
```text
|
96
106
|
[TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411 (in=/home/usracct/src/lobademo/hello_world.rb:4:in 'initialize'
|
97
107
|
[HelloWorld#hello] @x: 42 (in /home/usracct/src/loba/spec/hello_world.rb:9:in `hello')
|
98
108
|
Hello, Charlie
|
@@ -107,14 +117,14 @@ The expectation is that Loba statements are just for development or test trace s
|
|
107
117
|
|
108
118
|
`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 attempting to render anything to help minimize any impact on production code.
|
109
119
|
|
110
|
-
However, that behavior can be overridden by using the
|
120
|
+
However, that behavior can be overridden by using the option `production: true` as an additional argument to output a notice even when in the production environment. Note also behavior of the `log` option, which defaults to `false` (introduced in v2.0.0). In general, enabling in production should not be done, but we're consenting adults.
|
111
121
|
|
112
|
-
WARNING: this gem depends on the [binding_of_caller gem](https://rubygems.org/gems/binding_of_caller) -- use
|
122
|
+
WARNING: this gem depends on the [binding_of_caller gem](https://rubygems.org/gems/binding_of_caller) -- use `production: true` with their warning in mind:
|
113
123
|
> **Recommended for use only in debugging situations. Do not use this in production apps.**
|
114
124
|
|
115
|
-
These considerations also have an impact on how you install the Loba gem when using `bundler`. If you only install the gem for
|
125
|
+
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 can be a good thing if you never want them left in.
|
116
126
|
|
117
|
-
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
|
127
|
+
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 Loba statements to possibly work in production, then you should install the gem for all environments.
|
118
128
|
|
119
129
|
The following is the code example snippet but always logging even in Rails production environments:
|
120
130
|
|
@@ -122,12 +132,12 @@ The following is the code example snippet but always logging even in Rails produ
|
|
122
132
|
class HelloWorld
|
123
133
|
def initialize
|
124
134
|
@x = 42
|
125
|
-
Loba.ts production: true
|
135
|
+
Loba.ts log: true, production: true
|
126
136
|
@y = "Charlie"
|
127
137
|
end
|
128
138
|
|
129
139
|
def hello
|
130
|
-
Loba.val :@x, production: true
|
140
|
+
Loba.val :@x, log: true, production: true
|
131
141
|
puts "Hello, #{@y}" if @x == 42
|
132
142
|
Loba.ts true
|
133
143
|
end
|
@@ -139,11 +149,10 @@ HelloWorld.new.hello
|
|
139
149
|
|
140
150
|
See above Environment Notes if using with Rails.
|
141
151
|
|
142
|
-
|
143
152
|
Install as below to be generally available (recommended to restrict to only local use):
|
144
153
|
|
145
154
|
```bash
|
146
|
-
|
155
|
+
gem install loba
|
147
156
|
```
|
148
157
|
|
149
158
|
To bundle, add this line to your application's Gemfile:
|
@@ -160,18 +169,19 @@ or for all environments (for example, if `production: true` used):
|
|
160
169
|
gem 'loba', require: false
|
161
170
|
```
|
162
171
|
|
163
|
-
|
164
172
|
And then execute:
|
165
173
|
|
166
174
|
```bash
|
167
|
-
|
175
|
+
bundle install
|
168
176
|
```
|
169
177
|
|
170
178
|
## Development
|
171
179
|
|
172
180
|
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.
|
173
181
|
|
174
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
182
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
183
|
+
|
184
|
+
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).
|
175
185
|
|
176
186
|
## Changelog
|
177
187
|
|
data/SECURITY.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Security Policy
|
2
|
+
|
3
|
+
Loba is intended for diagnostic use in development and test environments.
|
4
|
+
|
5
|
+
Loba assumes consenting adults: it provides an explicit mechanism for temporary use in
|
6
|
+
production environments. Use in production environments is NOT recommended
|
7
|
+
and should be treated as a security vulnerability. Please see the README.md for
|
8
|
+
additional information.
|
9
|
+
|
10
|
+
## Supported Versions
|
11
|
+
|
12
|
+
| Version | Supported |
|
13
|
+
| ------- | ------------------ |
|
14
|
+
| 2.1.x | :white_check_mark: |
|
15
|
+
| 2.0.x | :white_check_mark: |
|
16
|
+
| < 2.0 | :x: |
|
17
|
+
|
18
|
+
## Reporting a Vulnerability
|
19
|
+
|
20
|
+
Please report suspected security vulnerabilities via
|
21
|
+
[Github issues][def].
|
22
|
+
For urgent concerns, write to
|
23
|
+
**[richard@newmanworks.com](mailto:richard@newmanworks.com)**.
|
24
|
+
|
25
|
+
[def]: https://github.com/rdnewman/loba/issues
|
data/lib/loba/error.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Loba
|
2
|
+
# Errors raised by Loba are subclasses of +Loba::Error+.
|
3
|
+
# Rescue this class to rescue any Loba-specific errors.
|
4
|
+
# @api private
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
# Error raised with an invalid logdev is specified
|
8
|
+
# @api private
|
9
|
+
class InvalidLogdevOptionError < Error; end
|
10
|
+
|
11
|
+
# Error raised with an invalid logger is specified
|
12
|
+
# @api private
|
13
|
+
class InvalidLoggerOptionError < Error; end
|
14
|
+
|
15
|
+
# Error raised with an invalid target for #puts is specified
|
16
|
+
# @api private
|
17
|
+
class InvalidOutOptionError < Error; end
|
18
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'English'
|
3
|
+
|
4
|
+
module Loba
|
5
|
+
module Internal
|
6
|
+
module Platform
|
7
|
+
# Custom logging formatter (for when Rails is not involved)
|
8
|
+
class Formatter
|
9
|
+
# Formats supplied message to write to logger.
|
10
|
+
#
|
11
|
+
# This formatter ignores +severity+, +time+, and +progname+ because it enforces
|
12
|
+
# that Loba messages are written out to the log without additional ornamentation.
|
13
|
+
#
|
14
|
+
# @param message [String] Loba content to write out
|
15
|
+
# @return [String] message formatted for writing to log
|
16
|
+
def call(_severity, _time, _progname, message)
|
17
|
+
"#{format(message)}#{$INPUT_RECORD_SEPARATOR}"
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def format(message)
|
23
|
+
case message
|
24
|
+
when ::String
|
25
|
+
message
|
26
|
+
when ::Exception
|
27
|
+
format_exception(message)
|
28
|
+
else
|
29
|
+
message.inspect
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def format_exception(err)
|
34
|
+
backtrace = err.backtrace
|
35
|
+
result = "#{err.message} (#{err.class})"
|
36
|
+
return result if backtrace.to_s.empty?
|
37
|
+
|
38
|
+
result + "\n#{backtrace}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Loba
|
2
|
+
module Internal
|
3
|
+
module Platform
|
4
|
+
# Internal module for considering Rails
|
5
|
+
module WithinRails
|
6
|
+
# Determines if Rails has its logger defined (generally +true+)
|
7
|
+
# @return [boolean] +true+ if +Rails.logger+ is defined
|
8
|
+
def logger?
|
9
|
+
Internal.boolean_cast(defined?(Rails)) && Internal.boolean_cast(Rails.logger)
|
10
|
+
end
|
11
|
+
module_function :logger?
|
12
|
+
|
13
|
+
# Determines if Rails is running in a production environment
|
14
|
+
# @return [boolean] +true+ if +Rails.env+ is +:production+
|
15
|
+
def production?
|
16
|
+
Internal.boolean_cast(defined?(Rails)) && Rails.env.production?
|
17
|
+
end
|
18
|
+
module_function :production?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,31 +1,25 @@
|
|
1
|
+
require_relative 'platform/formatter'
|
2
|
+
require_relative 'platform/within_rails'
|
3
|
+
|
1
4
|
module Loba
|
2
5
|
module Internal
|
3
|
-
# Internal
|
4
|
-
|
6
|
+
# Internal module for managing output and logging across Rails and non-Rails applications
|
7
|
+
module Platform
|
5
8
|
class << self
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
rescue StandardError
|
19
|
-
true # let it attempt to log anyway
|
20
|
-
end
|
21
|
-
end
|
9
|
+
# Provides mechanism for appropriate console output and logging.
|
10
|
+
#
|
11
|
+
# @note To avoid doubled output, if a non-Rails logger is to be logged to and +logdev+ is
|
12
|
+
# set to +$stdout+, then output will be suppressed (i.e., +settings.out+ is +false+).
|
13
|
+
# Doubled output can still occur; in that case, explicitly use +out: false+.
|
14
|
+
#
|
15
|
+
# @param settings [::Loba::Internal::Settings] settings for output control
|
16
|
+
# @return [lambda {|message| ...}] procedure for presenting output. Takes one argument,
|
17
|
+
# +message+ (String), for the output to be written
|
18
|
+
def writer(settings:)
|
19
|
+
lambda do |message|
|
20
|
+
puts(message) if settings.out?
|
22
21
|
|
23
|
-
|
24
|
-
def logger
|
25
|
-
if rails? && Rails.logger.present?
|
26
|
-
->(arg) { Rails.logger.debug arg }
|
27
|
-
else
|
28
|
-
->(arg) { puts arg }
|
22
|
+
settings.logger.debug { message } if settings.log?
|
29
23
|
end
|
30
24
|
end
|
31
25
|
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
module Loba
|
2
|
+
module Internal
|
3
|
+
# Internal class for tracking output and logging settings based on supplied options
|
4
|
+
class Settings
|
5
|
+
# @return [boolean] whether logging is performed
|
6
|
+
attr_reader :log
|
7
|
+
alias log? log
|
8
|
+
|
9
|
+
# @return [Logger] +::Logger+ used for logging; may be +nil+ if not logging
|
10
|
+
attr_reader :logger
|
11
|
+
|
12
|
+
# @return [nil, String, IO, File::NULL]
|
13
|
+
# any custom (overridden) logging device being written to; +nil+ if none specified
|
14
|
+
attr_reader :logdev
|
15
|
+
|
16
|
+
# @return [boolean] +false+ if console output is suppressed; otherwise, +true+
|
17
|
+
attr_reader :out
|
18
|
+
alias out? out
|
19
|
+
|
20
|
+
# @return [boolean]
|
21
|
+
# +true+ if Loba is enabled even within a Rails production environment; otherwise, +false+
|
22
|
+
attr_reader :production
|
23
|
+
alias production? production
|
24
|
+
|
25
|
+
# @param log [boolean]
|
26
|
+
# set to +false+ if no logging is ever wanted
|
27
|
+
# (default when not in Rails and +logger+ is nil);
|
28
|
+
# set to +true+ if logging is always wanted (default when in Rails or
|
29
|
+
# when +logger+ is set or +out+ is false);
|
30
|
+
# @param logger [Logger] override logging with specified Ruby Logger
|
31
|
+
# @param logdev [nil, String, IO, File::NULL]
|
32
|
+
# custom log device to use (when not in Rails); ignored if +logger+ is set;
|
33
|
+
# must be filename or IO object
|
34
|
+
# @param out [boolean]
|
35
|
+
# set to +false+ if console output is to be suppressed
|
36
|
+
# @param production [boolean]
|
37
|
+
# set to +true+ if Loba is to work even within a Rails production environment
|
38
|
+
# @note To avoid doubled output, if a non-Rails logger is to be logged to and +logdev+ is
|
39
|
+
# set to +$stdout+, then output will be suppressed (i.e., +settings.out+ is +false+).
|
40
|
+
# Doubled output can still occur; in that case, explicitly use +out: false+.
|
41
|
+
# @raise [InvalidLoggerOptionError] when an invalid logger is specified
|
42
|
+
# @raise [InvalidLogdevOptionError] when an invalid logdev is specified
|
43
|
+
def initialize(log: nil, logger: nil, logdev: nil, out: true, production: false)
|
44
|
+
@raw_log_argument = log
|
45
|
+
@log = validated_log(log)
|
46
|
+
@out = false
|
47
|
+
@production = validated_production(production)
|
48
|
+
|
49
|
+
return unless enabled?
|
50
|
+
|
51
|
+
@logger = validated_logger(logger)
|
52
|
+
@logdev = validated_logdev(logdev)
|
53
|
+
@out = validated_out(out)
|
54
|
+
|
55
|
+
configure_logging
|
56
|
+
end
|
57
|
+
|
58
|
+
# @return [Boolean] +true+ if Loba is used; otherwise, +false+
|
59
|
+
def enabled?
|
60
|
+
production? || !Internal::Platform::WithinRails.production?
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Boolean] +true+ if Loba is skipped (because of a production environment);
|
64
|
+
# otherwise, +false+
|
65
|
+
def disabled?
|
66
|
+
!enabled?
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
attr_reader :raw_log_argument
|
72
|
+
|
73
|
+
def validated_log(candidate_log)
|
74
|
+
Internal.boolean_cast(candidate_log)
|
75
|
+
end
|
76
|
+
|
77
|
+
def validated_logger(candidate_logger)
|
78
|
+
return if candidate_logger.nil?
|
79
|
+
raise InvalidLoggerOptionError unless candidate_logger.is_a?(::Logger)
|
80
|
+
|
81
|
+
@log = true unless log_explicit?
|
82
|
+
|
83
|
+
candidate_logger
|
84
|
+
end
|
85
|
+
|
86
|
+
def validated_logdev(candidate_logdev)
|
87
|
+
case candidate_logdev
|
88
|
+
when nil, String, IO, StringIO, File::NULL
|
89
|
+
candidate_logdev
|
90
|
+
else
|
91
|
+
raise InvalidLogdevOptionError
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def validated_out(candidate_out)
|
96
|
+
will_output = !logging_to_stdout? && out_param_cast(candidate_out)
|
97
|
+
|
98
|
+
@log = true unless will_output || log_explicit?
|
99
|
+
|
100
|
+
will_output
|
101
|
+
end
|
102
|
+
|
103
|
+
def validated_production(candidate_production)
|
104
|
+
Internal.boolean_cast(candidate_production)
|
105
|
+
end
|
106
|
+
|
107
|
+
def log_explicit?
|
108
|
+
!raw_log_argument.nil?
|
109
|
+
end
|
110
|
+
|
111
|
+
def logging_to_stdout?
|
112
|
+
log? && ((logdev == $stdout) || (logdev == '$stdout'))
|
113
|
+
end
|
114
|
+
|
115
|
+
def configure_logging
|
116
|
+
@log &&= enabled?
|
117
|
+
|
118
|
+
@logger ||= default_logger if log?
|
119
|
+
end
|
120
|
+
|
121
|
+
def default_logger
|
122
|
+
if Internal::Platform::WithinRails.logger?
|
123
|
+
::Rails.logger
|
124
|
+
else
|
125
|
+
::Logger.new((logdev || $stdout), formatter: Internal::Platform::Formatter.new)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def out_param_cast(value)
|
130
|
+
case value
|
131
|
+
when true, TrueClass
|
132
|
+
true
|
133
|
+
when nil, false, FalseClass, File::NULL
|
134
|
+
false
|
135
|
+
when String, Integer, IO, StringIO
|
136
|
+
Internal.boolean_cast(value)
|
137
|
+
else
|
138
|
+
raise InvalidOutOptionError
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -15,6 +15,11 @@ module Loba
|
|
15
15
|
reset!
|
16
16
|
end
|
17
17
|
|
18
|
+
# Increments timestamping, including attributes +timenum+ and +timewas+
|
19
|
+
# @return [Hash] timestamp details
|
20
|
+
# * :number => [Integer] incremented count of pings so far (attribute +timenum+)
|
21
|
+
# * :now => [Time] current date and time
|
22
|
+
# * :change => [Float] difference in seconds from any previous ping or reset
|
18
23
|
def ping
|
19
24
|
@timenum += 1
|
20
25
|
now = Time.now
|
@@ -24,9 +29,13 @@ module Loba
|
|
24
29
|
{ number: @timenum, now: now, change: change }
|
25
30
|
end
|
26
31
|
|
32
|
+
# Resets timestamping
|
33
|
+
# @return [NilClass] nil
|
27
34
|
def reset!
|
28
35
|
@timewas = Time.now
|
29
36
|
@timenum = 0
|
37
|
+
|
38
|
+
nil
|
30
39
|
end
|
31
40
|
end
|
32
41
|
end
|
@@ -30,7 +30,7 @@ module Loba
|
|
30
30
|
# @param argument [Symbol, Object] the value or variable for which information is
|
31
31
|
# to be retrieved
|
32
32
|
# @param inspect [Boolean] when true, force #inspect to be called against
|
33
|
-
#
|
33
|
+
# +argument+ when evaluating
|
34
34
|
# @param depth [Integer] depth in call stack to start evaluation from
|
35
35
|
# @return [String] value representation of argument for display
|
36
36
|
def value(argument:, inspect: true, depth: 0)
|
@@ -46,7 +46,7 @@ module Loba
|
|
46
46
|
|
47
47
|
# Builds a label for display based on the argument when instantiated.
|
48
48
|
# If the argument (when instantiated) is not a symbol, it may not be possible
|
49
|
-
# to infer a label; in that case,
|
49
|
+
# to infer a label; in that case, "[unknown value]"" is returned.
|
50
50
|
#
|
51
51
|
# @param argument [Symbol, Object] the value or variable for which information is
|
52
52
|
# to be retrieved
|
@@ -54,8 +54,8 @@ module Loba
|
|
54
54
|
# and a label can be inferred.
|
55
55
|
# * If any other type, it is assumed to be a literal value to
|
56
56
|
# and a label should be supplied when instantiated.
|
57
|
-
# @param
|
58
|
-
# possible inferred label
|
57
|
+
# @param explicit_label [String] when provided, an explicit label to use; will
|
58
|
+
# override any possible inferred label
|
59
59
|
#
|
60
60
|
# @return [String] label
|
61
61
|
def label(argument:, explicit_label: nil)
|
@@ -73,8 +73,8 @@ module Loba
|
|
73
73
|
# Evaluate an arguments value from where it's bound.
|
74
74
|
# @param argument [Symbol, Object] the value or variable for which information is
|
75
75
|
# to be retrieved
|
76
|
-
# @param inspect [Boolean] when true, force
|
77
|
-
#
|
76
|
+
# @param inspect [Boolean] when true, force +#inspect+ to be called against
|
77
|
+
# +argument+ when evaluating
|
78
78
|
# @param depth [Integer] depth in call stack to start evaluation from
|
79
79
|
# @return [Object] value of the argument when Loba was invoked
|
80
80
|
def evaluate(argument:, inspect: true, depth: 0)
|
data/lib/loba/internal/value.rb
CHANGED
@@ -16,8 +16,8 @@ module Loba
|
|
16
16
|
# and a label should be supplied when instantiated.
|
17
17
|
# @param label [String] when provided, an explicit label to use; will override any
|
18
18
|
# possible inferred label
|
19
|
-
# @param inspect [Boolean] when true, force
|
20
|
-
#
|
19
|
+
# @param inspect [Boolean] when true, force +#inspect+ to be called against
|
20
|
+
# +argument+ when evaluating
|
21
21
|
# @param depth_offset [Integer] depth in call stack to start evaluation from
|
22
22
|
#
|
23
23
|
# @return [Hash] various detail about the value being analyzed
|
@@ -25,7 +25,7 @@ module Loba
|
|
25
25
|
# * :line => [String] code line reference
|
26
26
|
# * :value => [String] value of argument (if nil, '-nil-' is returned)
|
27
27
|
# * :label => [String] label, ending with ":"
|
28
|
-
# (if not possible to infer,
|
28
|
+
# (if not possible to infer, "[unknown value]" is returned)
|
29
29
|
def phrases(argument:, label: nil, inspect: true, depth_offset: 0)
|
30
30
|
depth = depth_offset.nil? ? 0 : depth_offset.to_i
|
31
31
|
{
|
data/lib/loba/internal.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require_relative 'internal/platform'
|
2
|
+
require_relative 'internal/settings'
|
2
3
|
require_relative 'internal/time_keeper'
|
3
4
|
require_relative 'internal/value'
|
4
5
|
|
5
6
|
module Loba
|
7
|
+
# Internal functionality support
|
6
8
|
# @api private
|
7
9
|
module Internal
|
8
10
|
# Remove wrapping quotes on a string (produced by .inspect)
|
9
11
|
#
|
10
|
-
# @param
|
12
|
+
# @param content [String] the string (assumed to be produced from calling .inspect)
|
11
13
|
# to remove quotes (") that wrap a string
|
12
14
|
#
|
13
15
|
# @return [String, Object]
|
@@ -23,5 +25,31 @@ module Loba
|
|
23
25
|
content[1...-1]
|
24
26
|
end
|
25
27
|
module_function :unquote
|
28
|
+
|
29
|
+
# Canonical set of values interpreted to be false.
|
30
|
+
# Adapted from Rails's ActiveModule::Type::Boolean (2024)
|
31
|
+
FALSE_VALUES = [
|
32
|
+
false, 0, nil, '',
|
33
|
+
'0', :'0',
|
34
|
+
'f', :f,
|
35
|
+
'F', :F,
|
36
|
+
'false', :false, # rubocop:disable Lint/BooleanSymbol
|
37
|
+
'FALSE', :FALSE,
|
38
|
+
'off', :off,
|
39
|
+
'OFF', :OFF
|
40
|
+
].to_set.freeze
|
41
|
+
private_constant :FALSE_VALUES
|
42
|
+
|
43
|
+
# Casts any value to true or false (boolean)
|
44
|
+
#
|
45
|
+
# @param value [Object] the value to cast
|
46
|
+
#
|
47
|
+
# @return [true, false]
|
48
|
+
def boolean_cast(value)
|
49
|
+
return false if value.respond_to?(:empty) && value.empty?
|
50
|
+
|
51
|
+
!FALSE_VALUES.include?(value)
|
52
|
+
end
|
53
|
+
module_function :boolean_cast
|
26
54
|
end
|
27
55
|
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Loba # rubocop:disable Style/Documentation
|
2
|
+
# Outputs a timestamped notice, useful for quick traces to see the code path.
|
3
|
+
# Also does a simple elapsed time check since the previous timestamp notice to
|
4
|
+
# help with quick, minimalist profiling.
|
5
|
+
# @param production [boolean]
|
6
|
+
# set to +true+ if this timestamp notice is to be recorded
|
7
|
+
# when running in a Rails production environment
|
8
|
+
# @param log [boolean]
|
9
|
+
# set to +false+ if no logging is ever wanted
|
10
|
+
# (default when not in Rails and +logger+ is nil);
|
11
|
+
# set to +true+ if logging is always wanted (default when in Rails or
|
12
|
+
# when +logger+ is set or +out+ is false);
|
13
|
+
# @param logger [Logger] override logging with specified Ruby Logger
|
14
|
+
# @param logdev [nil, String, IO, File::NULL]
|
15
|
+
# custom log device to use (when not in Rails); ignored if +logger+ is set;
|
16
|
+
# must be filename or IO object
|
17
|
+
# @param out [boolean]
|
18
|
+
# set to +false+ if console output is to be suppressed
|
19
|
+
# @return [NilClass] nil
|
20
|
+
# @example Basic use
|
21
|
+
# def hello
|
22
|
+
# Loba.timestamp
|
23
|
+
# end
|
24
|
+
# #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
|
25
|
+
# @example Forced to output when in production environment
|
26
|
+
# def hello
|
27
|
+
# Loba.ts production: true # Loba.ts is a shorthand alias for Loba.timestamp
|
28
|
+
# end
|
29
|
+
# #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
|
30
|
+
# @example Forced to output to log in addition to $stdout
|
31
|
+
# def hello
|
32
|
+
# Loba.timestamp log: true
|
33
|
+
# end
|
34
|
+
# #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
|
35
|
+
# @note To avoid doubled output, if a non-Rails logger is to be logged to and +logdev+ is
|
36
|
+
# set to +$stdout+, then output will be suppressed (i.e., +settings.out+ is +false+).
|
37
|
+
# Doubled output can still occur; in that case, explicitly use +out: false+.
|
38
|
+
def timestamp( # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
39
|
+
production: false,
|
40
|
+
log: false,
|
41
|
+
logger: nil,
|
42
|
+
logdev: nil,
|
43
|
+
out: true
|
44
|
+
)
|
45
|
+
settings = Internal::Settings.new(
|
46
|
+
log: log, logger: logger, logdev: logdev, out: out, production: production
|
47
|
+
)
|
48
|
+
|
49
|
+
return unless settings.enabled?
|
50
|
+
|
51
|
+
# NOTE: while tempting, memoizing loba_logger can lead to surprises if
|
52
|
+
# Rails presence isn't constant
|
53
|
+
writer = Internal::Platform.writer(settings: settings)
|
54
|
+
|
55
|
+
begin
|
56
|
+
stats = Internal::TimeKeeper.instance.ping
|
57
|
+
writer.call(
|
58
|
+
# 60: light_black / grey
|
59
|
+
"#{Rainbow('[TIMESTAMP]').black.bg(60)}" \
|
60
|
+
"#{Rainbow(' #=').yellow.bg(:default)}" \
|
61
|
+
"#{format('%04d', stats[:number])}" \
|
62
|
+
"#{Rainbow(', diff=').yellow}" \
|
63
|
+
"#{format('%.6f', stats[:change])}" \
|
64
|
+
"#{Rainbow(', at=').yellow}" \
|
65
|
+
"#{format('%.6f', stats[:now].round(6).to_f)}" \
|
66
|
+
"#{Rainbow(" \t(in #{caller(1..1).first})").color(60)}" # warning: nested interpolation
|
67
|
+
)
|
68
|
+
rescue StandardError => e
|
69
|
+
writer.call Rainbow("[TIMESTAMP] #=FAIL, in=#{caller(1..1).first}, err=#{e}").red
|
70
|
+
end
|
71
|
+
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
module_function :timestamp
|
75
|
+
|
76
|
+
# Shorthand alias for Loba.timestamp.
|
77
|
+
# @!method ts(production: false, log: false, logger: nil, logdev: nil, out: true)
|
78
|
+
alias ts timestamp
|
79
|
+
module_function :ts
|
80
|
+
end
|
data/lib/loba/value.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module Loba # rubocop:disable Style/Documentation
|
2
|
+
# Outputs a value notice showing value of provided argument including method and
|
3
|
+
# class identification.
|
4
|
+
# @param argument [various] (required) the value to be evaluated and shown; if given as
|
5
|
+
# a +Symbol+, a label based on the argument will proceed the value the argument refers to
|
6
|
+
# @param label [String] explicit label to be used instead of attempting
|
7
|
+
# to infer from the argument; default is to attempt to infer a label from the argument
|
8
|
+
# @param inspect [boolean] +true+ if this value notice is to use +#inspect+ against the
|
9
|
+
# content being evaluated; otherwise, +false+
|
10
|
+
# @param production [boolean]
|
11
|
+
# set to +true+ if the value notice is to be recorded
|
12
|
+
# when running in a Rails production environment
|
13
|
+
# @param log [boolean]
|
14
|
+
# set to +false+ if no logging is ever wanted
|
15
|
+
# (default when not in Rails and +logger+ is nil);
|
16
|
+
# set to +true+ if logging is always wanted (default when in Rails or
|
17
|
+
# when +logger+ is set or +out+ is false);
|
18
|
+
# @param logger [Logger] override logging with specified Ruby Logger
|
19
|
+
# @param logdev [nil, String, IO, File::NULL]
|
20
|
+
# custom log device to use (when not in Rails); ignored if +logger+ is set;
|
21
|
+
# must be filename or IO object
|
22
|
+
# @param out [boolean]
|
23
|
+
# set to +false+ if console output is to be suppressed
|
24
|
+
# @return [NilClass] nil
|
25
|
+
# @example Using Symbol as argument
|
26
|
+
# class HelloWorld
|
27
|
+
# def hello(name)
|
28
|
+
# Loba.value :name # putting Loba statement to far left helps remember to remove later
|
29
|
+
# puts "Hello, #{name}!"
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
# HelloWorld.new.hello("Charlie")
|
33
|
+
# #=> [HelloWorld#hello] name: Charlie (at /path/to/file/hello_world.rb:3:in 'hello')
|
34
|
+
# #=> Hello, Charlie!
|
35
|
+
# @example Using non-Symbol as argument
|
36
|
+
# class HelloWorld
|
37
|
+
# def hello(name)
|
38
|
+
# Loba.val name # Loba.val is a shorthand alias for Loba.value
|
39
|
+
# puts "Hello, #{name}!"
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
# HelloWorld.new.hello("Charlie")
|
43
|
+
# #=> [HelloWorld#hello] Charlie (at /path/to/file/hello_world.rb:3:in 'hello')
|
44
|
+
# #=> Hello, Charlie!
|
45
|
+
# @example Using non-Symbol as argument with a label
|
46
|
+
# class HelloWorld
|
47
|
+
# def hello(name)
|
48
|
+
# Loba.value name, "Name:"
|
49
|
+
# puts "Hello, #{name}!"
|
50
|
+
# end
|
51
|
+
# end
|
52
|
+
# HelloWorld.new.hello("Charlie")
|
53
|
+
# #=> [HelloWorld#hello] Name: Charlie (at /path/to/file/hello_world.rb:3:in 'hello')
|
54
|
+
# #=> Hello, Charlie!
|
55
|
+
# @note To avoid doubled output, if a non-Rails logger is to be logged to and +logdev+ is
|
56
|
+
# set to +$stdout+, then output will be suppressed (i.e., +settings.out+ is +false+).
|
57
|
+
# Doubled output can still occur; in that case, explicitly use +out: false+.
|
58
|
+
def value( # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/ParameterLists
|
59
|
+
argument,
|
60
|
+
label: nil,
|
61
|
+
inspect: true,
|
62
|
+
production: false,
|
63
|
+
log: false,
|
64
|
+
logger: nil,
|
65
|
+
logdev: nil,
|
66
|
+
out: true
|
67
|
+
)
|
68
|
+
settings = Internal::Settings.new(
|
69
|
+
log: log, logger: logger, logdev: logdev, out: out, production: production
|
70
|
+
)
|
71
|
+
|
72
|
+
return unless settings.enabled?
|
73
|
+
|
74
|
+
text = Internal::Value.phrases(
|
75
|
+
argument: (argument.nil? ? :nil : argument),
|
76
|
+
label: label,
|
77
|
+
inspect: inspect,
|
78
|
+
depth_offset: 1
|
79
|
+
)
|
80
|
+
|
81
|
+
Internal::Platform.writer(settings: settings).call(
|
82
|
+
# NOTE: while tempting, memoizing Internal::Platform.logger can lead to surprises
|
83
|
+
# if Rails presence isn't constant
|
84
|
+
#
|
85
|
+
# warning: nested interpolation below (slight help to performance)
|
86
|
+
# 60: light_black
|
87
|
+
# 62: light_green
|
88
|
+
"#{Rainbow("#{text[:tag]} ").green.bg(:default)}" \
|
89
|
+
"#{Rainbow("#{text[:label]} ").color(62)}" \
|
90
|
+
"#{text[:value]}" \
|
91
|
+
"#{Rainbow(" \t(in #{text[:line]})").color(60)}"
|
92
|
+
)
|
93
|
+
|
94
|
+
nil
|
95
|
+
end
|
96
|
+
module_function :value
|
97
|
+
|
98
|
+
# rubocop:disable Layout/LineLength
|
99
|
+
# Shorthand alias for Loba.value.
|
100
|
+
# @!method val(argument, label: nil, inspect: true, production: false, log: false, logger: nil, logdev:nil, out: true)
|
101
|
+
alias val value
|
102
|
+
module_function :val
|
103
|
+
# rubocop:enable Layout/LineLength
|
104
|
+
end
|
data/lib/loba/version.rb
CHANGED
data/lib/loba.rb
CHANGED
@@ -1,127 +1,14 @@
|
|
1
1
|
require 'loba/version'
|
2
|
+
require 'loba/error'
|
2
3
|
require 'loba/internal'
|
3
4
|
|
4
5
|
require 'binding_of_caller'
|
5
6
|
require 'rainbow'
|
6
7
|
|
8
|
+
require 'loba/timestamp'
|
9
|
+
require 'loba/value'
|
10
|
+
|
7
11
|
# Loba module for quick tracing of Ruby and Rails.
|
8
|
-
#
|
9
|
-
# If not a Rails application, will use STDOUT.
|
12
|
+
# Will write to $stdout (generally) and, optionally, a log.
|
10
13
|
module Loba
|
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
|
13
|
-
# help with quick, minimalist profiling.
|
14
|
-
# @param production [Boolean] set to true if this timestamp notice is
|
15
|
-
# to be recorded when running in :production environment
|
16
|
-
# @return [NilClass] nil
|
17
|
-
# @example Basic use
|
18
|
-
# def hello
|
19
|
-
# Loba.timestamp
|
20
|
-
# end
|
21
|
-
# #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
|
22
|
-
# @example Forced to output when in production environment
|
23
|
-
# def hello
|
24
|
-
# Loba.ts production: true # Loba.ts is a shorthand alias for Loba.timestamp
|
25
|
-
# end
|
26
|
-
# #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
|
27
|
-
def timestamp(production: false)
|
28
|
-
return unless Internal::Platform.logging_ok?(production)
|
29
|
-
|
30
|
-
# produce timestamp notice
|
31
|
-
@loba_logger ||= Internal::Platform.logger
|
32
|
-
|
33
|
-
begin
|
34
|
-
stats = Internal::TimeKeeper.instance.ping
|
35
|
-
@loba_logger.call(
|
36
|
-
# 60: light_black / grey
|
37
|
-
"#{Rainbow('[TIMESTAMP]').black.bg(60)}" \
|
38
|
-
"#{Rainbow(' #=').yellow.bg(:default)}" \
|
39
|
-
"#{format('%04d', stats[:number])}" \
|
40
|
-
"#{Rainbow(', diff=').yellow}" \
|
41
|
-
"#{format('%.6f', stats[:change])}" \
|
42
|
-
"#{Rainbow(', at=').yellow}" \
|
43
|
-
"#{format('%.6f', stats[:now].round(6).to_f)}" \
|
44
|
-
"#{Rainbow(" \t(in #{caller(1..1).first})").color(60)}" # warning: nested interpolation
|
45
|
-
)
|
46
|
-
rescue StandardError => e
|
47
|
-
@loba_logger.call Rainbow("[TIMESTAMP] #=FAIL, in=#{caller(1..1).first}, err=#{e}").red
|
48
|
-
end
|
49
|
-
|
50
|
-
nil
|
51
|
-
end
|
52
|
-
module_function :timestamp
|
53
|
-
|
54
|
-
# Shorthand alias for Loba.timestamp.
|
55
|
-
alias ts timestamp
|
56
|
-
module_function :ts
|
57
|
-
|
58
|
-
# Outputs a value notice showing value of provided argument including method and
|
59
|
-
# class identification.
|
60
|
-
# @param argument [various] (required) the value to be evaluated and shown; if given as
|
61
|
-
# a Symbol, a label based on the argument will proceed the value the argument refers to
|
62
|
-
# @param label [String] explicit label to be used instead of attempting
|
63
|
-
# to infer from the argument; default is to attempt to infer a label from the argument
|
64
|
-
# @param inspect [Boolean] true if this value notice is to use #inspect against the
|
65
|
-
# content being evaluated; otherwise, false
|
66
|
-
# @param production [Boolean] set to true if this timestamp notice is
|
67
|
-
# to be recorded when running in :production environment
|
68
|
-
# @return [NilClass] nil
|
69
|
-
# @example Using Symbol as argument
|
70
|
-
# class HelloWorld
|
71
|
-
# def hello(name)
|
72
|
-
# Loba.value :name # putting Loba statement to far left helps remember to remove later
|
73
|
-
# puts "Hello, #{name}!"
|
74
|
-
# end
|
75
|
-
# end
|
76
|
-
# HelloWorld.new.hello("Charlie")
|
77
|
-
# #=> [HelloWorld#hello] name: Charlie (at /path/to/file/hello_world.rb:3:in `hello')
|
78
|
-
# #=> Hello, Charlie!
|
79
|
-
# @example Using non-Symbol as argument
|
80
|
-
# class HelloWorld
|
81
|
-
# def hello(name)
|
82
|
-
# Loba.val name # Loba.val is a shorthand alias for Loba.value
|
83
|
-
# puts "Hello, #{name}!"
|
84
|
-
# end
|
85
|
-
# end
|
86
|
-
# HelloWorld.new.hello("Charlie")
|
87
|
-
# #=> [HelloWorld#hello] Charlie (at /path/to/file/hello_world.rb:3:in `hello')
|
88
|
-
# #=> Hello, Charlie!
|
89
|
-
# @example Using non-Symbol as argument with a label
|
90
|
-
# class HelloWorld
|
91
|
-
# def hello(name)
|
92
|
-
# Loba.value name, "Name:"
|
93
|
-
# puts "Hello, #{name}!"
|
94
|
-
# end
|
95
|
-
# end
|
96
|
-
# HelloWorld.new.hello("Charlie")
|
97
|
-
# #=> [HelloWorld#hello] Name: Charlie (at /path/to/file/hello_world.rb:3:in `hello')
|
98
|
-
# #=> Hello, Charlie!
|
99
|
-
def value(argument, label: nil, inspect: true, production: false)
|
100
|
-
return nil unless Internal::Platform.logging_ok?(production)
|
101
|
-
|
102
|
-
@loba_logger ||= Internal::Platform.logger
|
103
|
-
|
104
|
-
text = Internal::Value.phrases(
|
105
|
-
argument: (argument.nil? ? :nil : argument),
|
106
|
-
label: label,
|
107
|
-
inspect: inspect,
|
108
|
-
depth_offset: 1
|
109
|
-
)
|
110
|
-
@loba_logger.call(
|
111
|
-
# warning: nested interpolation below (slight help to performance)
|
112
|
-
# 60: light_black
|
113
|
-
# 62: light_green
|
114
|
-
"#{Rainbow("#{text[:tag]} ").green.bg(:default)}" \
|
115
|
-
"#{Rainbow("#{text[:label]} ").color(62)}" \
|
116
|
-
"#{text[:value]}" \
|
117
|
-
"#{Rainbow(" \t(in #{text[:line]})").color(60)}"
|
118
|
-
)
|
119
|
-
|
120
|
-
nil
|
121
|
-
end
|
122
|
-
module_function :value
|
123
|
-
|
124
|
-
# Shorthand alias for Loba.value.
|
125
|
-
alias val value
|
126
|
-
module_function :val
|
127
14
|
end
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loba
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Newman
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '2.2'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '2.2'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: binding_of_caller
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,14 +30,14 @@ dependencies:
|
|
44
30
|
requirements:
|
45
31
|
- - "~>"
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
33
|
+
version: '3.1'
|
48
34
|
type: :runtime
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
38
|
- - "~>"
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
40
|
+
version: '3.1'
|
55
41
|
description: Handy methods for adding trace lines to output or Rails logs.
|
56
42
|
email:
|
57
43
|
- richard@newmanworks.com
|
@@ -63,12 +49,19 @@ files:
|
|
63
49
|
- CODE_OF_CONDUCT.md
|
64
50
|
- LICENSE
|
65
51
|
- README.md
|
52
|
+
- SECURITY.md
|
66
53
|
- lib/loba.rb
|
54
|
+
- lib/loba/error.rb
|
67
55
|
- lib/loba/internal.rb
|
68
56
|
- lib/loba/internal/platform.rb
|
57
|
+
- lib/loba/internal/platform/formatter.rb
|
58
|
+
- lib/loba/internal/platform/within_rails.rb
|
59
|
+
- lib/loba/internal/settings.rb
|
69
60
|
- lib/loba/internal/time_keeper.rb
|
70
61
|
- lib/loba/internal/value.rb
|
71
62
|
- lib/loba/internal/value/value_helper.rb
|
63
|
+
- lib/loba/timestamp.rb
|
64
|
+
- lib/loba/value.rb
|
72
65
|
- lib/loba/version.rb
|
73
66
|
homepage: https://github.com/rdnewman/loba
|
74
67
|
licenses:
|
@@ -78,7 +71,8 @@ metadata:
|
|
78
71
|
bug_tracker_uri: https://github.com/rdnewman/loba/issues
|
79
72
|
changelog_uri: https://github.com/rdnewman/loba/blob/main/CHANGELOG.md
|
80
73
|
documentation_uri: https://www.rubydoc.info/gems/loba
|
81
|
-
|
74
|
+
rubygems_mfa_required: 'true'
|
75
|
+
post_install_message:
|
82
76
|
rdoc_options: []
|
83
77
|
require_paths:
|
84
78
|
- lib
|
@@ -86,15 +80,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
80
|
requirements:
|
87
81
|
- - ">="
|
88
82
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
83
|
+
version: 3.0.6
|
90
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
85
|
requirements:
|
92
86
|
- - ">="
|
93
87
|
- !ruby/object:Gem::Version
|
94
88
|
version: '0'
|
95
89
|
requirements: []
|
96
|
-
rubygems_version: 3.
|
97
|
-
signing_key:
|
90
|
+
rubygems_version: 3.5.11
|
91
|
+
signing_key:
|
98
92
|
specification_version: 4
|
99
93
|
summary: 'Loba: Easy tracing for debugging.'
|
100
94
|
test_files: []
|