motion-lager 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +44 -0
- data/lib/motion-lager.rb +10 -0
- data/lib/project/lager.rb +122 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 66099ea7a5394f43442c51495fe5571393791238df233496cb92e76d2c0f3217
|
4
|
+
data.tar.gz: 643191e0517541105c8a9246858263666d50cb6f60f73347172d2b92b156cbd8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e7a4baa39337170f70ce903b186d517efd31c0654caae245f4e011e7d845d15e49a723f0af67971bb3b3911cd83a6fac202ce077cfcad25de1392115da5bc003
|
7
|
+
data.tar.gz: 3ab109f247ce0e258e28c8716d4c56b976302a528235b76edde96030946a1fb83927ed007468b9d6a6473f5623b876a6dd02b9deb1d5045df5b8f29482544d55
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Motion::Lager 🍺
|
2
|
+
|
3
|
+
Full featured logger for use in RubyMotion apps.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'motion-lager'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
1. Initialize an instance of the logger. Tip: assign it to a global (like `$log`) for easy access anywhere in your app.
|
18
|
+
```ruby
|
19
|
+
logger = Motion::Lager.new(level: 'debug') # default
|
20
|
+
```
|
21
|
+
|
22
|
+
2. Next, call a log level method and pass the message or object that you'd like to log. Log level methods are: `debug`, `info` (or `log`), `warn`, and `error`. You can also specify the name of a color if you'd like to override the default text color for that log level. You can also specify the background color as the third argument.
|
23
|
+
```ruby
|
24
|
+
logger.debug 'My debug message'
|
25
|
+
logger.debug my_object # pretty print objects
|
26
|
+
logger.debug 'Success!', :green
|
27
|
+
logger.warn 'Warning: Black on Yellow!', :black, :yellow
|
28
|
+
```
|
29
|
+
|
30
|
+
### Color Options
|
31
|
+
|
32
|
+
![Color Options](colors.png)
|
33
|
+
|
34
|
+
Note: Actual color may vary depending on console preferences. Example from Terminal app:
|
35
|
+
|
36
|
+
![Example of Terminal Color Preferences](color_prefs.png)
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
data/lib/motion-lager.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
unless defined?(Motion::Project::Config)
|
4
|
+
raise "This file must be required within a RubyMotion project Rakefile."
|
5
|
+
end
|
6
|
+
|
7
|
+
lib_dir_path = File.dirname(File.expand_path(__FILE__))
|
8
|
+
Motion::Project::App.setup do |app|
|
9
|
+
app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")).sort)
|
10
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module Motion
|
2
|
+
class Lager
|
3
|
+
COLORS = {
|
4
|
+
default: { fg: 39, bg: 49 },
|
5
|
+
black: { fg: 30, bg: 40 },
|
6
|
+
red: { fg: 31, bg: 41 },
|
7
|
+
green: { fg: 32, bg: 42 },
|
8
|
+
yellow: { fg: 33, bg: 43 },
|
9
|
+
blue: { fg: 34, bg: 44 },
|
10
|
+
magenta: { fg: 35, bg: 45 },
|
11
|
+
cyan: { fg: 36, bg: 46 },
|
12
|
+
white: { fg: 37, bg: 47 },
|
13
|
+
bright_black: { fg: 90, bg: 100 },
|
14
|
+
bright_red: { fg: 91, bg: 101 },
|
15
|
+
bright_green: { fg: 92, bg: 102 },
|
16
|
+
bright_yellow: { fg: 93, bg: 103 },
|
17
|
+
bright_blue: { fg: 94, bg: 104 },
|
18
|
+
bright_magenta: { fg: 95, bg: 105 },
|
19
|
+
bright_cyan: { fg: 96, bg: 106 },
|
20
|
+
bright_white: { fg: 97, bg: 107 },
|
21
|
+
}
|
22
|
+
# Aliases
|
23
|
+
COLORS[:gray] = COLORS[:bright_black]
|
24
|
+
COLORS[:light_blue] = COLORS[:bright_cyan]
|
25
|
+
|
26
|
+
def initialize(config = {})
|
27
|
+
@config = {}
|
28
|
+
@config[:level] = config.fetch(:level, :debug).to_sym
|
29
|
+
end
|
30
|
+
|
31
|
+
def debug(obj, color = :gray, bg_color = :default)
|
32
|
+
log_with_level(:debug, obj, color, bg_color)
|
33
|
+
end
|
34
|
+
|
35
|
+
def info(obj, color = :default, bg_color = :default)
|
36
|
+
log_with_level(:info, obj, color, bg_color)
|
37
|
+
end
|
38
|
+
alias_method :log, :info
|
39
|
+
|
40
|
+
def warn(obj, color = :yellow, bg_color = :default)
|
41
|
+
log_with_level(:warn, obj, color, bg_color)
|
42
|
+
end
|
43
|
+
|
44
|
+
def error(obj, color = :red, bg_color = :default)
|
45
|
+
log_with_level(:error, obj, color, bg_color)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def log_with_level(level, obj, color = :white, bg_color = :default)
|
51
|
+
return unless enabled?(level)
|
52
|
+
if obj.is_a? String
|
53
|
+
str = obj
|
54
|
+
else
|
55
|
+
str = inspect_object(obj)
|
56
|
+
end
|
57
|
+
puts str.to_s.split("\n").map {|line| colorize(line, color, bg_color) }.join("\n")
|
58
|
+
end
|
59
|
+
|
60
|
+
def enabled?(level)
|
61
|
+
case @config[:level]
|
62
|
+
when :debug
|
63
|
+
return true
|
64
|
+
when :info
|
65
|
+
return level != :debug
|
66
|
+
when :warn
|
67
|
+
return level != :debug && level != :info
|
68
|
+
when :error
|
69
|
+
return level == :error
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def colorize(str, fg_color, bg_color = :default)
|
74
|
+
return str unless defined?(MotionRepl) # only colorize when displaying in REPL
|
75
|
+
"\e[#{COLORS[fg_color][:fg]};#{COLORS[bg_color][:bg]}m#{str}\e[0m"
|
76
|
+
end
|
77
|
+
|
78
|
+
def inspect_object(obj)
|
79
|
+
case obj
|
80
|
+
when Hash then inspect_hash(obj)
|
81
|
+
when NSError then inspect_nserror(obj)
|
82
|
+
when NSURL then obj.to_s
|
83
|
+
else
|
84
|
+
obj.inspect
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def inspect_hash(hash)
|
89
|
+
message = "{"
|
90
|
+
hash.each do |k, v|
|
91
|
+
value_lines = []
|
92
|
+
inspect_object(v).lines.each_with_index do |line, i|
|
93
|
+
line = " #{line}" unless i == 0
|
94
|
+
value_lines << line
|
95
|
+
end
|
96
|
+
message += "\n #{inspect_object(k)} => #{value_lines.join},"
|
97
|
+
end
|
98
|
+
message += "\n}"
|
99
|
+
end
|
100
|
+
|
101
|
+
def inspect_nserror(err)
|
102
|
+
return "NSError was actually #{err.inspect}" unless err
|
103
|
+
|
104
|
+
message = "<#{err.class}" \
|
105
|
+
"\n Domain: #{err.domain}" \
|
106
|
+
"\n Code: #{err.code}" \
|
107
|
+
"\n Description: #{err.localizedDescription}"
|
108
|
+
user_info_lines = []
|
109
|
+
inspect_object(err.userInfo).lines.each_with_index do |line, i|
|
110
|
+
line = " #{line}" unless i == 0
|
111
|
+
user_info_lines << line
|
112
|
+
end
|
113
|
+
message +=
|
114
|
+
"\n UserInfo: #{user_info_lines.join}" \
|
115
|
+
"\n RecoveryOptions: #{err.localizedRecoveryOptions.inspect}" \
|
116
|
+
"\n RecoverySuggestion: #{err.localizedRecoverySuggestion.inspect}" \
|
117
|
+
"\n FailureReason: #{err.localizedFailureReason.inspect}" \
|
118
|
+
">"
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: motion-lager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Havens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Full featured logger for use in RubyMotion apps.
|
28
|
+
email:
|
29
|
+
- email@andrewhavens.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- README.md
|
35
|
+
- lib/motion-lager.rb
|
36
|
+
- lib/project/lager.rb
|
37
|
+
homepage: https://github.com/rubymotion-community/motion-lager
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.0.6
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Full featured logger for use in RubyMotion apps.
|
60
|
+
test_files: []
|