OSDN Git Service

ログを開始時刻ごとに分けるように変更
authorh2so5 <h2so5@git.sourceforge.jp>
Sat, 15 Sep 2012 04:27:57 +0000 (13:27 +0900)
committerh2so5 <h2so5@git.sourceforge.jp>
Sat, 15 Sep 2012 04:27:57 +0000 (13:27 +0900)
ログの表示に時間を追加
プリコンパイル済みヘッダを更新
サーバーのセッション周りを修正

client/stdafx.h
common/Logger.hpp
common/network/Session.cpp
common/network/Session.hpp
server/Account.hpp
server/stdafx.h

index 501f00b..664ff2b 100644 (file)
@@ -65,6 +65,7 @@
 #include <boost/timer.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/interprocess/managed_shared_memory.hpp>
+#include <boost/algorithm/string.hpp>
 #include <boost/format.hpp>
 #include <boost/thread.hpp>
 #include <boost/foreach.hpp>
index 7f9555b..20bd6ee 100644 (file)
@@ -5,6 +5,8 @@
 #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);
@@ -112,42 +132,42 @@ class Logger {
         }
 
         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_;
index 81d91ab..1b8d5f0 100644 (file)
@@ -45,7 +45,7 @@ namespace network {
         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)
@@ -256,7 +256,7 @@ namespace network {
         }
     }
 
-    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);
@@ -269,11 +269,12 @@ namespace network {
           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()) {
@@ -287,11 +288,11 @@ namespace network {
                     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);
         }
     }
 
@@ -306,7 +307,7 @@ namespace network {
         }
     }
 
-    void Session::FatalError()
+    void Session::FatalError(SessionPtr session_holder)
     {
         if (online_) {
             online_ = false;
index c7ba900..075f439 100644 (file)
@@ -28,6 +28,10 @@ namespace network {
     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
@@ -72,11 +76,12 @@ namespace network {
             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
@@ -107,7 +112,4 @@ namespace network {
             UserID id_;\r
     };\r
 \r
-    typedef boost::weak_ptr<Session> SessionWeakPtr;\r
-    typedef boost::shared_ptr<Session> SessionPtr;\r
-\r
 }\r
index a7f5d2f..1a6aeef 100644 (file)
@@ -57,6 +57,7 @@ class Account {
         {
             T old_value;
             if (!Get(user_id, property, &old_value) || old_value != value) {
+                               boost::unique_lock<boost::recursive_mutex> lock(mutex_);
 
                 if (user_map_.find(user_id) == user_map_.end()) {
                     user_map_[user_id] = PropertyMap();
@@ -107,4 +108,6 @@ class Account {
 
         unsigned int revision_;
         UserID max_user_id_;
+
+               boost::recursive_mutex mutex_;
 };
index e28be74..a0e3e2d 100644 (file)
@@ -50,6 +50,7 @@
 #include <boost/timer.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/interprocess/managed_shared_memory.hpp>
+#include <boost/algorithm/string.hpp>
 #include <boost/format.hpp>
 #include <boost/thread.hpp>
 #include <boost/foreach.hpp>