Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*!
- \file single.h
- \brief A simplistic file logger. Auto-timestamps each message.
- Member function @setFilters is not written yet. (Additionally uses a singleton template)
- \author ~Ziggy Stardust
- */
- #ifndef __LOG_H__
- #define __LOG_H__
- #include <cstdlib>
- #include <ctime>
- #include <string>
- #include <single.h>
- // [8.13.12 : 3:3:45] Engine Core : Loaded engine successfully.
- class Logger : public Singleton<Logger> {
- friend class Singleton<Logger>;
- private:
- Logger() {};
- ~Logger();
- public:
- /**
- @strFilters - Disables logging for included tags.
- Multiple tags must be separated
- with a bar - e.g. "System|Position|Graphics"
- */
- void setFilters(std::string strFilters);
- /**
- @strCategory - Determines log file message is placed into
- @strSection - Tags the message appropriately (used for modules, usually)
- @strMsg - Logged message
- */
- void log(std::string strCategory, std::string strSection, std::string strMsg);
- /**
- Automatically logs the category to "error". Kinda redundant.
- @strSection - Tags the message appropriately (used for modules, usually)
- @strMsg - Logged message
- */
- void logError(std::string strSection, std::string strMsg);
- };
- void Logger::log(std::string strCategory, std::string strSection, std::string strMsg) {
- std::string strFilePath = "log/" + strCategory + ".log";
- FILE* fileLog = fopen(strFilePath.c_str(), "a+");
- time_t t = time(0); // get time now
- struct tm* now = localtime( &t );
- fprintf(fileLog, "[%02hi.%02hi.%hi : %02hi:%02hi:%02hi] %s : %s\n",
- now->tm_mon, now->tm_mday, now->tm_year + 1900,
- now->tm_hour, now->tm_min, now->tm_sec,
- strSection.c_str(), strMsg.c_str());
- fclose(fileLog);
- }
- void Logger::logError(std::string strSection, std::string strMsg) {
- std::string strFilePath = "log/error.log";
- FILE* fileLog = fopen(strFilePath.c_str(), "a+");
- time_t t = time(0); // get time now
- struct tm* now = localtime( &t );
- fprintf(fileLog, "[%02hi.%02hi.%hi : %02hi:%02hi:%02hi] %s : %s",
- now->tm_mon, now->tm_mday, now->tm_year + 1900,
- now->tm_hour, now->tm_min, now->tm_sec,
- strSection.c_str(), strMsg.c_str());
- fclose(fileLog);
- }
- #endif //__LOG_H__
Advertisement
Add Comment
Please, Sign In to add comment