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