<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>SwarmApps Code Blog</title>
	<atom:link href="http://swarmapps.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://swarmapps.wordpress.com</link>
	<description>A C++ and Objective-C Code Source</description>
	<lastBuildDate>Sun, 20 Dec 2009 04:37:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='swarmapps.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>SwarmApps Code Blog</title>
		<link>http://swarmapps.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://swarmapps.wordpress.com/osd.xml" title="SwarmApps Code Blog" />
	<atom:link rel='hub' href='http://swarmapps.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Sending short alert emails from C++</title>
		<link>http://swarmapps.wordpress.com/2009/12/20/sending-short-alert-emails-from-c/</link>
		<comments>http://swarmapps.wordpress.com/2009/12/20/sending-short-alert-emails-from-c/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 04:34:36 +0000</pubDate>
		<dc:creator>swarmapps</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[General Code]]></category>
		<category><![CDATA[alerts]]></category>
		<category><![CDATA[automated response]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[system administration]]></category>
		<category><![CDATA[texting]]></category>

		<guid isPermaLink="false">http://swarmapps.wordpress.com/?p=36</guid>
		<description><![CDATA[I am a coding-army of one. It&#8217;s just me trying to realize the visions of cool apps that dance in my head throughout the day. This has it&#8217;s advantages (awesome flexibility) and disadvantages (slooowwww progress..). One problem I have is monitoring my servers throughout the day and night. I obviously don&#8217;t have a team of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swarmapps.wordpress.com&amp;blog=10626860&amp;post=36&amp;subd=swarmapps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am a coding-army of one.  It&#8217;s just me trying to realize the visions of cool apps that dance in my head throughout the day.</p>
<p>This has it&#8217;s advantages (awesome flexibility) and disadvantages (slooowwww progress..).  One problem I have is monitoring my servers throughout the day and night.  I obviously don&#8217;t have a team of sys admins watching my server performance all day long and monitoring my network topology.  But, since I am building commercial apps, I do need to get alerts when something terrible has gone wrong.  Hopefully, by planning ahead I&#8217;ll then be able to fix the problem as quickly as possible (like having ssh available on my iPhone!)</p>
<p>There are two ways that are a sure-fire way to alert me quickly, email or a text message.  It turns out, one simple command in a C++ program can send both to me using standard libraries:</p>
<p><em>system(&#8220;echo &#8216;Help I have crashed and I cannot get back up!&#8217; | sendmail myemail@mailserver.com&#8221;);</em></p>
<p>Now this requires your server to both allow &#8220;system()&#8221; calls and to have the standard command line tool &#8220;sendmail&#8221; set up properly.  I HIGHLY recommend you test both things before relying on this for an emergency call (see code snippet below)!</p>
<p>This can be even more useful if your mobile service provider has set up email-to-text services.  Luckily, most of the major providers have this <a href="http://www.stemkoski.com/sending-text-messages-using-php-for-free/" target="_blank">up and running</a>.  You simply replace the email address with the address you need to send to your phone:</p>
<p><em>system(&#8220;echo &#8216;Help I have crashed and I cannot get back up!&#8217; | sendmail 2125551212@txt.myprovider.com&#8221;);</em></p>
<p>Like magic, you&#8217;ll receive an instant alert that will send you scrambling away from almost any social interaction to try and solve!</p>
<p>I place these alerts in my SIGTERM and SIGINT <a href="http://www.cplusplus.com/reference/clibrary/csignal/signal/" target="_blank">signal catchers</a>.</p>
<p>Be warned, however, if your program gets sent a SIGKILL, hangs somewhere unusual, or your server hardware simply crumps somehow, you WILL NOT GET AN ALERT!  This is where you need a redundant, external, monitoring program running on separate hardware, power, and network connection as a belt-and-suspenders check.</p>
<p>You did build an interface in your mission-critical server for an external monitoring program to connect to didn&#8217;t you?</p>
<p>A little snippet of standalone test code you should be able to compile is shown below.  Remember to change the email address to match your own!</p>
<pre>#include &lt;stdlib.h&gt;

int main(int argc, char * argv[]) {
        system("echo 'c++ test' | sendmail myemail@myserver.com");
        return 0;
}</pre>
<p>Happy coding!</p>
<p>Mike</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/swarmapps.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/swarmapps.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/swarmapps.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/swarmapps.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/swarmapps.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/swarmapps.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/swarmapps.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/swarmapps.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/swarmapps.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/swarmapps.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/swarmapps.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/swarmapps.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/swarmapps.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/swarmapps.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swarmapps.wordpress.com&amp;blog=10626860&amp;post=36&amp;subd=swarmapps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swarmapps.wordpress.com/2009/12/20/sending-short-alert-emails-from-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5984ae1b724dfc10c70cf77ed04d59bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swarmapps</media:title>
		</media:content>
	</item>
		<item>
		<title>A quick way to get system resource usage info for your program in C++</title>
		<link>http://swarmapps.wordpress.com/2009/12/20/a-quick-way-to-get-system-resource-usage-info-for-your-program-in-c/</link>
		<comments>http://swarmapps.wordpress.com/2009/12/20/a-quick-way-to-get-system-resource-usage-info-for-your-program-in-c/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 03:59:57 +0000</pubDate>
		<dc:creator>swarmapps</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[General Code]]></category>
		<category><![CDATA[CPU usage]]></category>
		<category><![CDATA[instrumentation]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mac osx]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[system commands]]></category>
		<category><![CDATA[system resources]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://swarmapps.wordpress.com/?p=25</guid>
		<description><![CDATA[I&#8217;m working on a custom server for an app I&#8217;ve been writing.  I&#8217;ve already proven the concept and now I&#8217;m starting to get into serious, releasable code.  One open question I have is how many users can I expect a single instance of my server to handle.  To help me figure that out I&#8217;m instrumenting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swarmapps.wordpress.com&amp;blog=10626860&amp;post=25&amp;subd=swarmapps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a custom server for an app I&#8217;ve been writing.  I&#8217;ve already proven the concept and now I&#8217;m starting to get into serious, releasable code.  One open question I have is how many users can I expect a single instance of my server to handle.  To help me figure that out I&#8217;m instrumenting the heck out of my beta code and recording such things as polling loop time, data flow, and CPU usage.</p>
<p>Finding CPU usage from within a C++ program turned out to be one of the more obscure things to accomplish.  I did plenty of searching online and came up with many answers and even a few libraries that appear to give access to the data.  But, the libraries seemed overly large for what must be a simple task and the examples I found were incomplete.</p>
<p>In the end, I opted for using standard unix/linux commands.  From a command line, if you were to type &#8220;ps aux | grep MyAppName&#8221; you would get a line similar to this:</p>
<p><em>username   20657  99.6  0.0    75328    332 s000  R+   10:38PM   0:13.48 ./MyAppName</em></p>
<p>The first number after &#8220;username&#8221; is the CPU usage (99.6) of your program and the second number is the memory usage (0.0).  It seemed to me you should be able to use this formatted information pretty easily.</p>
<p>It turns out you can using a combination of system() command, command line piping and redirection, and file parsing.  It&#8217;s fast, simple, and effective..  Just what I was looking for.  The following code will get it done for you:</p>
<p>&#8212;-</p>
<pre>#include &lt;boost/algorithm/string.hpp&gt;
#include &lt;boost/regex.hpp&gt;
#include &lt;boost/algorithm/string/regex.hpp&gt;

#include &lt;time.h&gt;
#include &lt;stdlib.h&gt;

#include &lt;sstream&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

using namespace std;
using namespace boost;

int main (int argc, char * const argv[]) {
	// First get my process ID
	int myProcID = getpid();

	// Now we use system commands to find our process id and store the result in a standard text file
	// The "grep -v grep" command filters out the system command itself from the process search.
	stringstream theCommand;
	theCommand &lt;&lt; "ps aux | grep " &lt;&lt; myProcID &lt;&lt; " | grep -v grep &gt; result.txt";
	 time_t lastCheck = time(NULL);
         while (1) {
 		time_t now = time(NULL);
 		 // Only print every 5 seconds or so to prevent overwhelming the output
 		if ((now - lastCheck) &gt;= 4) {
			// The clock code is here to show how efficient this method can be.
			clock_t start = clock();

			// Run the command
			system(theCommand.str().c_str());

			// now open the text file and read the result line
			ifstream infile;
			infile.open("result.txt");
			string aLine;
			getline(infile, aLine);

			// print the line just so you can see it.
			cout &lt;&lt; aLine &lt;&lt; "\n";

			// set up a receiver for the boost split function
			vector&lt;string&gt; processParameters;
			// Split the recieved line on the " " seperator.  There are multiple spaces between parameters, so use token_compress_on
			split( processParameters, aLine, is_any_of(" "), token_compress_on );

			// Output the results
			cout &lt;&lt; "My CPU usage is " &lt;&lt; processParameters[2] &lt;&lt; ", and mem usage is " &lt;&lt; processParameters[3] &lt;&lt; "\n";

			// Close the file (you might want to delete it too.)
			infile.close();

			// reset my time check
			lastCheck = now;

			// show how quick this is.  On my machine it's ~ 650 clock cycles (at 1.83 GHz, thats approx 3 10-millionths of a second)
			cout &lt;&lt; "CPU check took " &lt;&lt; (clock() - start) &lt;&lt; " cycles to complete\n";

		}
	}
        return 0;

}</pre>
<p>&#8212;</p>
<p>I&#8217;ve used the <a href="http://www.boost.org" target="_blank">boost library</a> here to simplify the parsing of the text line.  You don&#8217;t need to do that if you don&#8217;t want to, but I assure you you&#8217;ll find using boost easier than spinning your own text parsing code.</p>
<p>Happy coding!</p>
<p>Mike</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/swarmapps.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/swarmapps.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/swarmapps.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/swarmapps.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/swarmapps.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/swarmapps.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/swarmapps.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/swarmapps.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/swarmapps.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/swarmapps.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/swarmapps.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/swarmapps.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/swarmapps.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/swarmapps.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swarmapps.wordpress.com&amp;blog=10626860&amp;post=25&amp;subd=swarmapps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swarmapps.wordpress.com/2009/12/20/a-quick-way-to-get-system-resource-usage-info-for-your-program-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5984ae1b724dfc10c70cf77ed04d59bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swarmapps</media:title>
		</media:content>
	</item>
		<item>
		<title>A simplification wrapper for the Crypto++ library</title>
		<link>http://swarmapps.wordpress.com/2009/11/24/a-simplification-wrapper-for-the-crypto-library/</link>
		<comments>http://swarmapps.wordpress.com/2009/11/24/a-simplification-wrapper-for-the-crypto-library/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 02:07:42 +0000</pubDate>
		<dc:creator>swarmapps</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[AES]]></category>
		<category><![CDATA[decryption]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[example code]]></category>
		<category><![CDATA[Im]]></category>
		<category><![CDATA[instruction]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://swarmapps.wordpress.com/?p=18</guid>
		<description><![CDATA[While working on a client-server based app, I suddenly came face to face with the need to secure and authenticate certain commands between the client and server.  I was certain there was a library out there to meet my needs and I was right.  I rapidly came across the Crypto++ library, a powerful set of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swarmapps.wordpress.com&amp;blog=10626860&amp;post=18&amp;subd=swarmapps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While working on a client-server based app, I suddenly came face to face with the need to secure and authenticate certain commands between the client and server.  I was certain there was a library out there to meet my needs and I was right.  I rapidly came across the <a title="crypto++ library homepage" href="http://www.cryptopp.com/" target="_blank">Crypto++ library</a>, a powerful set of cryptographic tools written and tested by experts.  I was in heaven!  Think of the time this has saved me!</p>
<p>There was only one problem.  The Crypto++ library is indeed a very powerful set of cryptographic tools, but I found the learning curve to get my first apps running with it to be very high.  I examined all the sample code I could find, worked through the API documentation, and examined the validation and test code before I finally started catching on.  Then I had to &#8220;grep&#8221; my way through the files to identify which headers I needed to include for a given function.</p>
<p>DISCLAIMER: All of this brainache is undoubtedly due to my own lack of skill and formal training.</p>
<p>Nevertheless, I overcame my deficiencies and learned how to encrypt and decrypt at will.  But, I was unhappy with the amount of extra code I had to put everywhere I wanted to encrypt and decrypt something.  So, I ended up writing a simplification wrapper to make my work easier.  My wrapper uses only a small subset of functionality and algorithms in Crypto++, but it makes using that subset very easy.</p>
<p>As a very small example, here is how you encrypt a given plaintext string:</p>
<p><span style="color:#008000;">string cipherText = BasicCryptoPPWrap::EncryptStringAES(&#8220;Encrypt me&#8221;, key, initVector, errFlag, errMsg);</span></p>
<p>The wrapper also implements a custom Advanced Encryption Standard protocol that stores the initial vector with the encrypted data, so you don&#8217;t have to track it yourself.  I have used this wrapper very successfully in my own work, I think you&#8217;ll find it useful too!</p>
<p>Wrapper functions:</p>
<ul>
<li>Implements symmetrical AES encryption and decryption</li>
<li>Hex encoding of binary data</li>
<li>Hex decoding of hex-coded strings</li>
<li>Generic string and file encryption</li>
<li>Custom protocol string and file encryption</li>
<li>Random bit and byte generators</li>
<li>SHA-256 Hash generator</li>
</ul>
<p>The wrapper is not a standalone solution.  It requires your code to link to libcryptopp and know where the cryptopp header files are found.  The <a title="Crypto++ homepage" href="http://www.cryptopp.com/" target="_blank">Crypto++</a> website can help you set up your system.  Once you get the validation suite to run successfully, you will be able to use this wrapper.</p>
<p>Get the files here:</p>
<ul>
<li><a title="Wrapper header source" href="http://pastebin.com/fa4454e3" target="_blank">BasicCryptoPPWrap.h</a> &#8211; The header file that makes it all happen</li>
<li><a title="Wrapper example code" href="http://pastebin.com/f6bd13d59" target="_blank">example.cpp</a> &#8211; An example file that takes you through all the functions available in the wrapper</li>
</ul>
<p>Good luck!  I release this wrapper into the public domain, but if you happen to use the code I wouldn&#8217;t mind hearing about it!  Credit might be nice too!</p>
<p>Mike</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/swarmapps.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/swarmapps.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/swarmapps.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/swarmapps.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/swarmapps.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/swarmapps.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/swarmapps.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/swarmapps.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/swarmapps.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/swarmapps.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/swarmapps.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/swarmapps.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/swarmapps.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/swarmapps.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swarmapps.wordpress.com&amp;blog=10626860&amp;post=18&amp;subd=swarmapps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swarmapps.wordpress.com/2009/11/24/a-simplification-wrapper-for-the-crypto-library/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5984ae1b724dfc10c70cf77ed04d59bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swarmapps</media:title>
		</media:content>
	</item>
		<item>
		<title>An easy, light-weight, dynamically adjustable, global logging class for C++</title>
		<link>http://swarmapps.wordpress.com/2009/11/23/an-easy-light-weight-dynamically-adjustable-global-logging-class-for-c/</link>
		<comments>http://swarmapps.wordpress.com/2009/11/23/an-easy-light-weight-dynamically-adjustable-global-logging-class-for-c/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 03:49:17 +0000</pubDate>
		<dc:creator>swarmapps</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[General Code]]></category>
		<category><![CDATA[example code]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[instruction]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://swarmapps.wordpress.com/?p=6</guid>
		<description><![CDATA[Every programmer eventually finds a need to display warnings, debug information, or just status updates as they build their code and watch it run.  Lots of times this takes the form of streaming text to std::cout. But, what if you only want certain information to print at certain times, or you&#8217;d like to suddenly change [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swarmapps.wordpress.com&amp;blog=10626860&amp;post=6&amp;subd=swarmapps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Every programmer eventually finds a need to display warnings, debug information, or just status updates as they build their code and watch it run.  Lots of times this takes the form of streaming text to <span style="color:#993366;">std::</span><span style="color:#993366;">cout. </span>But, what if you only want certain information to print at certain times, or you&#8217;d like to suddenly change from logging to a terminal to logging to a file?</p>
<p>After several tries, I came up with my logger class.  Basically, using static class members and c-style macros, I came up with a logging method that turns on and off easily, allows easy redirection, and is easy to use everywhere in your code.</p>
<p>At it&#8217;s simplest, this logger is used as such:</p>
<pre>#include &lt;iostream&gt;
#include "logger.h"

using namespace std;	

int main (int argc, char * const argv[]) {
	bool someLogType = true;
	LOG(someLogType) &lt;&lt; "This will be logged to clog\n";
	return 0
}</pre>
<p>But the logger class also offers globally accessible log types and the ability to redirect the output wherever you want.  The following snippet shows this use.</p>
<pre>#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include "logger.h"

using namespace std;	

int main (int argc, char * const argv[]) {
	// Set these to turn various logs on or off dynamically
	NORMAL = true;
	DEBUG = false;
	VERBOSE = true;

	LOG(NORMAL) &lt;&lt; "This will log\n";
	LOG(DEBUG) &lt;&lt; "But this one won't\n";
	LLOG(VERBOSE) &lt;&lt; "This one will log, but with the name of the logtype prepended to the message\n";

	// Now we redirect the log output to a custom location.  This could be a file or any ostream
	stringstream newLog;
	logger::setOutstream(newLog);

	LOG(NORMAL) &lt;&lt; "This will log\n";
	LOG(DEBUG) &lt;&lt; "But this one won't\n";
	LLOG(VERBOSE) &lt;&lt; "This one will log, but with the name of the logtype prepended to the message\n";

	string results = newLog.str();
	cout &lt;&lt; "In the new log: \n" &lt;&lt; results &lt;&lt; "\n";

	// Always reset the logger to clog before the program exits or you will get a BAD_ACCESS error
	// I'm still trying to work out that problem, but this is an easy fix for now
	logger::setOutstream(clog);
	return 0;

}</pre>
<p>Get the code here:</p>
<ul>
<li><a title="header file for logger" href="http://pastebin.com/f45f6b0de" target="_blank">logger.h</a></li>
<li><a title="Body file for logger class" href="http://pastebin.com/d73c152e9" target="_blank">logger.cpp</a></li>
<li><a title="Example code to demonstrate use of logger class" href="http://pastebin.com/f73edf9fa" target="_blank">example.cpp</a></li>
</ul>
<p>Good luck and good coding!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/swarmapps.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/swarmapps.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/swarmapps.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/swarmapps.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/swarmapps.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/swarmapps.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/swarmapps.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/swarmapps.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/swarmapps.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/swarmapps.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/swarmapps.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/swarmapps.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/swarmapps.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/swarmapps.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swarmapps.wordpress.com&amp;blog=10626860&amp;post=6&amp;subd=swarmapps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swarmapps.wordpress.com/2009/11/23/an-easy-light-weight-dynamically-adjustable-global-logging-class-for-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5984ae1b724dfc10c70cf77ed04d59bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swarmapps</media:title>
		</media:content>
	</item>
	</channel>
</rss>
