Browse > Home / Archive by category 'Techie'

| Subcribe via RSS

The Greatest PHP Debugging Function of All Time

December 22nd, 2009 | Comments | Posted in Techie

I normally try to keep the tech stuff to a minimum on the blog, but this function is in high demand from anyone who’s ever had it and then worked on another codebase without it.  Simple, but mind-blowingly useful. Without further ado, I present, YO.

NOTE: This is a re-write of some code I wrote at Yahoo!, so I wanted to give credit to the two folks who had a hand in the debugging system we built there, from which this is derived: Chris and Sara.

You’re welcome, America.

<?php
 
/**
 * The greatest debugging class in history. Here's why:
 * 
 *  - outputs to both the error log and optionally the screen.
 *  - formats output nicely so you can see your debugging output
 *  - shows you where the yo() call is IN THE OUTPUT so if you
 *    leave a yo() in someplace, you can track it down easy-peasy.
 *  - accepts multiple args just like var_dump
 *  - feel free to use the sub functions too (like var_dump_str)
 *   
 * @author Jacob Rosenberg <themasterof@rightuptop.com>
 * @author Sara Golemon
 * @author Chris Draycott
 *
 */
class Yo {
 
 
    /* {{{ */
    /**
     * getCallerLine
     *
     * gets the calling line of the function it was called from,
     * i.e., this function's grandparent.
     *
     * @returns string Name of calling function
     */
    static public function getCallerLine($levels = 2)
    {
        $bt = debug_backtrace();
 
        $file = isset($bt[$levels]['file']) ? $bt[$levels]['file'] : '';
        $line = isset($bt[$levels]['line']) ? $bt[$levels]['line'] : '';
 
        return array($file, $line);
    }
    /* }}} */
 
 
    /* {{{ */
    /**
     * getCaller
     *
     * gets the calling function of the function it was called from,
     * i.e., this function's grandparent.
     *
     * @returns string Name of calling function
     */
    static public function getCaller($levels = 2)
    {
        $bt = debug_backtrace();
 
        if (isset($bt[$levels])) {
            $caller = $bt[$levels]['function'];
 
            if (isset($bt[$levels]['class'])) {
                $caller = $bt[$levels]['class'] . "::" . $caller;
            }
        } else {
            $caller = 'main';
        }
 
        return $caller;
    }
 
 
    /* {{{ */
    /**
     * var_dump_str
     *
     * var_dump, but with returned output.
     * OB capture is gross, but print_r($x,1) sucks compared to var_dump.
     *
     * @param mixed PHP Variable to be var_dump'd
     * @returns string var_dump representation of that variable
     */
    function var_dump_str($var)
    {
        ob_start();
        var_dump($var);
        $out = ob_get_contents();
        ob_end_clean();
        return $out;
    }
    /* }}} */
 
    /**
     * isInternal
     *
     * determines if a user is an internal user.
     *
     * @return bool true if the user is internal and should see debug output
     */
    public static function isInternal()
    {
        // feel free to add some code here that
        // turns on debug output for only a subset of users.
        //
        return true;
    }
 
    /* }}} */
 
    /* {{{ */
    /**
     * yo - figure it out yourself, dummy
     *
     * @param mixed Variable(s) to be dumped (var args)
     * ...
     */
    public static function do_yo()
    {
        $internal = self::isInternal();
 
        $caller = self::getCaller();
 
        if ($internal) {
            echo "<pre><h1>YO!!: $caller</h1>";  
        }
 
        error_log("YO!!:: $caller");
 
        foreach (func_get_args() as $var) {
            $output = ($var === false) ? 'bool(false)' : Yo::var_dump_str($var);
            if ($internal) {
                echo htmlentities($output, ENT_QUOTES, 'UTF-8') . "\n";
            }
            error_log($output);
        }
 
        if ($internal) echo "</pre>";
    }
    /* }}} */
 
    /* {{{ */
    /**
     * yo_bt - figure it out yourself, dummy
     *
     */
    public static function do_yo_bt()
    {
        $internal = self::isInternal();
        $caller = self::getCaller();
 
        if ($internal) {
            echo "<pre><h1>YO bt!!: $caller</h1>";
        }
        error_log("YO bt!!:: $caller");
 
        $bt = debug_backtrace();
 
        foreach ($bt as $line) {
            if ($internal) echo $line['function'] . "\n";
            error_log($line['function']);
        }
 
        if ($internal) echo "</pre>";
    }
    /* }}} */
}
/* }}} */
 
// shortcuts! ------
function yo() { return Yo::do_yo(); }
function yo_bt() { return Yo::do_yo_bt(); }
Tags: , ,

FFFFound.com Digest RSS Feed

February 5th, 2009 | Comments Off | Posted in Techie

So I recently started browsing FFFFound.com for cool images, and I love it. When I started finding really cool stuff in there, I figured, hey, I’ll add this site to my RSS reader (as I normally do with sites and blogs that I enjoy). Well, the joke was on me, because ffffound updates every few seconds, which means that it completely takes over my RSS reader with posts. This was very annoying.

That’s when it occurred to me that what I wanted was a digest feed; a feed that combined a few hours worth of ffffound posts into one. And then I realized that this was not particularly hard to do. So I did it.

Here it is.

This feed updates every six hours, with a post that contains a table of thumbnails of all the ffffound posts within that six hour block. It’s perfect for any RSS readers that support basic HTML. Now I get one update every six hours with a bunch of cool images to browse. Perfect.

Enjoy!

Tags: , , ,

Crows > You

May 29th, 2008 | Comments Off | Posted in Techie

Here’s a fairly mind-blowing Ted talk on how smart crows are:

This video complemented nicely with my friends’ recent discussion about the ten smartest animals, of which crows are a member.

I dream of an army of demon crows doing my bidding.  Which is probably the plot of the new Michael Bay-produced version of The Birds (and no, that’s not a joke).

Tags: ,

Human Airbag

March 25th, 2008 | Comments Off | Posted in Personal, Techie

Check out this engadget article on new personal airbags aimed at motorcyclists.  I’m still hoping they invent a process by which they can fuse adamantium to my skeleton and make me invulnerable, but this will have to tide me over; take a look at this video:

 

 
Oops… I probably shouldn’t have posted that. As I write this, I can hear the sound of my mom storming into the garage and taking a sledgehammer to my bike.
 
For more info, take a peek at the Dainese website.
Tags: , ,

A Jumble of Science Stuff

July 9th, 2004 | Comments | Posted in Techie

A few cool science items:

  • Scientists at Caltech have made another step in reading monkey brain signals and turning them into corresponding movement. This has obvious implications for prosthetics; score another one for the reductionists.
  • Also, another advancement has been made in the study of quantum teleportation using entangled pairs. This one is a brain frier, and has interesting applications in the field of quantum cryptography. On a philosophical note, if I deconstruct your body on an atomic level and reconstruct you somewhere else using different atoms, are you the same person? What’s really nuts is that this type of question may become non-hypothetical during my own lifetime.
  • Lastly, keep an eye on this daily simulated view of earth from Cassini. What blows me away is how small the sun is when you’re orbiting Saturn. Craziness.