blob: 550afd9da9a6cc5ed0089ee45865da5d5af4b759 [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 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 Venture4d5a5dc2018-11-14 08:51:13 -080063 private:
Artem Senichevefd5d742018-10-24 16:14:04 +030064 /** @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 Venture4d5a5dc2018-11-14 08:51:13 -080094 private:
Artem Senichevefd5d742018-10-24 16:14:04 +030095 /** @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};