logging: rename the `DEBUG` log level to `DBG`
[folly.git] / folly / experimental / logging / ImmediateFileWriter.h
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 #pragma once
17
18 #include <folly/File.h>
19 #include <folly/Range.h>
20 #include <folly/experimental/logging/LogWriter.h>
21
22 namespace folly {
23
24 /**
25  * A LogWriter implementation that immediately writes to a file descriptor when
26  * it is invoked.
27  *
28  * The downside of this class is that logging I/O occurs directly in your
29  * normal program threads, so that logging I/O may block or slow down normal
30  * processing.
31  *
32  * However, one benefit of this class is that log messages are written out
33  * immediately, so if your program crashes, all log messages generated before
34  * the crash will have already been written, and no messages will be lost.
35  */
36 class ImmediateFileWriter : public LogWriter {
37  public:
38   /**
39    * Construct an ImmediateFileWriter that appends to the file at the specified
40    * path.
41    */
42   explicit ImmediateFileWriter(folly::StringPiece path);
43
44   /**
45    * Construct an ImmediateFileWriter that writes to the specified File object.
46    */
47   explicit ImmediateFileWriter(folly::File&& file);
48
49   using LogWriter::writeMessage;
50   void writeMessage(folly::StringPiece buffer, uint32_t flags = 0) override;
51   void flush() override;
52
53   /**
54    * Get the output file.
55    */
56   const folly::File& getFile() const {
57     return file_;
58   }
59
60  private:
61   ImmediateFileWriter(ImmediateFileWriter const&) = delete;
62   ImmediateFileWriter& operator=(ImmediateFileWriter const&) = delete;
63
64   folly::File file_;
65 };
66 } // namespace folly