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