Can't dump? Try a fake dump.

Earlier today on Twitter @samhamilton and I shared a few messages about finding a replacement for cfdump. Apparently, some ColdFusion hosts lock down access to internal Java components which unfortunately breaks cfdump. He linked to this forum posting as an example. I wasn’t able to replicate that on my ColdFusion 901 server so it may be an issue with 8 only. Either way I thought it would be fun to see if I could recreate cfdump quickly. I wrote the following tag in about 20 minutes (10 minutes for the initial version, then about 10 more minutes later on for small changes). It works best with arrays, structs, and queries, but will try to display a component as well. It also sniffs for JSON strings and will automatically deserialize them. The layout is - of course - not optimal. But it gets the job done. Here is a sample screen shot:

I’ve attached a zip of the custom tag to the bottom of this entry. But for those who just want to see the code, I’ve pasted it below. Basically it just does a few type checks and iterates through the data, recursively calling itself where it makes sense. Definitely not rocket science but maybe it will be useful. Of course, normally you don’t want to ever use cfdump in production. But I’ve often used it for error emails and other logging/reporting needs.

Edit on October 7: I added basic XML support.

<cfparam name="attributes.var"> <cfparam name="attributes.top" default="">

<cfif len(attributes.top) and not isNumeric(attributes.top)> <cfset attributes.top = ““> <cfelseif isNumeric(attributes.top) and (attributes.top lte 0 or round(attributes.top) neq attributes.top)> <cfset attributes.top = ““> </cfif>

<cfif not structKeyExists(request, “_cffakedump”)> <cfoutput> <style> table, th, td { border: 1px solid black; } td { padding: 5px; } th, .key { background-color: ##e3e392; } </style> </cfoutput> <cfset request[“_cffakedump”] = 0> <cfelse> <!— quick sanity check for pointers —> <cfset request[“_cffakedump”]++> <cfif request[“_cffakedump”] gt 1000> <cfabort/> </cfif> </cfif>

<cfif isSimpleValue(attributes.var)> <cfif attributes.var is ““> <cfoutput>[empty string]</cfoutput> <cfelseif isJSON(attributes.var)> <cfset newVar = deserializeJSON(attributes.var)> <cfoutput><table><tr><th>JSON</th></tr><tr><td></cfoutput> <cf_fakedump var=”#newVar#” top=”#attributes.top#”> <cfoutput></td></tr></table></cfoutput> <cfelse> <cfoutput>#attributes.var#</cfoutput> </cfif> <cfelseif isArray(attributes.var)> <cfoutput> <table> <tr> <th colspan=”2”>array (#arraylen(attributes.var)# items)</th> </tr> <cfif not len(attributes.top)> <cfset attributes.top = arrayLen(attributes.var)> </cfif> <cfloop index=”x” from=”1” to=”#min(arrayLen(attributes.var), attributes.top)#”> <tr valign=”top”> <td class=”key”>#x#</td> <td> <cfif isSimpleValue(attributes.var[x])> #attributes.var[x]# <cfelse> <cf_fakedump var=”#attributes.var[x]#” top=”#attributes.top#”> </cfif> </td> </tr> </cfloop> </table> </cfoutput> <cfelseif isObject(attributes.var)> <cfset data = getMetadata(attributes.var)> <cfoutput><table><tr><th>Component: #data.fullname#</th></tr><tr><td></cfoutput> <cf_fakedump var=”#data#”> <cfoutput></td></tr></table></cfoutput> <cfelseif isStruct(attributes.var)> <cfoutput> <table> <tr> <th colspan=”2”>struct</th> </tr> <cfloop item=”key” collection=”#attributes.var#”> <tr valign=”top”> <td class=”key”>#key#</td> <td> <cfif isSimpleValue(attributes.var[key])> #attributes.var[key]# <cfelse> <cf_fakedump var=”#attributes.var[key]#” top=”#attributes.top#”> </cfif> </td> </tr> </cfloop> </table> </cfoutput> <cfelseif isQuery(attributes.var)> <cfset cols = attributes.var.columnlist> <cfif not len(attributes.top)> <cfset attributes.top = attributes.var.recordCount> </cfif>

&lt;cfoutput&gt;
&lt;table&gt;
	&lt;tr&gt;
		&lt;th colspan="#listLen(cols)+1#"&gt;query (#attributes.var.recordcount# rows)&lt;/th&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;&nbsp;&lt;/td&gt;
		&lt;cfloop index="c" list="#cols#"&gt;
		&lt;th&gt;#c#&lt;/th&gt;
		&lt;/cfloop&gt;
	&lt;/tr&gt;
	&lt;cfloop query="attributes.var" endrow="#min(attributes.var.recordCount,attributes.top)#"&gt;
		&lt;tr&gt;
			&lt;td class="key"&gt;#currentRow#&lt;/td&gt;
			&lt;cfloop index="c" list="#cols#"&gt;
			&lt;td&gt;
				&lt;!--- could be complex ---&gt;
				&lt;cfset theVal = attributes.var[c][currentRow]&gt;
				&lt;cfif isSimpleValue(theVal)&gt;
					#theVal#
				&lt;cfelse&gt;
					&lt;cf_fakedump var="#theVal#" top="#attributes.top#"&gt;
				&lt;/cfif&gt;
			&lt;/td&gt;
			&lt;/cfloop&gt;
		&lt;/tr&gt;
	&lt;/cfloop&gt;
&lt;/table&gt;
&lt;/cfoutput&gt; &lt;cfelseif isXMLDoc(attributes.var) or isXMLNode(attributes.var)&gt;
&lt;cfoutput&gt;
&lt;table&gt;
	&lt;tr&gt;
		&lt;th colspan="2"&gt;&lt;cfif isXMLDoc(attributes.var)&gt;xml&lt;cfelseif isXmLNode(attributes.var)&gt;xml element&lt;/cfif&gt;&lt;/th&gt;
	&lt;/tr&gt;
	&lt;cfif isXMLDoc(attributes.var)&gt;
		&lt;cfset child = attributes.var.xmlRoot&gt;
		&lt;tr&gt;
			&lt;td&gt;#child.xmlName#&lt;/td&gt;
			&lt;td&gt;&lt;cf_fakedump var="#child#"&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;cfelseif isXMLNode(attributes.var)&gt;
		&lt;tr&gt;
			&lt;td&gt;xmlText&lt;/td&gt;
			&lt;td&gt;#attributes.var.xmlText#&lt;/td&gt;
		&lt;/tr&gt;
		&lt;cfset kids = attributes.var.xmlChildren&gt;
		&lt;cfloop index="x" from="1" to="#arrayLen(kids)#"&gt;
			&lt;tr&gt;
				&lt;td&gt;#kids[x].xmlname#&lt;/td&gt;
				&lt;td&gt;&lt;cf_fakedump var="#kids[x]#"&gt;&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/cfloop&gt;
	&lt;/cfif&gt;
&lt;/table&gt;
&lt;/cfoutput&gt; &lt;cfelse&gt;
&lt;cfoutput&gt;[FakeDump] Sorry, I couldn't handle this data.&lt;/cfoutput&gt; &lt;/cfif&gt; </code><p><a href='enclosures/C%3A%5Chosts%5C2009%2Ecoldfusionjedi%2Ecom%5Cenclosures%2Ffakedump1%2Ezip'>Download attached file.</a></p>
Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments