<?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; programming</title>
	<atom:link href="http://dan.lecocq.us/wordpress/tag/programming/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>Silly Mistakes</title>
		<link>http://dan.lecocq.us/wordpress/2009/12/22/silly-mistakes/</link>
		<comments>http://dan.lecocq.us/wordpress/2009/12/22/silly-mistakes/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 13:33:07 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=784</guid>
		<description><![CDATA[Every programmer has had this, or at least I like to think that every programmer has experienced this &#8211; you compile, press &#8220;go,&#8221; and epic failure. And the joy doesn&#8217;t stop there &#8211; then the debugging begins. Occasionally, one encounters a bug that gets the better of them, sometimes for hours, sometimes for days, and [...]]]></description>
			<content:encoded><![CDATA[<p>Every programmer has had this, or at least I like to think that every programmer has experienced this &#8211; you compile, press &#8220;go,&#8221; and epic failure.  And the joy doesn&#8217;t stop there &#8211; then the debugging begins.  Occasionally, one encounters a bug that gets the better of them, sometimes for hours, sometimes for days, and sometimes for <a href="http://en.wikipedia.org/wiki/Pentium_FDIV_bug">months</a>.</p>
<p>Hairs is pulled, teeth are gnashed, and eyeballs strain, scouring line after line.  You try to convince yourself that your algorithm is correct, and that each line of code is justified.  And yet, it still gets the better of you.</p>
<p>After perhaps eight hours, you swallow your pride, and ask a trusted friend to take a look.  Often the very act of explaining things to another human being is helpful, but sometimes you both have to dig into the code.  Maybe a third friend happens upon the two of you, and joins in.</p>
<p>Then, a light bulb goes off.  If you&#8217;re lucky, it&#8217;s a massive structural change that&#8217;s required, but sometimes, it&#8217;s a single line, or a single word or character, and you suddenly find yourself embarrassed.  But do not be.  Every programmer I&#8217;ve ever met, no matter how qualified has run into these problems.  Still, I find it easy to doubt my competence afterwards.</p>
<p>There are rare and beautiful moments when not only does code compile on the first try, but it runs as expected.  Few and far between, cherish these when they come.</p>
<p>This is all inspired from a recent bug I tracked down.  An embarrassing one.  Sure, had I read the 350 or so pages of the OpenGL ES 2.0 specification, I may have caught it earlier, but this was one of those times when it was a single word that had to change.  <strong>I tell myself that I won&#8217;t keep making these kinds of mistakes, and with each conquered bug I gain a tool, an experience point, and that&#8217;s what makes one&#8217;s craft.</strong></p>
<p>I&#8217;ve looked at the time I spend debugging, and I&#8217;ve noticed that the time it takes to solve a bug can often be reduced by leaving the problem for a bit.  Taking a walk, getting a cup of coffee, or sometimes watching an episode of Arrested Development.  <strong>The desire to find and fix a bug is a siren&#8217;s song &#8211; nearly impossible to walk away from, but often a bad idea.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2009/12/22/silly-mistakes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

