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

This commit is contained in:
xx
2024-06-10 17:39:19 +08:00
parent 28559861b2
commit bc25a924c6
4 changed files with 41 additions and 23 deletions

View File

@@ -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()

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;