blob: 4e9e00edf1c7235fed4a9da5ea054e6caf8eff86 [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
Adam Snaider13d48d92023-08-03 12:20:15 -070021#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 Schuh03e80a62019-12-28 15:18:54 -080027DEFINE_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 Kuszmaul011b67a2019-12-15 12:52:34 -080036namespace aos {
37namespace logging {
38namespace {
39void 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 Kuszmaul131aa042021-08-01 17:28:50 -070079 // Fix basename formatting.
80 LOG(INFO) << "Created log file (" << filename << "). Previous file was ("
81 << directory << "/" << previous << ").";
James Kuszmaul011b67a2019-12-15 12:52:34 -080082}
83
James Kuszmaul011b67a2019-12-15 12:52:34 -080084bool 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
106bool 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 Schuh73fcab12020-02-22 14:59:23 -0800110 VLOG(1) << "Trying to access" << test_device;
James Kuszmaul011b67a2019-12-15 12:52:34 -0800111 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 Schuh03e80a62019-12-28 15:18:54 -0800118
James Kuszmaul011b67a2019-12-15 12:52:34 -0800119} // namespace
120
Ravago Jones1a4bc762023-04-09 16:21:57 -0700121std::optional<std::string> MaybeGetLogName(const char *basename) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800122 if (FLAGS_logging_folder.empty()) {
123 char folder[128];
124 {
125 char dev_name[8];
Ravago Jones1a4bc762023-04-09 16:21:57 -0700126 if (!FindDevice(dev_name, sizeof(dev_name))) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800127 LOG(INFO) << "Waiting for a device";
Ravago Jones1a4bc762023-04-09 16:21:57 -0700128 return std::nullopt;
Austin Schuh03e80a62019-12-28 15:18:54 -0800129 }
130 snprintf(folder, sizeof(folder), "/media/%s1", dev_name);
Ravago Jones1a4bc762023-04-09 16:21:57 -0700131 if (!FoundThumbDrive(folder)) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800132 LOG(INFO) << "Waiting for" << folder;
Ravago Jones1a4bc762023-04-09 16:21:57 -0700133 return std::nullopt;
Austin Schuh03e80a62019-12-28 15:18:54 -0800134 }
135 snprintf(folder, sizeof(folder), "/media/%s1/", dev_name);
James Kuszmaul011b67a2019-12-15 12:52:34 -0800136 }
James Kuszmaul011b67a2019-12-15 12:52:34 -0800137
Austin Schuh03e80a62019-12-28 15:18:54 -0800138 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 Kuszmaul011b67a2019-12-15 12:52:34 -0800146 if (access(folder, R_OK | W_OK) == -1) {
James Kuszmaul011b67a2019-12-15 12:52:34 -0800147 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 Schuh2d0471d2020-02-29 13:27:07 -0800153
154 std::string log_base_name = tmp;
Austin Schuhe715eae2020-10-10 15:39:30 -0700155 std::string log_roborio_name = log_base_name + "/";
Austin Schuh2d0471d2020-02-29 13:27:07 -0800156 free(tmp);
157
James Kuszmaul011b67a2019-12-15 12:52:34 -0800158 char *tmp2;
Austin Schuhe715eae2020-10-10 15:39:30 -0700159 if (asprintf(&tmp2, "%s/%s-current", folder, basename) == -1) {
James Kuszmaul011b67a2019-12-15 12:52:34 -0800160 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 Schuh2d0471d2020-02-29 13:27:07 -0800165 if (symlink(log_roborio_name.c_str(), tmp2) == -1) {
166 PLOG(WARNING) << "symlink('" << log_roborio_name.c_str() << "', '" << tmp2
167 << "') failed";
James Kuszmaul011b67a2019-12-15 12:52:34 -0800168 }
169 free(tmp2);
170 }
Austin Schuh2d0471d2020-02-29 13:27:07 -0800171 return log_base_name;
James Kuszmaul011b67a2019-12-15 12:52:34 -0800172}
173
Ravago Jones1a4bc762023-04-09 16:21:57 -0700174std::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 Kuszmaul011b67a2019-12-15 12:52:34 -0800190} // namespace logging
191} // namespace aos