1 /*
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <folly/logging/ImmediateFileWriter.h>
18
19 #include <folly/FileUtil.h>
20 #include <folly/String.h>
21 #include <folly/logging/LoggerDB.h>
22 #include <folly/portability/Unistd.h>
23
24 namespace folly {
25
ImmediateFileWriter(StringPiece path)26 ImmediateFileWriter::ImmediateFileWriter(StringPiece path)
27 : file_{path.str(), O_WRONLY | O_APPEND | O_CREAT} {}
28
ImmediateFileWriter(folly::File && file)29 ImmediateFileWriter::ImmediateFileWriter(folly::File&& file)
30 : file_{std::move(file)} {}
31
writeMessage(StringPiece buffer,uint32_t)32 void ImmediateFileWriter::writeMessage(
33 StringPiece buffer, uint32_t /* flags */) {
34 // Write the data.
35 // We are doing direct file descriptor writes here, so there is no buffering
36 // of log message data. Each message is immediately written to the output.
37 auto ret = folly::writeFull(file_.fd(), buffer.data(), buffer.size());
38 if (ret < 0) {
39 int errnum = errno;
40 LoggerDB::internalWarning(
41 __FILE__,
42 __LINE__,
43 "error writing to log file ",
44 file_.fd(),
45 ": ",
46 errnoStr(errnum));
47 }
48 }
49
flush()50 void ImmediateFileWriter::flush() {}
51 } // namespace folly
52