Very Basic Logging for C++
by karl.lockhart on Jan.04, 2009, under Code
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 <iostream>
#include <fstream>
#include <string>
#include <time.h>
#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&);
Logger& operator= (const Logger&);
private:
std::ofstream file;
static Logger* pinstance;
};
#endif /* LOGGER_H_ */
/*
* 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 >= LOG_LEVEL_NO_LOGGING) && (logLevel <= LOG_LEVEL_VERY_VERBOSE))
{
loggingLevel = logLevel;
}
}
void Logger::SetLoggingTarget(int logTarget){
if ((logTarget >= LOG_TARGET_FILE) && (logTarget <= LOG_TARGET_BOTH))
{
loggingTarget = logTarget;
}
}
void Logger::Log(int logLevel, std::string logMsg)
{
if (logLevel <= loggingLevel)
{
std::string msgToWrite;
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &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 << msgToWrite;
break;
case LOG_TARGET_STDOUT:
std::cout << msgToWrite;
break;
case LOG_TARGET_BOTH:
msgToWrite += "\n";
file << msgToWrite;
std::cout << msgToWrite;
break;
}
file.flush();
}
}
Example Usage:
Logger *logger = Logger::Instance();
.
.
.
logger->Log(logger->LOG_LEVEL_INFO, “Starting the Plugin”);
Concept
by karl.lockhart on Dec.19, 2008, under Code
Well I have finally decided on the concept I will move forward with as the game I will develop. This decision has been a long time coming. I think the genre I have decided on is rather underused and the game will be fun above all else. The work required is not too bad either, I will disclose more after I have fleshed out the design documents. A page here is forthcoming as well.
Pics I Didn’t Get to Share
by karl.lockhart on Dec.16, 2008, under Life

- The Server, Ares, rides again

The Moon looked awesome
Coder’s Block
by karl.lockhart on Dec.16, 2008, under Code
So I am all set up to write my excellent game (demo). I have come up with and shot down several of my own ideas. I think I have coder’s block. I know I have the ability to code this, however, I seem to be having problems designing. I think I need to just take some time off and unwind. The cold spell has actually given me a few ideas.
We’ll see how it turns out I guess.
Tools of the Trade
by karl.lockhart on Dec.02, 2008, under Code
The last two weeks off work have been a very creative time for me. I have been playing around with a few tools that I can see making garage/one man game development easier. Before last week, I was very much into Irrlicht and Newton, but this all has changed. My toolbelt has been helped out quite a bit by changing perspectives. My new tools of choice jME (the jMonkey Engine), jME physics, Blender and MakeHuman.
JMonkey and JME physicis are pretty cool tools, for Java. The game/graphics engine is very well featured and the physics engine seems to be the same. They use jinput and LWJGL and provide very simple and useable starting points for creating games. I very much like both of these projects. C++ will always be the king of speed but Java doesn’t do that badly anymore.
Since I have been weaned from the high cost 3DSMAX and Poser, I have been using Blender. Blender has served me very well, it is an excellent tool. It provides functionality that you would otherwise have to pay high prices for. The place I have lacked here is in my ability to create good looking humanoids. Enter MakeHuman, this tool may not be as deep as Poser is but I really like the models it produces. The project seems to be heading towards a good place and I like it alot.
These tools are ecellent, and I have played with them quite a bit in the last two weeks. I am looking forward to finishing my demo now.
Ps3dise lost!
by karl.lockhart on Nov.11, 2008, under Life
Well, I had my first PS3 event. Nothing too major and kinda a good reset for the system.
So, here is what happened. I applied the latest upgrade after 50+ days idle (sometimes I hate my job). Figured, blow off some steam and play some GTA IV (Karl’s official game of blowing off steam). Update there as well, cool. So, then my TV goes blank… WTF!!! WTF!!! arghhh!!!! Power down, power down!! Beep! WTF is beep!!! Red light, oh no!!! Power…ooooooh….soothing Sony sound…mmmm….GTA IV loads up…ERROR….ERROR!!!….ERROR??!!! quit game….my savegames….all…say…GTA IV…oohhh grid savegame where art thou??!!….Neeeeiiiiiiinnnnnnnn!!!!!
So anyways, I had to format my drive
and my PS3 works fine again. But I guess it’s a good excuse to earn trophies…and I have my PS3 back so yay!!
CAUTION : SMS Scammers Ahead
by karl.lockhart on Oct.17, 2008, under Life
This morning I received a wierd text message:
1010100227
FRM:SYNERGYBANK
SUBJ:NOTICE
MSG: Your SYNERGY BANK account is closed due to unusual activity, call us now at 18669282990.
As far as I know, at least one other ATT customer got the same message. Wierdly ATT customer service had no clue what I was talking about and kept reassuring me that there was no fraud on my phone account. The even kindly offered to transfer me to billing to reassure me of this. Useless.
My next consideration was to call Synergy Bank, who seemed aware of the problem and got the call in number from me in order to report it to the FBI. Finally, closure.
Important thing to do in this case is NOT call the number and NOT give them any information. Synergy Bank seems to be aware and working the issue, you may want to give them a call and give them the number on your version of the SMS.
I am kinda concerned that SMS is now being used for evil, even more concerned that there isn’t some sort of process for ATT to take care of issues with their network being used for this sort of thing.
Remember it is always important not to call the numbers in these messages and give any information. Instead, find a confirmed number for the company if it can be verified to be legitimate and inform them of the message so that they can take action.
Family Reunion
by karl.lockhart on Jul.28, 2008, under Life
Well I am on my way to the family reunion. Rented a car and hit the road, stop 1 visiting family in Stone Mountain, GA. Got here yesterday after my flight, took the suggestion to stay the night since I was falling asleep on the couch and have mountain driving ahead of me. My uncle treated me to a pretty cool restaurant down here called ‘Nava’, good food, good times. Kinda ready to hit the road. Will post more and add pics as the trip progresses.
New Apartment
by karl.lockhart on Jul.28, 2008, under Life
I have finally moved in to my new apartment. It is awesome, or will be after I unpack. I am loving the space I have there, the staff is nice and all in all it is the kinda place I think I could enjoy living in. The move was crazy as all moves are, but it is over.
Newton and Irrlicht
by admin on Jul.07, 2008, under Code
This weekend, was one of celebration for many reasons. Independence/freedom was definitely the bigger of the two ideas worthy of celebration. But my small win was finally integrating the Newton dynamics library with the Irrlicht 3D engine.
The libraries do integrate pretty well, the only issues I had were with some of the subtleties. In general, it is simply a matter of figuring out how to translate between Newton and Irrlicht’s matrices. This is actually accomplished very easily with a memory copy. I fought improper placement of the x, y, z values but once I figured out where they should be, the libraries made the problems minimal.