Module: Dlog

Defined in:
lib/dlog.rb

Defined Under Namespace

Modules: Benchmark, Benchslow, Color, Formatter, NoBenchmark, Nolog, StderrFormatter

Constant Summary collapse

ROOT =

The applications ROOT dir, to shorten the source line

if defined?(RAILS_ROOT)
  RAILS_ROOT
else
  File.expand_path(Dir.getwd) 
end
HOME =

The user’s HOME dir, to shorten the source line

ENV["HOME"] + "/"
@@mode =

– dlog modi ——————————————————

:debug

Class Method Summary collapse

Class Method Details

.debug(*args) ⇒ Object



181
182
183
# File 'lib/dlog.rb', line 181

def self.debug(*args)
  log :debug, args
end

.debug!(&block) ⇒ Object



151
# File 'lib/dlog.rb', line 151

def self.debug!(&block); set_mode :debug, █ end

.debug?Boolean

Returns:

  • (Boolean)


155
# File 'lib/dlog.rb', line 155

def self.debug?; @@mode == :debug; end

.dlog_caller(source) ⇒ Object

get a caller description, for debug mode



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dlog.rb', line 117

def self.dlog_caller(source)
  if source =~ /^(.*):(\d+)/
    file, line = $1, $2
    if file == "(irb)"
      "[irb]:"
    else
      file = File.expand_path(file)

      file.gsub!(ROOT, ".") or
      file.gsub!(HOME, "~/")

      "#{file}(#{line}):"
    end
  else
    "[dlog]:"
  end
end

.error(*args) ⇒ Object



169
170
171
# File 'lib/dlog.rb', line 169

def self.error(*args)
  log :error, args
end

.info(*args) ⇒ Object



177
178
179
# File 'lib/dlog.rb', line 177

def self.info(*args)
  log :info, args
end

.irb?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/dlog.rb', line 71

def self.irb?
  caller.detect do |s| s =~ /irb\/workspace.rb/ end != nil
end

.log(severity, args, source_offset = 1) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dlog.rb', line 49

def self.log(severity, args, source_offset = 1)
  msg = ""
  was_string = true
  args.map do |s|
    msg += was_string ? " " : ", " unless msg.empty?
    msg += ((was_string = s.is_a?(String)) ? s : s.inspect)
  end

  source = caller[source_offset]
  msg = "#{release? ? rlog_caller(source) : dlog_caller(source)} #{msg}"
  msg = Color.send severity, msg
  
  logger = self.logger
  logger.send severity, msg

  if irb? && !log_to_stderr?
    STDERR.puts msg
  end
  
  args.first
end

.log_to_stderr?Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/dlog.rb', line 75

def self.log_to_stderr?
  logdev = logger.instance_variable_get("@logdev")
  logdev.dev == STDERR if logdev.respond_to?(:dev)
end

.loggerObject



39
40
41
42
43
44
45
46
47
# File 'lib/dlog.rb', line 39

def self.logger
  @logger ||= if defined?(RAILS_DEFAULT_LOGGER)
    RAILS_DEFAULT_LOGGER
  else
    logger = Logger.new(STDERR)
    logger.formatter = StderrFormatter
    logger
  end
end

.logger=(logger) ⇒ Object

The logger device.



35
36
37
# File 'lib/dlog.rb', line 35

def self.logger=(logger)
  @logger = logger
end

.quiet!(&block) ⇒ Object



152
# File 'lib/dlog.rb', line 152

def self.quiet!(&block); set_mode :quiet, █ end

.quiet?Boolean

Returns:

  • (Boolean)


156
# File 'lib/dlog.rb', line 156

def self.quiet?; @@mode == :quiet; end

.release!(&block) ⇒ Object



150
# File 'lib/dlog.rb', line 150

def self.release!(&block); set_mode :release, █ end

.release?Boolean

Returns:

  • (Boolean)


154
# File 'lib/dlog.rb', line 154

def self.release?; @@mode == :release; end

.rlog_caller(source) ⇒ Object

get a caller description, for release mode



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dlog.rb', line 101

def self.rlog_caller(source)
  if source =~ /^(.*):(\d+)/
    file, line = $1, $2
    file, line = $1, $2
    if file == "(irb)"
      "[irb]:"
    else
      "[" + File.basename(file).sub(/\.[^\.]*$/, "") + "]:"
    end
  else
    "[log]"
  end
end

.set_mode(mode, &block) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/dlog.rb', line 158

def self.set_mode(mode, &block)
  if !block_given?
    old = mode
  else
    old, @@mode = @@mode, mode
    yield
  end
ensure
  @@mode = old
end

.warn(*args) ⇒ Object



173
174
175
# File 'lib/dlog.rb', line 173

def self.warn(*args)
  log :warn, args
end