Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //.H
- #pragma once
- #include <vector>
- #include <string>
- #include <fstream>
- #include <boost/date_time/posix_time/posix_time.hpp>
- using namespace std;
- class Log
- {
- public:
- Log() { }
- ~Log() { Save(); Close(); delete File; }
- bool SetUp(wstring fileName);
- void Say(wstring message);
- void SayLn(wstring message) { Say(message + L"\n"); }
- void Save();
- void Close();
- private:
- wstring Buffer;
- wofstream* File;
- };
- wstring static FormatTime(boost::posix_time::ptime now)
- {
- using namespace boost::posix_time;
- static std::locale loc(std::wcout.getloc(),
- new wtime_facet(L"%Y%m%d_%H%M%S"));
- std::basic_stringstream<wchar_t> wss;
- wss.imbue(loc);
- wss << now;
- return wss.str();
- }
- wstring static formLogNumber()
- {
- using namespace boost::posix_time;
- return FormatTime(second_clock::universal_time());
- }
- //.CPP
- #include "logging.h"
- bool Log::SetUp(wstring fileName)
- {
- try
- {
- File = new wofstream(fileName);
- }
- catch (exception ex)
- {
- #ifdef DEBUG
- throw ex;
- #endif
- return false;
- }
- return true;
- }
- void Log::Close()
- {
- if (!File) return;
- if (Buffer.length() > 0)
- Save();
- File->close();
- }
- void Log::Save()
- {
- File->write(Buffer.data(), Buffer.length());
- File->flush();
- Buffer = wstring(L"");
- }
- void Log::Say(wstring message)
- {
- Buffer += message;
- if (Buffer.length() > 8192)
- Save();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement