March 29, 2022
Add IP attribute to cfdump
Comments
(1)
March 29, 2022
Add IP attribute to cfdump
Good ol' ColdFusion developer that has 20 years of experience with the language 
Master 8 posts
Followers: 2 people
(1)

I created my own solution for this functionality. Check it out and let me know what you think!

Below is first an Index.cfm to demonstrate things, and the Debug.cfc which it calls. (The Debug.cfc is presumed found in a “cfc” folder placed in the same directory or as defined by a mapping which could point to any other location).

Index.cfm:

<cfscript>
  testList = "One,Two,Three";
  testArray = [1,{2:2,3:3}];
  debug = CreateObject("component","cfc.debug");
  debug.dump([testList,testArray]);
  debug.dump(var="undefinedValue", ip="127.0.0.1", evaluate=true, wrapperAttr='class="undefined"');
  debug.dump(var="undefinedValue", ip="127.0.0.1", evaluate=false);
  debug.dump(var=CGI, ip="127.0.0.1", ifDefined="url.showCgiDump", expand=true);
</cfscript>

And cfc/Debug.cfc:

component {
  this.ip = "127.0.0.1";
  /**
  * CF Dump for debug IPs only
  *
  *
  * @ip List of IP addresses for dump showing
  * @evaluate Evaluates var attribute value
  * @errorValue Shows this value if evaluate=true and var attribute value is not defined
  * @wrapperAttr Wraps dump result in div with pointed attributes
  * @ifDefined Not shows dump if variable from this attribute is not defined
  *
  * Other attributes are general cfdump/writeDump attributes
  *
  * Example:
  * debug = CreateObject("component","cfc.debug");
  * debug.dump(var=CGI, ip="127.0.0.1", ifDefined="url.showCgiDump", expand=true);
  * debug.dump(var="undefinedValue", ip="127.0.0.1", evaluate=true, abort=true);
  *
  */
  function dump(
    var="debugDumpNull",
    ip=this.ip,
    abort=false,
    expand=arguments.abort,
    showUdfs=false,
    evaluate=false,
    errorValue="NOT DEFINED",
    wrapperAttr="",
    ifDefined="",
    format="text",
    hide="all",
    show="all",
    keys="9999",
    top="9999",
    label="",
    metainfo=true,
    output="browser"
    ) 
  {
    if (len(ifDefined) && !isDefined(ifDefined)) return "";
    local.toShow = this.checkIp(arguments.ip);
    if (toShow) {
      try {
        if (isSimpleValue(arguments.var) && arguments.var eq "debugDumpNull") {
          local.value = session;
          } else if (isSimpleValue(arguments.var) && evaluate) {
          local.value = (isDefined(var)) ? evaluate(var) : errorValue;
        } else {
          local.value = var;
        }
        if (len(wrapperAttr)) writeOutput('<div #wrapperAttr#>');
        local.dumpArgs = "output,format,abort,label,metainfo,top,show,hide,keys,expand,showUDFs";
        local.args = structNew();
        for (var arg in dumpArgs) {
          if (structKeyExists(arguments, arg)) {
            local.args[arg] = arguments[arg];
          }
        }
        writeDump(var=value, argumentcollection=args);
        if (len(wrapperAttr)) writeOutput('</div>');
      } catch (any e) {
        writeDump(var="Dump Error", expand=expand, abort=abort, showUdfs=false);
      }
    }
  }

  /**
  * Checks if IP matches the specified
  *
  */

  function checkIp(ip=this.ip ) {
    return (arguments.ip eq "" || (listFind(arguments.ip, cgi.remote_addr)));
  }
}

 

1 Comment
2022-03-31 14:19:13
2022-03-31 14:19:13

Hey, Igor. Interesting idea, here. Thanks.

Like
Add Comment