blob: 02924404bf824b77887b62fe6edd6217bf3fc597 [file] [log] [blame]
Artem Senicheve8837d52020-06-07 11:59:04 +03001// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2020 YADRO
3
4#pragma once
5
6#include "log_buffer.hpp"
7
8#include <filesystem>
9
10/**
11 * @class FileStorage
12 * @brief Persistent file storage with automatic log file rotation.
13 */
14class FileStorage
15{
16 public:
17 /**
18 * @brief Constructor.
19 *
20 * @param[in] path absolute path to the output directory
21 * @param[in] prefix prefix used for log file names
22 * @param[in] maxFiles max number of log files that can be stored
23 *
24 * @throw std::exception in case of errors
25 */
26 FileStorage(const std::string& path, const std::string& prefix,
27 size_t maxFiles);
28
29 /**
30 * @brief Save log buffer to a file.
31 *
32 * @param[in] buf buffer with log message to save
33 *
34 * @throw std::exception in case of errors
35 *
36 * @return path to saved file
37 */
38 std::string save(const LogBuffer& buf) const;
39
40 private:
41 /**
42 * @brief Prepare output directory for a new log file and construct path.
43 *
44 * @throw std::exception in case of errors
45 *
46 * @return full path to the new file
47 */
48 std::string newFile() const;
49
50 /**
51 * @brief Rotate log files in the output directory by removing the oldest
52 * logs.
53 *
54 * @throw std::exception in case of errors
55 */
56 void rotate() const;
57
58 private:
59 /** @brief Output directory. */
60 std::filesystem::path outDir;
61 /** @brief Prefix used for log file names. */
62 std::string filePrefix;
63 /** @brief Max number of log files that can be stored. */
64 size_t filesLimit;
65};