CppLogging  1.0.4.0
C++ Logging Library
file_appender.h
Go to the documentation of this file.
1 
9 #ifndef CPPLOGGING_APPENDERS_FILE_APPENDER_H
10 #define CPPLOGGING_APPENDERS_FILE_APPENDER_H
11 
12 #include "logging/appender.h"
13 
14 #include "filesystem/filesystem.h"
15 
16 #include <atomic>
17 
18 namespace CppLogging {
19 
21 
29 class FileAppender : public Appender
30 {
31 public:
33 
39  explicit FileAppender(const CppCommon::Path& file, bool truncate = false, bool auto_flush = false, bool auto_start = true);
40  FileAppender(const FileAppender&) = delete;
42  virtual ~FileAppender();
43 
44  FileAppender& operator=(const FileAppender&) = delete;
46 
47  // Implementation of Appender
48  bool IsStarted() const noexcept override { return _started; }
49  bool Start() override;
50  bool Stop() override;
51  void AppendRecord(Record& record) override;
52  void Flush() override;
53 
54 private:
55  std::atomic<bool> _started{false};
56  CppCommon::Timestamp _retry{0};
57  CppCommon::File _file;
58  bool _truncate;
59  bool _auto_flush;
60 
62  /*
63  - If the file is opened and ready to write immediately returns true
64  - If the last retry was earlier than 100ms immediately returns false
65  - If the file is closed try to open it for writing, returns true/false
66 
67  \return 'true' if the file was successfully prepared, 'false' if the file failed to be prepared
68  */
69  bool PrepareFile();
71  /*
72  \return 'true' if the file was successfully closed, 'false' if the file failed to close
73  */
74  bool CloseFile();
75 };
76 
77 } // namespace CppLogging
78 
81 #endif // CPPLOGGING_APPENDERS_FILE_APPENDER_H
Logging appender interface definition.
Logging appender interface.
Definition: appender.h:33
bool IsStarted() const noexcept override
Is the logging element started?
Definition: file_appender.h:48
FileAppender & operator=(FileAppender &&)=delete
bool Start() override
Start the logging element.
FileAppender & operator=(const FileAppender &)=delete
FileAppender(FileAppender &&)=delete
FileAppender(const FileAppender &)=delete
void AppendRecord(Record &record) override
Append the given logging record.
void Flush() override
Flush the logging appender.
bool Stop() override
Stop the logging element.
FileAppender(const CppCommon::Path &file, bool truncate=false, bool auto_flush=false, bool auto_start=true)
Initialize the appender with a given file, truncate/append and auto-flush flags.
Logging record.
Definition: record.h:37
C++ Logging project definitions.
Definition: appender.h:15