@@ -85,9 +85,7 @@ void AsyncFileSink::cleanup()
8585
8686void AsyncFileSink::updateInnerValues ()
8787{
88- filename_prefix_ = file_path_ + ' /' + file_prefix_ + ' .' ;
89- sym_filename_ = filename_prefix_ + " latest.log" ;
90-
88+ sym_filepath_ = file_path_ + ' /' + file_prefix_ + ' .' + " latest.log" ;
9189 CHECK_CLOSE_RESET_FD (fd_);
9290}
9391
@@ -118,7 +116,7 @@ void AsyncFileSink::flush()
118116bool AsyncFileSink::checkAndCreateLogFile ()
119117{
120118 if (fd_ >= 0 ) {
121- if (util::fs::IsFileExist (log_filename_ ))
119+ if (util::fs::IsFileExist (log_filepath_ ))
122120 return true ;
123121 CHECK_CLOSE_RESET_FD (fd_);
124122 }
@@ -137,31 +135,36 @@ bool AsyncFileSink::checkAndCreateLogFile()
137135 strftime (timestamp, sizeof (timestamp), " %Y%m%d_%H%M%S" , &tm);
138136 }
139137
140- std::string log_filename;
138+ // ! 生成新的日志文件名与路径
139+ std::string log_filename; // ! 日志文件名称,如:demo.20250402.123.log
140+ std::string log_filepath; // ! 日志文件路径,如:/var/log/demo.20250402.123.log
141141 int postfix = 0 ;
142+
142143 do {
143- log_filename = filename_prefix_ + timestamp + ' .' + std::to_string (pid_) + " .log" ;
144+ log_filename = file_prefix_ + ' . ' + timestamp + ' .' + std::to_string (pid_) + " .log" ;
144145 if (postfix != 0 ) {
145146 log_filename += ' .' ;
146147 log_filename += std::to_string (postfix);
147148 }
149+ log_filepath = file_path_ + ' /' + log_filename;
148150 ++postfix;
149- } while (util::fs::IsFileExist (log_filename)); // ! 避免在同一秒多次创建日志文件,都指向同一日志名
150- log_filename_ = std::move (log_filename);
151+ } while (util::fs::IsFileExist (log_filepath)); // ! 避免在同一秒多次创建日志文件,都指向同一日志名
152+
153+ log_filepath_ = std::move (log_filepath);
151154
152155 int flags = O_CREAT | O_WRONLY | O_APPEND;
153156 if (file_sync_enable_)
154157 flags |= O_DSYNC;
155158
156- fd_ = ::open (log_filename_ .c_str (), flags, S_IRUSR | S_IWUSR);
159+ fd_ = ::open (log_filepath_ .c_str (), flags, S_IRUSR | S_IWUSR);
157160 if (fd_ < 0 ) {
158- cerr << " Err: open file " << log_filename_ << " fail. error:" << errno << ' ,' << strerror (errno) << endl;
161+ cerr << " Err: open file " << log_filepath_ << " fail. error:" << errno << ' ,' << strerror (errno) << endl;
159162 return false ;
160163 }
161164
162165 total_write_size_ = 0 ;
163- util::fs::RemoveFile (sym_filename_ , false );
164- util::fs::MakeSymbolLink (log_filename_, sym_filename_ , false );
166+ util::fs::RemoveFile (sym_filepath_ , false );
167+ util::fs::MakeSymbolLink (log_filename, sym_filepath_ , false );
165168
166169 return true ;
167170}
0 commit comments