chenshuo / muduo

Event-driven network library for multi-threaded Linux server in C++11
https://github.com/chenshuo/muduo
Other
14.82k stars 5.17k forks source link

程序启动后很快退出,AsyncLogging无法打印日志到文件 #439

Closed liujiejie closed 2 years ago

liujiejie commented 4 years ago

GitHub issues are for tracking bugs, not for general discussing like a forum.

If you have a general question to ask, send it to mailing list:

muduo-library@googlegroups.com

Or visit: https://groups.google.com/forum/#!forum/muduo-library

When file an issue muduo, please provide a SSCCE: Short, Self Contained, Correct (Compilable), Example.

Also specify the exact environment where the issue occurs:

Linux distro and version? x86 or ARM? 32-bit or 64-bit?

Branch (cpp98/cpp11/cpp17) and version of muduo?

Version of cmake, gcc and boost? (If not from distro.)

muduo::AsyncLogging log_g; void GWOutputFunc(const char msg, int len) { log_g->append(msg, len); } void GWLogFlush() { log_g->stop(); }

bool GWEngine::InitLog() { char *path = getenv("HOME"); char name[256] = { 0 }; sprintf(name, "%s/log", path);

if (access(name, F_OK) != 0)
{
    mkdir(name, 0755);
}

    sprintf(name, "%s/%s", name, app_arg_.instance_name.c_str());
size_t kOneGB = 1000 * 1024 * 1024;
rlimit rl = { 2 * kOneGB, 2 * kOneGB };
setrlimit(RLIMIT_AS, &rl);
static muduo::AsyncLogging log(name, kOneGB, 1);
log_g = &log;
muduo::Logger::setLogLevel((muduo::Logger::LogLevel)(app_arg_.log_level));
muduo::Logger::setOutput(GWOutputFunc);
    muduo::Logger::setFlush(GWLogFlush);
log.start();

return 0;

} 程序启动后很快退出,异步日志AsyncLogging无法打印日志到文件,这个是啥原因啊?

firefeifei commented 4 years ago

问题解决了吗

fortunely commented 2 years ago

muduo异步日志模块,后端默认是3秒flush一次,快速启动后退出,日志消息还在FixedBuffer中,没有flush到内核缓存/磁盘。 前端Logger的析构中只有FATAL级别的日志消息,才会调用g_flush。

如果要解决这个问题,可以在进程退出前,主动flush一次。