ne_ziggy

Simple Logging

Sep 13th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. /*!
  2.    \file       single.h
  3.    \brief      A simplistic file logger. Auto-timestamps each message.
  4.            Member function @setFilters is not written yet. (Additionally uses a singleton template)
  5.    \author     ~Ziggy Stardust
  6. */
  7.  
  8. #ifndef __LOG_H__
  9. #define __LOG_H__
  10.  
  11. #include <cstdlib>
  12. #include <ctime>
  13. #include <string>
  14. #include <single.h>
  15.  
  16. //  [8.13.12 : 3:3:45] Engine Core : Loaded engine successfully.
  17. class Logger : public Singleton<Logger> {
  18.     friend class Singleton<Logger>;
  19. private:
  20.     Logger() {};
  21.     ~Logger();
  22. public:
  23.     /**
  24.     @strFilters - Disables logging for included tags.
  25.                       Multiple tags must be separated
  26.                       with a bar - e.g. "System|Position|Graphics"
  27.     */
  28.     void setFilters(std::string strFilters);
  29.  
  30.     /**
  31.     @strCategory - Determines log file message is placed into
  32.     @strSection  - Tags the message appropriately (used for modules, usually)
  33.     @strMsg      - Logged message
  34.     */
  35.     void log(std::string strCategory, std::string strSection, std::string strMsg);
  36.  
  37.     /**
  38.     Automatically logs the category to "error". Kinda redundant.
  39.     @strSection  - Tags the message appropriately (used for modules, usually)
  40.     @strMsg      - Logged message
  41.     */
  42.     void logError(std::string strSection, std::string strMsg);
  43. };
  44.  
  45. void Logger::log(std::string strCategory, std::string strSection, std::string strMsg) {
  46.     std::string strFilePath = "log/" + strCategory + ".log";
  47.     FILE* fileLog = fopen(strFilePath.c_str(), "a+");
  48.  
  49.     time_t t = time(0);   // get time now
  50.     struct tm* now = localtime( &t );
  51.     fprintf(fileLog, "[%02hi.%02hi.%hi : %02hi:%02hi:%02hi] %s : %s\n",
  52.                 now->tm_mon, now->tm_mday, now->tm_year + 1900,
  53.                 now->tm_hour, now->tm_min, now->tm_sec,
  54.                 strSection.c_str(), strMsg.c_str());
  55.     fclose(fileLog);
  56. }
  57.  
  58. void Logger::logError(std::string strSection, std::string strMsg) {
  59.     std::string strFilePath = "log/error.log";
  60.     FILE* fileLog = fopen(strFilePath.c_str(), "a+");
  61.  
  62.     time_t t = time(0);   // get time now
  63.     struct tm* now = localtime( &t );
  64.     fprintf(fileLog, "[%02hi.%02hi.%hi : %02hi:%02hi:%02hi] %s : %s",
  65.                 now->tm_mon, now->tm_mday, now->tm_year + 1900,
  66.                 now->tm_hour, now->tm_min, now->tm_sec,
  67.                 strSection.c_str(), strMsg.c_str());
  68.     fclose(fileLog);
  69. }
  70.  
  71. #endif //__LOG_H__
Advertisement
Add Comment
Please, Sign In to add comment