Give log_name a destination flag
When we want to use logger on a pi, we want to provide a flag to name
the log file. While we are doing that, lets get rid of the ifdef which
does something different on the roboRIO, and clean up configuration.cc
some more.
Change-Id: I185770ea6061c747c1af749458c9f7c85344fb96
diff --git a/aos/BUILD b/aos/BUILD
index aef8a59..6b3c5bc 100644
--- a/aos/BUILD
+++ b/aos/BUILD
@@ -313,7 +313,6 @@
"//aos/network:team_number",
"//aos/util:file",
"@com_github_google_glog//:glog",
- "@com_google_absl//absl/base",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/strings",
],
diff --git a/aos/configuration.cc b/aos/configuration.cc
index 4a98297..b37c324 100644
--- a/aos/configuration.cc
+++ b/aos/configuration.cc
@@ -9,7 +9,6 @@
#include <unistd.h>
#include <string_view>
-#include "absl/base/call_once.h"
#include "absl/container/btree_set.h"
#include "aos/configuration_generated.h"
#include "aos/flatbuffer_merge.h"
@@ -78,44 +77,6 @@
namespace configuration {
namespace {
-void DoGetRootDirectory(char** retu) {
- ssize_t size = 0;
- *retu = NULL;
- while (true) {
- size += 256;
- if (*retu != nullptr) delete *retu;
- *retu = new char[size];
-
- ssize_t ret = readlink("/proc/self/exe", *retu, size);
- if (ret < 0) {
- if (ret != -1) {
- LOG(WARNING) << "it returned " << ret << ", not -1";
- }
-
- PLOG(FATAL) << "readlink(\"/proc/self/exe\", " << *retu << ", " << size
- << ") failed";
- }
- if (ret < size) {
- void *last_slash = memrchr(*retu, '/', ret);
- if (last_slash == NULL) {
- *retu[ret] = '\0';
- LOG(FATAL) << "couldn't find a '/' in \"" << *retu << "\"";
- }
- *static_cast<char *>(last_slash) = '\0';
- LOG(INFO) << "got a root dir of \"" << *retu << "\"";
- return;
- }
- }
-}
-
-void DoGetLoggingDirectory(char** retu) {
- static const char kSuffix[] = "/../../tmp/robot_logs";
- const char *root = GetRootDirectory();
- *retu = new char[strlen(root) + sizeof(kSuffix)];
- strcpy(*retu, root);
- strcat(*retu, kSuffix);
-}
-
// Extracts the folder part of a path. Returns ./ if there is no path.
std::string_view ExtractFolder(
const std::string_view filename) {
@@ -425,20 +386,6 @@
return result;
}
-const char *GetRootDirectory() {
- static char *root_dir; // return value
- static absl::once_flag once_;
- absl::call_once(once_, DoGetRootDirectory, &root_dir);
- return root_dir;
-}
-
-const char *GetLoggingDirectory() {
- static char *retu; // return value
- static absl::once_flag once_;
- absl::call_once(once_, DoGetLoggingDirectory, &retu);
- return retu;
-}
-
FlatbufferDetachedBuffer<Configuration> ReadConfig(
const std::string_view path) {
// We only want to read a file once. So track the visited files in a set.
diff --git a/aos/configuration.h b/aos/configuration.h
index f2cf499..ab235a3 100644
--- a/aos/configuration.h
+++ b/aos/configuration.h
@@ -70,16 +70,6 @@
// TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config);
-// Returns the "root directory" for this run. Under linux, this is the
-// directory where the executable is located (from /proc/self/exe)
-// The return value will always be to a static string, so no freeing is
-// necessary.
-const char *GetRootDirectory();
-// Returns the directory where logs get written. Relative to GetRootDirectory().
-// The return value will always be to a static string, so no freeing is
-// necessary.
-const char *GetLoggingDirectory();
-
} // namespace configuration
// Compare and equality operators for Channel. Note: these only check the name
diff --git a/aos/logging/log_displayer.cc b/aos/logging/log_displayer.cc
index 24bebbd..d3e5728 100644
--- a/aos/logging/log_displayer.cc
+++ b/aos/logging/log_displayer.cc
@@ -59,6 +59,36 @@
"To view the statuses of the shooter hall effects in realtime:\n"
"\t`log_displayer -f -n shooter -l DEBUG | grep .Position`\n";
+std::string GetRootDirectory() {
+ ssize_t size = 0;
+ std::unique_ptr<char[]> retu;
+ while (true) {
+ size += 256;
+ retu.reset(new char[size]);
+
+ ssize_t ret = readlink("/proc/self/exe", retu.get(), size);
+ if (ret < 0) {
+ if (ret != -1) {
+ LOG(WARNING) << "it returned " << ret << ", not -1";
+ }
+
+ PLOG(FATAL) << "readlink(\"/proc/self/exe\", " << retu.get() << ", " << size
+ << ") failed";
+ }
+ if (ret < size) {
+ void *last_slash = memrchr(retu.get(), '/', ret);
+ if (last_slash == NULL) {
+ retu.get()[ret] = '\0';
+ LOG(FATAL) << "couldn't find a '/' in \"" << retu.get() << "\"";
+ }
+ *static_cast<char *>(last_slash) = '\0';
+ LOG(INFO) << "got a root dir of \"" << retu.get() << "\"";
+ return std::string(retu.get());
+ }
+ }
+}
+
+
void PrintHelpAndExit() {
fprintf(stderr, "Usage: %s %s", program_invocation_name, kArgsHelp);
fprintf(stderr, "\nExample usages:\n\n%s", kExampleUsages);
@@ -66,14 +96,15 @@
// Get the possible executables from start_list.txt.
FILE *start_list = fopen("start_list.txt", "r");
if (!start_list) {
- ::std::string path(::aos::configuration::GetRootDirectory());
+ ::std::string path(GetRootDirectory());
path += "/start_list.txt";
start_list = fopen(path.c_str(), "r");
if (!start_list) {
- printf("\nCannot open start_list.txt. This means that the\n"
- "possible arguments for the -n option cannot be shown. log_displayer\n"
- "looks for start_list.txt in the current working directory and in\n"
- "%s.\n\n", ::aos::configuration::GetRootDirectory());
+ printf(
+ "\nCannot open start_list.txt. This means that the\npossible "
+ "arguments for the -n option cannot be shown. log_displayer\nlooks "
+ "for start_list.txt in the current working directory and in\n%s.\n\n",
+ path.c_str());
AOS_PLOG(FATAL, "Unable to open start_list.txt");
}
}
diff --git a/aos/logging/log_namer.cc b/aos/logging/log_namer.cc
index d4584ef..22db82a 100644
--- a/aos/logging/log_namer.cc
+++ b/aos/logging/log_namer.cc
@@ -16,6 +16,15 @@
#include "aos/configuration.h"
#include "glog/logging.h"
+DEFINE_string(logging_folder,
+#ifdef AOS_ARCHITECTURE_arm_frc
+ "",
+#else
+ "./logs",
+#endif
+ "The folder to log to. If empty, search for the /media/sd*1/ "
+ "folder and place logs there.");
+
namespace aos {
namespace logging {
namespace {
@@ -66,7 +75,6 @@
<< previous << ").";
}
-#ifdef AOS_ARCHITECTURE_arm_frc
bool FoundThumbDrive(const char *path) {
FILE *mnt_fp = setmntent("/etc/mtab", "r");
if (mnt_fp == nullptr) {
@@ -101,31 +109,35 @@
}
return false;
}
-#endif
+
} // namespace
std::string GetLogName(const char *basename) {
-#ifdef AOS_ARCHITECTURE_arm_frc
- char folder[128];
- {
- char dev_name[8];
- while (!FindDevice(dev_name, sizeof(dev_name))) {
- LOG(INFO) << "Waiting for a device";
- sleep(5);
+ if (FLAGS_logging_folder.empty()) {
+ char folder[128];
+ {
+ char dev_name[8];
+ while (!FindDevice(dev_name, sizeof(dev_name))) {
+ LOG(INFO) << "Waiting for a device";
+ sleep(5);
+ }
+ snprintf(folder, sizeof(folder), "/media/%s1", dev_name);
+ while (!FoundThumbDrive(folder)) {
+ LOG(INFO) << "Waiting for" << folder;
+ sleep(1);
+ }
+ snprintf(folder, sizeof(folder), "/media/%s1/", dev_name);
}
- snprintf(folder, sizeof(folder), "/media/%s1", dev_name);
- while (!FoundThumbDrive(folder)) {
- LOG(INFO) << "Waiting for" << folder;
- sleep(1);
- }
- snprintf(folder, sizeof(folder), "/media/%s1/", dev_name);
- }
- if (access(folder, F_OK) == -1) {
-#else
- const char *folder = configuration::GetLoggingDirectory();
+ if (access(folder, F_OK) == -1) {
+ LOG(FATAL) << "folder '" << folder
+ << "' does not exist. please create it.";
+ }
+
+ FLAGS_logging_folder = folder;
+ }
+ const char *folder = FLAGS_logging_folder.c_str();
if (access(folder, R_OK | W_OK) == -1) {
-#endif
LOG(FATAL) << "folder '" << folder << "' does not exist. please create it.";
}
LOG(INFO) << "logging to folder '" << folder << "'";