class Timmy::Logger

Public Class Methods

finalize() click to toggle source
# File lib/timmy/logger.rb, line 52
def finalize
  save
  put_profile if profile?
end
match_replay_header(line) click to toggle source
# File lib/timmy/logger.rb, line 20
def match_replay_header(line)
  line.match(/^TIMMY-SESSION:v1:(?<s>\d+\.\d{9})$/)
end
match_replay_line(line) click to toggle source
# File lib/timmy/logger.rb, line 24
def match_replay_line(line)
  line.match(/^(?<s>\d+(\.\d+)?)(?<t>e)? (?<content>.*)/)
end
put_eof() click to toggle source
# File lib/timmy/logger.rb, line 44
def put_eof
  put_output(feint("EOF")) unless @quiet
end
put_output(output, error = false) click to toggle source
# File lib/timmy/logger.rb, line 28
def put_output(output, error = false)
  duration = MasterTimer.get

  if @quiet
    puts output
  else
    formatted_duration = format_duration(duration)
    formatted_duration = red(formatted_duration) if error
    puts "\e[0m" + feint(formatted_duration) + " " + output
  end
  $stdout.flush

  @output ||= ''
  @output += sprintf("%.9f%s %s\n", duration, error ? 'e' : '', output)
end
put_timer(timer) click to toggle source
# File lib/timmy/logger.rb, line 48
def put_timer(timer)
  do_put_timer(timer) unless @quiet
end
set_output_dir(dir) click to toggle source
# File lib/timmy/logger.rb, line 4
def set_output_dir(dir)
  @output_dir = File.expand_path(dir)
end
set_precision(precision) click to toggle source
# File lib/timmy/logger.rb, line 12
def set_precision(precision)
  @precision = precision
end
set_profile(profile) click to toggle source
# File lib/timmy/logger.rb, line 16
def set_profile(profile)
  @profile = profile
end
set_quiet(quiet) click to toggle source
# File lib/timmy/logger.rb, line 8
def set_quiet(quiet)
  @quiet = quiet
end

Private Class Methods

bold(string) click to toggle source
# File lib/timmy/logger.rb, line 115
def bold(string)
  "\e[1m#{string}\e[0m"
end
do_put_timer(timer) click to toggle source
# File lib/timmy/logger.rb, line 71
def do_put_timer(timer)
  puts format_timer(timer)
  $stdout.flush
end
feint(string) click to toggle source
# File lib/timmy/logger.rb, line 119
def feint(string)
  "\e[2m#{string}\e[0m"
end
format_duration(duration) click to toggle source
# File lib/timmy/logger.rb, line 110
def format_duration(duration)
  format = precision > 0 ? "%d:%0#{3 + precision}.#{precision}f" : "%d:%02d"
  sprintf(format, duration / 60, duration % 60)
end
format_id(id) click to toggle source
# File lib/timmy/logger.rb, line 106
def format_id(id)
  id.to_s
end
format_timer(timer) click to toggle source
# File lib/timmy/logger.rb, line 93
def format_timer(timer)
  string = (
    bold(format_duration(timer.duration)) +
    " " +
    bold(green(format_id(timer.definition.id)))
  )

  string += " " + green("(" + timer.group + ")") if timer.group
  string += " #{timer.label}" if timer.label

  string
end
green(string) click to toggle source
# File lib/timmy/logger.rb, line 123
def green(string)
  "\e[32m#{string}\e[0m"
end
output_dir() click to toggle source
# File lib/timmy/logger.rb, line 131
def output_dir
  @output_dir ||= "/tmp"
end
precision() click to toggle source
# File lib/timmy/logger.rb, line 135
def precision
  @precision ||= 0
end
profile?() click to toggle source
# File lib/timmy/logger.rb, line 139
def profile?
  @profile = false if @profile == nil
  @profile
end
put_profile() click to toggle source
# File lib/timmy/logger.rb, line 76
def put_profile
  slowest_timers = TargetedTimerManager
    .stopped
    .sort_by { |timer| -timer.duration }
    .slice(0, 10)

  return unless slowest_timers.any?

  puts "Slowest targeted timers:"

  slowest_timers.each do |timer|
    put_timer(timer)
  end

  puts
end
red(string) click to toggle source
# File lib/timmy/logger.rb, line 127
def red(string)
  "\e[31m#{string}\e[0m"
end
save() click to toggle source
# File lib/timmy/logger.rb, line 59
def save
  suffix = "#{MasterTimer.start.to_i}+#{MasterTimer.get.to_i}"
  filename = File.join(output_dir, "timmy-#{suffix}.log")

  return if File.exists?(filename)

  header = sprintf("TIMMY-SESSION:v1:%.9f\n", MasterTimer.start)
  File.write(filename, header + @output)
  puts feint("Log written to #{filename}")
  puts
end