Forum OpenACS Q&A: Response to Debugging in OpenACS

Collapse
Posted by Kevin Crosbie on
I wrote this proc to do my debugging:

ad_proc -public log_tcl_error {
{-errmsg {}}
{-param_list {}}
} {

	set msg "
------------- Error in proc: [lindex [info level -1] 0] -------------
"
	append msg "Error message follows:
"
	append msg "$errmsg
"
	if { [llength $param_list] > 0 } {
		append msg "
Parameters:
"
		foreach param $param_list {
			upvar 1 $param varname
			# Handle arrays
			if { [array exists varname] } {
				append msg "(Array) $param = [array get varname]
"
			}	elseif { [info exists varname] } {
				append msg "$param = $varname
"
			} else {
				append msg "$param didn't exist
"
			}
		}
	}
	append msg "----------------------------------------------------------------------"

	ns_log Error $msg

}

You can call this in your code by doing:
log_tcl_error -errmsg $errmsg -param_list {param_name_1 param_name_2}

where errmsg is a string and param_name_1 and param_name_2 are the names of variables you want to print out.
Note, param can be an array, list, string.   It can't be an ns_set or a multirow.
This is best used in db_transaction on_error blocks and in catch blocks where an error message is available.

This proc will print the following to the error log:

------------- Error in proc: a_proc_in_my_tcl_folder -------------
Error message follows:
ora8.c:3930:ora_tcl_command: error in `OCIStmtExecute ()': ORA-01747: invalid user.table.column, table.column, or column specification

SQL: 
				update a_table 
				set a_col = :col_val,
			 !>>>!where pk_id = :myid 
			

Parameters:
param_1 = 123
param_2 = 456
----------------------------------------------------------------------

When debugging, just run:
tail -f /apps/aol33/log/my-server-error.log | grep -A 15 "------------- Error"

This will print out 15 lines every time it sees an error.

Hope this helps.