blob: d5005bbaaa27aa639adbab5a20c1b9dea64015e8 [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 <mntent.h>
James Kuszmaul011b67a2019-12-15 12:52:34 -08005#include <unistd.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07006
7#include <cerrno>
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
Stephan Pleines0960c262024-05-31 20:29:24 -070011#include <ostream>
James Kuszmaul011b67a2019-12-15 12:52:34 -080012#include <string>
13
Stephan Pleines0960c262024-05-31 20:29:24 -070014#include "gflags/gflags.h"
James Kuszmaul011b67a2019-12-15 12:52:34 -080015#include "glog/logging.h"
16
Jim Ostrowski0dd46662024-02-25 17:08:26 -080017#include "aos/configuration.h"
18#include "aos/time/time.h"
19
Adam Snaider13d48d92023-08-03 12:20:15 -070020#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 Schuh03e80a62019-12-28 15:18:54 -080026DEFINE_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 Pleinesf63bde82024-01-13 15:59:33 -080035namespace aos::logging {
James Kuszmaul011b67a2019-12-15 12:52:34 -080036namespace {
37void 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 Ostrowski0dd46662024-02-25 17:08:26 -080055 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 Kuszmaul011b67a2019-12-15 12:52:34 -080060 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 Ostrowski0dd46662024-02-25 17:08:26 -080077 // 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 Kuszmaul011b67a2019-12-15 12:52:34 -080084 PLOG(FATAL) << "couldn't create final name";
85 }
James Kuszmaul131aa042021-08-01 17:28:50 -070086 // Fix basename formatting.
Austin Schuh2b378b42024-02-26 22:01:04 -080087 LOG(INFO) << "Created log file (" << *filename << "). Previous file was ("
James Kuszmaul131aa042021-08-01 17:28:50 -070088 << directory << "/" << previous << ").";
James Kuszmaul011b67a2019-12-15 12:52:34 -080089}
90
James Kuszmaul011b67a2019-12-15 12:52:34 -080091bool 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
113bool 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 Schuh73fcab12020-02-22 14:59:23 -0800117 VLOG(1) << "Trying to access" << test_device;
James Kuszmaul011b67a2019-12-15 12:52:34 -0800118 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 Schuh03e80a62019-12-28 15:18:54 -0800125
James Kuszmaul011b67a2019-12-15 12:52:34 -0800126} // namespace
127
Ravago Jones1a4bc762023-04-09 16:21:57 -0700128std::optional<std::string> MaybeGetLogName(const char *basename) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800129 if (FLAGS_logging_folder.empty()) {
130 char folder[128];
131 {
132 char dev_name[8];
Ravago Jones1a4bc762023-04-09 16:21:57 -0700133 if (!FindDevice(dev_name, sizeof(dev_name))) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800134 LOG(INFO) << "Waiting for a device";
Ravago Jones1a4bc762023-04-09 16:21:57 -0700135 return std::nullopt;
Austin Schuh03e80a62019-12-28 15:18:54 -0800136 }
137 snprintf(folder, sizeof(folder), "/media/%s1", dev_name);
Ravago Jones1a4bc762023-04-09 16:21:57 -0700138 if (!FoundThumbDrive(folder)) {
Austin Schuh03e80a62019-12-28 15:18:54 -0800139 LOG(INFO) << "Waiting for" << folder;
Ravago Jones1a4bc762023-04-09 16:21:57 -0700140 return std::nullopt;
Austin Schuh03e80a62019-12-28 15:18:54 -0800141 }
142 snprintf(folder, sizeof(folder), "/media/%s1/", dev_name);
James Kuszmaul011b67a2019-12-15 12:52:34 -0800143 }
James Kuszmaul011b67a2019-12-15 12:52:34 -0800144
Austin Schuh03e80a62019-12-28 15:18:54 -0800145 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 Kuszmaul011b67a2019-12-15 12:52:34 -0800153 if (access(folder, R_OK | W_OK) == -1) {
James Kuszmaul011b67a2019-12-15 12:52:34 -0800154 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 Schuh2d0471d2020-02-29 13:27:07 -0800160
161 std::string log_base_name = tmp;
Austin Schuhe715eae2020-10-10 15:39:30 -0700162 std::string log_roborio_name = log_base_name + "/";
Austin Schuh2d0471d2020-02-29 13:27:07 -0800163 free(tmp);
164
James Kuszmaul011b67a2019-12-15 12:52:34 -0800165 char *tmp2;
Austin Schuhe715eae2020-10-10 15:39:30 -0700166 if (asprintf(&tmp2, "%s/%s-current", folder, basename) == -1) {
James Kuszmaul011b67a2019-12-15 12:52:34 -0800167 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 Schuh2d0471d2020-02-29 13:27:07 -0800172 if (symlink(log_roborio_name.c_str(), tmp2) == -1) {
173 PLOG(WARNING) << "symlink('" << log_roborio_name.c_str() << "', '" << tmp2
174 << "') failed";
James Kuszmaul011b67a2019-12-15 12:52:34 -0800175 }
176 free(tmp2);
177 }
Austin Schuh2d0471d2020-02-29 13:27:07 -0800178 return log_base_name;
James Kuszmaul011b67a2019-12-15 12:52:34 -0800179}
180
Ravago Jones1a4bc762023-04-09 16:21:57 -0700181std::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 Pleinesf63bde82024-01-13 15:59:33 -0800197} // namespace aos::logging