<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Party to the World &#187; hacks</title>
	<atom:link href="http://dan.lecocq.us/wordpress/tag/hacks/feed/" rel="self" type="application/rss+xml" />
	<link>http://dan.lecocq.us/wordpress</link>
	<description>Life, love, and computer science</description>
	<lastBuildDate>Fri, 06 Jan 2012 17:21:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Wrapping printf(1)</title>
		<link>http://dan.lecocq.us/wordpress/2011/02/24/wrapping-printf/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/02/24/wrapping-printf/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 19:39:18 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[ninja magic]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=914</guid>
		<description><![CDATA[Working on an application that had become a little&#8230; verbose, I decided it was finally time to wrap my prints in a function that could easily be switched on or off depending on whether or not I wanted it to be verbose. One approach I had seen before (from my OS professor) that has a [...]]]></description>
			<content:encoded><![CDATA[<p>Working on an application that had become a little&#8230; verbose, I decided it was finally time to wrap my prints in a function that could easily be switched on or off depending on whether or not I wanted it to be verbose.  One approach I had seen before (from my OS professor) that has a certain amount of merit is to wrap statements with a macro:<br />
<code><br />
#ifdef DEBUG<br />
printf(....);<br />
#endif<br />
</code></p>
<p>The nice thing about this approach is that debugging can be turned on or off easily at compile time.  However, my experience has been that it leads to a lot of typing, and seeing too many macros in the middle of code makes my brain explode in a fiery rage.  So, I figured that if I wrapped my prints in another function (I called mine &#8216;log&#8217; and &#8216;error&#8217;), I could avoid this whole mess and keep my sanity.  I&#8217;ve done this with a number of other projects in other languages, but I had to learn some magic to do it in C.</p>
<p>Lesson Learned #1 : Variable arguments. It turns out you can define functions that take a variable number of arguments with <em>va_list</em> (from <a href="http://en.wikipedia.org/wiki/Stdarg.h"><stdarg.h></a>).  You define such a function:<br />
<code><br />
void log(const char* fmt, ...) {<br />
    va_list args;<br />
    va_start(args, fmt);<br />
    ...<br />
    va_end(args);<br />
}<br />
</code></p>
<p>Lesson Learned #2 : However, from what I can gather, you can&#8217;t just inject printf directly into this. However, having anticipated this, there is a set of functions designed for cases like this: <em>vfprintf, vprintf, vsnprintf, vsprintf</em>. The &#8216;v&#8217; stands for va_list (variable-argument list), and you can use them just like you&#8217;d expect:<br />
<code><br />
void log(const char* fmt, ...) {<br />
    va_list args;<br />
    va_start(args, fmt);<br />
    fprintf(log_fd, "NOTE : ");<br />
    vfprintf(log_fd, fmt, args);<br />
    va_end(args);<br />
}<br />
</code></p>
<p>The thing I like about this approach is that you have control over how log messages get printed in <em>one</em> place.  So, for example, if I provided another function, <em>setLogFD</em>, then I could easily just set the file descriptor where all log messages get printed.  So easy!  Something I&#8217;ve used this for in other instances (especially servers) is to also inject additional information like a timestamp on every message.  So, when I call:<br />
<code><br />
log("Some event '%s' just happened.\n", event_name);<br />
</code></p>
<p>Then I automatically get &#8220;NOTE : &#8221; and maybe a timestamp prefixed on that message.  Which make code look clean, and adds a great deal of functionality.  I actually added another function <em>error(&#8230;)</em> that prints to a different file descriptor in case I want to suppress debug messages, but no error messages.  For additional layers of debugging, you might do something like this:<br />
<code><br />
void log(int level, const char* fmt, ...) {<br />
    va_list args;<br />
    va_start(args, fmt);<br />
    FILE* fd = my_log_files[level];<br />
    fprintf(fd, "NOTE : ");<br />
    vfprintf(fd, fmt, args);<br />
    va_end(args);<br />
}<br />
</code></p>
<p>This way, at startup, you could easily set some of the file descriptors in <em>my_log_files</em> to stderr and some to point to /dev/null or otherwise dissolve into the ether.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/02/24/wrapping-printf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Like MacGyver, Survivin&#8217; For The Very First Time</title>
		<link>http://dan.lecocq.us/wordpress/2007/10/03/like-macgyver-survivin-for-the-very-first-time/</link>
		<comments>http://dan.lecocq.us/wordpress/2007/10/03/like-macgyver-survivin-for-the-very-first-time/#comments</comments>
		<pubDate>Thu, 04 Oct 2007 03:17:03 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[can]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[MacGyver]]></category>
		<category><![CDATA[stove]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[toys]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/2007/10/03/like-macgyver-survivin-for-the-very-first-time/</guid>
		<description><![CDATA[Like MacGy-ay-ay-ay-ver. Scenario: you need to boil some water to incapacitate a guard (a hot cup of tea puts him right to sleep. Also, he&#8217;s very trusting when it comes to strangers offering him tea), and you&#8217;ve got sandpaper, two soda cans, a razor, a tuft of fiberglass insulation, a tack and a bottle of [...]]]></description>
			<content:encoded><![CDATA[<p>Like MacGy-ay-ay-ay-ver.</p>
<p>Scenario: you need to boil some water to incapacitate a guard (a hot cup of tea puts him right to sleep.  Also, he&#8217;s very trusting when it comes to strangers offering him tea), and you&#8217;ve got sandpaper, two soda cans, a razor, a tuft of fiberglass insulation, a tack and a bottle of Heet at your disposal.  Also, the internet to watch <a href="http://www.metacafe.com/watch/839102/cool_little_miniature_stove/">this metacafe</a> video.</p>
<p><a href='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/10/macgyver-stove.png' title='MacGyver Stove'><img style="float:left;margin:5px" src='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/10/macgyver-stove.thumbnail.png' alt='MacGyver Stove' /></a>I had seen this before and in various design complexities, but I really like the compactness and ease of this one.  I decided to scrounge up some Heet ($2.10 at a local gas station), a couple of cans, sandpaper and a razor blade (both on hand), and pull a little piece of insulation off of some that happened to be sitting in our basement.</p>
<p>It got surprisingly hot &#8211; I tried setting an oven rack over it with a kettle (to knock out that tea-loving guard), and it started to melt one of the bars of the rack.  I expected it to be hot, but not hot enough to melt parts of an oven rack.  Who knew?</p>
<p>Success: certainly.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2007/10/03/like-macgyver-survivin-for-the-very-first-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ghetto Shunt</title>
		<link>http://dan.lecocq.us/wordpress/2007/09/27/ghetto-shunt/</link>
		<comments>http://dan.lecocq.us/wordpress/2007/09/27/ghetto-shunt/#comments</comments>
		<pubDate>Thu, 27 Sep 2007 18:18:15 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[ghetto shunt]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[XBMC]]></category>
		<category><![CDATA[xbox]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/2007/09/27/ghetto-shunt/</guid>
		<description><![CDATA[I&#8217;ve been putting together a media server for my newly-hacked XBox media center, and I had an old 120 gig hard drive laying around that wasn&#8217;t seeing much use. I thought I&#8217;d throw it in the box, giving me 240 gigs total to keep online for my viewing pleasure. The problem was, however, that I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been putting together a media server for my newly-hacked <a href="http://dan.lecocq.us/wordpress/2007/09/24/xbmc/">XBox media center</a>, and I had an old 120 gig hard drive laying around that wasn&#8217;t seeing much use.  I thought I&#8217;d throw it in the box, giving me 240 gigs total to keep online for my viewing pleasure.  The problem was, however, that I had removed the shunt at some point, and couldn&#8217;t find one around the house (who keeps these, anyway)?  So, presenting my very ghetto make-shift shunt:</p>
<p><a href='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/sany0050.png' title='Ghetto Shunt'><img style="float:left;margin:5px" src='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/sany0050.thumbnail.png' alt='Ghetto Shunt' /></a>Made from one of those yellow connectors (if you know the name of what I&#8217;m talking about, please let me know), I snapped off the part where you put the wire in and then clamp it with pliers.  Before using that, I made sure I had the right setting (I wanted this one as the slave HD), by touching a screwdriver to the two pins associated with that setting and held it there as I booted the machine.  Probably a safe/smart move (but in all reality, how much current flows through those pins, anyway?).</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2007/09/27/ghetto-shunt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XBMC</title>
		<link>http://dan.lecocq.us/wordpress/2007/09/24/xbmc/</link>
		<comments>http://dan.lecocq.us/wordpress/2007/09/24/xbmc/#comments</comments>
		<pubDate>Tue, 25 Sep 2007 00:18:05 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[hacks]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[toys]]></category>
		<category><![CDATA[XBMC]]></category>
		<category><![CDATA[xbox]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/2007/09/24/xbmc/</guid>
		<description><![CDATA[My XBox is now my gateway to my media. $4.73 for Splinter Cell (how this particular hack gets the Linux installer running), and $21.81 for an Action Replay kit to transfer the files. Now, all the media on the LAN are at the fingertips of my XBox. Music, Pictures, Movies and TV Shows &#8211; the [...]]]></description>
			<content:encoded><![CDATA[<p>My XBox is now my gateway to my media.  $4.73 for Splinter Cell (how this particular hack gets the Linux installer running), and $21.81 for an Action Replay kit to transfer the files. Now, all the media on the LAN are at the fingertips of my XBox.  Music, Pictures, Movies and TV Shows &#8211; the whole gambit.</p>
<p>I used <a href="http://lifehacker.com/software/geek-to-live/transform-your-classic-xbox-into-a-killer-media-center-299809.php">this Lifehacker article</a> as a guideline.  All in all, it took about an hour.</p>
<table>
<tr>
<td>
<a href='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot003.png' title='Watching Scrubs'><img src='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot003.thumbnail.png' alt='Watching Scrubs' /></a>
</td>
<td>
<a href='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot004.png' title='TV Shows'><img src='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot004.thumbnail.png' alt='TV Shows' /></a>
</td>
<td>
<a href='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot006.png' title='Music'><img src='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot006.thumbnail.png' alt='Music' /></a>
</td>
</tr>
<tr>
<td>
<a href='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot008.png' title='Weather / RSS'><img src='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot008.thumbnail.png' alt='Weather / RSS' /></a>
</td>
<td>
<a href='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot009.png' title='Watching A Movie'><img src='http://dan.lecocq.us/wordpress/wp-content/uploads/2007/09/screenshot009.thumbnail.png' alt='Watching A Movie' /></a>
</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2007/09/24/xbmc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

