Class: Lti2Commons::WireLogSupport::WireLog

Inherits:
Object
  • Object
show all
Defined in:
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.



67
68
69
70
71
72
73
# File 'lib/lti2_commons/wire_log.rb', line 67

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.



65
66
67
# File 'lib/lti2_commons/wire_log.rb', line 65

def is_logging
  @is_logging
end

#output_file_nameObject

Returns the value of attribute output_file_name.



65
66
67
# File 'lib/lti2_commons/wire_log.rb', line 65

def output_file_name
  @output_file_name
end

Instance Method Details

#clear_logObject



75
76
77
78
79
# File 'lib/lti2_commons/wire_log.rb', line 75

def clear_log
  output_file = File.open(@output_file_name, "a+")
  output_file.truncate(0)
  output_file.close
end

#flush(options = {}) ⇒ Object



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
106
107
108
# File 'lib/lti2_commons/wire_log.rb', line 81

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 { |c|
      if c == '<'
        buffer << "&lt;"
      elsif c == '>'
        buffer << "&gt;"
      else
        buffer << c
      end
    }
  end
  if options.has_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



110
111
112
113
114
# File 'lib/lti2_commons/wire_log.rb', line 110

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

#log_bufferObject



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/lti2_commons/wire_log.rb', line 146

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
  unless @log_buffer
    @log_buffer = StringIO.new
  end
  @log_buffer
end

#log_response(response, title = nil) ⇒ Object



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
# File 'lib/lti2_commons/wire_log.rb', line 116

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
    if is_logging
      raw_log "Body:"
    end
    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



142
143
144
# File 'lib/lti2_commons/wire_log.rb', line 142

def newline
  raw_log "\n"
end

#raw_log(s) ⇒ Object



160
161
162
163
# File 'lib/lti2_commons/wire_log.rb', line 160

def raw_log s
  @log_buffer = log_buffer
  @log_buffer.puts(s)
end

#timestampObject



165
166
167
# File 'lib/lti2_commons/wire_log.rb', line 165

def timestamp
  raw_log(Time.new)
end