Class: Lti2Commons::WireLogSupport::WireLog

Inherits:
Object
  • Object
show all
Defined in:
lib/lti2_commons/lib/lti2_commons/wire_log.rb

Constant Summary collapse

STATUS_CODES =
{
  100 => 'Continue',
  101 => 'Switching Protocols',
  102 => 'Processing',

  200 => 'OK',
  201 => 'Created',
  202 => 'Accepted',
  203 => 'Non-Authoritative Information',
  204 => 'No Content',
  205 => 'Reset Content',
  206 => 'Partial Content',
  207 => 'Multi-Status',
  226 => 'IM Used',

  300 => 'Multiple Choices',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  305 => 'Use Proxy',
  307 => 'Temporary Redirect',

  400 => 'Bad Request',
  401 => 'Unauthorized',
  402 => 'Payment Required',
  403 => 'Forbidden',
  404 => 'Not Found',
  405 => 'Method Not Allowed',
  406 => 'Not Acceptable',
  407 => 'Proxy Authentication Required',
  408 => 'Request Timeout',
  409 => 'Conflict',
  410 => 'Gone',
  411 => 'Length Required',
  412 => 'Precondition Failed',
  413 => 'Request Entity Too Large',
  414 => 'Request-URI Too Long',
  415 => 'Unsupported Media Type',
  416 => 'Requested Range Not Satisfiable',
  417 => 'Expectation Failed',
  422 => 'Unprocessable Entity',
  423 => 'Locked',
  424 => 'Failed Dependency',
  426 => 'Upgrade Required',

  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Timeout',
  505 => 'HTTP Version Not Supported',
  507 => 'Insufficient Storage',
  510 => 'Not Extended'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wire_log_name, output_file, is_html_output = true) ⇒ WireLog

Returns a new instance of WireLog.



64
65
66
67
68
69
70
# 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

Instance Attribute Details

#is_loggingObject

Returns the value of attribute is_logging.



62
63
64
# File 'lib/lti2_commons/lib/lti2_commons/wire_log.rb', line 62

def is_logging
  @is_logging
end

#output_file_nameObject

Returns the value of attribute output_file_name.



62
63
64
# File 'lib/lti2_commons/lib/lti2_commons/wire_log.rb', line 62

def output_file_name
  @output_file_name
end

Instance Method Details

#clear_logObject



72
73
74
75
76
# 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 = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Object



107
108
109
110
111
# File 'lib/lti2_commons/lib/lti2_commons/wire_log.rb', line 107

def log(s)
  timestamp
  raw_log("#{s}")
  flush
end

#log_bufferObject



141
142
143
144
145
146
147
148
149
150
151
# 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) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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

#newlineObject



137
138
139
# File 'lib/lti2_commons/lib/lti2_commons/wire_log.rb', line 137

def newline
  raw_log("\n")
end

#raw_log(s) ⇒ Object



153
154
155
156
# 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

#timestampObject



158
159
160
# File 'lib/lti2_commons/lib/lti2_commons/wire_log.rb', line 158

def timestamp
  raw_log(Time.new)
end