-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathAsyncFileLog.cpp
More file actions
165 lines (148 loc) · 5.04 KB
/
Copy pathAsyncFileLog.cpp
File metadata and controls
165 lines (148 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/AsyncFileLog.h"
#include "td/utils/port/FileFd.h"
#include "td/utils/port/path.h"
#include "td/utils/port/sleep.h"
#include "td/utils/port/StdStreams.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/Time.h"
namespace td {
#if !TD_THREAD_UNSUPPORTED
Status AsyncFileLog::init(string path, int64 rotate_threshold, bool redirect_stderr) {
CHECK(path_.empty());
CHECK(!path.empty());
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);
if (r_path.is_error()) {
path_ = std::move(path);
} else {
path_ = r_path.move_as_ok();
}
TRY_RESULT(size, fd.get_size());
queue_ = td::make_unique<MpscPollableQueue<Query>>();
queue_->init();
logging_thread_ = td::thread(
[queue = queue_.get(), fd = std::move(fd), path = path_, size, rotate_threshold, redirect_stderr]() mutable {
auto after_rotation = [&] {
fd.close();
auto r_fd = FileFd::open(path, FileFd::Create | FileFd::Write | FileFd::Append);
if (r_fd.is_error()) {
process_fatal_error(PSLICE() << r_fd.error() << " in " << __FILE__ << " at " << __LINE__ << '\n');
}
fd = r_fd.move_as_ok();
if (!Stderr().empty() && redirect_stderr) {
fd.get_native_fd().duplicate(Stderr().get_native_fd()).ignore();
}
auto r_size = fd.get_size();
if (r_fd.is_error()) {
process_fatal_error(PSLICE() << "Failed to get log size: " << r_fd.error() << " in " << __FILE__ << " at "
<< __LINE__ << '\n');
}
size = r_size.move_as_ok();
};
auto append = [&](CSlice slice) {
if (size > rotate_threshold) {
auto status = rename(path, PSLICE() << path << ".old");
if (status.is_error()) {
process_fatal_error(PSLICE() << status << " in " << __FILE__ << " at " << __LINE__ << '\n');
}
after_rotation();
}
while (!slice.empty()) {
if (redirect_stderr) {
while (has_log_guard()) {
// spin
}
}
auto r_size = fd.write(slice);
if (r_size.is_error()) {
process_fatal_error(PSLICE() << r_size.error() << " in " << __FILE__ << " at " << __LINE__ << '\n');
}
auto written = r_size.ok();
size += static_cast<int64>(written);
slice.remove_prefix(written);
}
};
while (true) {
int ready_count = queue->reader_wait_nonblock();
if (ready_count == 0) {
queue->reader_get_event_fd().wait(1000);
continue;
}
bool need_close = false;
while (ready_count-- > 0) {
Query query = queue->reader_get_unsafe();
switch (query.type_) {
case Query::Type::Log:
append(query.data_);
break;
case Query::Type::AfterRotation:
after_rotation();
break;
case Query::Type::Close:
need_close = true;
break;
default:
process_fatal_error("Invalid query type in AsyncFileLog");
}
}
queue->reader_flush();
if (need_close) {
fd.close();
break;
}
}
});
return Status::OK();
}
AsyncFileLog::~AsyncFileLog() {
if (queue_ == nullptr) {
return;
}
Query query;
query.type_ = Query::Type::Close;
queue_->writer_put(std::move(query));
logging_thread_.join();
}
vector<string> AsyncFileLog::get_file_paths() {
vector<string> result;
if (!path_.empty()) {
result.push_back(path_);
result.push_back(PSTRING() << path_ << ".old");
}
return result;
}
void AsyncFileLog::after_rotation() {
Query query;
query.type_ = Query::Type::AfterRotation;
if (queue_ == nullptr) {
process_fatal_error("AsyncFileLog is not inited");
}
queue_->writer_put(std::move(query));
}
void AsyncFileLog::do_append(int log_level, CSlice slice) {
Query query;
query.data_ = slice.str();
if (queue_ == nullptr) {
process_fatal_error("AsyncFileLog is not inited");
}
queue_->writer_put(std::move(query));
if (log_level == VERBOSITY_NAME(FATAL)) {
// it is not thread-safe to join logging_thread_ there, so just wait for the log line to be printed
auto end_time = Time::now() + 1.0;
while (!queue_->is_empty() && Time::now() < end_time) {
usleep_for(1000);
}
usleep_for(5000); // allow some time for the log line to be actually printed
}
}
#endif
} // namespace td