#pragma once
#include <iostream>
#include "unicode.hpp"
+#include <boost/algorithm/string.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
#ifndef _WIN32
#define OutputDebugString(str) (std::cout << str)
class Logger {
// Singleton
private:
- Logger() : ofs_("mmo_log.txt") {}
+ Logger() : ofs_(GetLogFileName()) {
+ }
+
Logger(const Logger& logger) {}
virtual ~Logger() {}
+ inline tstring GetTimeString() const
+ {
+ using namespace boost::posix_time;
+ ptime now = second_clock::universal_time();
+ return unicode::ToTString(to_iso_extended_string(now));
+ }
+
+ inline std::string GetLogFileName() const
+ {
+ using namespace boost::posix_time;
+ ptime now = second_clock::universal_time();
+ std::string date_string = to_iso_extended_string(now);
+ boost::algorithm::replace_all(date_string, ":", "_");
+ return "log_" + date_string + ".txt";
+ }
+
public:
static void Info(const tstring& format) {
getInstance().Log(_T("INFO: "), format);
}
void Log(const tstring& prefix, const tstring& format) {
- auto out = prefix + format + _T("\n");
+ auto out = GetTimeString() + _T("> ") + prefix + format + _T("\n");
OutputDebugString(out.c_str());
- // std::wcout << unicode::ToWString(out);
- //ofs_ << unicode::ToString(out);
+ std::cout << unicode::ToString(out);
+ ofs_ << unicode::ToString(out) << std::flush;
}
template<class T1>
void Log(const tstring& prefix, const tstring& format, const T1& t1) {
- auto out = prefix + (tformat(format) % t1).str() + _T("\n");
+ auto out = GetTimeString() + _T("> ") + prefix + (tformat(format) % t1).str() + _T("\n");
OutputDebugString(out.c_str());
- // std::wcout << unicode::ToWString(out);
- //ofs_ << unicode::ToString(out);
+ std::cout << unicode::ToString(out);
+ ofs_ << unicode::ToString(out) << std::flush;
}
template<class T1, class T2>
void Log(const tstring& prefix, const tstring& format, const T1& t1, const T2& t2) {
- auto out = prefix + (tformat(format) % t1 % t2).str() + _T("\n");
+ auto out = GetTimeString() + _T("> ") + prefix + (tformat(format) % t1 % t2).str() + _T("\n");
OutputDebugString(out.c_str());
- // std::wcout << unicode::ToWString(out);
- //ofs_ << unicode::ToString(out);
+ std::cout << unicode::ToString(out);
+ ofs_ << unicode::ToString(out) << std::flush;
}
template<class T1, class T2, class T3>
void Log(const tstring& prefix, const tstring& format, const T1& t1, const T2& t2, const T3& t3) {
- auto out = prefix + (tformat(format) % t1 % t2 % t3).str() + _T("\n");
+ auto out = GetTimeString() + _T("> ") + prefix + (tformat(format) % t1 % t2 % t3).str() + _T("\n");
OutputDebugString(out.c_str());
- // std::wcout << unicode::ToWString(out);
- //ofs_ << unicode::ToString(out);
+ std::cout << unicode::ToString(out);
+ ofs_ << unicode::ToString(out) << std::flush;
}
template<class T1, class T2, class T3, class T4>
void Log(const tstring& prefix, const tstring& format, const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
- auto out = prefix + (tformat(format) % t1 % t2 % t3 % t4).str() + _T("\n");
+ auto out = GetTimeString() + _T("> ") + prefix + (tformat(format) % t1 % t2 % t3 % t4).str() + _T("\n");
OutputDebugString(out.c_str());
- // std::wcout << unicode::ToWString(out);
- //ofs_ << unicode::ToString(out);
+ std::cout << unicode::ToString(out);
+ ofs_ << unicode::ToString(out) << std::flush;
}
std::ofstream ofs_;
write_byte_sum_ += msg.size();
UpdateWriteByteAverage();
- io_service_tcp_.post(boost::bind(&Session::DoWriteTCP, this, msg));
+ io_service_tcp_.post(boost::bind(&Session::DoWriteTCP, this, msg, shared_from_this()));
}
void Session::SyncSend(const Command& command)
}
}
- void Session::DoWriteTCP(const std::string msg)
+ void Session::DoWriteTCP(const std::string msg, SessionPtr session_holder)
{
bool write_in_progress = !send_queue_.empty();
send_queue_.push(msg);
boost::asio::async_write(socket_tcp_,
boost::asio::buffer(s->data(), s->size()),
boost::bind(&Session::WriteTCP, this,
- boost::asio::placeholders::error, s));
+ boost::asio::placeholders::error, s, session_holder));
}
}
- void Session::WriteTCP(const boost::system::error_code& error, boost::shared_ptr<std::string> holder)
+ void Session::WriteTCP(const boost::system::error_code& error,
+ boost::shared_ptr<std::string> holder, SessionPtr session_holder)
{
if (!error) {
if (!send_queue_.empty()) {
boost::asio::async_write(socket_tcp_,
boost::asio::buffer(s->data(), s->size()),
boost::bind(&Session::WriteTCP, this,
- boost::asio::placeholders::error, s));
+ boost::asio::placeholders::error, s, session_holder));
}
}
} else {
- FatalError();
+ FatalError(session_holder);
}
}
}
}
- void Session::FatalError()
+ void Session::FatalError(SessionPtr session_holder)
{
if (online_) {
online_ = false;
typedef std::shared_ptr<CallbackFunc> CallbackFuncPtr;\r
typedef long UserID;\r
\r
+ class Session;\r
+ typedef boost::weak_ptr<Session> SessionWeakPtr;\r
+ typedef boost::shared_ptr<Session> SessionPtr;\r
+\r
class Session : public boost::enable_shared_from_this<Session> {\r
public:\r
Session(boost::asio::io_service& io_service_tcp);\r
Command Deserialize(const std::string& msg);\r
\r
void ReceiveTCP(const boost::system::error_code& error);\r
- void DoWriteTCP(const std::string);\r
- void WriteTCP(const boost::system::error_code& error, boost::shared_ptr<std::string> holder);\r
+ void DoWriteTCP(const std::string, SessionPtr session_holder);\r
+ void WriteTCP(const boost::system::error_code& error,\r
+ boost::shared_ptr<std::string> holder, SessionPtr session_holder);\r
void FetchTCP(const std::string&);\r
\r
- void FatalError();\r
+ void FatalError(SessionPtr session_holder = SessionPtr());\r
\r
protected:\r
// ソケット\r
UserID id_;\r
};\r
\r
- typedef boost::weak_ptr<Session> SessionWeakPtr;\r
- typedef boost::shared_ptr<Session> SessionPtr;\r
-\r
}\r