Support redirect_stderr in AsyncFileLog.

This commit is contained in:
levlam 2022-10-15 10:17:42 +03:00
parent bfd8c8dcf3
commit 5217a86e0d
2 changed files with 11 additions and 4 deletions

View File

@ -8,15 +8,19 @@
#include "td/utils/port/FileFd.h" #include "td/utils/port/FileFd.h"
#include "td/utils/port/path.h" #include "td/utils/port/path.h"
#include "td/utils/port/StdStreams.h"
#include "td/utils/SliceBuilder.h" #include "td/utils/SliceBuilder.h"
namespace td { namespace td {
Status AsyncFileLog::init(string path, int64 rotate_threshold) { Status AsyncFileLog::init(string path, int64 rotate_threshold, bool redirect_stderr) {
CHECK(path_.empty()); CHECK(path_.empty());
CHECK(!path.empty()); CHECK(!path.empty());
TRY_RESULT(fd, FileFd::open(path, FileFd::Create | FileFd::Write | FileFd::Append)); TRY_RESULT(fd, FileFd::open(path, FileFd::Create | FileFd::Write | FileFd::Append));
if (!Stderr().empty() && redirect_stderr) {
fd.get_native_fd().duplicate(Stderr().get_native_fd()).ignore();
}
auto r_path = realpath(path, true); auto r_path = realpath(path, true);
if (r_path.is_error()) { if (r_path.is_error()) {
@ -29,8 +33,8 @@ Status AsyncFileLog::init(string path, int64 rotate_threshold) {
queue_ = td::make_unique<MpscPollableQueue<Query>>(); queue_ = td::make_unique<MpscPollableQueue<Query>>();
queue_->init(); queue_->init();
logging_thread_ = logging_thread_ = td::thread(
td::thread([queue = queue_.get(), fd = std::move(fd), path = path_, size, rotate_threshold]() mutable { [queue = queue_.get(), fd = std::move(fd), path = path_, size, rotate_threshold, redirect_stderr]() mutable {
auto after_rotation = [&] { auto after_rotation = [&] {
ScopedDisableLog disable_log; // to ensure that nothing will be printed to the closed log ScopedDisableLog disable_log; // to ensure that nothing will be printed to the closed log
fd.close(); fd.close();
@ -39,6 +43,9 @@ Status AsyncFileLog::init(string path, int64 rotate_threshold) {
process_fatal_error(PSLICE() << r_fd.error() << " in " << __FILE__ << " at " << __LINE__ << '\n'); process_fatal_error(PSLICE() << r_fd.error() << " in " << __FILE__ << " at " << __LINE__ << '\n');
} }
fd = r_fd.move_as_ok(); fd = r_fd.move_as_ok();
if (!Stderr().empty() && redirect_stderr) {
fd.get_native_fd().duplicate(Stderr().get_native_fd()).ignore();
}
size = 0; size = 0;
}; };
auto append = [&](CSlice slice) { auto append = [&](CSlice slice) {

View File

@ -24,7 +24,7 @@ class AsyncFileLog final : public LogInterface {
AsyncFileLog &operator=(AsyncFileLog &&) = delete; AsyncFileLog &operator=(AsyncFileLog &&) = delete;
~AsyncFileLog(); ~AsyncFileLog();
Status init(string path, int64 rotate_threshold); Status init(string path, int64 rotate_threshold, bool redirect_stderr = true);
private: private:
struct Query { struct Query {