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/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 << "'";