On May 12, 2008, at 1:36 PM, Frederick C. wrote:
I think it would help to see more of what you’ve done. What you wrote
below seems reasonable enough, but you haven’t shown how you’re using
or exactly what errors you’re getting.
Thanks for your time on this Fred…
This is a system adapted from a framework of my own in another
language. There may be a more idiomatic way to do the same, but it’s
something I implemented right away when I started with rails as
something already familiar to me in concept and usage to help get me
going.
Current code (distilled to essentials):
#----- environment.rb --------
require ‘init_constants’
#----- init_constants.rb --------
DISABLED = false
ENABLED = 1
CHATTY = 3
VERBOSE = 5
TRACE_FILE = ‘log/request_trace.log’
$request_trace = DISABLED
#----- application.rb --------
class ApplicationController < ActionController::Base
before_filter :initialize_response
def initialize_response
…other code…
$request_trace = ENABLED
if $request_trace
File.delete(TRACE_FILE) if FileTest::exist?(TRACE_FILE)
$request_trace = Logger.new(TRACE_FILE)
$request_trace.info(“ApplicationController.initialize_response”)
end
…other code…
end
#------------------------------
At this point, I can now do the following types of things in any code
at any time…
$request_trace.info("_SOME_MESSAGE_HERE") if $request_trace
or to control level of detail
$request_trace.info(“SOME_MESSAGE_HERE”) if $request_trace >=
CHATTY
where SOME_MESSAGE_HERE is replace by some string I want written
to the trace log.
I end up with a file which has a series of messages representing a
“trace” through my code. I use this to track which methods are being
called, states of vars after complex decisions and algorithms etc
just to keep an eye on things as I code/experiment.
Maybe there’s another way to do this (i.e. more convential
debuggers), but at this point I thought it would be an interesting
exercise to incorporate this code so it could be used like this:
#----- application.rb --------
class ApplicationController < ActionController::Base
before_filter :initialize_response
request_trace :chatty
And then supported by a more portable file with the guts of the
process…
#----- request_trace.rb --------
would still need init_constants
not sure where to relocate that just yet
module ActionController #:nodoc:
module RequestTrace #:nodoc:
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def request_trace(status)
case status
when :enabled
$request_trace = ENABLED
when :chatty
$request_trace = CHATTY
when :verbose
$request_trace = VERBOSE
else
$request_trace = DISABLED
end
if $request_trace
File.delete(TRACE_FILE) if FileTest::exist?(TRACE_FILE)
$request_trace = Logger.new(TRACE_FILE)
$request_trace.info
(“ApplicationController.initialize_response”)
end
end
end
end
end
When I implement the above code, I get the following error:
NoMethodError
undefined method `request_trace’ for ApplicationController:Class
– gw