Class: CCLog

Inherits:
Object
  • Object
show all
Defined in:
lib/ls4/lib/cclog.rb

Overview

CCLog Copyright © 2010 FURUHASHI Sadayuki

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: TTYColor

Constant Summary collapse

LEVEL_TRACE =
0
LEVEL_DEBUG =
1
LEVEL_INFO =
2
LEVEL_WARN =
3
LEVEL_ERROR =
4
LEVEL_FATAL =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = LEVEL_TRACE, out = $stdout) ⇒ CCLog

Returns a new instance of CCLog.



46
47
48
49
50
51
52
53
54
# File 'lib/ls4/lib/cclog.rb', line 46

def initialize(level = LEVEL_TRACE, out = $stdout)
  if out.tty?
    enable_color
  else
    disable_color
  end
  @level = level
  @out = out
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



77
78
79
# File 'lib/ls4/lib/cclog.rb', line 77

def level
  @level
end

#outObject

Returns the value of attribute out.



76
77
78
# File 'lib/ls4/lib/cclog.rb', line 76

def out
  @out
end

Instance Method Details

#debug(*args, &block) ⇒ Object Also known as: DEBUG



97
98
99
100
101
102
# File 'lib/ls4/lib/cclog.rb', line 97

def debug(*args, &block)
  return if @level > LEVEL_DEBUG
  args << block.call if block
  msg = args.join
  puts "#{@color_debug}#{caller_line(1,true)}: #{msg}#{@color_reset}"
end

#debug_backtrace(backtrace = $!.backtrace) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/ls4/lib/cclog.rb', line 105

def debug_backtrace(backtrace=$!.backtrace)
  return if @level > LEVEL_DEBUG
  backtrace.each {|msg|
    puts "#{@color_debug}#{caller_line(4,true)}: #{msg}#{@color_reset}"
  }
  nil
end

#disable_colorObject



66
67
68
69
70
71
72
73
74
# File 'lib/ls4/lib/cclog.rb', line 66

def disable_color
  @color_trace = ''
  @color_debug = ''
  @color_info  = ''
  @color_warn  = ''
  @color_error = ''
  @color_fatal = ''
  @color_reset = ''
end

#enable_colorObject



56
57
58
59
60
61
62
63
64
# File 'lib/ls4/lib/cclog.rb', line 56

def enable_color
  @color_trace = TTYColor::BLUE
  @color_debug = TTYColor::WHITE
  @color_info  = TTYColor::GREEN
  @color_warn  = TTYColor::YELLOW
  @color_error = TTYColor::MAGENTA
  @color_fatal = TTYColor::RED
  @color_reset = TTYColor::NORMAL
end

#error(*args, &block) ⇒ Object Also known as: ERROR



160
161
162
163
164
165
# File 'lib/ls4/lib/cclog.rb', line 160

def error(*args, &block)
  return if @level > LEVEL_ERROR
  args << block.call if block
  msg = args.join
  puts "#{@color_error}#{caller_line(1)}: #{msg}#{@color_reset}"
end

#error_backtrace(backtrace = $!.backtrace) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/ls4/lib/cclog.rb', line 168

def error_backtrace(backtrace=$!.backtrace)
  return if @level > LEVEL_ERROR
  backtrace.each {|msg|
    puts "#{@color_error}#{caller_line(4)}: #{msg}#{@color_reset}"
  }
  nil
end

#fatal(*args, &block) ⇒ Object Also known as: FATAL



181
182
183
184
185
186
# File 'lib/ls4/lib/cclog.rb', line 181

def fatal(*args, &block)
  return if @level > LEVEL_FATAL
  args << block.call if block
  msg = args.join
  puts "#{@color_fatal}#{caller_line(1)}: #{msg}#{@color_reset}"
end

#fatal_backtrace(backtrace = $!.backtrace) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/ls4/lib/cclog.rb', line 189

def fatal_backtrace(backtrace=$!.backtrace)
  return if @level > LEVEL_FATAL
  backtrace.each {|msg|
    puts "#{@color_fatal}#{caller_line(4)}: #{msg}#{@color_reset}"
  }
  nil
end

#info(*args, &block) ⇒ Object Also known as: INFO



118
119
120
121
122
123
# File 'lib/ls4/lib/cclog.rb', line 118

def info(*args, &block)
  return if @level > LEVEL_INFO
  args << block.call if block
  msg = args.join
  puts "#{@color_info}#{caller_line(1,true)}: #{msg}#{@color_reset}"
end

#info_backtrace(backtrace = $!.backtrace) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/ls4/lib/cclog.rb', line 126

def info_backtrace(backtrace=$!.backtrace)
  return if @level > LEVEL_INFO
  backtrace.each {|msg|
    puts "#{@color_info}#{caller_line(4,true)}: #{msg}#{@color_reset}"
  }
  nil
end

#on_debug(&block) ⇒ Object



92
93
94
95
# File 'lib/ls4/lib/cclog.rb', line 92

def on_debug(&block)
  return if @level > LEVEL_DEBUG
  block.call if block
end

#on_error(&block) ⇒ Object



155
156
157
158
# File 'lib/ls4/lib/cclog.rb', line 155

def on_error(&block)
  return if @level > LEVEL_ERROR
  block.call if block
end

#on_fatal(&block) ⇒ Object



176
177
178
179
# File 'lib/ls4/lib/cclog.rb', line 176

def on_fatal(&block)
  return if @level > LEVEL_FATAL
  block.call if block
end

#on_info(&block) ⇒ Object



113
114
115
116
# File 'lib/ls4/lib/cclog.rb', line 113

def on_info(&block)
  return if @level > LEVEL_INFO
  block.call if block
end

#on_trace(&block) ⇒ Object



79
80
81
82
# File 'lib/ls4/lib/cclog.rb', line 79

def on_trace(&block)
  return if @level > LEVEL_TRACE
  block.call if block
end

#on_warn(&block) ⇒ Object



134
135
136
137
# File 'lib/ls4/lib/cclog.rb', line 134

def on_warn(&block)
  return if @level > LEVEL_WARN
  block.call if block
end

#puts(msg) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/ls4/lib/cclog.rb', line 197

def puts(msg)
  @out.puts(msg)
  @out.flush
  msg
rescue
  # FIXME
  nil
end

#trace(*args, &block) ⇒ Object Also known as: TRACE



84
85
86
87
88
89
# File 'lib/ls4/lib/cclog.rb', line 84

def trace(*args, &block)
  return if @level > LEVEL_TRACE
  args << block.call if block
  msg = args.join
  puts "#{@color_trace}#{caller_line(1,true)}: #{msg}#{@color_reset}"
end

#warn(*args, &block) ⇒ Object Also known as: WARN



139
140
141
142
143
144
# File 'lib/ls4/lib/cclog.rb', line 139

def warn(*args, &block)
  return if @level > LEVEL_WARN
  args << block.call if block
  msg = args.join
  puts "#{@color_warn}#{caller_line(1)}: #{msg}#{@color_reset}"
end

#warn_backtrace(backtrace = $!.backtrace) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/ls4/lib/cclog.rb', line 147

def warn_backtrace(backtrace=$!.backtrace)
  return if @level > LEVEL_WARN
  backtrace.each {|msg|
    puts "#{@color_warn}#{caller_line(4)}: #{msg}#{@color_reset}"
  }
  nil
end