class Lti2Commons::WireLogSupport::WireLog

Constants

STATUS_CODES

Attributes

is_logging[RW]
output_file_name[RW]

Public Class Methods

new(wire_log_name, output_file, is_html_output = true) click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 64
def initialize(wire_log_name, output_file, is_html_output = true)
  @output_file_name = output_file
  @is_logging = true
  @wire_log_name = wire_log_name
  @log_buffer = nil
  @is_html_output = is_html_output
end

Public Instance Methods

clear_log() click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 72
def clear_log
  output_file = File.open(@output_file_name, 'a+')
  output_file.truncate(0)
  output_file.close
end
flush(options = {}) click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 78
def flush(options = {})
  output_file = File.open(@output_file_name, 'a+')
  @log_buffer.rewind
  buffer = @log_buffer.read
  if @is_html_output
    oldbuffer = buffer.dup
    buffer = ''
    oldbuffer.each_char do |c|
      if c == '<'
        buffer << '&lt;'
      elsif c == '>'
        buffer << '&gt;'
      else
        buffer << c
      end
    end
  end
  if options.key? :css_class
    css_class = options[:css_class]
  else
    css_class = "#{@wire_log_name}"
  end
  output_file.puts("<div class=\"#{css_class}\"><pre>") if @is_html_output
  output_file.write(buffer)
  output_file.puts("\n</div></pre>") if @is_html_output
  output_file.close
  @log_buffer = nil
end
log(s) click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 107
def log(s)
  timestamp
  raw_log("#{s}")
  flush
end
log_buffer() click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 141
def log_buffer
  # put in the css header if file doesn't exist
  unless File.size? @output_file_name
    @output_file = File.open(@output_file_name, 'a')
    @output_file.puts '<link rel="stylesheet" type="text/css" href="wirelog.css" />'
    @output_file.puts ''
    @output_file.close
  end
  @log_buffer = StringIO.new unless @log_buffer
  @log_buffer
end
log_response(response, title = nil) click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 113
def log_response(response, title = nil)
  timestamp
  raw_log(title.nil? ? 'Response' : "Response: #{title}")
  raw_log("Status: #{response.code} #{STATUS_CODES[response.code]}")
  headers = response.headers
  unless headers.blank?
    raw_log('Headers:')
    headers.each { |k, v| raw_log("#{k}: #{v}") if k.downcase =~ /^content/ }
  end

  if response.body
    # the following is expensive so do only when needed
    raw_log('Body:') if @is_logging
    begin
      json_obj = JSON.load(response.body)
      raw_log(JSON.pretty_generate(json_obj))
    rescue
      raw_log("#{response.body}")
    end
  end
  newline
  flush(css_class: "#{@wire_log_name}Response")
end
newline() click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 137
def newline
  raw_log("\n")
end
raw_log(s) click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 153
def raw_log(s)
  @log_buffer = log_buffer
  @log_buffer.puts(s)
end
timestamp() click to toggle source
# File lib/lti2_commons/lib/lti2_commons/wire_log.rb, line 158
def timestamp
  raw_log(Time.new)
end