James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 1 | #include "aos/logging/log_namer.h" |
| 2 | |
| 3 | #include <dirent.h> |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 4 | #include <mntent.h> |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 5 | #include <unistd.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 6 | |
| 7 | #include <cerrno> |
| 8 | #include <cstdio> |
| 9 | #include <cstdlib> |
| 10 | #include <cstring> |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 11 | #include <ostream> |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 14 | #include "absl/flags/flag.h" |
| 15 | #include "absl/log/check.h" |
| 16 | #include "absl/log/log.h" |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 17 | |
Jim Ostrowski | 0dd4666 | 2024-02-25 17:08:26 -0800 | [diff] [blame] | 18 | #include "aos/configuration.h" |
| 19 | #include "aos/time/time.h" |
| 20 | |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 21 | #if defined(__clang) |
| 22 | #pragma clang diagnostic ignored "-Wformat-nonliteral" |
| 23 | #elif defined(__GNUC__) |
| 24 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
| 25 | #endif |
| 26 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 27 | ABSL_FLAG(std::string, logging_folder, |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 28 | #ifdef AOS_ARCHITECTURE_arm_frc |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 29 | "", |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 30 | #else |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 31 | "./logs", |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 32 | #endif |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 33 | "The folder to log to. If empty, search for the /media/sd*1/ " |
| 34 | "folder and place logs there."); |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 35 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 36 | namespace aos::logging { |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 37 | namespace { |
| 38 | void AllocateLogName(char **filename, const char *directory, |
| 39 | const char *basename) { |
| 40 | int fileindex = 0; |
| 41 | DIR *const d = opendir(directory); |
| 42 | if (d == nullptr) { |
| 43 | PLOG(FATAL) << "could not open directory" << directory; |
| 44 | } |
| 45 | int index = 0; |
| 46 | while (true) { |
| 47 | errno = 0; |
| 48 | struct dirent *const dir = readdir(d); |
| 49 | if (dir == nullptr) { |
| 50 | if (errno == 0) { |
| 51 | break; |
| 52 | } else { |
| 53 | PLOG(FATAL) << "readdir(" << d << ") failed"; |
| 54 | } |
| 55 | } else { |
Jim Ostrowski | 0dd4666 | 2024-02-25 17:08:26 -0800 | [diff] [blame] | 56 | char previous_date[512]; |
| 57 | // Look for previous index and date |
| 58 | const std::string format_string = std::string(basename) + "-%d_%s"; |
| 59 | if (sscanf(dir->d_name, format_string.c_str(), &index, &previous_date) == |
| 60 | 2) { |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 61 | if (index >= fileindex) { |
| 62 | fileindex = index + 1; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | closedir(d); |
| 68 | |
| 69 | char previous[512]; |
| 70 | ::std::string path = ::std::string(directory) + "/" + basename + "-current"; |
| 71 | ssize_t len = ::readlink(path.c_str(), previous, sizeof(previous)); |
| 72 | if (len != -1) { |
| 73 | previous[len] = '\0'; |
| 74 | } else { |
| 75 | previous[0] = '\0'; |
| 76 | LOG(INFO) << "Could not find " << path; |
| 77 | } |
Jim Ostrowski | 0dd4666 | 2024-02-25 17:08:26 -0800 | [diff] [blame] | 78 | // Remove subsecond accuracy (after the "."). We don't need it, and it makes |
| 79 | // the string very long |
| 80 | std::string time_short = aos::ToString(aos::realtime_clock::now()); |
| 81 | time_short = time_short.substr(0, time_short.find(".")); |
| 82 | |
| 83 | if (asprintf(filename, "%s/%s-%03d_%s", directory, basename, fileindex, |
| 84 | time_short.c_str()) == -1) { |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 85 | PLOG(FATAL) << "couldn't create final name"; |
| 86 | } |
James Kuszmaul | 131aa04 | 2021-08-01 17:28:50 -0700 | [diff] [blame] | 87 | // Fix basename formatting. |
Austin Schuh | 2b378b4 | 2024-02-26 22:01:04 -0800 | [diff] [blame] | 88 | LOG(INFO) << "Created log file (" << *filename << "). Previous file was (" |
James Kuszmaul | 131aa04 | 2021-08-01 17:28:50 -0700 | [diff] [blame] | 89 | << directory << "/" << previous << ")."; |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 90 | } |
| 91 | |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 92 | bool FoundThumbDrive(const char *path) { |
| 93 | FILE *mnt_fp = setmntent("/etc/mtab", "r"); |
| 94 | if (mnt_fp == nullptr) { |
| 95 | LOG(FATAL) << "Could not open /etc/mtab"; |
| 96 | } |
| 97 | |
| 98 | bool found = false; |
| 99 | struct mntent mntbuf; |
| 100 | char buf[256]; |
| 101 | while (!found) { |
| 102 | struct mntent *mount_list = getmntent_r(mnt_fp, &mntbuf, buf, sizeof(buf)); |
| 103 | if (mount_list == nullptr) { |
| 104 | break; |
| 105 | } |
| 106 | if (strcmp(mount_list->mnt_dir, path) == 0) { |
| 107 | found = true; |
| 108 | } |
| 109 | } |
| 110 | endmntent(mnt_fp); |
| 111 | return found; |
| 112 | } |
| 113 | |
| 114 | bool FindDevice(char *device, size_t device_size) { |
| 115 | char test_device[10]; |
| 116 | for (char i = 'a'; i < 'z'; ++i) { |
| 117 | snprintf(test_device, sizeof(test_device), "/dev/sd%c", i); |
Austin Schuh | 73fcab1 | 2020-02-22 14:59:23 -0800 | [diff] [blame] | 118 | VLOG(1) << "Trying to access" << test_device; |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 119 | if (access(test_device, F_OK) != -1) { |
| 120 | snprintf(device, device_size, "sd%c", i); |
| 121 | return true; |
| 122 | } |
| 123 | } |
| 124 | return false; |
| 125 | } |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 126 | |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 127 | } // namespace |
| 128 | |
Ravago Jones | 1a4bc76 | 2023-04-09 16:21:57 -0700 | [diff] [blame] | 129 | std::optional<std::string> MaybeGetLogName(const char *basename) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 130 | if (absl::GetFlag(FLAGS_logging_folder).empty()) { |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 131 | char folder[128]; |
| 132 | { |
| 133 | char dev_name[8]; |
Ravago Jones | 1a4bc76 | 2023-04-09 16:21:57 -0700 | [diff] [blame] | 134 | if (!FindDevice(dev_name, sizeof(dev_name))) { |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 135 | LOG(INFO) << "Waiting for a device"; |
Ravago Jones | 1a4bc76 | 2023-04-09 16:21:57 -0700 | [diff] [blame] | 136 | return std::nullopt; |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 137 | } |
| 138 | snprintf(folder, sizeof(folder), "/media/%s1", dev_name); |
Ravago Jones | 1a4bc76 | 2023-04-09 16:21:57 -0700 | [diff] [blame] | 139 | if (!FoundThumbDrive(folder)) { |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 140 | LOG(INFO) << "Waiting for" << folder; |
Ravago Jones | 1a4bc76 | 2023-04-09 16:21:57 -0700 | [diff] [blame] | 141 | return std::nullopt; |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 142 | } |
| 143 | snprintf(folder, sizeof(folder), "/media/%s1/", dev_name); |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 144 | } |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 145 | |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 146 | if (access(folder, F_OK) == -1) { |
| 147 | LOG(FATAL) << "folder '" << folder |
| 148 | << "' does not exist. please create it."; |
| 149 | } |
| 150 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 151 | absl::SetFlag(&FLAGS_logging_folder, folder); |
Austin Schuh | 03e80a6 | 2019-12-28 15:18:54 -0800 | [diff] [blame] | 152 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 153 | const std::string folder = absl::GetFlag(FLAGS_logging_folder); |
| 154 | if (access(folder.c_str(), R_OK | W_OK) == -1) { |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 155 | LOG(FATAL) << "folder '" << folder << "' does not exist. please create it."; |
| 156 | } |
| 157 | LOG(INFO) << "logging to folder '" << folder << "'"; |
| 158 | |
| 159 | char *tmp; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 160 | AllocateLogName(&tmp, folder.c_str(), basename); |
Austin Schuh | 2d0471d | 2020-02-29 13:27:07 -0800 | [diff] [blame] | 161 | |
| 162 | std::string log_base_name = tmp; |
Austin Schuh | e715eae | 2020-10-10 15:39:30 -0700 | [diff] [blame] | 163 | std::string log_roborio_name = log_base_name + "/"; |
Austin Schuh | 2d0471d | 2020-02-29 13:27:07 -0800 | [diff] [blame] | 164 | free(tmp); |
| 165 | |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 166 | char *tmp2; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 167 | if (asprintf(&tmp2, "%s/%s-current", folder.c_str(), basename) == -1) { |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 168 | PLOG(WARNING) << "couldn't create current symlink name"; |
| 169 | } else { |
| 170 | if (unlink(tmp2) == -1 && (errno != EROFS && errno != ENOENT)) { |
| 171 | LOG(WARNING) << "unlink('" << tmp2 << "') failed"; |
| 172 | } |
Austin Schuh | 2d0471d | 2020-02-29 13:27:07 -0800 | [diff] [blame] | 173 | if (symlink(log_roborio_name.c_str(), tmp2) == -1) { |
| 174 | PLOG(WARNING) << "symlink('" << log_roborio_name.c_str() << "', '" << tmp2 |
| 175 | << "') failed"; |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 176 | } |
| 177 | free(tmp2); |
| 178 | } |
Austin Schuh | 2d0471d | 2020-02-29 13:27:07 -0800 | [diff] [blame] | 179 | return log_base_name; |
James Kuszmaul | 011b67a | 2019-12-15 12:52:34 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Ravago Jones | 1a4bc76 | 2023-04-09 16:21:57 -0700 | [diff] [blame] | 182 | std::string GetLogName(const char *basename) { |
| 183 | std::optional<std::string> log_base_name; |
| 184 | |
| 185 | while (true) { |
| 186 | log_base_name = MaybeGetLogName(basename); |
| 187 | |
| 188 | if (log_base_name.has_value()) { |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | sleep(5); |
| 193 | } |
| 194 | |
| 195 | return log_base_name.value(); |
| 196 | } |
| 197 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 198 | } // namespace aos::logging |