Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/file_operations.h" |
| 2 | |
Stephan Pleines | 5ecc8f1 | 2024-05-31 20:35:21 -0700 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <ostream> |
| 5 | |
Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 6 | #include "absl/strings/match.h" |
| 7 | #include "glog/logging.h" |
| 8 | |
| 9 | namespace aos::logger::internal { |
| 10 | |
| 11 | bool IsValidFilename(std::string_view filename) { |
| 12 | return absl::EndsWith(filename, ".bfbs") || |
| 13 | absl::EndsWith(filename, ".bfbs.xz") || |
| 14 | absl::EndsWith(filename, ".bfbs.sz"); |
| 15 | } |
| 16 | |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 17 | void LocalFileOperations::FindLogs(std::vector<File> *files) { |
| 18 | auto MaybeAddFile = [&files](std::string_view filename, size_t size) { |
Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 19 | if (!IsValidFilename(filename)) { |
| 20 | VLOG(1) << "Ignoring " << filename << " with invalid extension."; |
| 21 | } else { |
| 22 | VLOG(1) << "Found log " << filename; |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 23 | files->emplace_back(File{ |
| 24 | .name = std::string(filename), |
| 25 | .size = size, |
| 26 | }); |
Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 27 | } |
| 28 | }; |
| 29 | if (std::filesystem::is_directory(filename_)) { |
| 30 | VLOG(1) << "Searching in " << filename_; |
| 31 | for (const auto &file : |
| 32 | std::filesystem::recursive_directory_iterator(filename_)) { |
| 33 | if (!file.is_regular_file()) { |
| 34 | VLOG(1) << file << " is not file."; |
| 35 | continue; |
| 36 | } |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 37 | MaybeAddFile(file.path().string(), file.file_size()); |
Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 38 | } |
| 39 | } else { |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 40 | MaybeAddFile(filename_, std::filesystem::file_size(filename_)); |
Alexei Strots | 1bf05be | 2023-04-21 11:07:37 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | } // namespace aos::logger::internal |