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 Parse input log stream and append messages to the storage. |
| 37 | * |
| 38 | * @param[in] data - pointer to the message buffer |
| 39 | * @param[in] len - length of the buffer in bytes |
| 40 | */ |
| 41 | void parse(const char* data, size_t len); |
| 42 | |
| 43 | /** @brief Clear (reset) storage. */ |
| 44 | void clear(); |
| 45 | |
| 46 | /** @brief Check storage for empty. |
| 47 | * |
| 48 | * @return true if storage is empty |
| 49 | */ |
| 50 | bool empty() const; |
| 51 | |
| 52 | /** @brief Save messages from storage to the specified file. |
| 53 | * |
| 54 | * @param[in] fileName - path to the file |
| 55 | * |
| 56 | * @return error code, 0 if operation completed successfully |
| 57 | */ |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 58 | int save(const char* fileName) const; |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 59 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 60 | private: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 61 | /** @struct Message |
| 62 | * @brief Represent log message (single line from host log). |
| 63 | */ |
| 64 | struct Message |
| 65 | { |
| 66 | /** @brief Timestamp (message creation time). */ |
| 67 | time_t timeStamp; |
| 68 | /** @brief Text of the message. */ |
| 69 | std::string text; |
| 70 | }; |
| 71 | |
| 72 | /** @brief Append new message to the storage. |
| 73 | * |
| 74 | * @param[in] msg - pointer to the message buffer |
| 75 | * @param[in] len - length of the buffer in bytes |
| 76 | */ |
| 77 | void append(const char* msg, size_t len); |
| 78 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 79 | /** @brief Shrink storage by removing oldest messages. */ |
| 80 | void shrink(); |
| 81 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 82 | private: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 83 | /** @brief List of messages. */ |
| 84 | std::list<Message> messages_; |
| 85 | /** @brief Flag to indicate that the last message is incomplete. */ |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 86 | bool last_complete_ = true; |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 87 | }; |