blob: 5361c88ff52ea94acfb9d0e0c99d051179c09176 [file] [log] [blame]
Artem Senichevefd5d742018-10-24 16:14:04 +03001/**
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 Venture4d5a5dc2018-11-14 08:51:13 -080023#include <zlib.h>
24
Artem Senichevefd5d742018-10-24 16:14:04 +030025#include <ctime>
26#include <list>
27#include <string>
Artem Senichevefd5d742018-10-24 16:14:04 +030028
29/** @class LogStorage
30 * @brief Log storage implementation.
31 * All functions within this class are not thread-safe.
32 */
33class LogStorage
34{
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080035 public:
Artem Senichevefd5d742018-10-24 16:14:04 +030036 /** @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 Senichevafc73732020-05-09 19:04:51 +030058 int save(const char* fileName) const;
Artem Senichevefd5d742018-10-24 16:14:04 +030059
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080060 private:
Artem Senichevefd5d742018-10-24 16:14:04 +030061 /** @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 Senichevefd5d742018-10-24 16:14:04 +030079 /** @brief Shrink storage by removing oldest messages. */
80 void shrink();
81
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080082 private:
Artem Senichevefd5d742018-10-24 16:14:04 +030083 /** @brief List of messages. */
84 std::list<Message> messages_;
85 /** @brief Flag to indicate that the last message is incomplete. */
Artem Senichevafc73732020-05-09 19:04:51 +030086 bool last_complete_ = true;
Artem Senichevefd5d742018-10-24 16:14:04 +030087};