Created
December 23, 2012 20:23
-
-
Save Wunkolo/4365812 to your computer and use it in GitHub Desktop.
Logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Logger.h" | |
#include <iostream> | |
#include <stdio.h> | |
#include <time.h> | |
#include <sstream> | |
#include <stdarg.h> | |
void Logger::SetLogFile(const std::string FileName) | |
{ | |
sFileName = FileName; | |
} | |
//Notes: | |
//VA lists always act weird when you pass an argument that uses | |
//references rather than just plain objects | |
//attempts at using std::string& would cause it to use random pointers | |
//stick to using plain types such as std::string instead | |
void Logger::Log(const std::string fmt, ...) | |
{ | |
if (fmt == "") | |
{ | |
return; | |
} | |
va_list args; | |
char buffer[1024]; | |
memset(buffer,0,1024); | |
va_start(args,fmt); | |
vsprintf(buffer,fmt.c_str(),args); | |
va_end(args); | |
Write(std::string(buffer)); | |
} | |
void Logger::Error(const std::string fmt, ...) | |
{ | |
if (fmt == "") | |
{ | |
return; | |
} | |
va_list args; | |
char buffer[1024]; | |
memset(buffer,0,1024); | |
va_start(args,fmt); | |
vsprintf(buffer,fmt.c_str(),args); | |
va_end(args); | |
Write("Error: " + std::string(buffer)); | |
} | |
void Logger::FatalError(const std::string fmt, ...) | |
{ | |
if (fmt == "") | |
{ | |
return; | |
} | |
va_list args; | |
char buffer[1024]; | |
memset(buffer,0,1024); | |
va_start(args,fmt); | |
vsprintf(buffer,fmt.c_str(),args); | |
va_end(args); | |
Write("FATAL: " + std::string(buffer)); | |
} | |
void Logger::Warning(const std::string fmt, ...) | |
{ | |
if (fmt == "") | |
{ | |
return; | |
} | |
va_list args; | |
char buffer[1024]; | |
memset(buffer,0,1024); | |
va_start(args,fmt); | |
vsprintf(buffer,fmt.c_str(),args); | |
va_end(args); | |
Write("Warning: " + std::string(buffer)); | |
} | |
void Logger::Write(const std::string Message) | |
{ | |
//Appends to end of file. | |
fOut.open(sFileName.c_str(),std::ios::app); | |
if (fOut.is_open()) | |
{ | |
std::stringstream Entry; | |
//Timestamp | |
time_t rawtime = time(&rawtime); | |
struct tm *TimeInfo = localtime(&rawtime); | |
Entry << "[" << TimeInfo->tm_mon << " " << TimeInfo->tm_mday << "-" << TimeInfo->tm_year << " " | |
<< TimeInfo->tm_hour << ":" << TimeInfo->tm_min << ":" << TimeInfo->tm_sec << "] | "; | |
//Append message | |
Entry << Message << std::endl; | |
//If echo is enabled, print to standard out | |
if (bEcho) | |
{ | |
std::cout << Entry.str(); | |
} | |
//Write to file | |
fOut << Entry.str(); | |
} | |
fOut.close(); | |
} | |
void Logger::SetEcho(bool Echo) | |
{ | |
bEcho = Echo; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <fstream> | |
#include <string> | |
class Logger | |
{ | |
public: | |
void SetLogFile(const std::string FileName = "./log.txt"); | |
void Log(const std::string fmt, ...); | |
void Error(const std::string fmt, ...); | |
void FatalError(const std::string fmt, ...); | |
void Warning(const std::string fmt, ...); | |
//Echos to the standard console output while writing to log. | |
void SetEcho(bool Echo = true); | |
private: | |
std::ofstream fOut; | |
void Write(const std::string Message); | |
std::string sFileName; | |
bool bEcho; | |
}; |
Simple, straightforward, I love it.
If I could just suggest this fix in the date:
void Logger::Write(const std::string Message)
{
//Appends to end of file.
fOut.open(sFileName.c_str(),std::ios::app);
if (fOut.is_open())
{
std::stringstream Entry;
Entry << std::setfill('0');
//Timestamp
time_t rawtime = time(&rawtime);
struct tm *TimeInfo = localtime(&rawtime);
Entry << "[" << std::setw(4) << TimeInfo->tm_year+1900 << "-" << std::setw(2) << TimeInfo->tm_mon << "-" << TimeInfo->tm_mday << " "
<< TimeInfo->tm_hour << ":" << TimeInfo->tm_min << ":" << TimeInfo->tm_sec << "] | ";
//Append message
Entry << Message << std::endl;
//If echo is enabled, print to standard out
if (bEcho)
{
std::cout << Entry.str();
}
//Write to file
fOut << Entry.str();
}
fOut.close();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ho yeah thank you!!