2 * Copyright 2004-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <condition_variable>
22 #include <folly/File.h>
23 #include <folly/Range.h>
24 #include <folly/Synchronized.h>
25 #include <folly/experimental/logging/LogWriter.h>
30 * A LogWriter implementation that asynchronously writes to a file descriptor.
32 * This class performs the log I/O in a separarate thread.
34 * The advantage of this class over ImmediateFileWriter is that logging I/O can
35 * never slow down or block your normal program operation. If log messages are
36 * generated faster than they can be written, messages will be dropped (and an
37 * indication of how many messages were dropped will be written to the log file
38 * when we are able to catch up a bit.)
40 * However, one downside is that if your program crashes, not all log messages
41 * may have been written, so you may lose messages generated immediately before
44 class AsyncFileWriter : public LogWriter {
47 * Construct an AsyncFileWriter that appends to the file at the specified
50 explicit AsyncFileWriter(folly::StringPiece path);
53 * Construct an AsyncFileWriter that writes to the specified File object.
55 explicit AsyncFileWriter(folly::File&& file);
59 void writeMessage(folly::StringPiece buffer, uint32_t flags = 0) override;
60 void writeMessage(std::string&& buffer, uint32_t flags = 0) override;
63 * Block until the I/O thread has finished writing all messages that
64 * were already enqueued when flush() was called.
70 * A simple implementation using two queues.
71 * All writer threads enqueue into one queue while the I/O thread is
72 * processing the other.
74 * We could potentially also provide an implementation using folly::MPMCQueue
75 * in the future, which may improve contention under very high write loads.
78 std::array<std::vector<std::string>, 2> queues;
80 bool ioThreadDone{false};
81 uint64_t ioThreadCounter{0};
82 size_t maxBufferBytes{1024 * 1024};
83 size_t currentBufferSize{0};
84 size_t numDiscarded{0};
86 std::vector<std::string>* getCurrentQueue() {
87 return &queues[ioThreadCounter & 0x1];
92 void performIO(std::vector<std::string>* ioQueue);
94 void onIoError(const std::exception& ex);
95 std::string getNumDiscardedMsg(size_t numDiscarded);
98 folly::Synchronized<Data, std::mutex> data_;
100 * messageReady_ is signaled by writer threads whenever they add a new
101 * message to the current queue.
103 std::condition_variable messageReady_;
105 * ioCV_ is signaled by the I/O thread each time it increments
106 * the ioThreadCounter (once each time around its loop).
108 std::condition_variable ioCV_;
113 * This should come last, since all other member variables need to be
114 * constructed before the I/O thread starts.
116 std::thread ioThread_;