Advertisement
RaWRCoder

Logging

Nov 5th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. //.H
  2. #pragma once
  3. #include <vector>
  4. #include <string>
  5. #include <fstream>
  6. #include <boost/date_time/posix_time/posix_time.hpp>
  7.  
  8. using namespace std;
  9.  
  10. class Log
  11. {
  12. public:
  13.     Log() { }
  14.     ~Log() { Save(); Close(); delete File; }
  15.     bool SetUp(wstring fileName);
  16.  
  17.     void Say(wstring message);
  18.     void SayLn(wstring message) { Say(message + L"\n"); }
  19.  
  20.     void Save();
  21.     void Close();
  22.  
  23. private:
  24.     wstring Buffer;
  25.     wofstream* File;
  26. };
  27.  
  28. wstring static FormatTime(boost::posix_time::ptime now)
  29. {
  30.     using namespace boost::posix_time;
  31.     static std::locale loc(std::wcout.getloc(),
  32.         new wtime_facet(L"%Y%m%d_%H%M%S"));
  33.  
  34.     std::basic_stringstream<wchar_t> wss;
  35.     wss.imbue(loc);
  36.     wss << now;
  37.     return wss.str();
  38. }
  39.  
  40. wstring static formLogNumber()
  41. {
  42.     using namespace boost::posix_time;
  43.     return FormatTime(second_clock::universal_time());
  44. }
  45.  
  46.  
  47. //.CPP
  48. #include "logging.h"
  49.  
  50.  
  51. bool Log::SetUp(wstring fileName)
  52. {
  53.     try
  54.     {
  55.         File = new wofstream(fileName);
  56.     }
  57.     catch (exception ex)
  58.     {
  59. #ifdef DEBUG
  60.         throw ex;
  61. #endif
  62.         return false;
  63.     }
  64.     return true;
  65. }
  66.  
  67. void Log::Close()
  68. {
  69.     if (!File) return;
  70.     if (Buffer.length() > 0)
  71.         Save();
  72.     File->close();
  73. }
  74.  
  75. void Log::Save()
  76. {
  77.     File->write(Buffer.data(), Buffer.length());
  78.     File->flush();
  79.     Buffer = wstring(L"");
  80. }
  81.  
  82. void Log::Say(wstring message)
  83. {
  84.     Buffer += message;
  85.     if (Buffer.length() > 8192)
  86.         Save();
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement