Move SearchDirectory into a common file

Change-Id: I34fe0f2b89852eaec404adca39e396a317dfeb37
diff --git a/aos/events/logging/log_cat.cc b/aos/events/logging/log_cat.cc
index 7dabc18..5d205a8 100644
--- a/aos/events/logging/log_cat.cc
+++ b/aos/events/logging/log_cat.cc
@@ -5,7 +5,6 @@
 #include <string>
 #include <string_view>
 #include <vector>
-#include "dirent.h"
 
 #include "aos/configuration.h"
 #include "aos/events/logging/logger.h"
@@ -33,11 +32,6 @@
 DEFINE_bool(pretty, false,
             "If true, pretty print the messages on multiple lines");
 
-bool EndsWith(std::string_view str, std::string_view ending) {
-  return str.size() >= ending.size() &&
-         str.substr(str.size() - ending.size()) == ending;
-}
-
 // Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
 // glog and to allow the user to readily redirect just the logged output
 // independent of any debugging information on stderr.
@@ -64,33 +58,6 @@
   }
 }
 
-void SearchDirectory(std::vector<std::string> *files, std::string filename) {
-  DIR *directory = opendir(filename.c_str());
-
-  if (directory == nullptr) {
-    // its not a directory
-    // it could be a file
-    // or it could not exist
-    if (EndsWith(filename, ".bfbs") || EndsWith(filename, ".bfbs.xz")) {
-      files->emplace_back(filename);
-    }
-    return;
-  }
-
-  struct dirent *directory_entry;
-  while ((directory_entry = readdir(directory)) != nullptr) {
-    std::string next_filename = directory_entry->d_name;
-    if (next_filename == "." || next_filename == "..") {
-      continue;
-    }
-
-    std::string path = filename + "/" + next_filename;
-    SearchDirectory(files, path);
-  }
-
-  closedir(directory);
-}
-
 int main(int argc, char **argv) {
   gflags::SetUsageMessage(
       "Usage:\n"
@@ -156,10 +123,8 @@
     LOG(FATAL) << "Expected at least 1 logfile as an argument.";
   }
 
-  std::vector<std::string> unsorted_logfiles;
-  for (int i = 1; i < argc; ++i) {
-    SearchDirectory(&unsorted_logfiles, argv[i]);
-  }
+  const std::vector<std::string> unsorted_logfiles =
+      aos::logger::FindLogs(argc, argv);
 
   const std::vector<aos::logger::LogFile> logfiles =
       aos::logger::SortParts(unsorted_logfiles);