olp-cpp-sdk  1.22.0
FileAppender.h
1 /*
2  * Copyright (C) 2019-2021 HERE Europe B.V.
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  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <fstream>
23 #include <string>
24 #include <utility>
25 
26 #include <olp/core/CoreApi.h>
27 #include <olp/core/logging/Appender.h>
28 #include <olp/core/logging/MessageFormatter.h>
29 
30 namespace olp {
31 namespace logging {
35 class CORE_API FileAppender : public IAppender {
36  public:
44  explicit FileAppender(
45  const std::string& fileName, bool append = false,
47  ~FileAppender() override;
48 
54  bool isValid() const;
55 
61  inline const std::string& getFileName() const;
62 
68  inline bool getAppendFile() const;
69 
75  inline const MessageFormatter& getMessageFormatter() const;
76 
82  inline FileAppender& setMessageFormatter(MessageFormatter formatter);
83 
84  IAppender& append(const LogMessage& message) override;
85 
86  private:
87  std::string m_fileName;
88  bool m_appendFile;
89  MessageFormatter m_formatter;
90  std::ofstream m_stream;
91 
92  FileAppender(const FileAppender&) = delete;
93  FileAppender& operator=(const FileAppender&) = delete;
94 };
95 
96 inline const std::string& FileAppender::getFileName() const {
97  return m_fileName;
98 }
99 
100 inline bool FileAppender::getAppendFile() const { return m_appendFile; }
101 
103  return m_formatter;
104 }
105 
107  MessageFormatter formatter) {
108  m_formatter = std::move(formatter);
109  return *this;
110 }
111 
112 } // namespace logging
113 } // namespace olp
An appender that prints messages to a file.
Definition: FileAppender.h:35
FileAppender & setMessageFormatter(MessageFormatter formatter)
Sets the message formatter.
Definition: FileAppender.h:106
const MessageFormatter & getMessageFormatter() const
Gets the message formatter.
Definition: FileAppender.h:102
const std::string & getFileName() const
Gets the name of the file.
Definition: FileAppender.h:96
FileAppender(const std::string &fileName, bool append=false, MessageFormatter formatter=MessageFormatter::createDefault())
Creates a FileAppender instance.
bool getAppendFile() const
Checks whether the file has the appender.
Definition: FileAppender.h:100
IAppender & append(const LogMessage &message) override
Appends the message using char strings.
bool isValid() const
Checks whether the stream is open and can be written to.
Appends a message to the log.
Definition: Appender.h:32
Specifies how messages are formatted.
Definition: MessageFormatter.h:61
static MessageFormatter createDefault()
Creates the default message formatter.
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24
Contains data used for a log message.
Definition: LogMessage.h:31