Skip to content

Commit ecfb8fe

Browse files
authored
Merge pull request #1 from Sija/develop
v1.1
2 parents 1a74d9a + 7d430a3 commit ecfb8fe

File tree

8 files changed

+75
-12
lines changed

8 files changed

+75
-12
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ where you would typically write `puts …` or `pp …`, but with a few extras.
3030
3131
2. Run `shards install`
3232

33+
3. Make sure you compile your program with ENV variable `DEBUG` set to `1`
34+
(for instance `DEBUG=1 shards build`). Otherwise all `debug!(…)` calls
35+
will become a no-op.
36+
37+
4. Once your program is compiled, you need to pass `DEBUG=1` again on the
38+
program start, in order to activate `debug!(…)` logging. Alternatively,
39+
you can call `Debug.enabled = true` within your code to achieve the same
40+
behaviour.
41+
3342
## Usage
3443

3544
```crystal
@@ -61,14 +70,6 @@ The code above produces this output:
6170

6271
## Configuration
6372

64-
- Make sure you compile your program with ENV variable `DEBUG` set to `1`
65-
(for instance `DEBUG=1 shards build`). Otherwise all `debug!(…)` calls
66-
will become a no-op.
67-
- Once your program is compiled, you need to pass `DEBUG=1` again on the
68-
program start, in order to activate `debug!(…)` logging. Alternatively,
69-
you can call `Debug.enabled = true` within your code to achieve the same
70-
behaviour.
71-
7273
You can change the global defaults by calling `Debug.configure` with a block:
7374

7475
```crystal
@@ -85,8 +86,11 @@ global defaults related to the logging itself.
8586

8687
```crystal
8788
Debug::Logger.configure do |settings|
89+
settings.progname = "foo.cr"
90+
8891
settings.show_severity = false
8992
settings.show_datetime = true
93+
settings.show_progname = true
9094
9195
settings.colors[:datetime] = :dark_gray
9296
settings.colors[:progname] = :light_blue

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: debug
2-
version: 1.0.0
2+
version: 1.1.0
33

44
authors:
55
- Sijawusz Pur Rahnama <[email protected]>

spec/debug/to_debug/object.cr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require "../../spec_helper"
2+
3+
class FooToDebug
4+
def initialize(@bar : String); end
5+
end
6+
7+
describe Debug do
8+
context "Object#to_debug" do
9+
it "works for Value-s" do
10+
true.to_debug.should contain "true"
11+
:foo.to_debug.should contain ":foo"
12+
438006.to_debug.should contain "438006"
13+
end
14+
15+
it "works for Reference-s" do
16+
# class name
17+
FooToDebug.new("baz").to_debug.should contain "Foo"
18+
# ivar name
19+
FooToDebug.new("baz").to_debug.should contain "bar"
20+
# ivar value
21+
FooToDebug.new("baz").to_debug.should contain "baz"
22+
end
23+
end
24+
25+
context "Object#to_debug(IO)" do
26+
it "works for Value-s" do
27+
str = String.build do |io|
28+
:foo.to_debug(io).should be_nil
29+
end
30+
str.should contain ":foo"
31+
end
32+
33+
it "works for Reference-s" do
34+
str = String.build do |io|
35+
FooToDebug.new("whatever").to_debug(io)
36+
end
37+
str.should contain "whatever"
38+
end
39+
end
40+
end

spec/debug_spec.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "./spec_helper"
2+
require "./debug/**"
23

34
class Foo
45
end

src/debug.cr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ module Debug
8989
end
9090
end
9191

92+
%str << '\n' if %exp['\n']?
9293
%str << %exp
9394
.colorize(%colors[:expression])
95+
9496
%str << " = "
9597
.colorize(%colors[:decorator])
96-
%val.to_debug(%str)
98+
99+
%val.to_debug.tap do |%pretty_val|
100+
%str << '\n' if %pretty_val['\n']?
101+
%str << %pretty_val
102+
end
97103
%str << " (" << typeof(%val).to_s.colorize(%colors[:type]) << ')'
98104
end
99105

src/debug/logger/settings.cr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
class Debug::Logger
22
class Settings
3+
private module LoggerDelegators
4+
delegate :logger, :logger=, to: Debug
5+
delegate :level, :progname, :progname=, to: :logger
6+
7+
def level=(level : ::Logger::Severity)
8+
logger.level = level
9+
end
10+
end
11+
12+
extend LoggerDelegators
13+
314
class_property? show_severity : Bool = true
415
class_property? show_datetime : Bool = false
516
class_property? show_progname : Bool = true

src/debug/to_debug/object.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Object
66
end
77

88
def to_debug : String
9-
String.build &->to_debug(IO)
9+
# https://github.com/crystal-lang/crystal/issues/8198
10+
String.build { |io| to_debug(io) }
1011
end
1112
end

src/debug/version.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Debug
2-
VERSION = "1.0.0"
2+
VERSION = "1.1.0"
33
end

0 commit comments

Comments
 (0)