blob: c1dd30a720c7d46e7eef49af29687f7f7287ff0c [file] [log] [blame]
James Kuszmaul011b67a2019-12-15 12:52:34 -08001#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 Schuh03e80a62019-12-28 15:18:54 -080019DEFINE_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 Kuszmaul011b67a2019-12-15 12:52:34 -080028namespace aos {
29namespace logging {
30namespace {
31void 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 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
115std::string GetLogName(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];
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 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
168} // namespace logging
169} // namespace aos