blob: 3deb89d36530e54fe35e35fa53df0f0be68e57bb [file] [log] [blame]
James Kuszmaul011b67a2019-12-15 12:52:34 -08001#include "aos/logging/log_namer.h"
2
3#include <dirent.h>
James Kuszmaul011b67a2019-12-15 12:52:34 -08004#include <fcntl.h>
5#include <mntent.h>
6#include <pwd.h>
James Kuszmaul011b67a2019-12-15 12:52:34 -08007#include <sys/types.h>
James Kuszmaul011b67a2019-12-15 12:52:34 -08008#include <unistd.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07009
10#include <cerrno>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <ctime>
James Kuszmaul011b67a2019-12-15 12:52:34 -080015#include <string>
16
James Kuszmaul011b67a2019-12-15 12:52:34 -080017#include "glog/logging.h"
18
Philipp Schrader790cb542023-07-05 21:06:52 -070019#include "aos/configuration.h"
20
Austin Schuh03e80a62019-12-28 15:18:54 -080021DEFINE_string(logging_folder,
22#ifdef AOS_ARCHITECTURE_arm_frc
23 "",
24#else
25 "./logs",
26#endif
27 "The folder to log to. If empty, search for the /media/sd*1/ "
28 "folder and place logs there.");
29
James Kuszmaul011b67a2019-12-15 12:52:34 -080030namespace aos {
31namespace logging {
32namespace {
33void AllocateLogName(char **filename, const char *directory,
34 const char *basename) {
35 int fileindex = 0;
36 DIR *const d = opendir(directory);
37 if (d == nullptr) {
38 PLOG(FATAL) << "could not open directory" << directory;
39 }
40 int index = 0;
41 while (true) {
42 errno = 0;
43 struct dirent *const dir = readdir(d);
44 if (dir == nullptr) {
45 if (errno == 0) {
46 break;
47 } else {
48 PLOG(FATAL) << "readdir(" << d << ") failed";
49 }
50 } else {
51 const std::string format_string = std::string(basename) + "-%d";
52 if (sscanf(dir->d_name, format_string.c_str(), &index) == 1) {
53 if (index >= fileindex) {
54 fileindex = index + 1;
55 }
56 }
57 }
58 }
59 closedir(d);
60
61 char previous[512];
62 ::std::string path = ::std::string(directory) + "/" + basename + "-current";
63 ssize_t len = ::readlink(path.c_str(), previous, sizeof(previous));
64 if (len != -1) {
65 previous[len] = '\0';
66 } else {
67 previous[0] = '\0';
68 LOG(INFO) << "Could not find " << path;
69 }
70 if (asprintf(filename, "%s/%s-%03d", directory, basename, fileindex) == -1) {
71 PLOG(FATAL) << "couldn't create final name";
72 }
James Kuszmaul131aa042021-08-01 17:28:50 -070073 // Fix basename formatting.
74 LOG(INFO) << "Created log file (" << filename << "). Previous file was ("
75 << directory << "/" << previous << ").";
James Kuszmaul011b67a2019-12-15 12:52:34 -080076}
77
James Kuszmaul011b67a2019-12-15 12:52:34 -080078bool 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
100bool 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);
Austin Schuh73fcab12020-02-22 14:59:23 -0800104 VLOG(1) << "Trying to access" << test_device;
James Kuszmaul011b67a2019-12-15 12:52:34 -0800105 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 Schuh03e80a62019-12-28 15:18:54 -0800112
James Kuszmaul011b67a2019-12-15 12:52:34 -0800113} // namespace
114
Ravago Jones1a4bc762023-04-09 16:21:57 -0700115std::optional<std::string> MaybeGetLogName(const char *basename) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800116 if (FLAGS_logging_folder.empty()) {
117 char folder[128];
118 {
119 char dev_name[8];
Ravago Jones1a4bc762023-04-09 16:21:57 -0700120 if (!FindDevice(dev_name, sizeof(dev_name))) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800121 LOG(INFO) << "Waiting for a device";
Ravago Jones1a4bc762023-04-09 16:21:57 -0700122 return std::nullopt;
Austin Schuh03e80a62019-12-28 15:18:54 -0800123 }
124 snprintf(folder, sizeof(folder), "/media/%s1", dev_name);
Ravago Jones1a4bc762023-04-09 16:21:57 -0700125 if (!FoundThumbDrive(folder)) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800126 LOG(INFO) << "Waiting for" << folder;
Ravago Jones1a4bc762023-04-09 16:21:57 -0700127 return std::nullopt;
Austin Schuh03e80a62019-12-28 15:18:54 -0800128 }
129 snprintf(folder, sizeof(folder), "/media/%s1/", dev_name);
James Kuszmaul011b67a2019-12-15 12:52:34 -0800130 }
James Kuszmaul011b67a2019-12-15 12:52:34 -0800131
Austin Schuh03e80a62019-12-28 15:18:54 -0800132 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 Kuszmaul011b67a2019-12-15 12:52:34 -0800140 if (access(folder, R_OK | W_OK) == -1) {
James Kuszmaul011b67a2019-12-15 12:52:34 -0800141 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);
Austin Schuh2d0471d2020-02-29 13:27:07 -0800147
148 std::string log_base_name = tmp;
Austin Schuhe715eae2020-10-10 15:39:30 -0700149 std::string log_roborio_name = log_base_name + "/";
Austin Schuh2d0471d2020-02-29 13:27:07 -0800150 free(tmp);
151
James Kuszmaul011b67a2019-12-15 12:52:34 -0800152 char *tmp2;
Austin Schuhe715eae2020-10-10 15:39:30 -0700153 if (asprintf(&tmp2, "%s/%s-current", folder, basename) == -1) {
James Kuszmaul011b67a2019-12-15 12:52:34 -0800154 PLOG(WARNING) << "couldn't create current symlink name";
155 } else {
156 if (unlink(tmp2) == -1 && (errno != EROFS && errno != ENOENT)) {
157 LOG(WARNING) << "unlink('" << tmp2 << "') failed";
158 }
Austin Schuh2d0471d2020-02-29 13:27:07 -0800159 if (symlink(log_roborio_name.c_str(), tmp2) == -1) {
160 PLOG(WARNING) << "symlink('" << log_roborio_name.c_str() << "', '" << tmp2
161 << "') failed";
James Kuszmaul011b67a2019-12-15 12:52:34 -0800162 }
163 free(tmp2);
164 }
Austin Schuh2d0471d2020-02-29 13:27:07 -0800165 return log_base_name;
James Kuszmaul011b67a2019-12-15 12:52:34 -0800166}
167
Ravago Jones1a4bc762023-04-09 16:21:57 -0700168std::string GetLogName(const char *basename) {
169 std::optional<std::string> log_base_name;
170
171 while (true) {
172 log_base_name = MaybeGetLogName(basename);
173
174 if (log_base_name.has_value()) {
175 break;
176 }
177
178 sleep(5);
179 }
180
181 return log_base_name.value();
182}
183
James Kuszmaul011b67a2019-12-15 12:52:34 -0800184} // namespace logging
185} // namespace aos