Class: Fairy::Log
- Inherits:
-
Object
- Object
- Fairy::Log
- Extended by:
- Forwardable
- Defined in:
- lib/fairy/share/log.rb
Constant Summary collapse
- LEVELS =
[:FATAL, :ERROR, :WARN, :INFO, :VERBOSE, :DEBUG]
- MESSAGE_LEVEL =
CONF.LOG_LEVEL
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#LOCAL_OUTPUT_DEV ⇒ Object
Returns the value of attribute LOCAL_OUTPUT_DEV.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #debug_p(*args) ⇒ Object
-
#initialize ⇒ Log
constructor
A new instance of Log.
- #log(sender, str = nil, &block) ⇒ Object (also: #puts)
- #log_backtrace(sender = nil) ⇒ Object
-
#log_exception(sender = $!, exception = $!) ⇒ Object
Log::log_exception(sender, exception, level = :WARN) Log::log_exception(exception, level = :WARN).
-
#logf(sender, format = nil, *args) ⇒ Object
(also: #printf)
Log::log(sender, format, args…) Log::log(format, args,…).
- #nop(*args) ⇒ Object
- #set_local_output_dev(dev = CONF.LOG_LOCAL_OUTPUT_DEV) ⇒ Object
- #start_exporter ⇒ Object
- #stop_export ⇒ Object
Constructor Details
#initialize ⇒ Log
Returns a new instance of Log.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fairy/share/log.rb', line 20 def initialize @logger = nil @host = `hostname`.chomp @type = $0 @pid = nil @export_thread = nil @mutex = Mutex.new @puts_mutex = Mutex.new @buffer = [] @buffer_mutex = Mutex.new @buffer_cv = XThread::ConditionVariable.new set_local_output_dev start_exporter end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
86 87 88 |
# File 'lib/fairy/share/log.rb', line 86 def host @host end |
#LOCAL_OUTPUT_DEV ⇒ Object
Returns the value of attribute LOCAL_OUTPUT_DEV.
89 90 91 |
# File 'lib/fairy/share/log.rb', line 89 def LOCAL_OUTPUT_DEV @LOCAL_OUTPUT_DEV end |
#logger ⇒ Object
Returns the value of attribute logger.
85 86 87 |
# File 'lib/fairy/share/log.rb', line 85 def logger @logger end |
#pid ⇒ Object
Returns the value of attribute pid.
88 89 90 |
# File 'lib/fairy/share/log.rb', line 88 def pid @pid end |
#type ⇒ Object
Returns the value of attribute type.
87 88 89 |
# File 'lib/fairy/share/log.rb', line 87 def type @type end |
Class Method Details
.method_added(method) ⇒ Object
80 81 82 |
# File 'lib/fairy/share/log.rb', line 80 def method_added(method) (class<<self;self;end).def_delegator :@the_log, method end |
Instance Method Details
#debug_p(*args) ⇒ Object
213 214 215 216 217 |
# File 'lib/fairy/share/log.rb', line 213 def debug_p(*objs) for o in objs log(self, o.inspect) end end |
#log(sender, str = nil, &block) ⇒ Object Also known as: puts
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/fairy/share/log.rb', line 95 def log(sender, str = nil, &block) bt = caller(0).select{|l| /fairy.*(share\/log)|__FORWARDABLE__|forwardable/ !~ l} bt.first =~ /\/([^\/]*\.rb):([0-9]+):in `(.*)'$/ file_name = $1 line_no = $2 method = $3 if sender.kind_of?(String) str = sender sender_type = "[UNDEF]" else begin sender_type = sender.log_id rescue sender_type = sender.class.name.sub(/Fairy::/, "") end end time = Time.now prefix = time.strftime("%Y/%m/%d %H:%M:%S") prefix.concat sprintf(".%06d %s ", time.usec, @host) mes = sprintf("%s%s%s %s[%s] %s#%s: ", @type, @pid ? "\##{@pid}": "", Thread.current["name"] ? Thread.current["name"]: "", file_name, line_no, sender_type, method) if block_given? sio = StringIO.new(mes, "a+") yield sio else mes.concat str end mes.chomp! if @LOCAL_OUTPUT_DEV @puts_mutex.synchronize do begin @LOCAL_OUTPUT_DEV.local_stdout.puts mes rescue @LOCAL_OUTPUT_DEV.puts mes end end end if @logger @buffer_mutex.synchronize do @buffer.push prefix+mes @buffer_cv.signal end else $stdout.puts "****Loggerが設定されていません****" end end |
#log_backtrace(sender = nil) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/fairy/share/log.rb', line 180 def log_backtrace(sender = nil) log(sender) do |sio| for l in caller(0) sio.puts l end end end |
#log_exception(sender = $!, exception = $!) ⇒ Object
Log::log_exception(sender, exception, level = :WARN) Log::log_exception(exception, level = :WARN)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/fairy/share/log.rb', line 161 def log_exception(sender = $!, exception=$!) if sender.kind_of?(Exception) exception = sender sender = "UNDEF" end log(sender) do |sio| if exception.kind_of?(Exception) sio.puts "#{exception.message}: #{exception.class}" for l in exception.backtrace sio.puts l end else sio.puts "Unknown exception rised!!(Exp=#{exception.inspect})" sio.puts "Backtorace: " log_backtrace(sender) end end end |
#logf(sender, format = nil, *args) ⇒ Object Also known as: printf
Log::log(sender, format, args…) Log::log(format, args,…)
154 155 156 |
# File 'lib/fairy/share/log.rb', line 154 def logf(sender, format=nil, *args) log(sender, sprintf(format, *args)) end |
#nop(*args) ⇒ Object
188 |
# File 'lib/fairy/share/log.rb', line 188 def nop(*args); end |
#set_local_output_dev(dev = CONF.LOG_LOCAL_OUTPUT_DEV) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fairy/share/log.rb', line 57 def set_local_output_dev(dev = CONF.LOG_LOCAL_OUTPUT_DEV) case dev when nil @LOCAL_OUTPUT_DEV = nil when String, Symbol begin @LOCAL_OUTPUT_DEV = eval(dev.to_s) rescue Log::warn(self, "Can't set local output dev") Log::warn(self, "Use old local output dev") end else @LOCAL_OUTPUT_DEV = dev end end |
#start_exporter ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fairy/share/log.rb', line 41 def start_exporter @export_thread = Thread.start { loop do buf = nil @buffer_mutex.synchronize do while @buffer.empty? @buffer_cv.wait(@buffer_mutex) end buf = @buffer.dup @buffer.clear end @logger.(buf) end } end |
#stop_export ⇒ Object
91 92 93 |
# File 'lib/fairy/share/log.rb', line 91 def stop_export @export_thread.exit end |