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