040c5c608fd3a434372aaf26ce2101ab5ec83e69
[folly.git] / folly / experimental / logging / ImmediateFileWriter.cpp
1 /*
2  * Copyright 2004-present Facebook, Inc.
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 #include <folly/experimental/logging/ImmediateFileWriter.h>
17
18 #include <folly/FileUtil.h>
19 #include <folly/String.h>
20 #include <folly/experimental/logging/LoggerDB.h>
21
22 using folly::StringPiece;
23
24 namespace folly {
25
26 ImmediateFileWriter::ImmediateFileWriter(StringPiece path)
27     : file_{path.str(), O_WRONLY | O_APPEND | O_CREAT} {}
28
29 ImmediateFileWriter::ImmediateFileWriter(folly::File&& file)
30     : file_{std::move(file)} {}
31
32 void ImmediateFileWriter::writeMessage(StringPiece buffer) {
33   // Write the data.
34   // We are doing direct file descriptor writes here, so there is no buffering
35   // of log message data.  Each message is immediately written to the output.
36   auto ret = folly::writeFull(file_.fd(), buffer.data(), buffer.size());
37   if (ret < 0) {
38     int errnum = errno;
39     LoggerDB::internalWarning(
40         __FILE__,
41         __LINE__,
42         "error writing to log file ",
43         file_.fd(),
44         ": ",
45         errnoStr(errnum));
46   }
47 }
48 }