Don’t call it a comeback!

December 9th, 2009

I confess, I have some minor narcissistic tendencies. For example, the App Store search bar on my iPhone usually contains my own name, so I can check on the ratings of my apps at a glance.

That’s how I noticed this new review of FatWatch, my weight tracking app for iPhone:

It does exactly what it says it does, but it’s far too expensive and the developer seems to have abandoned it. Get one of the free weight apps and save yourself the dough.

Ouch!

In a world of disposable mobile apps, FatWatch might seem expensive, but it’s actually quite cheap for what it does: tracking your weight against a moving average in a well-designed application. The average is key; it’s the only useful way to track a human being’s weight, and none of the free apps can do the math for you.

For devotees of The Hacker’s Diet, it also lets you import your weight history from the old Palm Eat Watch app and export it to your computer any time you choose.

As for the other concern, I assure you that FatWatch has not been abandoned. I use it every day! I suppose it is overdue for an update, but that’s only to add new features, as (save for a minor cosmetic issue) no bugs have been reported in FatWatch 1.4.

The good news is that I recently completed a contract project that puts me in a comfortable enough financial position to devote time to a FatWatch update. So, watch this space, and if you’re interested in beta testing, wait for an announcement soon.

Nicer ways to view UNIX man pages

November 29th, 2009

There is a wealth of information available in the UNIX manual pages, but it feels a little silly to be reading text out of a terminal window, especially since I paid so much money for all this fancy font rendering technology.

Here are some alternatives:

To view a man page in Xcode’s documentation window, simply select “Open man Page…” from the Help menu. For some reason, I hadn’t noticed that menu item until I read this hint on accessing it via AppleScript. Nicely formatted and references to other man pages are hyperlinked.

Bwana allows you to read man pages in any web browser, by registering itself as a protocol handler for man: URLs. Once installed, you can type man:perl in your browser’s address bar or open man:perl at a command prompt to read a manual page in your browser. Like Xcode, cross-references become hyperlinks, but it formats text using Courier. Source code is available, so I guess if I care enough I can do something about it.

Finally, a surprisingly short incantation will open any man page as a beautifully formatted document in Preview:

man -t perl | open -f -a /Applications/Preview.app

The -t option tells man to output PostScript, and the -f option tells open to put its input into a temporary file and pass that along to the specified application.

After reading that hint I set about writing a shell function so I could type manp perl to open Perl’s manual page in Preview. However, Preview’s PostScript to PDF conversion is kind of slow, so I wound up writing something slightly more sophisticated:

function manp {
    local M=`man -w $*`               # Get path of page source.
    if [ -z $M ]; then return; fi     # Quit if it doesn't exist.
    local N=`basename $M .gz`         # Extract the name of the file and
    local P=$TMPDIR/man.$N.pdf        # use it to create a PDF file name.
    if [ ! -e $P ]; then              # If the PDF file doesn't exist,
        echo Creating PDF for $N...
        man -t $1 | pstopdf -i -o $P  # generate it.
    fi
    open $P                           # Open the PDF version.
}

Then I went back and read the comments, which contain a dozen or so different versions of the same idea. But mine’s the best! Copy and paste it into the your .profile if you agree.

Don’t printf when you can tcpdump

November 10th, 2009

I’m working on an app that talks to a web service, and in the course of debugging it’s good to know what exactly is being sent to and from the web server. I had been doing so with the tried and true method of printf() debugging (this is Cocoa, so NSLog() debugging, actually), but it was getting to be a pain:

  1. If I print all network traffic all the time, it overwhelms my console, making it useless for any other kind of output. So instead, I’m constantly inserting or removing NSLog() statements as I work. Not to mention having to reproduce a request because the right logging statements weren’t in place the first time around.
  2. NSURLConnection returns downloaded information as an NSData object, so simply passing it to NSLog() dumps a lot of useless hexadecimal code to the screen. That means I must first create an NSString from the data, print it, then release it. (I can’t use %s, the data isn’t null terminated.)
  3. NSURLConnection does a lot of behind the scenes work, like storing cookies and setting Content-Length headers. That’s nice, but that makes it hard to know exactly what’s being sent on the wire.

And that’s when it occurred to me: why not just watch what’s on the wire? tcpdump is a command line utility which monitors network traffic and prints out packets that you specify.

Here’s the incantation to monitor HTTP traffic to and from a specified host:

sudo tcpdump -l -q -A "host (Specified Host) and tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)"

To explain briefly, sudo runs the command as root, -l enables line buffering, -q hides some of the less interesting protocol information, and -A prints the content of each packet in ASCII. The filtering expression that follows selects packets to or from (Specified Host), to or from port 80, and ignoring SYN, FIN, ACK-only, and other non-data packets. I’ll confess, I don’t understand that last part completely, I copied it from the tcpdump man page.

To make the output a little easier to read, I pipe the output to a Perl script I quickly hacked together which watches for the packet header lines and outputs the ANSI escape codes to render them in bold. But I’ll leave that as an exercise to you, dear reader.

There’s no need to download anything, tcpdump is already installed on your Mac. (I don’t know if it’s part of the standard install or the Developer Tools, but what do you care?)

Now, what would be really sweet is a graphical app to do this. I found Cocoa Packet Analyzer, but it’s a little low-level for me. I don’t care about packet specifics; I’d prefer something that reconstructed the tcp streams in an easy to navigate way.