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