Below is a class and methods for adding to a log file. A way, small way, to debug your applications if your running without the console and for people to be able to send something back to you so you can figure out why the application is having errors on their system. The code pretty much explains itself, I hope that it can come in to use by someone and prove to be helpful
Code: Select all
//--------------------------------------------------------------------------
/// CLogger Class
/// Copyright 2006, Alexander M. Loren
/// Notes: This class is totaly library indipentant, all that is needed
/// to use this class; is to plug it into your solution and compile.
/// Credits: nVidia example referance.
//--------------------------------------------------------------------------
#ifndef __CLOGGER_H__
#define __CLOGGER_H__
#include <fstream>
#include <vector>
#include <string>
class CLogger
{
private:
static std::vector<std::string> hLogQueue;
static const char* hLogPath;
public:
static void Initialize(const char* lpstrLogFile = NULL);
static void Process();
static void WriteInfoLog(const std::string& lpstrInfo,bool bTab = false);
static void WriteErrorLog(const std::string& lpstrError,bool bTab = false);
static void WriteFatalLog(const std::string& lpstrFatal,bool bTab = false);
};
#endif
Code: Select all
//--------------------------------------------------------------------------
/// CLogger Class
/// Copyright 2006, Alexander M. Loren
/// Notes: This class is totaly library indipentant, all that is needed
/// to use this class; is to plug it into your solution and compile.
/// Credits: nVidia example referance.
//--------------------------------------------------------------------------
#include "CLogger.h"
//--------------------------------------------------------------------------
/// Summary: Pre-Definition of methods used.
//--------------------------------------------------------------------------
std::vector<std::string> CLogger::hLogQueue;
const char* CLogger::hLogPath = NULL;
//--------------------------------------------------------------------------
/// Method: CLogger::Initialize()
/// Summary: Initialization of logger engine.
/// Paramaters: (const char* - Log file path name)
//--------------------------------------------------------------------------
void CLogger::Initialize(const char* lpstrLogFile)
{
hLogPath = !lpstrLogFile ? "console.log" : lpstrLogFile;
std::ofstream hLogFile(hLogPath);
hLogFile.close();
}
//--------------------------------------------------------------------------
/// Method: CLogger::Process()
/// Summary: Run through queued list and output to log file.
/// Paramaters: -NONE-
//--------------------------------------------------------------------------
void CLogger::Process()
{
if(!hLogQueue.size())
{
return;
}
std::ofstream hLogFile(hLogPath,std::ios::app);
for(size_t t = 0; t < hLogQueue.size(); t++)
{
hLogFile << hLogQueue[t];
}
hLogQueue.clear();
hLogFile.close();
}
//--------------------------------------------------------------------------
/// Method: CLogger::WriteInfoLog()
/// Summary: Writes the information message to the log.
/// Paramaters: (std::string& - Error message)
//--------------------------------------------------------------------------
void CLogger::WriteInfoLog(const std::string& lpstrInfo,bool bTab)
{
if(bTab)
{
hLogQueue.push_back(std::string("<+> \t") + lpstrInfo + "\n");
}
else
{
hLogQueue.push_back(std::string("<+> ") + lpstrInfo + "\n");
}
if(hLogQueue.size() >= 10)
{
Process();
}
}
//--------------------------------------------------------------------------
/// Method: CLogger::WriteErrorLog()
/// Summary: Writes the error message to the log.
/// Paramaters: (std::string& - Error message)
//--------------------------------------------------------------------------
void CLogger::WriteErrorLog(const std::string& lpstrError,bool bTab)
{
if(lpstrError.size())
{
if(bTab)
{
hLogQueue.push_back(std::string("<!> \t") + lpstrError + "\n");
}
else
{
hLogQueue.push_back(std::string("<!> ") + lpstrError + "\n");
}
Process();
}
}
//--------------------------------------------------------------------------
/// Method: CLogger::WriteFatalLog()
/// Summary: Writes the fatal error message to the log.
/// Paramaters: (std::string& - Fatal error message)
//--------------------------------------------------------------------------
void CLogger::WriteFatalLog(const std::string& lpstrFatal,bool bTab)
{
if(lpstrFatal.size())
{
if(bTab)
{
hLogQueue.push_back(std::string("<X> \t") + lpstrFatal + "\n");
}
else
{
hLogQueue.push_back(std::string("<X> ") + lpstrFatal + "\n");
}
Process();
}
}