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