Skip to content

Commit 879796d

Browse files
committed
fix(log): 1.12.7, 解决AsyncFileSink的路径为相对路径时,出现xxx.latest.log软链接异常的问题
1 parent 94f3eeb commit 879796d

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

modules/log/async_file_sink.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ void AsyncFileSink::cleanup()
8585

8686
void 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()
118116
bool 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
}

modules/log/async_file_sink.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AsyncFileSink : public AsyncSink {
3939
void setFilePrefix(const std::string &file_path);
4040
void setFileMaxSize(size_t max_size) { file_max_size_ = max_size; }
4141
void setFileSyncEnable(bool enable);
42-
std::string currentFilename() const { return log_filename_; }
42+
std::string currentFilePath() const { return log_filepath_; }
4343

4444
protected:
4545
void updateInnerValues();
@@ -56,9 +56,8 @@ class AsyncFileSink : public AsyncSink {
5656
bool file_sync_enable_ = false;
5757
pid_t pid_ = 0;
5858

59-
std::string filename_prefix_;
60-
std::string sym_filename_;
61-
std::string log_filename_;
59+
std::string log_filepath_;
60+
std::string sym_filepath_;
6261

6362
std::vector<char> buffer_;
6463

modules/log/async_file_sink_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ TEST(AsyncFileSink, RemoveLogFileDuringWriting)
103103
ch.setFilePath("/tmp/tbox");
104104
ch.setFilePrefix("remove_log_file_during_writing");
105105
ch.enable();
106-
util::fs::RemoveFile(ch.currentFilename());
106+
util::fs::RemoveFile(ch.currentFilePath());
107107
LogInfo("Hello");
108108
std::this_thread::sleep_for(std::chrono::seconds(1));
109-
EXPECT_TRUE(util::fs::IsFileExist(ch.currentFilename()));
109+
EXPECT_TRUE(util::fs::IsFileExist(ch.currentFilePath()));
110110
ch.cleanup();
111111
}
112112

@@ -118,7 +118,7 @@ TEST(AsyncFileSink, Truncate)
118118
ch.setFilePath("/tmp/tbox");
119119
ch.setFilePrefix("truncate");
120120
ch.enable();
121-
util::fs::RemoveFile(ch.currentFilename());
121+
util::fs::RemoveFile(ch.currentFilePath());
122122

123123
std::string tmp(200, 'x');
124124
LogInfo("%s", tmp.c_str());

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 12
24-
TBOX_VERSION_REVISION := 6
24+
TBOX_VERSION_REVISION := 7

0 commit comments

Comments
 (0)