Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions spec/debug_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ describe Debug do
end
end

it "should be false when ENV debug key is set to value other than '1'" do
it "should be true when ENV debug key is set to other shell-truthy values" do
previous_env = ENV[ENV_DEBUG_KEY]?
begin
ENV[ENV_DEBUG_KEY] = "10"
ENV[ENV_DEBUG_KEY] = "true"
Debug.enabled?.should be_true

ENV[ENV_DEBUG_KEY] = "123"
Debug.enabled?.should be_true
ensure
ENV[ENV_DEBUG_KEY] = previous_env
end
end

it "should be false when ENV debug key is set to value other shell-falsey values" do
previous_env = ENV[ENV_DEBUG_KEY]?
begin
ENV[ENV_DEBUG_KEY] = "false"
Debug.enabled?.should be_false

ENV[ENV_DEBUG_KEY] = ""
Debug.enabled?.should be_false
ensure
ENV[ENV_DEBUG_KEY] = previous_env
Expand Down Expand Up @@ -85,6 +101,11 @@ describe Debug do
end
end

it "prints debug statements at the macro level" do
result = {{ system("./spec/macro_debug.sh").stringify }}
result.should match(/This is a message/)
end

context "with literals" do
it "works with Nil" do
assert_debug nil
Expand Down
3 changes: 3 additions & 0 deletions spec/macro_debug.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "../src/debug"

macro_debug!("This is a message.")
3 changes: 3 additions & 0 deletions spec/macro_debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

crystal run -DDEBUG spec/macro_debug.cr
21 changes: 11 additions & 10 deletions src/debug.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ require "colorize"

module Debug
{% begin %}
ACTIVE = {{ env("DEBUG") == "1" }}
{% end %}
ACTIVE = {{ flag?(:DEBUG) || (env("DEBUG") && env("DEBUG") != "0" && env("DEBUG") != "false" && env("DEBUG") != "") }}

class_property? enabled : Bool?
class_property? enabled : Bool?

def self.enabled? : Bool
case enabled = @@enabled
when Nil
ENV["DEBUG"]? == "1"
else
enabled
def self.enabled? : Bool
case enabled = @@enabled
when Nil
!!({{ flag?(:DEBUG) }} || (ENV["DEBUG"]? && ENV["DEBUG"] != "0" && ENV["DEBUG"] != "false" && ENV["DEBUG"].presence))
else
enabled
end
end
end
{% end %}

macro log(*args,
severity = :debug,
Expand Down Expand Up @@ -119,4 +119,5 @@ module Debug
end
end

require "./macro_debug"
require "./debug/**"
39 changes: 39 additions & 0 deletions src/macro_debug.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Debug
# This constant contains the colors used when highlighting macro
# debugging statements via `mdebug!`. For more information on these
# codes, see this link:
# [https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit)
MDEBUG_COLORS = {
:severity => "82",
:separator => "230",
:file => "8",
:lineno => "8",
:message => "15",
}
end

# This is the main macro level debugging statement. It takes a message, and
# an optional severity level, and outputs, during macro evaluation, the
# debugging statement, highlighted according to the color codes in the
# `Debug::MDEBUG_COLORS` constant.
#
# If debugging is not active (either the `DEBUG` flag is not set, or the
# `DEBUG` environment variable is not set to a truthy value), this macro
# will do nothing.
macro macro_debug!(message,
severity = :debug,
file = __FILE__,
line = __LINE__)
{% if ::Debug::ACTIVE %}
\{%
puts [
"\e[38;5;{{ ::Debug::MDEBUG_COLORS[:severity].id }}m{{ severity.upcase.id }}\e[0m",
"\e[38;5;{{ ::Debug::MDEBUG_COLORS[:separator].id }}m -- \e[0m",
"\e[38;5;{{ ::Debug::MDEBUG_COLORS[:file].id }}m{{ file.id }}\e[0m",
"\e[38;5;{{ ::Debug::MDEBUG_COLORS[:separator].id }}m:\e[0m",
"\e[38;5;{{ ::Debug::MDEBUG_COLORS[:lineno].id }}m{{ line }}\e[0m",
"\e[38;5;{{ ::Debug::MDEBUG_COLORS[:separator].id }}m -- \e[0m",
"\e[38;5;{{ ::Debug::MDEBUG_COLORS[:message].id }}m{{ message.id }}\e[0m"].join("")
%}
{% end %}
end