Created
May 14, 2014 03:57
-
-
Save eri451/3aa11b67ffbf1b0f7200 to your computer and use it in GitHub Desktop.
simple logger
This file contains 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
/******************************************************** | |
* usage: * | |
* #include "log.h" * | |
* logger info(INFO); * | |
* logger err(ERROR); * | |
* logger debug(DEBUG); * | |
* * | |
* info << std::string msg + "\n"; * | |
* debug << std::string msg + "\n"; * | |
* err << std::string msg + "\n"; * | |
* * | |
* license: * | |
* GPLv3 * | |
********************************************************/ | |
#ifndef LOG_H | |
#define LOG_H | |
#include <string> | |
#include <ctime> | |
#include <fstream> | |
#define LOGDIR "./logs/" | |
// make this longer for more levels | |
enum type { | |
ERROR, | |
DEBUG, | |
INFO | |
}; | |
class logger | |
{ | |
private: | |
std::string dir; | |
std::string path; | |
std::ofstream file; | |
int type; | |
public: | |
logger(int type) | |
{ | |
dir = LOGDIR; | |
// make this longer for more levels | |
switch (type) | |
{ | |
case 0 : path = dir + "error.log"; | |
break; | |
case 1 : path = dir + "debug.log"; | |
break; | |
case 2 : path = dir + "info.log"; | |
break; | |
default: return; | |
} | |
// append | |
file.open(path.c_str(), std::ofstream::app); | |
} | |
~logger() | |
{ | |
file.close(); | |
} | |
int operator<< (const std::string msg) | |
{ | |
char mbstr[100]; | |
std::string message; | |
std::time_t timer; | |
// get current time | |
timer = std::time(NULL); | |
// leider _kein_ C++11 | |
// if(file.is_open()) | |
// file << std:put_time(std::localtime(&timer),"%TT%F) + message; | |
if (std::strftime(mbstr, sizeof(mbstr), "%TT%F", std::localtime(&timer)) | |
&& file.is_open()) | |
file << mbstr << " :\t" << msg << std::endl; | |
return 0; | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment