Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 1 | /** |
| 2 | * @brief Log storage. |
| 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 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 23 | #include <zlib.h> |
| 24 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 25 | #include <ctime> |
| 26 | #include <list> |
| 27 | #include <string> |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 28 | |
| 29 | /** @class LogStorage |
| 30 | * @brief Log storage implementation. |
| 31 | * All functions within this class are not thread-safe. |
| 32 | */ |
| 33 | class LogStorage |
| 34 | { |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 35 | public: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 36 | /** @brief Constructor. */ |
| 37 | LogStorage(); |
| 38 | |
| 39 | /** @brief Parse input log stream and append messages to the storage. |
| 40 | * |
| 41 | * @param[in] data - pointer to the message buffer |
| 42 | * @param[in] len - length of the buffer in bytes |
| 43 | */ |
| 44 | void parse(const char* data, size_t len); |
| 45 | |
| 46 | /** @brief Clear (reset) storage. */ |
| 47 | void clear(); |
| 48 | |
| 49 | /** @brief Check storage for empty. |
| 50 | * |
| 51 | * @return true if storage is empty |
| 52 | */ |
| 53 | bool empty() const; |
| 54 | |
| 55 | /** @brief Save messages from storage to the specified file. |
| 56 | * |
| 57 | * @param[in] fileName - path to the file |
| 58 | * |
| 59 | * @return error code, 0 if operation completed successfully |
| 60 | */ |
| 61 | int write(const char* fileName) const; |
| 62 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 63 | private: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 64 | /** @struct Message |
| 65 | * @brief Represent log message (single line from host log). |
| 66 | */ |
| 67 | struct Message |
| 68 | { |
| 69 | /** @brief Timestamp (message creation time). */ |
| 70 | time_t timeStamp; |
| 71 | /** @brief Text of the message. */ |
| 72 | std::string text; |
| 73 | }; |
| 74 | |
| 75 | /** @brief Append new message to the storage. |
| 76 | * |
| 77 | * @param[in] msg - pointer to the message buffer |
| 78 | * @param[in] len - length of the buffer in bytes |
| 79 | */ |
| 80 | void append(const char* msg, size_t len); |
| 81 | |
| 82 | /** @brief Write message to the file. |
| 83 | * |
| 84 | * @param[in] fd - descriptor of the file to write |
| 85 | * @param[in] msg - message to write |
| 86 | * |
| 87 | * @return error code, 0 if operation completed successfully |
| 88 | */ |
| 89 | int write(gzFile fd, const Message& msg) const; |
| 90 | |
| 91 | /** @brief Shrink storage by removing oldest messages. */ |
| 92 | void shrink(); |
| 93 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 94 | private: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 95 | /** @brief List of messages. */ |
| 96 | std::list<Message> messages_; |
| 97 | /** @brief Flag to indicate that the last message is incomplete. */ |
| 98 | bool last_complete_; |
| 99 | }; |