<?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>Life and Code &#187; logging</title>
	<atom:link href="http://www.karlockhart.com/wordpress/tag/logging/feed" rel="self" type="application/rss+xml" />
	<link>http://www.karlockhart.com/wordpress</link>
	<description>Pixels bleed just like us...</description>
	<lastBuildDate>Wed, 14 Jul 2010 17:13:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Very Basic Logging for C++</title>
		<link>http://www.karlockhart.com/wordpress/2009/01/04/very-basic-logging-for-c</link>
		<comments>http://www.karlockhart.com/wordpress/2009/01/04/very-basic-logging-for-c#comments</comments>
		<pubDate>Mon, 05 Jan 2009 02:24:01 +0000</pubDate>
		<dc:creator>karl.lockhart</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[project]]></category>

		<guid isPermaLink="false">http://www.karlockhart.com/wordpress/?p=28</guid>
		<description><![CDATA[I am always in need of very basic logging for my projects, here is the logger I wrote for my XPFF project, remember this code is as-is as the project is still very active: /* * Logger.h * * Created on: Jan 4, 2009 * Author: Karl */ #include &#60;iostream&#62; #include &#60;fstream&#62; #include &#60;string&#62; #include [...]]]></description>
			<content:encoded><![CDATA[<p>I am always in need of very basic logging for my projects, here is the logger I wrote for my XPFF project, remember this code is as-is as the project is still very active:</p>
<pre><span id="more-28"></span>/*
 * Logger.h
 *
 *  Created on: Jan 4, 2009
 *      Author: Karl
 */
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;time.h&gt;

#ifndef LOGGER_H_
#define LOGGER_H_

class Logger {
public:
	static Logger* Instance();
	~Logger();

	//Logging Levels
	const static int LOG_LEVEL_NO_LOGGING = 0;
	const static int LOG_LEVEL_FATAL = 1;
	const static int LOG_LEVEL_SEVERE = 2;
	const static int LOG_LEVEL_WARNING = 3;
	const static int LOG_LEVEL_INFO = 4;
	const static int LOG_LEVEL_CONFIG = 5;
	const static int LOG_LEVEL_VERBOSE = 6;
	const static int LOG_LEVEL_VERY_VERBOSE = 7;

	//Logging Targets
	const static int LOG_TARGET_FILE = 1;
	const static int LOG_TARGET_STDOUT = 2;
	const static int LOG_TARGET_BOTH = 3;

	void SetLoggingLevel(int);

	void SetLoggingTarget(int);

	void Log(int, std::string);

protected:
	Logger();
	Logger(const Logger&amp;);
	Logger&amp; operator= (const Logger&amp;);

private:
	std::ofstream file;
	static Logger* pinstance;
};

#endif /* LOGGER_H_ */</pre>
<pre>/*
 * Logger.cpp
 *
 *  Created on: Jan 4, 2009
 *      Author: Karl
 */

#include "Logger.h"

Logger* Logger::pinstance = 0;
int loggingLevel = 4;
int loggingTarget = 1;

Logger* Logger::Instance()
{
	if (pinstance == 0)
	{
		pinstance = new Logger;
	}
	return pinstance;
}

Logger::Logger()
{
	file.open ("logger.log");
	Log(LOG_LEVEL_INFO, "Log Opened");
}

Logger::~Logger()
{
	file.close();
}

void Logger::SetLoggingLevel(int logLevel)
{
	if ((logLevel &gt;= LOG_LEVEL_NO_LOGGING) &amp;&amp; (logLevel &lt;= LOG_LEVEL_VERY_VERBOSE))
	{
		loggingLevel = logLevel;
	}
}

void Logger::SetLoggingTarget(int logTarget){
	if ((logTarget &gt;= LOG_TARGET_FILE) &amp;&amp; (logTarget &lt;= LOG_TARGET_BOTH))
	{
		loggingTarget = logTarget;
	}
}

void Logger::Log(int logLevel, std::string logMsg)
{
	if (logLevel &lt;= loggingLevel)
	{
		std::string msgToWrite;
		time_t rawtime;
		struct tm * timeinfo;

		time ( &amp;rawtime );
		timeinfo = localtime ( &amp;rawtime );
		msgToWrite += asctime(timeinfo);
		msgToWrite.erase(msgToWrite.end()-1,msgToWrite.end());
		msgToWrite += " - ";

		switch (logLevel) {

		case LOG_LEVEL_FATAL:
			msgToWrite += "FATAL - ";
			break;
		case LOG_LEVEL_SEVERE:
			msgToWrite += "SEVERE - ";
			break;
		case LOG_LEVEL_WARNING:
			msgToWrite += "WARNING - ";
			break;
		case LOG_LEVEL_INFO:
			msgToWrite += "INFO - ";
			break;
		case LOG_LEVEL_CONFIG:
			msgToWrite += "CONFIG - ";
			break;
		case LOG_LEVEL_VERBOSE:
			msgToWrite += "VERBOSE - ";
			break;
		case LOG_LEVEL_VERY_VERBOSE:
			msgToWrite += "VERY VERBOSE - ";
			break;
		default:
			msgToWrite += "UNKNOWN LEVEL - ";
			break;
		}

		msgToWrite += logMsg;
		msgToWrite += "\n";

		switch(loggingTarget){
		case LOG_TARGET_FILE:
			file &lt;&lt; msgToWrite;
			break;
		case LOG_TARGET_STDOUT:
			std::cout &lt;&lt; msgToWrite;
			break;
		case LOG_TARGET_BOTH:
			msgToWrite += "\n";
			file &lt;&lt; msgToWrite;
			std::cout &lt;&lt; msgToWrite;
			break;
		}
	file.flush();
	}

}</pre>
<p>Example Usage:</p>
<p>Logger *logger = Logger::Instance();</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>logger-&gt;Log(logger-&gt;LOG_LEVEL_INFO, &#8220;Starting the Plugin&#8221;);</p>
<p> </p>
<pre> </pre>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c&amp;title=Very+Basic+Logging+for+C%2B%2B" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c&amp;title=Very+Basic+Logging+for+C%2B%2B" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c&amp;title=Very+Basic+Logging+for+C%2B%2B" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c&amp;headline=Very+Basic+Logging+for+C%2B%2B" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Very+Basic+Logging+for+C%2B%2B&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Very+Basic+Logging+for+C%2B%2B&amp;u=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Very+Basic+Logging+for+C%2B%2B&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Very+Basic+Logging+for+C%2B%2B&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Very+Basic+Logging+for+C%2B%2B&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c&amp;title=Very+Basic+Logging+for+C%2B%2B&amp;summary=&amp;source=" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F01%2F04%2Fvery-basic-logging-for-c" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://www.karlockhart.com/wordpress/2009/01/04/very-basic-logging-for-c/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
