Advertisement
HITOA

Log file class

Jul 18th, 2021
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. class LogStream {
  2. public:
  3.     explicit LogStream(const std::string& path) : out(path, std::ios::binary) {};
  4.     LogStream(const LogStream&) = delete;
  5.     LogStream(LogStream&&) = delete;
  6.  
  7.     LogStream& operator=(const LogStream&) = delete;
  8.     LogStream& operator=(LogStream&&) = delete;
  9.  
  10.     ~LogStream() { out.close(); };
  11. public:
  12.     static std::string GetStringTime() {
  13.         std::chrono::time_point now = std::chrono::system_clock::now();
  14.         std::time_t now_t = std::chrono::system_clock::to_time_t(now);
  15.         std::tm now_tm = *std::localtime(&now_t);
  16.         char buffer[80];
  17.         strftime(buffer, 80, "[%c]", &now_tm);
  18.         return buffer;
  19.     }
  20. public:
  21.     void Write(const std::string& msg) { std::copy(msg.begin(), msg.end(), std::ostreambuf_iterator(out)); }
  22.     void WriteLine(const std::string& msg) { Write(msg + "\n"); }
  23.     void Log(const std::string& msg) { WriteLine(GetStringTime() + " [Info] : " + msg); }
  24.     void Err(const std::string& msg) { WriteLine(GetStringTime() + " [ERROR] : " + msg); }
  25. private:
  26.     std::ofstream out;
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement