Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_FILE_OPERATIONS_H_ |
| 2 | #define AOS_EVENTS_LOGGING_FILE_OPERATIONS_H_ |
| 3 | |
| 4 | #include <filesystem> |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
| 8 | namespace aos::logger::internal { |
| 9 | |
| 10 | // Predicate to include or exclude file to be considered as a log file. |
| 11 | bool IsValidFilename(std::string_view filename); |
| 12 | |
| 13 | // Abstraction that supports listing of the logs on file system and S3. It is |
| 14 | // associated with either a single file or directory that contains log files. |
| 15 | class FileOperations { |
| 16 | public: |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 17 | struct File { |
| 18 | std::string name; |
Andrew Schneer | 4ca5628 | 2024-02-14 17:59:51 -0800 | [diff] [blame] | 19 | size_t size; // bytes. |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 20 | }; |
| 21 | |
Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 22 | virtual ~FileOperations() = default; |
| 23 | |
| 24 | virtual bool Exists() = 0; |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 25 | virtual void FindLogs(std::vector<File> *files) = 0; |
Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | // Implements FileOperations with standard POSIX filesystem APIs. These work on |
| 29 | // files local to the machine they're running on. |
| 30 | class LocalFileOperations final : public FileOperations { |
| 31 | public: |
| 32 | explicit LocalFileOperations(std::string_view filename) |
| 33 | : filename_(filename) {} |
| 34 | |
| 35 | bool Exists() override { return std::filesystem::exists(filename_); } |
| 36 | |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 37 | void FindLogs(std::vector<File> *files) override; |
Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 38 | |
| 39 | private: |
| 40 | std::string filename_; |
| 41 | }; |
| 42 | |
| 43 | } // namespace aos::logger::internal |
| 44 | |
| 45 | #endif // AOS_EVENTS_LOGGING_FILE_OPERATIONS_H_ |