logging: add a NEVER_DISCARD flag to LogWriter
[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(
33     StringPiece buffer,
34     uint32_t /* flags */) {
35   // Write the data.
36   // We are doing direct file descriptor writes here, so there is no buffering
37   // of log message data.  Each message is immediately written to the output.
38   auto ret = folly::writeFull(file_.fd(), buffer.data(), buffer.size());
39   if (ret < 0) {
40     int errnum = errno;
41     LoggerDB::internalWarning(
42         __FILE__,
43         __LINE__,
44         "error writing to log file ",
45         file_.fd(),
46         ": ",
47         errnoStr(errnum));
48   }
49 }
50 }