Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #ifndef LOG_HPP_INCLUDED
  2. #define LOG_HPP_INCLUDED
  3.  
  4. #include <fstream>
  5. #include <chrono>
  6. #include <ctime>
  7. #include <stdexcept>
  8.  
  9. std::string getProperDate() //plus jamais ça :'(
  10. {
  11. auto tmp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
  12. auto str = std::localtime(&tmp); //aïe
  13. char s[256]; // *se sent mal*
  14. strftime(s, 256, "%F | %T", str); // pan ! *bruit de tir*
  15. return std::string(s);
  16. }
  17.  
  18. class Log
  19. {
  20. public:
  21. static Log& instance()
  22. {
  23. static Log *instance = new Log();
  24. return *instance;
  25. }
  26.  
  27. void write(std::string const& data)
  28. {
  29.  
  30. file_ << "[" << getProperDate() << "] : ";
  31. file_ << data << std::endl;
  32. }
  33.  
  34. private:
  35. std::ofstream file_;
  36.  
  37. Log() : file_("log.txt", std::ios::out | std::ios::app) //ctor deleted
  38. {
  39. if (!file_) throw std::runtime_error("Unable to open log file.");
  40. }
  41. };
  42.  
  43. #endif // LOG_HPP_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement