lib/scout/log.rb in scout-gear-8.0.0 vs lib/scout/log.rb in scout-gear-8.1.0
- old
+ new
@@ -10,13 +10,13 @@
attr_accessor :severity
attr_writer :tty_size, :logfile
end
SEVERITY_NAMES ||= begin
- names = %w(DEBUG LOW MEDIUM HIGH INFO WARN ERROR NONE )
+ names = %w(DEBUG LOW MEDIUM HIGH INFO WARN ERROR NONE )
names.each_with_index do |name,i|
- eval "#{ name } = #{ i }"
+ eval "#{ name } = #{ i }"
end
names
end
def self.default_severity
@@default_severity ||= begin
@@ -30,45 +30,49 @@
end
end
@@default_severity
end
- case ENV['SCOUT_LOG']
- when 'DEBUG'
+ case ENV['SCOUT_LOG']
+ when 'DEBUG'
self.severity = DEBUG
- when 'LOW'
+ when 'LOW'
self.severity = LOW
- when 'MEDIUM'
+ when 'MEDIUM'
self.severity = MEDIUM
- when 'HIGH'
+ when 'HIGH'
self.severity = HIGH
when nil
self.severity = default_severity
else
self.severity = default_severity
end
def self.tty_size
- @tty_size = begin
- IO.console.winsize.last
- rescue
- begin
- `tput li`
- rescue
- 80
- end
- end
+ @@tty_size ||= Log.ignore_stderr do
+ begin
+ IO.console.winsize.last
+ rescue Exception
+ begin
+ res = `tput li`
+ res = nil if res == ""
+ res || ENV["TTY_SIZE"] || 80
+ rescue Exception
+ ENV["TTY_SIZE"] || 80
+ end
+ end
+ end
end
def self.last_caller(stack)
line = nil
pos ||= 0
- while line.nil? or line =~ /scout\/log\.rb/ and stack.any?
- line = stack.shift
+ while line.nil? or line =~ /scout\/log\.rb/ and stack.any?
+ line = stack.shift
end
line ||= caller.first
line.gsub('`', "'")
end
@@ -127,13 +131,40 @@
def self.clear_line(out = STDOUT)
out.puts Log.return_line << " " * (Log.tty_size || 80) << Log.return_line unless nocolor
end
+ MUTEX = Mutex.new
+ def self.log_write(str)
+ MUTEX.synchronize do
+ if logfile.nil?
+ begin
+ STDERR.write str
+ rescue
+ end
+ else
+ logfile.write str
+ end
+ end
+ end
+
+ def self.log_puts(str)
+ MUTEX.synchronize do
+ if logfile.nil?
+ begin
+ STDERR.puts str
+ rescue
+ end
+ else
+ logfile.puts str
+ end
+ end
+ end
+
LAST = "log"
def self.logn(message = nil, severity = MEDIUM, &block)
- return if severity < self.severity
+ return if severity < self.severity
message ||= block.call if block_given?
return if message.nil?
time = Time.now.strftime("%m/%d/%y-%H:%M:%S.%L")
@@ -145,21 +176,18 @@
prefix = time << color(severity) << "[" << sev_str << "]" << color(0)
end
message = "" << highlight << message << color(0) if severity >= INFO
str = prefix << " " << message.to_s
- if logfile.nil?
- STDERR.write str
- else
- logfile.write str
- end
+ log_write str
+
Log::LAST.replace "log"
nil
end
def self.log(message = nil, severity = MEDIUM, &block)
- return if severity < self.severity
+ return if severity < self.severity
message ||= block.call if block_given?
return if message.nil?
message = message + "\n" unless message[-1] == "\n"
self.logn message, severity, &block
end
@@ -248,13 +276,13 @@
end
end unless stack.nil?
end
def self.tsv(tsv, example = false)
- STDERR.puts Log.color :magenta, "TSV log: " << Log.last_caller(caller).gsub('`',"'")
- STDERR.puts Log.color(:blue, "=> "<< Log.fingerprint(tsv), true)
- STDERR.puts Log.color(:cyan, "=> " << tsv.summary)
+ log_puts Log.color :magenta, "TSV log: " << Log.last_caller(caller).gsub('`',"'")
+ log_puts Log.color(:blue, "=> "<< Log.fingerprint(tsv), true)
+ log_puts Log.color(:cyan, "=> " << tsv.summary)
if example && ! tsv.empty?
key = case example
when TrueClass, :first, "first"
tsv.keys.first
when :random, "random"
@@ -264,37 +292,37 @@
end
values = tsv[key]
values = [values] if tsv.type == :flat || tsv.type == :single
if values.nil?
- STDERR.puts Log.color(:blue, "Key (#{tsv.key_field}) not present: ") + key
+ log_puts Log.color(:blue, "Key (#{tsv.key_field}) not present: ") + key
else
- STDERR.puts Log.color(:blue, "Key (#{tsv.key_field}): ") + key
+ log_puts Log.color(:blue, "Key (#{tsv.key_field}): ") + key
tsv.fields.zip(values).each do |field,value|
- STDERR.puts Log.color(:magenta, field + ": ") + (Array === value ? value * ", " : value.to_s)
+ log_puts Log.color(:magenta, field + ": ") + (Array === value ? value * ", " : value.to_s)
end
end
end
end
def self.stack(stack)
if ENV["SCOUT_ORIGINAL_STACK"] == 'true'
- STDERR.puts Log.color :magenta, "Stack trace [#{Process.pid}]: " << Log.last_caller(caller)
+ log_puts Log.color :magenta, "Stack trace [#{Process.pid}]: " << Log.last_caller(caller)
color_stack(stack).each do |line|
- STDERR.puts line
+ log_puts line
end
else
- STDERR.puts Log.color :magenta, "Stack trace [#{Process.pid}]: " << Log.last_caller(caller)
+ log_puts Log.color :magenta, "Stack trace [#{Process.pid}]: " << Log.last_caller(caller)
color_stack(stack.reverse).each do |line|
- STDERR.puts line
+ log_puts line
end
end
end
def self.count_stack
if ! $count_stacks
Log.debug "Counting stacks at: " << caller.first
- return
+ return
end
$stack_counts ||= {}
head = $count_stacks_head
stack = caller[1..head+1]
stack.reverse.each do |line,i|