From d9c18aeb8164248ccadc726d92ea94e47638fb4d Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 9 Sep 2019 15:32:14 +0200 Subject: [PATCH 1/5] Support setting Logger#{level,progname} properties through Logger.settings --- README.md | 3 +++ src/debug/logger/settings.cr | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/README.md b/README.md index 437c55f..68e15e5 100644 --- a/README.md +++ b/README.md @@ -85,8 +85,11 @@ global defaults related to the logging itself. ```crystal Debug::Logger.configure do |settings| + settings.progname = "foo.cr" + settings.show_severity = false settings.show_datetime = true + settings.show_progname = true settings.colors[:datetime] = :dark_gray settings.colors[:progname] = :light_blue diff --git a/src/debug/logger/settings.cr b/src/debug/logger/settings.cr index a876e46..dcd242e 100644 --- a/src/debug/logger/settings.cr +++ b/src/debug/logger/settings.cr @@ -1,5 +1,16 @@ class Debug::Logger class Settings + private module LoggerDelegators + delegate :logger, :logger=, to: Debug + delegate :level, :progname, :progname=, to: :logger + + def level=(level : ::Logger::Severity) + logger.level = level + end + end + + extend LoggerDelegators + class_property? show_severity : Bool = true class_property? show_datetime : Bool = false class_property? show_progname : Bool = true From dee691f62a4a9ef76a10ef31977244b800bf2447 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 9 Sep 2019 15:44:01 +0200 Subject: [PATCH 2/5] Reorganize README instructions a bit --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 68e15e5..71b8d6c 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,15 @@ where you would typically write `puts …` or `pp …`, but with a few extras. 2. Run `shards install` +3. Make sure you compile your program with ENV variable `DEBUG` set to `1` + (for instance `DEBUG=1 shards build`). Otherwise all `debug!(…)` calls + will become a no-op. + +4. Once your program is compiled, you need to pass `DEBUG=1` again on the + program start, in order to activate `debug!(…)` logging. Alternatively, + you can call `Debug.enabled = true` within your code to achieve the same + behaviour. + ## Usage ```crystal @@ -61,14 +70,6 @@ The code above produces this output: ## Configuration -- Make sure you compile your program with ENV variable `DEBUG` set to `1` - (for instance `DEBUG=1 shards build`). Otherwise all `debug!(…)` calls - will become a no-op. -- Once your program is compiled, you need to pass `DEBUG=1` again on the - program start, in order to activate `debug!(…)` logging. Alternatively, - you can call `Debug.enabled = true` within your code to achieve the same - behaviour. - You can change the global defaults by calling `Debug.configure` with a block: ```crystal From d02217b4cfcac864d8b9d29e76b6cdc6ab131938 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Thu, 19 Sep 2019 00:15:13 +0200 Subject: [PATCH 3/5] Print multiline output with extra newline for readability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There’s a downside tho’ - we allocate another `String::Builder` just to check for existence of `\n` character… --- src/debug.cr | 8 +++++++- src/debug/to_debug/object.cr | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/debug.cr b/src/debug.cr index b0f787b..607d72a 100644 --- a/src/debug.cr +++ b/src/debug.cr @@ -89,11 +89,17 @@ module Debug end end + %str << '\n' if %exp['\n']? %str << %exp .colorize(%colors[:expression]) + %str << " = " .colorize(%colors[:decorator]) - %val.to_debug(%str) + + %val.to_debug.tap do |%pretty_val| + %str << '\n' if %pretty_val['\n']? + %str << %pretty_val + end %str << " (" << typeof(%val).to_s.colorize(%colors[:type]) << ')' end diff --git a/src/debug/to_debug/object.cr b/src/debug/to_debug/object.cr index 5215162..0fc7d87 100644 --- a/src/debug/to_debug/object.cr +++ b/src/debug/to_debug/object.cr @@ -6,6 +6,7 @@ class Object end def to_debug : String - String.build &->to_debug(IO) + # https://github.com/crystal-lang/crystal/issues/8198 + String.build { |io| to_debug(io) } end end From 729efcf944aaaefc1849313aa54748bf12172939 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Thu, 19 Sep 2019 23:22:17 +0200 Subject: [PATCH 4/5] Add specs covering Object#to_debug extensions --- spec/debug/to_debug/object.cr | 40 +++++++++++++++++++++++++++++++++++ spec/debug_spec.cr | 1 + 2 files changed, 41 insertions(+) create mode 100644 spec/debug/to_debug/object.cr diff --git a/spec/debug/to_debug/object.cr b/spec/debug/to_debug/object.cr new file mode 100644 index 0000000..5e32bb1 --- /dev/null +++ b/spec/debug/to_debug/object.cr @@ -0,0 +1,40 @@ +require "../../spec_helper" + +class FooToDebug + def initialize(@bar : String); end +end + +describe Debug do + context "Object#to_debug" do + it "works for Value-s" do + true.to_debug.should contain "true" + :foo.to_debug.should contain ":foo" + 438006.to_debug.should contain "438006" + end + + it "works for Reference-s" do + # class name + FooToDebug.new("baz").to_debug.should contain "Foo" + # ivar name + FooToDebug.new("baz").to_debug.should contain "bar" + # ivar value + FooToDebug.new("baz").to_debug.should contain "baz" + end + end + + context "Object#to_debug(IO)" do + it "works for Value-s" do + str = String.build do |io| + :foo.to_debug(io).should be_nil + end + str.should contain ":foo" + end + + it "works for Reference-s" do + str = String.build do |io| + FooToDebug.new("whatever").to_debug(io) + end + str.should contain "whatever" + end + end +end diff --git a/spec/debug_spec.cr b/spec/debug_spec.cr index 67b2854..b704ead 100644 --- a/spec/debug_spec.cr +++ b/spec/debug_spec.cr @@ -1,4 +1,5 @@ require "./spec_helper" +require "./debug/**" class Foo end From 7d430a3c6743bc09235fb6206ffae9a194ed235d Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Thu, 19 Sep 2019 23:37:11 +0200 Subject: [PATCH 5/5] Bump version to 1.1.0 --- shard.yml | 2 +- src/debug/version.cr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shard.yml b/shard.yml index 0680388..4a915c1 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: debug -version: 1.0.0 +version: 1.1.0 authors: - Sijawusz Pur Rahnama diff --git a/src/debug/version.cr b/src/debug/version.cr index 5436fe6..18dc3bb 100644 --- a/src/debug/version.cr +++ b/src/debug/version.cr @@ -1,3 +1,3 @@ module Debug - VERSION = "1.0.0" + VERSION = "1.1.0" end