blob: 3513dac54434488970a6a170d44af36809cca7e5 [file] [log] [blame]
Artem Senichevefd5d742018-10-24 16:14:04 +03001/**
2 * @brief Log manager.
3 *
4 * This file is part of HostLogger project.
5 *
6 * Copyright (c) 2018 YADRO
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#pragma once
22
23#include "log_storage.hpp"
24
Artem Senichevefd5d742018-10-24 16:14:04 +030025/** @class LogManager
26 * @brief Log manager.
27 * All functions within this class are not thread-safe.
28 */
29class LogManager
30{
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080031 public:
Artem Senichevefd5d742018-10-24 16:14:04 +030032 /** @brief Constructor. */
33 LogManager();
34
35 /** @brief Destructor. */
36 ~LogManager();
37
38 /** @brief Open the host's log stream.
39 *
40 * @return error code, 0 if operation completed successfully
41 */
42 int openHostLog();
43
44 /** @brief Close the host's log stream.
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080045 */
Artem Senichevefd5d742018-10-24 16:14:04 +030046 void closeHostLog();
47
48 /** @brief Get file descriptor by host's log stream.
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080049 * Descriptor can be used to register it in an external polling
50 * manager.
Artem Senichevefd5d742018-10-24 16:14:04 +030051 *
52 * @return file descriptor (actually it is an opened socket)
53 */
54 int getHostLogFd() const;
55
56 /** @brief Handle incoming data from host's log stream.
57 *
58 * @return error code, 0 if operation completed successfully
59 */
60 int handleHostLog();
61
62 /** @brief Flush log storage: save currently collected messages to a file,
63 * reset the storage and rotate log files.
64 *
65 * @return error code, 0 if operation completed successfully
66 */
67 int flush();
68
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080069 private:
Artem Senichevefd5d742018-10-24 16:14:04 +030070 /** @brief Read incoming data from host's log stream.
71 *
72 * @param[out] buffer - buffer to write incoming data
73 * @param[in] bufferLen - maximum size of the buffer in bytes
74 * @param[out] readLen - on output contain number of bytes read from stream
75 *
76 * @return error code, 0 if operation completed successfully
77 */
78 int readHostLog(char* buffer, size_t bufferLen, size_t& readLen) const;
79
80 /** @brief Prepare the path to save the log.
81 * Warning: the name is used in function rotateLogFiles(),
82 * make sure you don't brake sorting rules.
83 *
84 * @return path to new log file including its name
85 */
86 std::string prepareLogPath() const;
87
88 /** @brief Create path for log files.
89 *
90 * @return error code, 0 if operation completed successfully
91 */
92 int createLogPath() const;
93
94 /** @brief Rotate log files in the directory.
95 * Function remove oldest files and keep up to maxFiles_ log files.
96 *
97 * @return error code, 0 if operation completed successfully
98 */
99 int rotateLogFiles() const;
100
Patrick Venture4d5a5dc2018-11-14 08:51:13 -0800101 private:
Artem Senichevefd5d742018-10-24 16:14:04 +0300102 /** @brief Log storage. */
103 LogStorage storage_;
104 /** @brief File descriptor of the input log stream. */
105 int fd_;
106};