| 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 |  | 
| Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 25 | /** @class LogManager | 
 | 26 |  *  @brief Log manager. | 
 | 27 |  *         All functions within this class are not thread-safe. | 
 | 28 |  */ | 
 | 29 | class LogManager | 
 | 30 | { | 
| Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 31 |   public: | 
| Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 32 |     /** @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 Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 45 |      */ | 
| Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 46 |     void closeHostLog(); | 
 | 47 |  | 
 | 48 |     /** @brief Get file descriptor by host's log stream. | 
| Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 49 |      *         Descriptor can be used to register it in an external polling | 
 | 50 |      * manager. | 
| Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 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 |  | 
| Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 69 |   private: | 
| Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 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 |  | 
| Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 101 |   private: | 
| Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 102 |     /** @brief Log storage. */ | 
 | 103 |     LogStorage storage_; | 
 | 104 |     /** @brief File descriptor of the input log stream. */ | 
 | 105 |     int fd_; | 
 | 106 | }; |