Просмотр исходного кода

修复启动失败日志线程停掉导致无日志问题

xx 2 лет назад
Родитель
Сommit
a2d505cbbf
4 измененных файлов с 41 добавлено и 23 удалено
  1. 6 1
      src/core/app.cpp
  2. 2 0
      src/core/entry.cpp
  3. 31 22
      src/core/log.cpp
  4. 2 0
      src/core/log.h

+ 6 - 1
src/core/app.cpp

@@ -89,7 +89,12 @@ bool fastweb::app::start(const std::string& config_filepath)
 		
 	this->subscribe->start();
 
-	return m_center->start();
+	if (m_center->start())
+	{
+		m_lastErrorDesc = m_center->last_error();
+		return false;
+	}
+	return true;
 }
 
 void fastweb::app::stop()

+ 2 - 0
src/core/entry.cpp

@@ -13,7 +13,9 @@ extern "C" {
 		fastweb::app* app = new fastweb::app();
 		if (app->start(config_filepath) == false)
 		{
+
 			app->log->error("fastweb start failed," + app->last_error(), __FILE__, __func__, __LINE__);
+			app->log->write();
 			delete app;
 			return nullptr;
 		}

+ 31 - 22
src/core/log.cpp

@@ -46,29 +46,10 @@ bool fastweb::log::run()
         ITHREAD_WAIT_MSEC(1000);
         return true;
     }
-    std::string logcontent;
-    std::string new_time = time::now_time(app()->config->log.name);
-    if (new_time == "")
-    {
-        std::cout << "error: The log file name is incorrect: " << app()->config->log.name << std::endl;
+   
+    if (write() == false)
         return false;
-    }
-    if (m_current_name != new_time)
-    {
-        m_current_name = new_time;
-        ylib::file::create_dir(app()->config->log.dir, true);
-        std::string newfilepath = app()->config->log.dir + "/" + new_time;
-        m_file.close();
-        if (m_file.open(newfilepath) == false)
-        {
-            std::cout << "error: open log file failed, filepath: " << newfilepath << std::endl;
-            return false;
-        }
-    }
-    while (m_queue.pop(logcontent))
-    {
-        m_file.appead(logcontent);
-    }
+
     ITHREAD_WAIT_MSEC(1000);
     return true;
 }
@@ -133,3 +114,31 @@ void fastweb::log::lua(const std::string& msg)
 {
     this->print("[LUA  ] ", msg,"","",0, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
 }
+
+bool fastweb::log::write()
+{
+    std::string logcontent;
+    std::string new_time = time::now_time(app()->config->log.name);
+    if (new_time == "")
+    {
+        std::cout << "error: The log file name is incorrect: " << app()->config->log.name << std::endl;
+        return false;
+    }
+    if (m_current_name != new_time)
+    {
+        m_current_name = new_time;
+        ylib::file::create_dir(app()->config->log.dir, true);
+        std::string newfilepath = app()->config->log.dir + "/" + new_time;
+        m_file.close();
+        if (m_file.open(newfilepath) == false)
+        {
+            std::cout << "error: open log file failed, filepath: " << newfilepath << std::endl;
+            return false;
+        }
+    }
+    while (m_queue.pop(logcontent))
+    {
+        m_file.appead(logcontent);
+    }
+    return true;
+}

+ 2 - 0
src/core/log.h

@@ -17,6 +17,8 @@ namespace fastweb
 		void warn(const std::string& msg, const std::string& filepath, const std::string& func, int line);
 		void debug(const std::string& msg, const std::string& filepath, const std::string& func, int line);
 		void lua(const std::string& msg);
+
+		bool write();
 	private:
 		void print(const std::string& type, const std::string& msg, const std::string& filepath, const std::string& func, int line, int color, bool error);
 		bool run() override;