Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 1 | /** |
| 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 | |
| 25 | |
| 26 | /** @class LogManager |
| 27 | * @brief Log manager. |
| 28 | * All functions within this class are not thread-safe. |
| 29 | */ |
| 30 | class LogManager |
| 31 | { |
| 32 | public: |
| 33 | /** @brief Constructor. */ |
| 34 | LogManager(); |
| 35 | |
| 36 | /** @brief Destructor. */ |
| 37 | ~LogManager(); |
| 38 | |
| 39 | /** @brief Open the host's log stream. |
| 40 | * |
| 41 | * @return error code, 0 if operation completed successfully |
| 42 | */ |
| 43 | int openHostLog(); |
| 44 | |
| 45 | /** @brief Close the host's log stream. |
| 46 | */ |
| 47 | void closeHostLog(); |
| 48 | |
| 49 | /** @brief Get file descriptor by host's log stream. |
| 50 | * Descriptor can be used to register it in an external polling manager. |
| 51 | * |
| 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 | |
| 69 | private: |
| 70 | /** @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 | |
| 101 | private: |
| 102 | /** @brief Log storage. */ |
| 103 | LogStorage storage_; |
| 104 | /** @brief File descriptor of the input log stream. */ |
| 105 | int fd_; |
| 106 | }; |